package com.sifang.modules.flowable.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.utils.EmployeRoleUtils;
import com.sifang.modules.flowable.constants.ItemConstants;
import com.sifang.modules.flowable.service.ItemManageService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectInstallService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
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.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import com.sifang.modules.taskdispatch.service.CrmReviewGroupMemberService;
import com.sifang.modules.taskdispatch.vo.GroupMemberInfo;
import com.sifang.modules.taskdispatch.vo.SignStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

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

    @Autowired
    private ItemManageService itemManageService;
    @Autowired
    ProjectInstallService projectInstallService;
    @Autowired
    TraderService traderService;
    @Autowired
    CrmReviewGroupMemberService groupMemberService;
    @Autowired
    PartnerService partnerService;

    //流程事项待处理页面
    @RequestMapping(value = "lcWaitDealItemPage")
    public String lcWaitDealItemPage(HttpServletRequest request, Model model){
        List<Record> provincesList = AddressUtils.getProvincesList();
        model.addAttribute("province",provincesList);
        //获取所有方案类型
        List<Record> typeList = projectInstallService.findProjectInstalls();
        model.addAttribute("type",typeList);
        return "modules/yangguang/systemSetting/flowable/item/lcWaitDealItemPage";
    }

    //流程事项待处理页面数据
    @ResponseBody
    @RequestMapping(value = "lcWaiteDealItemList")
    public String lcWaiteDealItemList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
        if(User.USER_TYPE_WXG.equals(user.getUserType())){  //平台员工
            EmployeRoleUtils.getUserDataAuthority(user.getId(),map);
        }
        itemManageService.lcWaiteDealItemList(page,map,operator,request);
        return page.renderJson();
    }

    //已处理页面
    @RequestMapping(value = "lcEndDealItemPage")
    public String lcEndDealItemPage(HttpServletRequest request, Model model){
        List<Record> provincesList = AddressUtils.getProvincesList();
        model.addAttribute("province",provincesList);
        //获取所有方案类型
        List<Record> typeList = projectInstallService.findProjectInstalls();
        model.addAttribute("type",typeList);
        return "modules/yangguang/systemSetting/flowable/item/lcEndDealItemPage";
    }

    //已处理页面数据
    @ResponseBody
    @RequestMapping(value = "lcEndDealItemList")
    public String lcEndDealItemList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
        itemManageService.lcEndDealItemList(page,map,operator);
        return page.renderJson();
    }

    @RequestMapping(value = "/proj/waitDealItemPage")
    public String projWaitDealItemPage(HttpServletRequest request, Model model){
        List<Record> provincesList = AddressUtils.getProvincesList();
        model.addAttribute("province",provincesList);
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        //获取所有方案类型
//        List<Record> typeList = projectInstallService.findProjectInstalls();
        GroupMemberInfo groupMemberInfo = groupMemberService.getGroupMemberInfo(UserUtils.getUser().getId());
        model.addAttribute("signStatus", groupMemberService.signStatus(groupMemberInfo));
        model.addAttribute("isLeader", groupMemberService.isGroupLeader(groupMemberInfo));
        model.addAttribute("userId", UserUtils.getUser().getId());
        model.addAttribute("reviewMembers", groupMemberService.findAllMembers());
        model.addAttribute("partnerList", partnerService.partnerList());
//        model.addAttribute("type",typeList);

        return "modules/yangguang/systemSetting/flowable/item/projWaitDealItemPage";
    }

    //待处理页面数据
    @ResponseBody
    @RequestMapping(value = "/proj/waiteDealItemList")
    public String projWaiteDealItemList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
        if(User.USER_TYPE_WXG.equals(user.getUserType())){  //平台员工
            EmployeRoleUtils.getUserDataAuthority(user.getId(),map);
        }
        itemManageService.projWaiteDealItemList(page,map,operator,request, ItemConstants.QUERY_TYPE_WAIT_AUDIT);
        return page.renderJson();
    }

    //待处理页面数据
    @ResponseBody
    @RequestMapping(value = "/proj/waiteDispatchItemList")
    public String waiteDispatchItemList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
