package com.sifang.modules.taskdispatch.controller;

import cn.hutool.core.util.StrUtil;
import com.sifang.modules.customer.utils.EmployeRoleUtils;
import com.sifang.modules.flowable.entity.ItemManage;
import com.sifang.modules.flowable.service.ItemManageService;
import com.sifang.modules.q7link.utils.Q7DataUtil;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmPlatformEmpDao;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.taskdispatch.dao.CrmPriorReviewSettingDao;
import com.sifang.modules.taskdispatch.dao.CrmPriorReviewSettingMemberDao;
import com.sifang.modules.taskdispatch.entity.CrmPriorReviewSetting;
import com.sifang.modules.taskdispatch.entity.CrmPriorReviewSettingMember;
import com.sifang.modules.taskdispatch.service.CrmPriorReviewSettingMemberService;
import org.springframework.ui.Model;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import com.sifang.modules.taskdispatch.service.CrmPriorReviewSettingService;
import com.sifang.modules.taskdispatch.vo.PriorReviewSettingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping(value = "${adminPath}/priorReviewSetting")
public class CrmPriorReviewSettingController extends BaseExceptionController {

    @Autowired
    private CrmPriorReviewSettingService priorReviewSettingService;

    @Autowired
    TraderService traderService;

    @Autowired
    CrmPlatformEmpDao platformEmpDao;

    @Autowired
    private CrmPriorReviewSettingDao priorReviewSettingDao;

    @Autowired
    private CrmPriorReviewSettingMemberService priorReviewSettingMemberService;

    @Autowired
    private ItemManageService itemManage;

    @RequestMapping(value = "add")
    public String add(Model model, HttpServletRequest request){
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        String bw = "并网验收组";
        String az = "安装验收组";
        String da = "档案审核组";
        String sj = "设计审核组";
        List<Record> empBw = platformEmpDao.findNotInReviewGroup(bw);
        List<Record> empAz = platformEmpDao.findNotInReviewGroup(az);
        List<Record> empDa = platformEmpDao.findNotInReviewGroup(da);
        List<Record> empSj = platformEmpDao.findNotInReviewGroup(sj);
        model.addAttribute("empBw", empBw);
        model.addAttribute("empAz", empAz);
        model.addAttribute("empDa", empDa);
        model.addAttribute("empSj", empSj);
        return "modules/yangguang/systemSetting/flowable/item/projPriorReviewSettingAdd";
    }

