package com.sifang.modules.youkua.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.PartnerConstants;
import com.sifang.modules.customer.dao.CrmAcceptingReportStockDao;
import com.sifang.modules.customer.dao.CrmArchiveSurveyStockDao;
import com.sifang.modules.customer.dao.CrmCustomerEntryStockDao;
import com.sifang.modules.customer.dao.CrmCustomerEntrySurveyRecordStockDao;
import com.sifang.modules.customer.entity.*;
import com.sifang.modules.customer.form.AddEntryReq;
import com.sifang.modules.customer.form.AddReportReq;
import com.sifang.modules.customer.form.AddReportSecondReq;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectSetting;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartner;
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.GuardUtils;
import com.sifang.modules.sys.web.BaseExceptionController;
import com.sifang.modules.utils.util.IsInteger;
import com.sifang.modules.youkua.service.NhPowerStationService;
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.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private NhPowerStationService nhPowerStationService;
    @Autowired
    private PartnerService partnerService;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CrmCustomerEntrySurveyRecordStockDao surveyRecordStockDao;
    @Autowired
    private CrmArchiveSurveyStockDao archiveSurveyStockDao;
    @Autowired
    private CrmCustomerEntryStockDao entryStockDao;
    @Autowired
    private CrmAcceptingReportStockDao reportStockDao;
    /**
     * 编辑客户信息
     */
    @RequestMapping("editCustomerInfo")
    public String editCustomerInfo(HttpServletRequest request, Model model){
        String entryId = request.getParameter("entryId");
        Record entry = nhPowerStationService.findByEntryId(entryId);
        model.addAttribute("entry",entry);
        Record surveyRecord = nhPowerStationService.findRecordByEntryId(entryId);
        model.addAttribute("surveyRecord",surveyRecord);
        //地址信息
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("pros",pros);
        String province = entry.getStr("province");
        String city = entry.getStr("city");
        //根据省查询下面的
        List<Record> citys = AddressUtils.getCityByProName(province);
        List<Record> areas = AddressUtils.getAreaByCityName(city);
        model.addAttribute("faccitys", citys);
        model.addAttribute("areas", areas);
        model.addAttribute("partner", partnerService.getById(PartnerConstants.PARTNER_ID_GF));
        return "modules/yangguang/customer/entry/stock/editCustomerInfoPage";
    }


    /**
     * 编辑勘测信息
     */
    @RequestMapping("editSurveyRecordInfo")
    public String editSurveyRecordInfo(HttpServletRequest request, Model model){
        String entryId = request.getParameter("entryId");
        Record entry = nhPowerStationService.findByEntryId(entryId);
        model.addAttribute("entry",entry);
        Record surveyRecord = nhPowerStationService.findRecordByEntryId(entryId);
        model.addAttribute("surveyRecord",surveyRecord);
        Record sur = nhPowerStationService.findArchiveSurveyByEntryId(entryId);
        model.addAttribute("sur",sur);
        Record report = nhPowerStationService.findAcceptingReportByEntryId(entryId);
        model.addAttribute("report",report);
        int attachFilesCount = 0;
        if(StringUtil.isNotBlank(report.getStr("safety_disclose_doc_img"))){
            String[] arr=report.getStr("safety_disclose_doc_img").split(",");
            attachFilesCount = arr.length;
        }
        model.addAttribute("attachFilesCount", attachFilesCount);
        //逆变器信息
        List<Record> inverterList = nhPowerStationService.findinverterList(entryId);
        model.addAttribute("inverterList",inverterList);
        //地址信息
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("pros",pros);
        String province = entry.getStr("province");
        String city = entry.getStr("city");
        //根据省查询下面的
        List<Record> citys = AddressUtils.getCityByProName(province);
        List<Record> areas = AddressUtils.getAreaByCityName(city);
        model.addAttribute("faccitys", citys);
        model.addAttribute("areas", areas);
        model.addAttribute("partner", partnerService.getById(PartnerConstants.PARTNER_ID_GF));
        return "modules/yangguang/customer/entry/stock/editSurveyRecordInfo";
    }
    /**
     * 电站详情 entryDetail
     */
    @RequestMapping("/entryDetail")
    public String entryDetail(String entryId, String opt, Model model) {
        CrmCustomerEntryStock customerEntry = entryStockDao.get(entryId);
        CrmPartner crmPartner = partnerService.getById(customerEntry.getPartnerId());
        model.addAttribute("partner", crmPartner);
        CrmCustomerEntrySurveyRecordStock surveyRecord = surveyRecordStockDao.get(entryId);
        User user = UserUtils.getUser();
        model.addAttribute("user", UserUtils.getUser());
        model.addAttribute("customerEntry", customerEntry);
        String stationName=customerEntry.getProvince()+customerEntry.getArea()+customerEntry.getTenantName()+(customerEntry.getActualCapacity().stripTrailingZeros().toPlainString())+"KW光伏电站";
        model.addAttribute("stationName", stationName);
        model.addAttribute("mobile",customerEntry.getMobile());
        model.addAttribute("idCode",customerEntry.getIdCode());
        model.addAttribute("opt", opt);
        model.addAttribute("quaimgsCount",0);
        Record sur = nhPowerStationService.findArchiveSurveyByEntryId(entryId);
        model.addAttribute("sur",sur);
        model.addAttribute("surveyRecord", surveyRecord);
        if (StringUtil.isNotBlank(surveyRecord.getHouseOwnership())) {
            String[] arr = surveyRecord.getHouseOwnership().split(",");
            model.addAttribute("quaimgs", arr);
            model.addAttribute("quaimgsCount", arr.length);
        }
        //逆变器信息
        List<Record> inverterList = nhPowerStationService.findinverterList(entryId);
        model.addAttribute("nbqList",inverterList);
        //逆变器信息
        List<Record> zjList = nhPowerStationService.findZjList(entryId);
        model.addAttribute("zjList",zjList);
        CrmAcceptingReportStock reportStock = reportStockDao.get(entryId);
        model.addAttribute("report",reportStock);
        return "modules/yangguang/customer/entry/stock/stockEntryDetail";
    }
    /**
     * 保存勘测信息
     */
    @ResponseBody
    @RequestMapping(value = "updateSurveyRecordInfo")
    public Object updateSurveyRecordInfo(@RequestBody AddEntryReq req) {
        return nhPowerStationService.updateSurveyRecordInfo(req);
    }

    /**
     * 去编辑完工信息页面
     */
    @RequestMapping("editWGInfo")
    public String editWGInfo (HttpServletRequest request, Model model){
        String entryId = request.getParameter("entryId");
        CrmCustomerEntryStock entry = entryStockDao.get(entryId);
        model.addAttribute("customerEntry",entry);
        CrmAcceptingReportStock reportStock = reportStockDao.get(entryId);
        model.addAttribute("report",reportStock);
        int filingDocumentPhotoCount = 0;
        if(StringUtil.isNotBlank(reportStock.getOtherFiles())){
            String[] arr=reportStock.getOtherFiles().split(",");
            filingDocumentPhotoCount = arr.length;
        }
        model.addAttribute("filingDocumentPhotoCount", filingDocumentPhotoCount);
        int discloseDocImgCount = 0;
        if(StringUtil.isNotBlank(reportStock.getDiscloseDocImg())){
            String[] arr=reportStock.getDiscloseDocImg().split(",");
            discloseDocImgCount = arr.length;
        }
        model.addAttribute("discloseDocImgCount", discloseDocImgCount);
        //逆变器信息
        List<Record> inverterList = nhPowerStationService.findinverterList(entryId);
        model.addAttribute("nbqList",inverterList);
        //逆变器信息
        List<Record> zjList = nhPowerStationService.findZjList(entryId);
        model.addAttribute("zjList",zjList);
        return "modules/yangguang/customer/entry/stock/editWGInfoPage";
    }

    @ResponseBody
    @RequestMapping("/updateWGInfo")
    public Result<Void> updateWGInfo( AddReportReq req) {
        GuardUtils.notBlank(req.getEntryId(), "entryId不能为空");
        try {
            nhPowerStationService.updateWGInfo(req);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 去编辑并网信息页面
     */
    @RequestMapping("editBWInfo")
    public String editBWInfo (HttpServletRequest request, Model model){
        String entryId = request.getParameter("entryId");
        CrmCustomerEntryStock entry = entryStockDao.get(entryId);
        model.addAttribute("entry",entry);
        CrmAcceptingReportStock reportStock = reportStockDao.get(entryId);
        int supplementUrlCount = 0;
        if(StringUtil.isNotBlank(reportStock.getSupplementUrl())){
            String[] arr=reportStock.getSupplementUrl().split(",");
            supplementUrlCount = arr.length;
        }
        model.addAttribute("supplementUrlCount", supplementUrlCount);
        model.addAttribute("report",reportStock);
        //地址信息
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("pros", pros);
        String province = entry.getElectricProvince();
        String city = entry.getElectricCity();
        //根据省查询下面的
        List<Record> citys = AddressUtils.getCityByProName(province);
        List<Record> areas = AddressUtils.getAreaByCityName(city);
        model.addAttribute("faccitys", citys);
        model.addAttribute("areas", areas);
        List<Record> procodes = addressService.getProviceCodeList();
        model.addAttribute("procodes", procodes);
        return "modules/yangguang/customer/entry/stock/editBWInfoPage";
    }

    @ResponseBody
    @RequestMapping("/updateBWInfo")
    public Result<Void> updateBWInfo(@RequestBody  AddReportSecondReq req) {
        GuardUtils.notBlank(req.getEntryId(), "entryId不能为空");
        try {
            nhPowerStationService.updateBWInfo(req);
            return Result.ok();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 同步状态
     */
    @ResponseBody
    @RequestMapping("tbStatus")
    public Result tbStatus(@RequestBody List<String> ids){
        return nhPowerStationService.tbStatus(ids);
    }
    /**
     * 推送客户信息
     */
    @ResponseBody
    @RequestMapping("inputCustomerInfo")
    public Result inputCustomerInfo(String entryId){
        try {
            nhPowerStationService.stockEntryPush(entryId);
        }catch (IllegalArgumentException e){
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }
    /**
     * 推送电站信息
     */
    @ResponseBody
    @RequestMapping("inputStockEntryInfo")
    public Result inputStockEntryInfo(String entryId){
        try {
            nhPowerStationService.stockEntryStationInputPiece(entryId);
        }catch (IllegalArgumentException e){
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }


    /**
     * 推送新系统存量电站客户信息
     */
    @ResponseBody
    @RequestMapping("customerEntryPush")
    public Result customerEntryPush(String entryId){
        try {
            User user = UserUtils.getUser();
            Operator operator = Operator.of(user);
            nhPowerStationService.customerEntryPush(entryId,operator);
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

    /**
     * 推送新系统存量电站档案信息
     */
    @ResponseBody
    @RequestMapping("inputCustomerEntryInfo")
    public Result inputCustomerEntryInfo(String entryId){
        try {
            User user = UserUtils.getUser();
            Operator operator = Operator.of(user);
            nhPowerStationService.inputCustomerEntryInfo(entryId,operator);
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }

}
