package cn.pluss.platform.controller.MerchantPlugin;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.MerchantPlugin;
import cn.pluss.platform.merchant.MerchantPluginService;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/merchantPlugin")
public class MerchantPluginController {

    @Autowired
    private MerchantPluginService merchantPluginService;

    @GetMapping("/index")
    public String index() {
        return "merchantPlugin/merchantPluginList";
    }

    @GetMapping("/page")
    @ResponseBody
    public Map<String, Object> listMerchantPlugin(@RequestParam(defaultValue = "1") Integer currPage, Integer size,MerchantPlugin merchantPlugin) {
        QueryWrapper<MerchantPlugin> queryWrapper = new QueryWrapper<>();
        queryWrapper.last("limit " + ((currPage - 1) * size) + "10");
        if (StringUtil.isNotEmpty(merchantPlugin.getUserId())){
            queryWrapper.eq("userId",merchantPlugin.getUserId());
        }
        List<MerchantPlugin> merchantPluginList = merchantPluginService.list(queryWrapper);
        for (MerchantPlugin m : merchantPluginList) {
            if (StringUtil.isNotEmpty(m.getExtra())) {
                m.setPosMerId(m.getExtra().getString("posMerId"));
                m.setTermList(m.getExtra().getString("termList"));
            }
        }
        int count = merchantPluginService.count();
        Map<String, Object> result = new HashMap<>();
        result.put("merchantPluginList", merchantPluginList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, size));
        result.put("currPage", currPage);
        result.put("code", 200);
        return result;
    }

    @GetMapping(value = "/detail/{id}")
    public String detail(@PathVariable(value = "id", required = false) Integer id, HttpServletRequest request) {
        MerchantPlugin merchantPlugin = merchantPluginService.getById(id);
        if (StringUtil.isNotEmpty(merchantPlugin.getExtra())) {
            merchantPlugin.setPosMerId(merchantPlugin.getExtra().getString("posMerId"));
            merchantPlugin.setTermList(merchantPlugin.getExtra().getString("termList"));
        }
        request.setAttribute("merchantPlugin", merchantPlugin);
        return "merchantPlugin/detail";
    }

    @PostMapping("/updatePlugin")
    @ResponseBody
    public Result<MerchantPlugin> updatePlugin(@RequestBody MerchantPlugin merchantPlugin){
        MerchantPlugin pluginServiceById = merchantPluginService.getById(merchantPlugin.getId());
        pluginServiceById.getExtra().put("termList",merchantPlugin.getTermList());
        merchantPluginService.updateById(pluginServiceById);
        return ResultGenerator.genSuccessResult("修改成功！",null);
    }
}