package com.sifang.modules.sunshine.SystemSetting.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CrmCustomerIncomingProductPara;
import com.sifang.modules.insurance.service.InsuranceListService;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmProjectCompanyPcdDao;
import com.sifang.modules.sunshine.SystemSetting.entity.ProjectPcd;
import com.sifang.modules.sunshine.SystemSetting.entity.ProjectRentVariable;
import com.sifang.modules.sunshine.SystemSetting.form.AddAreaVo;
import com.sifang.modules.sunshine.SystemSetting.form.AddProjectVo;
import com.sifang.modules.sunshine.SystemSetting.service.PlanService;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectCompanyService;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectInfoSettingService;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectInstallService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.sys.util.GuardUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
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.multipart.MultipartFile;

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

/**
 * @author hongzq
 * 系统设置-信息设置
 */
@Controller
@RequestMapping("${adminPath}/sysSettings/projectInfoSetting")
public class ProjectInfoSettingController extends BaseExceptionController {

    @Autowired
    ProjectInfoSettingService projectInfoService;

    @Autowired
    ProjectInstallService projectInstallService;

    @Autowired
    ProjectCompanyService projectCompanyService;

    @Autowired
    private PartnerService partnerService;

    @Autowired
    InsuranceListService insuranceListService;

    @Autowired
    AddressService addressService;


    @Autowired
    PlanService planService;


    @Autowired
    CrmProjectCompanyPcdDao companyPcdDao;

    @RequestMapping(value = "/index")
    public String toProjectSetting(Model model) {
        List<Record> company=projectCompanyService.getProjectCompany("0");
        model.addAttribute("company",null);
        List<Record> productLine=projectInfoService.getProductLineList();
        model.addAttribute("productLine",productLine);
        model.addAttribute("partnerList", partnerService.getAllForChoose());
        return "modules/yangguang/systemSetting/projectInfo/projectInfoSetting";
    }

    @RequestMapping(value = "/toAdd")
    public String toAdd(String id, Model model) {
        List<Record> company = new ArrayList<>();
        if (StrKit.notBlank(id)) {
            Record setting = projectInfoService.getProjectSettingDetail(id);
            model.addAttribute("id",id);
            model.addAttribute("setting", setting);
            List<ProjectRentVariable> variables= projectInfoService.getProjectVariable(id);
            model.addAttribute("variables",variables);
            List<CrmCustomerIncomingProductPara> getProductPara = projectInfoService.getProductPara(setting.getStr("code"));
            model.addAttribute("productPara",getProductPara);
            List<ProjectPcd> getProjectArea = projectInfoService.getProjectArea(id);
            model.addAttribute("areaList",getProjectArea);
            String projectCompanyId = setting.getStr("project_company_id");
            if (StringUtil.isNotBlank(projectCompanyId)){
                String[] pcIds = projectCompanyId.split(",");
                for (String pcId : pcIds) {
                    company.add(projectCompanyService.getProjectCompanyDetail(pcId));
                }
            }
        }

        List<Record> epcCompany = projectCompanyService.getProjectCompany("1");
        model.addAttribute("companyList", company);
        model.addAttribute("epcCompany", epcCompany);
        List<Record> productLine=projectInfoService.getProductLineList();
        model.addAttribute("productLine",productLine);
        model.addAttribute("insta", projectInstallService.findProjectInstalls());
        model.addAttribute("insuranceList", insuranceListService.insuranceList());
      //  model.addAttribute("insuranceList", null);
        //model.addAttribute("insuranceList", insuranceListService.insuranceLists(null));
        model.addAttribute("partnerList", partnerService.getAllForChoose());
        List<Record> assemblySpecsList = planService.getElectricSetModel("3");
        model.addAttribute("assemblySpecsList", assemblySpecsList);

        return "modules/yangguang/systemSetting/projectInfo/addProjectInfo";
    }