//        List<Record> traderList = traderService.getTopTrader();
//        model.addAttribute("traderList", traderList);
        if(User.USER_TYPE_WXG.equals(user.getUserType())){  //平台员工
            EmployeRoleUtils.getUserDataAuthority(user.getId(),map);
        }
        itemManageService.projWaiteDealItemList(page,map,operator,request, ItemConstants.QUERY_TYPE_WAIT_ASSIGN);
        return page.renderJson();
    }

    //已处理页面
    @RequestMapping(value = "/proj/endAuditItemPage")
    public String projEndDealItemPage(HttpServletRequest request, Model model){
        List<Record> provincesList = AddressUtils.getProvincesList();
        model.addAttribute("province",provincesList);
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        //获取所有方案类型
//        List<Record> typeList = projectInstallService.findProjectInstalls();
//        model.addAttribute("type",typeList);
        model.addAttribute("reviewMembers", groupMemberService.findAllMembers());
        model.addAttribute("partnerList", partnerService.partnerList());

        return "modules/yangguang/systemSetting/flowable/item/projEndDealItemPage";
    }

    //已处理页面数据
    @ResponseBody
    @RequestMapping(value = "/proj/endDealItemList")
    public String projEndDealItemList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
        itemManageService.projEndDealItemList(page,map,operator);
        return page.renderJson();
    }

    //待分配页面
    @RequestMapping(value = "/proj/waitDispatchItemPage")
    public String projWaitDispatchItemPage(HttpServletRequest request, Model model){
        List<Record> provincesList = AddressUtils.getProvincesList();
        model.addAttribute("province",provincesList);
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        model.addAttribute("partnerList", partnerService.partnerList());
        //获取所有方案类型
//        List<Record> typeList = projectInstallService.findProjectInstalls();
//        model.addAttribute("type",typeList);
        return "modules/yangguang/systemSetting/flowable/item/projWaitDispatchItemPage";
    }

    //审单设置
    @RequestMapping(value = "/proj/auditSettingPage")
    public String projReviewSettingPage(HttpServletRequest request, Model model){
        List<Record> provincesList = AddressUtils.getProvincesList();
        model.addAttribute("province",provincesList);
        //获取所有方案类型
        List<Record> typeList = projectInstallService.findProjectInstalls();
        model.addAttribute("type",typeList);
        model.addAttribute("partners", partnerService.partnerList());
        return "modules/yangguang/systemSetting/flowable/item/projReviewSettingPage";
    }

    //优先审单设置
    @RequestMapping(value = "/proj/priorAuditSettingPage")
    public String priorAuditSettingPage(HttpServletRequest request, Model model){
        //代理商列表
        List<Record> traderList = traderService.getTopTrader();
        model.addAttribute("traderList", traderList);
        return "modules/yangguang/systemSetting/flowable/item/projPriorReviewSettingPage";
    }

    //待处理页面
    @RequestMapping(value = "waitDealItemPage")
    public String waitDealItemPage(HttpServletRequest request, Model model){
        return "modules/yangguang/systemSetting/flowable/item/waitDealItemPage";
    }

    //待处理页面数据
    @ResponseBody
    @RequestMapping(value = "waiteDealItemList")
    public String waiteDealItemList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
        if(User.USER_TYPE_WXG.equals(user.getUserType())){  //平台员工
            EmployeRoleUtils.getUserDataAuthority(user.getId(),map);
        }
        itemManageService.waiteDealItemList(page,map,operator,request);
        return page.renderJson();
    }

    //已处理页面
    @RequestMapping(value = "endDealItemPage")
    public String endDealItemPage(HttpServletRequest request, Model model){
        return "modules/yangguang/systemSetting/flowable/item/endDealItemPage";
    }

    //已处理页面数据
    @ResponseBody
    @RequestMapping(value = "endDealItemList")
    public String endDealItemList(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        User user= UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
        itemManageService.endDealItemList(page,map,operator);
        return page.renderJson();
    }

    //待处理页面数据
    @ResponseBody
    @RequestMapping(value = "getWaitDealListForHomePage")
    public Object getWaitDealListForHomePage(HttpServletRequest request){
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        page.setPage(1);
        page.setLimit(11);
        if(map.containsKey("limit") && map.get("limit") != null){
            page.setLimit(Integer.valueOf(map.get("limit")));
        }
        User user = UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user), user.getUserType());
        if(User.USER_TYPE_WXG.equals(user.getUserType())){  //平台员工
            EmployeRoleUtils.getUserDataAuthority(user.getId(),map);
        }
        map.put("employeeUserId", user.getId());
        return itemManageService.getWaitDealListForHomePage(page, map, operator, request);
    }
}
