package com.iamberry.wechat.handles.admin;

import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.agentInfo.AgentConfig;
import com.iamberry.wechat.core.entity.agentInfo.AgentTooth;
import com.iamberry.wechat.core.entity.product.ProductColor;
import com.iamberry.wechat.face.agentInfo.AgentInfoService;
import com.iamberry.wechat.face.porduct.ProductColorService;
import com.iamberry.wechat.tools.ResultInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 代理商逻辑处理类
 * Created by liuzhiwei on 2017/10/11.
 */
@Controller
@RequestMapping("/admin/agentTooth")
public class AdminAgentToothHandler {

    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private ProductColorService productColorService;

    @RequestMapping("/select_agent_config_list")
    public ModelAndView selectAgentConfigList(HttpServletRequest request,
                                              @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
                                              @RequestParam(value = "pageNO", defaultValue = "1", required = false) Integer pageNO) throws Exception {
        ModelAndView mv = new ModelAndView("admin/agentInfo/agentConfigList");
        StringBuilder url = new StringBuilder("/admin/agentTooth/select_agent_config_list");
        AgentConfig config = new AgentConfig();
        PageBean page = new PageBean();
        page.setPageSize(pageSize);
        page.setPageNumber(pageNO);
        page.initRecordBegin();
        config.setPage(page);
        //查询代理商配置金额集合
        List<AgentConfig> configList = agentInfoService.listAgentConfig(config);
        mv.addObject("configList", configList);
        mv.addObject("pageNO", pageNO);
        return mv;
    }

    @RequestMapping("/_add_agent_config")
    public ModelAndView addAgentConfigUI(HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView("admin/agentInfo/addAgentConfig");
        return mv;
    }

    /**
     * 查询商品列表
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/select_agent_and_product")
    public ResultMsg selectAgentAndProduct(HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();
        Map<String,Object> map = new HashMap<String,Object>();
        ProductColor productColor = new ProductColor();
        productColor.setPage(null);
        //查询产品颜色集合
        List<ProductColor> colorList = productColorService.selectProductColorList(productColor);
        AgentTooth agent = new AgentTooth();
        agent.setAgentStatus(1);
        //查询代理商集合
        List<AgentTooth> agentList = agentInfoService.listAgentTooth(agent);
        map.put("colorList",colorList);
        map.put("agentList",agentList);
        msg.setData(map);
        msg.setStatus(true);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        return msg;
    }

    @ResponseBody
    @RequestMapping("/add_agent_config")
    public ResultMsg addAgentConfig(HttpServletRequest request) throws  Exception {
        ResultMsg msg = new ResultMsg();
        String productColor = request.getParameter("productColor");
        String agentId = request.getParameter("sel_agent");
        if (!StringUtils.isNotEmpty(productColor)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("您没有输入优惠价请重新输入!");
        }
        if (!StringUtils.isNotEmpty(agentId)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("您没有选择代理商,请选择!");
        }
        //拆分产品id和金额
        String[] productPrice = productColor.split(",");
        List<AgentConfig> list = new ArrayList<AgentConfig>();
        for (int i = 0;i < productPrice.length;i++) {
            String[] color = productPrice[i].split(":");
            AgentConfig config = new AgentConfig();
            config.setAgentId(Integer.parseInt(agentId));
            config.setColorId(Integer.parseInt(color[0]));
            config.setConfigAmount(Integer.parseInt(color[1]));
            config.setConfigStatus(1);
            list.add(config);
        }
        int num = agentInfoService.addAgentConfigList(list);
        if (num > 0) {
            msg.setResultCode(ResultInfo.SUCCESSCODE);
            msg.setStatus(true);
            return msg;
        } else {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setMessage("添加代理商优惠价失败!");
            msg.setStatus(false);
            return msg;
        }
    }

    @RequestMapping("/_update_agent_config")
    public ModelAndView updateAgentConfig(HttpServletRequest request) throws Exception {
        String configId = request.getParameter("configId");
        ModelAndView mv = new ModelAndView("admin/agentInfo/updateAgentConfig");
        mv.addObject("configId",configId);
        return mv;
    }

    @ResponseBody
    @RequestMapping("/get_agent_config_By_Id")
    public ResultMsg getAgentConfigById(HttpServletRequest request) throws Exception{
        ResultMsg msg = new ResultMsg();
        Map<String,Object> map = new HashMap<String,Object>();
        String configId = request.getParameter("configId");
        if (!StringUtils.isNotEmpty(configId)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("该配置信息不存在!");
        }
        AgentConfig config = new AgentConfig();
        config.setConfigId(Integer.parseInt(configId));
        config.setPage(null);
        //查询代理商配置金额集合
        List<AgentConfig> configList = agentInfoService.listAgentConfig(config);

        ProductColor productColor = new ProductColor();
        productColor.setPage(null);
        //查询产品颜色集合
        List<ProductColor> colorList = productColorService.selectProductColorList(productColor);
        AgentTooth agent = new AgentTooth();
        agent.setAgentStatus(1);
        //查询代理商集合
        List<AgentTooth> agentList = agentInfoService.listAgentTooth(agent);
        map.put("colorList",colorList);
        map.put("agentList",agentList);
        map.put("config",configList.get(0));
        msg.setData(map);
        msg.setResultCode(ResultInfo.SUCCESSCODE);
        msg.setStatus(true);
        return msg;
    }

    @ResponseBody
    @RequestMapping("/update_agent_config")
    public ResultMsg updateAgentConfig(HttpServletRequest request,AgentConfig config) throws Exception{
        ResultMsg msg = new ResultMsg();
        if (!StringUtils.isNotEmpty(config.getConfigId().toString())) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            msg.setMessage("修改代理商优惠价配置失败!");
        }
        //修改代理商优惠价配置信息
        int num = agentInfoService.updateAgentConfig(config);
        if (num > 0) {
            msg.setResultCode(ResultInfo.SUCCESSCODE);
            msg.setStatus(true);
        } else {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
        }
        return msg;
    }

    @ResponseBody
    @RequestMapping("/update_agent_config_status")
    public ResultMsg updateAgentConfigStatus(HttpServletRequest request) throws Exception {
        ResultMsg msg = new ResultMsg();
        String configId = request.getParameter("configId");
        String configStatus = request.getParameter("configStatus");
        AgentConfig config = new AgentConfig();
        if (!StringUtils.isNotEmpty(configId)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
        if (!StringUtils.isNotEmpty(configStatus)) {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
            return msg;
        }
        config.setConfigId(Integer.parseInt(configId));
        config.setConfigStatus(Integer.parseInt(configStatus));
        //修改代理商优惠价配置信息
        int num = agentInfoService.updateAgentConfig(config);
        if (num > 0) {
            msg.setResultCode(ResultInfo.SUCCESSCODE);
            msg.setStatus(true);
        } else {
            msg.setResultCode(ResultInfo.ERRORCODE);
            msg.setStatus(false);
        }
        return msg;
    }
}