    @RequestMapping(value = "/projectInfoDetail")
    public String projectInfoDetail(String id, Model model) {
        List<Record> company = new ArrayList<>();
        if (StrKit.notBlank(id)) {
            Record setting = projectInfoService.getProjectSettingDetail(id);
            model.addAttribute("id",id);
            model.addAttribute("setting", setting);
            List<ProjectRentVariable> variables= projectInfoService.getProjectVariable(id);
            model.addAttribute("variables",variables);
            List<CrmCustomerIncomingProductPara> getProductPara = projectInfoService.getProductPara(setting.getStr("code"));
            model.addAttribute("productPara",getProductPara);
            List<ProjectPcd> getProjectArea = projectInfoService.getProjectArea(id);
            model.addAttribute("areaList",getProjectArea);
            String projectCompanyId = setting.getStr("project_company_id");
            if (StringUtil.isNotBlank(projectCompanyId)){
                String[] pcIds = projectCompanyId.split(",");
                for (String pcId : pcIds) {
                    company.add(projectCompanyService.getProjectCompanyDetail(pcId));
                }
            }
        }

        List<Record> epcCompany = projectCompanyService.getProjectCompany("1");
        model.addAttribute("companyList", company);
        model.addAttribute("epcCompany", epcCompany);
        List<Record> productLine=projectInfoService.getProductLineList();
        model.addAttribute("productLine",productLine);
        model.addAttribute("insta", projectInstallService.findProjectInstalls());
        model.addAttribute("insuranceList", insuranceListService.insuranceList());
        //  model.addAttribute("insuranceList", null);
        //model.addAttribute("insuranceList", insuranceListService.insuranceLists(null));
        model.addAttribute("partnerList", partnerService.getAllForChoose());
        List<Record> assemblySpecsList = planService.getElectricSetModel("3");
        model.addAttribute("assemblySpecsList", assemblySpecsList);

        return "modules/yangguang/systemSetting/projectInfo/projectInfoDetail";
    }


    //根据epc选择保险内容


    @ResponseBody
    @RequestMapping(value = "getEpc")
    public Object getEpc(HttpServletRequest request) {
        String id = request.getParameter("id");
        List<Record> list = insuranceListService.insuranceLists(id);
        return  list;

    }