    @RequestMapping(value = "edit")
    public String edit(Model model, HttpServletRequest request){
        String id = request.getParameter("id");
        CrmPriorReviewSetting reviewSetting = priorReviewSettingDao.get(id);
        model.addAttribute("reviewSetting", reviewSetting);
        List<CrmPriorReviewSettingMember> memberList = priorReviewSettingMemberService.findReviewSettingMemberById(id);
        Map<String,List<CrmPriorReviewSettingMember>> map = memberList.stream().collect(Collectors.groupingBy(CrmPriorReviewSettingMember::getGroupName));
        Set ketSet = map.keySet();
        for (Object key:ketSet){
            if("设计审核".equals(key)){
                List<String> sjMainList = map.get(key).stream().filter(mem -> mem.getFlag().equals("1")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                List<String> sjSpareList = map.get(key).stream().filter(mem -> mem.getFlag().equals("2") || mem.getFlag().equals("3")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                String sjMainStr = sjMainList.get(0).toString();
                String sjSpareStr = StrUtil.join(",", sjSpareList);
                model.addAttribute("sjMainStr", sjMainStr);
                model.addAttribute("sjSpareStr", sjSpareStr);
            }else if("档案审核".equals(key)){
                List<String> daMainList = map.get(key).stream().filter(mem -> mem.getFlag().equals("1")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                List<String> daSpareList = map.get(key).stream().filter(mem -> mem.getFlag().equals("2") || mem.getFlag().equals("3")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                String daMainStr = daMainList.get(0).toString();
                String daSpareStr = StrUtil.join(",", daSpareList);
                model.addAttribute("daMainStr", daMainStr);
                model.addAttribute("daSpareStr", daSpareStr);
            }else if("安装审核".equals(key)){
                List<String> azMainList = map.get(key).stream().filter(mem -> mem.getFlag().equals("1")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                List<String> azSpareList = map.get(key).stream().filter(mem -> mem.getFlag().equals("2") || mem.getFlag().equals("3")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                String azMainStr = azMainList.get(0).toString();
                String azSpareStr = StrUtil.join(",", azSpareList);
                model.addAttribute("azMainStr", azMainStr);
                model.addAttribute("azSpareStr", azSpareStr);
            }else if("并网审核".equals(key)){
                List<String> bwMainList = map.get(key).stream().filter(mem -> mem.getFlag().equals("1")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                List<String> bwSpareList = map.get(key).stream().filter(mem -> mem.getFlag().equals("2") || mem.getFlag().equals("3")).map(CrmPriorReviewSettingMember::getEmployeeId).collect(Collectors.toList());
                String bwMainStr = bwMainList.get(0).toString();
                String bwSpareStr = StrUtil.join(",", bwSpareList);
                model.addAttribute("bwMainStr", bwMainStr);
                model.addAttribute("bwSpareStr", bwSpareStr);
            }
        }

        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        String bw = "并网验收组";
        String az = "安装验收组";
        String da = "档案审核组";
        String sj = "设计审核组";
        List<Record> empBw = platformEmpDao.findNotInReviewGroup(bw);
        List<Record> empAz = platformEmpDao.findNotInReviewGroup(az);
        List<Record> empDa = platformEmpDao.findNotInReviewGroup(da);
        List<Record> empSj = platformEmpDao.findNotInReviewGroup(sj);
        model.addAttribute("empBw", empBw);
        model.addAttribute("empAz", empAz);
        model.addAttribute("empDa", empDa);
        model.addAttribute("empSj", empSj);
        return "modules/yangguang/systemSetting/flowable/item/projPriorReviewSettingEdit";
    }

    /**
     * 优先审核列表
     */
    @ResponseBody
    @RequestMapping("/getPriorReviewSettingList")
    public Object getPriorReviewSettingList(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        priorReviewSettingService.getPriorReviewSettingList(page, map);
        return page.renderJson();
    }

    /**
     * 新增优先审核
     */
    @ResponseBody
    @RequestMapping("/addPriorReviewSetting")
    public Result addPriorReviewSetting(@RequestBody PriorReviewSettingVo reviewSettingVo) {
        User user = UserUtils.getUser();
        Operator operator = Operator.of(user);
        try {
            Result result = priorReviewSettingService.addPriorReviewSetting(reviewSettingVo, operator);
            return result;
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 修改优先审核
     */
    @ResponseBody
    @RequestMapping("/editPriorReviewSetting")
    public Result editPriorReviewSetting(@RequestBody PriorReviewSettingVo reviewSettingVo) {
        try {
            Result result = priorReviewSettingService.editPriorReviewSetting(reviewSettingVo);
            return result;
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 删除优先审核
     */
    @ResponseBody
    @RequestMapping("/deletePriorReviewSetting")
    public Result deletePriorReviewSetting(String id) {
        try {
            priorReviewSettingService.deletePriorReviewSetting(id);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 禁用/启用
     */
    @ResponseBody
    @RequestMapping("/changeStatus")
    public Result changeStatus(String id, String status, String traderId) {
        try {
            Result result = priorReviewSettingService.changeStatus(id,status,traderId);
            return result;
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 审核Tab页
     */
    @RequestMapping(value = "examineMainTab")
    public String examineMainTab(Model model, HttpServletRequest request){
        String id = request.getParameter("id");
        Map<String, String> map = new HashMap<>();
        User user= UserUtils.getUser();
        if(User.USER_TYPE_WXG.equals(user.getUserType())){
            EmployeRoleUtils.getUserDataAuthority(user.getId(),map);
        }
        map.put("employeeUserId",user.getId());
        map.put("formKeys","91,92,93,94,95,96,97,98,99,100");
        List<Record> examine = itemManage.getExamineLogList(map);
        model.addAttribute("examine", examine);
        model.addAttribute("id", id);
        return "modules/yangguang/systemSetting/flowable/item/examineMainTab";
    }

}