    // 启用、禁用    0.正常 1.删除 2.已禁用
    @ResponseBody
    @RequestMapping(value = "/changeProjectSettingStatus")
    public Result changeProjectSettingStatus(String id,String status){
        projectInfoService.changeProjectSettingStatus(id,status);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public Result<Void> add(@RequestBody AddProjectVo projectVo) {
        checkParams(projectVo);
        projectInfoService.add(projectVo);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/edit")
    public Result<Void> edit(@RequestBody AddProjectVo projectVo) {
        GuardUtils.notBlank(projectVo.getId(), "id不能为空");
        checkParams(projectVo);
        projectInfoService.edit(projectVo);
        return Result.ok();
    }


    //项目列表界面
    @RequestMapping(value = "addProjectComInfo")
    public String addProjectComInfo(HttpServletRequest request, Model model) {

        List<Record> province = addressService.getProvinces();//获取所有的省
        model.addAttribute("province", province);
        return "modules/yangguang/systemSetting/projectInfo/addProjectCompanyInfo";
    }

    //项目列表数据
    @ResponseBody
    @RequestMapping(value = "ProjectComInfoPage")
    public String addProjectComInfoPage(HttpServletRequest request, Model model) {
        // 获取表单数据
        String area = request.getParameter("areaList");
        List<AddAreaVo> traderIdVo= JSON.parseArray(area,AddAreaVo.class);
        for (AddAreaVo addAreaVo : traderIdVo) {
            System.out.println(addAreaVo.getArea());
        }
        Map<String, String> map = getParams(request);
        LayuiPage page = new LayuiPage(request);
        projectCompanyService.projectCompanyPage(page, map,traderIdVo);
        return page.renderJson();
    }



//    @ResponseBody
//    @RequestMapping(value = "ajaxForInventoryOrderData")
//    public Object ajaxForInventoryOrderData(HttpServletRequest request) {
//
//
//        Ascription ascription = CrmUtils.getAscription();
//        map.put("inventoryObjectId", ascription.getAscriptionId());
//        inventoryOrderService.getInventoryOrderPage(page, map);
//        return page.renderJson();
//    }


    @ResponseBody
    @RequestMapping(value = "/delete")
    public Result<Void> delete(@RequestParam(value = "ids") String ids) {
        GuardUtils.notBlank(ids, "ids不能为空");
        projectInfoService.delete(ids);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/ajaxForList")
    public Object ajaxForProjectSettings(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage<Record> page = new LayuiPage<>(request);
        projectInfoService.listProjectSettings(page,map);
        return page.renderJson();
    }

    @RequestMapping(value = "lookVariables")
    public String lookVariables(String id,Model model){
        List<ProjectRentVariable> variables= projectInfoService.getProjectVariable(id);
        model.addAttribute("variables",variables);
        return "modules/yangguang/systemSetting/projectInfo/variablesDetail";
    }

    @ResponseBody
    @RequestMapping(value = "getProjectById")
    public Object getProjectById(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        return projectInfoService.getProjectById(ids);
    }

    private void checkParams(AddProjectVo AddProjectVo) {
        GuardUtils.notBlank(AddProjectVo.getCode(), "产品编号不能为空");
        GuardUtils.notBlank(AddProjectVo.getName(), "产品名称不能为空");
        GuardUtils.notBlank(AddProjectVo.getProductLineId(), "所属产品线不能为空");
       /* GuardUtils.notBlank(AddProjectVo.getProjectCompanyId(), "所属项目公司不能为空");*/
        GuardUtils.notNull(AddProjectVo.getEpcPrice(), "EPC不能为空");
        GuardUtils.notBlank(AddProjectVo.getPayCycle(), "租金支付周期不能为空");
    }

    @ResponseBody
    @RequestMapping(value = "getProjectList")
    public Object getProjectList(){
        return projectInfoService.getProjectList();
    }

    /**
     * 导入产品信息弹框
     */
    @RequestMapping(value = "importProjectInfoForm")
    public String importProjectInfoForm(){
        return "modules/yangguang/systemSetting/projectInfo/importProjectInfoForm";
    }

    /**
     * 导入产品信息参数弹框
     */
    @RequestMapping(value = "importProjectInfoParameter")
    public String importProjectInfoParameter(){
        return "modules/yangguang/systemSetting/projectInfo/importProjectInfoParameter";
    }

    /**
     * 检验产品信息格式
     * @param file
     * @return
     * @throws IOException
     */

    @ResponseBody
    @RequestMapping(value = "checkImportProjectInfo")
    public Object checkImportProjectInfo(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("extName", StringUtil.getFileExtName(file.getOriginalFilename()));
        Map<String, Object> retMap = projectInfoService.checkImportProjectInfo(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }


    /**
     * 导入产品信息
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "importProjectInfo")
    @ResponseBody
    public JSONObject importProjectInfo(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("fileName", file.getOriginalFilename());
        Map<String, Object> retMap = projectInfoService.importProjectInfo(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }

    /**
     * 检验产品信息参数格式
     * @param file
     * @return
     * @throws IOException
     */

    @ResponseBody
    @RequestMapping(value = "checkImportProjectParameterInfo")
    public Object checkImportProjectParameterInfo(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("extName", StringUtil.getFileExtName(file.getOriginalFilename()));
        Map<String, Object> retMap = projectInfoService.checkImportProjectParameterInfo(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }


    /**
     * 导入产品参数信息
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "importProjectParameterInfo")
    @ResponseBody
    public JSONObject importProjectParameterInfo(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("fileName", file.getOriginalFilename());
        Map<String, Object> retMap = projectInfoService.importProjectParameterInfo(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }

}
