package com.sifang.modules.changes.service;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.changes.dao.CrmCompletionInfoChangeDao;
import com.sifang.modules.changes.dao.CustomerEntryChangeRecordDao;
import com.sifang.modules.changes.dao.CustomerEntryCompletionChangeRecordDetailDao;
import com.sifang.modules.changes.entity.CustomerEntryCompletionChangeRecord;
import com.sifang.modules.changes.entity.CustomerEntryCompletionChangeRecordDetail;
import com.sifang.modules.changes.vo.CrmCompletionInfoChangeDTO;
import com.sifang.modules.changes.vo.SurveyAuditVo;
import com.sifang.modules.customer.Service.CustomerEntryMsgService;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.customer.Service.GuangFaManagemnetService;
import com.sifang.modules.customer.Service.OperatingLeaseService;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.customer.constant.PartnerConstants;
import com.sifang.modules.customer.constant.SourceConstants;
import com.sifang.modules.customer.dao.*;
import com.sifang.modules.customer.entity.*;
import com.sifang.modules.customer.form.AddReportReq;
import com.sifang.modules.flowable.constants.ItemConstants;
import com.sifang.modules.flowable.dao.ItemManageDao;
import com.sifang.modules.flowable.dao.ItemTaskDetailDao;
import com.sifang.modules.flowable.entity.ItemManage;
import com.sifang.modules.flowable.utils.FormUtils;
import com.sifang.modules.platform.utils.PlantformUtils;
import com.sifang.modules.setting.constant.ProcessSetConstants;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.common.utils.IdGen;
import com.sifang.modules.sys.dao.FormSetDao;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.FormSetUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.taskdispatch.dao.CrmReviewDispatchDao;
import com.sifang.modules.taskdispatch.service.CrmPriorReviewSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hz
 */

@Service

public class CrmCompletionInfoChangeService extends BaseService {

    private Logger loger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CrmCompletionInfoChangeDao changeDao;

    @Autowired
    CrmAcceptingReportDao reportDao;

    @Autowired
    CustomerEntryDao entryDao;

    @Autowired
    CustomerEntryMsgService entryMsgService;

    @Autowired
    CustomerEntrySurveyRecordDao surveyRecordDao;

    @Autowired
    CustomerEntryService entryService;

    @Autowired
    private CrmCustomerIncomingApplicationDao applicationDao;

    @Autowired
    private ItemManageDao itemManageDao;
    @Autowired
    private CrmReviewDispatchDao reviewDispatchDao;

    @Autowired
    private ItemTaskDetailDao itemTaskDetailDao;

    @Autowired
    private OperatingLeaseService operatingLeaseService;
    @Autowired
    private GuangFaManagemnetService guangFaManagemnetService;
    @Autowired
    private CustomerEntryChangeRecordDao customerEntryChangeRecordDao;

    @Autowired
    private FormSetDao formSetDao;

    @Autowired
    private CrmAcceptingReportDetailDao reportDetailDao;

    @Autowired
    private CustomerEntryCompletionChangeRecordDetailDao changeRecordDetailDao;

    @Autowired
    private CrmPriorReviewSettingService crmPriorReviewSettingService;

    public void listData(LayuiPage<Record> page, Map<String, String> reqMap, String flag, Operator operator) {
        page.setData(changeDao.selectList(page, reqMap, flag, operator));
    }

    public CustomerEntryCompletionChangeRecord selectById(String id) {
        return changeDao.get(id);
    }

    public CustomerEntryCompletionChangeRecord selectPre(String entryId, int num) {
        return changeDao.selectPre(entryId, num);
    }

    /**
     * 添加变更记录
     * @param recordVo
     */
    @Transactional(rollbackFor = Exception.class)
    public Result addChangeRecord(CrmCompletionInfoChangeDTO recordVo) {
        if (StringUtil.isAnyBlank(recordVo.getEntryId())) {
            return Result.fail("缺少必要数据");
        }
        if (changeDao.selectNoAudited(recordVo.getEntryId(), null) > 0) {
            return Result.fail("该电站有待审核的变更记录，请审核后再提交");
        }
        if (customerEntryChangeRecordDao.getIfHaveCompleteChange(recordVo.getEntryId()) > 0) {
            return Result.fail("该电站有待资方审核的变更记录，请审核后再提交");
        }
        CustomerEntry entry = entryDao.get(recordVo.getEntryId());
        CrmAcceptingReport report = reportDao.get(entry.getReceivingReportId());
        CustomerEntrySurveyRecord surveyRecord = surveyRecordDao.findOneByEntryId(recordVo.getEntryId());

        if (StringUtil.equals(recordVo.getOtherImgs1(), report.getOtherImgs1()) && StringUtil.equals(recordVo.getFangChan(), surveyRecord.getHouseOwnership())
                && StringUtil.equals(recordVo.getOtherImgs2(), report.getOtherImgs2())
                && StringUtil.equals(recordVo.getOtherImgs3(), report.getOtherImgs3()) && StringUtil.equals(recordVo.getOtherImgs4(), report.getOtherImgs4())
                && StringUtil.equals(recordVo.getDzjc1(), report.getDzjc1()) && StringUtil.equals(recordVo.getDzjc2(), report.getDzjc2())
                && StringUtil.equals(recordVo.getZjImg1(), report.getZjImg1()) && StringUtil.equals(recordVo.getZjImg2(), report.getZjImg2())
                && StringUtil.equals(recordVo.getZjImg3(), report.getZjImg3()) && StringUtil.equals(recordVo.getZjImg4(), report.getZjImg4())
                && StringUtil.equals(recordVo.getZjImg5(), report.getZjImg5()) && StringUtil.equals(recordVo.getComponentImg1(), report.getComponentImg1())
                && StringUtil.equals(recordVo.getComponentImg2(), report.getComponentImg2()) && StringUtil.equals(recordVo.getSizeMeasureImgs(), report.getSizeMeasureImgs())
                && StringUtil.equals(recordVo.getFixOtherImg(), report.getFixOtherImg()) && StringUtil.equals(recordVo.getTotalImgs(), report.getTotalImgs())
                && StringUtil.equals(recordVo.getBwInnerImg(), report.getBwInnerImg()) && StringUtil.equals(recordVo.getDcImgs(), report.getDcImgs())
                && StringUtil.equals(recordVo.getNbqZbImg(), report.getNbqZbImg()) && StringUtil.equals(recordVo.getDqsbAnImgOne(), report.getDqsbAnImgOne())
                && StringUtil.equals(recordVo.getNbqPlateImg(), report.getNbqPlateImg()) && StringUtil.equals(recordVo.getZjPlateImg(), report.getZjPlateImg())
                && StringUtil.equals(recordVo.getOpenWayVoltageImgs(), report.getOpenWayVoltageImgs()) && StringUtil.equals(recordVo.getJdImg1(), report.getJdImg1())
                && StringUtil.equals(recordVo.getJdImg2(), report.getJdImg2()) && StringUtil.equals(recordVo.getJdImg3(), report.getJdImg3())
                && StringUtil.equals(recordVo.getJdImg4(), report.getJdImg4()) && StringUtil.equals(recordVo.getJdImg5(), report.getJdImg5())
                && StringUtil.equals(recordVo.getJdImg6(), report.getJdImg6()) && StringUtil.equals(recordVo.getFilingDocumentPhoto(), surveyRecord.getFilingDocumentPhoto())
                && StringUtil.equals(recordVo.getZjReport(), report.getZjReport()) && StringUtil.equals(recordVo.getOtherFiles(), report.getOtherFiles())
                && StringUtil.equals(recordVo.getSafetyWarningImg2(), report.getSafetyWarningImg2()) && StringUtil.equals(recordVo.getBkImg1(), report.getBkImg1())
                && StringUtil.equals(recordVo.getSafetyWarningImg3(), report.getSafetyWarningImg3()) && StringUtil.equals(recordVo.getBkImg3(), report.getBkImg3())
                && StringUtil.equals(recordVo.getSafetyWarningImg(), report.getSafetyWarningImg()) && StringUtil.equals(recordVo.getYkImg1(), report.getYkImg1())
                && StringUtil.equals(recordVo.getZjImg8(), report.getZjImg8()) && StringUtil.equals(recordVo.getNbqZlcImg(), report.getNbqZlcImg())
                && StringUtil.equals(recordVo.getZjImg9(), report.getZjImg9()) && StringUtil.equals(recordVo.getBwJdImg(), report.getBwJdImg())
                && StringUtil.equals(recordVo.getZjImg10(), report.getZjImg10()) && StringUtil.equals(recordVo.getJdImg8(), report.getJdImg8())
                && StringUtil.equals(recordVo.getZjImg11(), report.getZjImg11()) && StringUtil.equals(recordVo.getJdImg9(), report.getJdImg9())
                && StringUtil.equals(recordVo.getZjImg12(), report.getZjImg12()) && StringUtil.equals(recordVo.getJdImg10(), report.getJdImg10())
                && StringUtil.equals(recordVo.getZjImg13(), report.getZjImg13()) && StringUtil.equals(recordVo.getJdImg11(), report.getJdImg11())
                && StringUtil.equals(recordVo.getZjImg14(), report.getZjImg14()) && StringUtil.equals(recordVo.getFsImg1(), report.getFsImg1())
                && StringUtil.equals(recordVo.getComponentImg3(), report.getComponentImg3()) && StringUtil.equals(recordVo.getFsImg2(), report.getFsImg2())
                && StringUtil.equals(recordVo.getComponentImg4(), report.getComponentImg4()) && StringUtil.equals(recordVo.getFsImg3(), report.getFsImg3())
                && StringUtil.equals(recordVo.getOtherImgs5(), report.getOtherImgs5()) && StringUtil.equals(recordVo.getOtherImgs6(), report.getOtherImgs6())
                && StringUtil.equals(recordVo.getSafetyDiscloseDocImg(), report.getSafetyDiscloseDocImg()) && StringUtil.equals(recordVo.getBkImg2(), report.getBkImg2())
                && StringUtil.equals(recordVo.getKgConditionFiles(), report.getKgConditionFiles()) && StringUtil.equals(recordVo.getSupplementVideoUrl(), report.getSupplementVideoUrl())
                && StringUtil.equals(recordVo.getWaterMudClampImgs(), report.getWaterMudClampImgs())
        ) {
            return Result.fail("请至少修改一条数据");
        }

        //获取当前已变更次数
        User user = UserUtils.getUser();
        Date now = new Date();
        CustomerEntryCompletionChangeRecord record = new CustomerEntryCompletionChangeRecord();
        Integer maxNum = changeDao.selectMaxNum(recordVo.getEntryId());
        if (maxNum == null) {
            record.setNum(1);
            //把第一次的存起来
            CustomerEntryCompletionChangeRecord zeroRecord = new CustomerEntryCompletionChangeRecord();
            zeroRecord.setNum(0);
            zeroRecord.setId(IdGen.uuid());
            zeroRecord.setNumber(PlantformUtils.getOrderNumber());
            zeroRecord.setEntryId(entry.getId());
            zeroRecord.setPowerStationCode(entry.getPowerStationCode());
            zeroRecord.setHouseOwnership(surveyRecord.getHouseOwnership());
            zeroRecord.setOtherImgs1(report.getOtherImgs1());
            zeroRecord.setOtherImgs2(report.getOtherImgs2());
            zeroRecord.setOtherImgs3(report.getOtherImgs3());
            zeroRecord.setOtherImgs4(report.getOtherImgs4());
            zeroRecord.setDzjc1(report.getDzjc1());
            zeroRecord.setDzjc2(report.getDzjc2());
            zeroRecord.setZjImg1(report.getZjImg1());
            zeroRecord.setZjImg2(report.getZjImg2());
            zeroRecord.setZjImg3(report.getZjImg3());
            zeroRecord.setZjImg4(report.getZjImg4());
            zeroRecord.setZjImg5(report.getZjImg5());
            zeroRecord.setComponentImg1(report.getComponentImg1());
            zeroRecord.setComponentImg2(report.getComponentImg2());
            zeroRecord.setSizeMeasureImgs(report.getSizeMeasureImgs());
            zeroRecord.setFixOtherImg(report.getFixOtherImg());
            zeroRecord.setTotalImgs(report.getTotalImgs());
            zeroRecord.setBwInnerImg(report.getBwInnerImg());
            zeroRecord.setDcImgs(report.getDcImgs());
            zeroRecord.setNbqZbImg(report.getNbqZbImg());
            zeroRecord.setDqsbAnImgOne(report.getDqsbAnImgOne());
            zeroRecord.setNbqPlateImg(report.getNbqPlateImg());
            zeroRecord.setZjPlateImg(report.getZjPlateImg());
            zeroRecord.setOpenWayVoltageImgs(report.getOpenWayVoltageImgs());
            zeroRecord.setJdImg1(report.getJdImg1());
            zeroRecord.setJdImg2(report.getJdImg2());
            zeroRecord.setJdImg3(report.getJdImg3());
            zeroRecord.setJdImg4(report.getJdImg4());
            zeroRecord.setJdImg5(report.getJdImg5());
            zeroRecord.setJdImg6(report.getJdImg6());
            zeroRecord.setFilingDocumentPhoto(surveyRecord.getFilingDocumentPhoto());
            zeroRecord.setZjReport(report.getZjReport());
            zeroRecord.setOtherFiles(report.getOtherFiles());
            zeroRecord.setSafetyWarningImg(report.getSafetyWarningImg());
            zeroRecord.setSafetyWarningImg2(report.getSafetyWarningImg2());
            zeroRecord.setSafetyWarningImg3(report.getSafetyWarningImg3());
            zeroRecord.setSafetyDiscloseDocImg(report.getSafetyDiscloseDocImg());
            zeroRecord.setZjImg8(report.getZjImg8());
            zeroRecord.setZjImg9(report.getZjImg9());
            zeroRecord.setZjImg10(report.getZjImg10());
            zeroRecord.setZjImg11(report.getZjImg11());
            zeroRecord.setZjImg12(report.getZjImg12());
            zeroRecord.setZjImg13(report.getZjImg13());
            zeroRecord.setZjImg14(report.getZjImg14());
            zeroRecord.setComponentImg3(report.getComponentImg3());
            zeroRecord.setComponentImg4(report.getComponentImg4());
            zeroRecord.setBkImg1(report.getBkImg1());
            zeroRecord.setBkImg2(report.getBkImg2());
            zeroRecord.setBkImg3(report.getBkImg3());
            zeroRecord.setYkImg1(report.getYkImg1());
            zeroRecord.setNbqZlcImg(report.getNbqZlcImg());
            zeroRecord.setBwJdImg(report.getBwJdImg());
            zeroRecord.setJdImg8(report.getJdImg8());
            zeroRecord.setJdImg9(report.getJdImg9());
            zeroRecord.setJdImg10(report.getJdImg10());
            zeroRecord.setJdImg11(report.getJdImg11());
            zeroRecord.setFsImg1(report.getFsImg1());
            zeroRecord.setFsImg2(report.getFsImg2());
            zeroRecord.setFsImg3(report.getFsImg3());
            zeroRecord.setOtherImgs5(report.getOtherImgs5());
            zeroRecord.setOtherImgs6(report.getOtherImgs6());
            zeroRecord.setStatus("1");
            zeroRecord.setCreator(CrmUtils.getUserXM(user));
            zeroRecord.setCreateBy(user.getId());
            zeroRecord.setCreateTime(now);
            zeroRecord.setSupplementVideoUrl(report.getSupplementVideoUrl());
            zeroRecord.setCompleteRemark(report.getCompleteRemark());
            zeroRecord.setKgConditionFiles(report.getKgConditionFiles());
            zeroRecord.setWaterMudClampImgs(report.getWaterMudClampImgs());
            changeDao.save(zeroRecord);
        } else {
            record.setNum(maxNum + 1);
        }
        record.setId(IdGen.uuid());
        record.setNumber(PlantformUtils.getOrderNumber());
        record.setEntryId(recordVo.getEntryId());
        record.setPowerStationCode(entry.getPowerStationCode());
        record.setHouseOwnership(recordVo.getFangChan());
        record.setOtherImgs1(recordVo.getOtherImgs1());
        record.setOtherImgs2(recordVo.getOtherImgs2());
        record.setOtherImgs3(recordVo.getOtherImgs3());
        record.setOtherImgs4(recordVo.getOtherImgs4());
        record.setDzjc1(recordVo.getDzjc1());
        record.setDzjc2(recordVo.getDzjc2());
        record.setZjImg1(recordVo.getZjImg1());
        record.setZjImg2(recordVo.getZjImg2());
        record.setZjImg3(recordVo.getZjImg3());
        record.setZjImg4(recordVo.getZjImg4());
        record.setZjImg5(recordVo.getZjImg5());
        record.setComponentImg1(recordVo.getComponentImg1());
        record.setComponentImg2(recordVo.getComponentImg2());
        record.setSizeMeasureImgs(recordVo.getSizeMeasureImgs());
        record.setFixOtherImg(recordVo.getFixOtherImg());
        record.setTotalImgs(recordVo.getTotalImgs());
        record.setBwInnerImg(recordVo.getBwInnerImg());
        record.setDcImgs(recordVo.getDcImgs());
        record.setNbqZbImg(recordVo.getNbqZbImg());
        record.setDqsbAnImgOne(recordVo.getDqsbAnImgOne());
        record.setNbqPlateImg(recordVo.getNbqPlateImg());
        record.setZjPlateImg(recordVo.getZjPlateImg());
        record.setOpenWayVoltageImgs(recordVo.getOpenWayVoltageImgs());
        record.setJdImg1(recordVo.getJdImg1());
        record.setJdImg2(recordVo.getJdImg2());
        record.setJdImg3(recordVo.getJdImg3());
        record.setJdImg4(recordVo.getJdImg4());
        record.setJdImg5(recordVo.getJdImg5());
        record.setJdImg6(recordVo.getJdImg6());
        record.setFilingDocumentPhoto(recordVo.getFilingDocumentPhoto());
        record.setZjReport(recordVo.getZjReport());
        record.setOtherFiles(recordVo.getOtherFiles());
        record.setSafetyWarningImg(recordVo.getSafetyWarningImg());
        record.setSafetyWarningImg2(recordVo.getSafetyWarningImg2());
        record.setSafetyWarningImg3(recordVo.getSafetyWarningImg3());
        record.setSafetyDiscloseDocImg(recordVo.getSafetyDiscloseDocImg());
        record.setZjImg8(recordVo.getZjImg8());
        record.setZjImg9(recordVo.getZjImg9());
        record.setZjImg10(recordVo.getZjImg10());
        record.setZjImg11(recordVo.getZjImg11());
        record.setZjImg12(recordVo.getZjImg12());
        record.setZjImg13(recordVo.getZjImg13());
        record.setZjImg14(recordVo.getZjImg14());
        record.setComponentImg3(recordVo.getComponentImg3());
        record.setComponentImg4(recordVo.getComponentImg4());
        record.setBkImg1(recordVo.getBkImg1());
        record.setBkImg2(recordVo.getBkImg2());
        record.setBkImg3(recordVo.getBkImg3());
        record.setYkImg1(recordVo.getYkImg1());
        record.setNbqZlcImg(recordVo.getNbqZlcImg());
        record.setBwJdImg(recordVo.getBwJdImg());
        record.setJdImg8(recordVo.getJdImg8());
        record.setJdImg9(recordVo.getJdImg9());
        record.setJdImg10(recordVo.getJdImg10());
        record.setJdImg11(recordVo.getJdImg11());
        record.setFsImg1(recordVo.getFsImg1());
        record.setFsImg2(recordVo.getFsImg2());
        record.setFsImg3(recordVo.getFsImg3());
        record.setOtherImgs5(recordVo.getOtherImgs5());
        record.setOtherImgs6(recordVo.getOtherImgs6());
        record.setReason(recordVo.getReason());
        record.setSupplementVideoUrl(recordVo.getSupplementVideoUrl());
        record.setCompleteRemark(recordVo.getCompleteRemark());
        record.setKgConditionFiles(recordVo.getKgConditionFiles());
        record.setStatus("0");
        record.setCreator(CrmUtils.getUserXM(user));
        record.setCreateBy(user.getId());
        record.setCreateTime(now);
        record.setWaterMudClampImgs(recordVo.getWaterMudClampImgs());
        //清空完工变更备注
        record.setRemarks("");
        changeDao.save(record);

        entryMsgService.saveCustomerEntryMsg(Operator.of(user), entry.getId(), CustomerEntryConstants.ENTRY_ADD_CHANGE
                , String.format("第%d次提交电站完工信息变更申请", record.getNum()), null);

        //所有的变更审核 要走待办事项审批流程，默认审批人为原审核人员  完工信息变更审核 查询的是安装验收/完工信息变更审核人；
        String stationName = CrmUtils.getStationName(entry);
        ItemManage itemManage=itemManageDao.addItemChange(FormUtils.ENTRY_CHANGE_WGBG,entry.getId(),record.getId(),"完工信息变更("+stationName+"-"+entry.getPowerStationCode()+")-"+ DateUtils.formatDate(now,"yyyy-MM-dd HH:mm:ss"),now,user.getUserName());
        //获取最近一次 档案审核/客户信息变更审核人
        String userId=itemManageDao.getLastAuditUserId("93,96",entry.getId());
        if(StringUtil.isNotBlank(userId)){
//            itemManage.setAssignAuditUserId(userId);
            itemManage.setPreferUserId(userId);
        }
        itemManageDao.save(itemManage);
        //大代理商优先审核
        String groupName = "安装审核";
        crmPriorReviewSettingService.traderPriorityReview(entry.getCreateTraderId(),itemManage.getId(),groupName);
        return Result.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    public Result addHRChangeRecord(AddReportReq req){
        if (changeDao.selectNoAudited(req.getEntryId(), null) > 0) {
            return Result.fail("该电站有待审核的变更记录，请审核后再提交");
        }
        if (customerEntryChangeRecordDao.getIfHaveCompleteChange(req.getEntryId()) > 0) {
            return Result.fail("该电站有待资方审核的变更记录，请审核后再提交");
        }

        CustomerEntry entry = entryDao.get(req.getEntryId());
        // 校验必填项
        List<Record> checkList = formSetDao.findListByCondition("09", entry.getPartnerId());
        Result result = entryService.requiredCheckForInstallHr(req, checkList);
        if (!"200".equals(result.getCode())) {
           return result;
        }
        CustomerEntrySurveyRecord surveyRecord = surveyRecordDao.findOneByEntryId(entry.getId());

        List<CrmAcceptingReportDetail> oldReportDetailList = reportDetailDao.findByEntryId(entry.getId());
        Map<String,CrmAcceptingReportDetail> oldEntryReportMap = oldReportDetailList.stream().collect(Collectors.toMap(e -> e.getProjectId(), e -> e, (a, b) -> a));
        List<CrmAcceptingReportDetail>  changeRecordDetailList=req.getCompleteList();
        /*int sameCount=0;
        if(StringUtil.equals(req.getFilingDocumentCode(),surveyRecord.getFilingDocumentCode()) && StringUtil.equals(req.getFilingDocumentPhoto(),surveyRecord.getFilingDocumentPhoto())){
            sameCount++;
        }
        for(CrmAcceptingReportDetail recordVo:changeRecordDetailList){
                CrmAcceptingReportDetail report=oldEntryReportMap.get(recordVo.getProjectId());
            if (StringUtil.equals(recordVo.getOtherImgs1(), report.getOtherImgs1())
                    && StringUtil.equals(recordVo.getOtherImgs2(), report.getOtherImgs2())
                    && StringUtil.equals(recordVo.getOtherImgs3(), report.getOtherImgs3()) && StringUtil.equals(recordVo.getOtherImgs4(), report.getOtherImgs4())
                    && StringUtil.equals(recordVo.getDzjc1(), report.getDzjc1()) && StringUtil.equals(recordVo.getDzjc2(), report.getDzjc2())
                    && StringUtil.equals(recordVo.getZjImg1(), report.getZjImg1()) && StringUtil.equals(recordVo.getZjImg2(), report.getZjImg2())
                    && StringUtil.equals(recordVo.getZjImg3(), report.getZjImg3()) && StringUtil.equals(recordVo.getZjImg4(), report.getZjImg4())
                    && StringUtil.equals(recordVo.getZjImg5(), report.getZjImg5()) && StringUtil.equals(recordVo.getComponentImg1(), report.getComponentImg1())
                    && StringUtil.equals(recordVo.getComponentImg2(), report.getComponentImg2()) && StringUtil.equals(recordVo.getSizeMeasureImgs(), report.getSizeMeasureImgs())
                    && StringUtil.equals(recordVo.getFixOtherImg(), report.getFixOtherImg()) && StringUtil.equals(recordVo.getTotalImgs(), report.getTotalImgs())
                    && StringUtil.equals(recordVo.getBwInnerImg(), report.getBwInnerImg()) && StringUtil.equals(recordVo.getDcImgs(), report.getDcImgs())
                    && StringUtil.equals(recordVo.getNbqZbImg(), report.getNbqZbImg()) && StringUtil.equals(recordVo.getDqsbAnImgOne(), report.getDqsbAnImgOne())
                    && StringUtil.equals(recordVo.getNbqPlateImg(), report.getNbqPlateImg()) && StringUtil.equals(recordVo.getZjPlateImg(), report.getZjPlateImg())
                    && StringUtil.equals(recordVo.getOpenWayVoltageImgs(), report.getOpenWayVoltageImgs()) && StringUtil.equals(recordVo.getJdImg1(), report.getJdImg1())
                    && StringUtil.equals(recordVo.getJdImg2(), report.getJdImg2()) && StringUtil.equals(recordVo.getJdImg3(), report.getJdImg3())
                    && StringUtil.equals(recordVo.getJdImg4(), report.getJdImg4()) && StringUtil.equals(recordVo.getJdImg5(), report.getJdImg5())
                    && StringUtil.equals(recordVo.getJdImg6(), report.getJdImg6())
                    && StringUtil.equals(recordVo.getZjReport(), report.getZjReport()) && StringUtil.equals(recordVo.getOtherFiles(), report.getOtherFiles())
                    && StringUtil.equals(recordVo.getSafetyWarningImg2(), report.getSafetyWarningImg2()) && StringUtil.equals(recordVo.getBkImg1(), report.getBkImg1())
                    && StringUtil.equals(recordVo.getSafetyWarningImg3(), report.getSafetyWarningImg3()) && StringUtil.equals(recordVo.getBkImg3(), report.getBkImg3())
                    && StringUtil.equals(recordVo.getSafetyWarningImg(), report.getSafetyWarningImg()) && StringUtil.equals(recordVo.getYkImg1(), report.getYkImg1())
                    && StringUtil.equals(recordVo.getZjImg8(), report.getZjImg8()) && StringUtil.equals(recordVo.getNbqZlcImg(), report.getNbqZlcImg())
                    && StringUtil.equals(recordVo.getZjImg9(), report.getZjImg9()) && StringUtil.equals(recordVo.getBwJdImg(), report.getBwJdImg())
                    && StringUtil.equals(recordVo.getZjImg10(), report.getZjImg10()) && StringUtil.equals(recordVo.getJdImg8(), report.getJdImg8())
                    && StringUtil.equals(recordVo.getZjImg11(), report.getZjImg11()) && StringUtil.equals(recordVo.getJdImg9(), report.getJdImg9())
                    && StringUtil.equals(recordVo.getZjImg12(), report.getZjImg12()) && StringUtil.equals(recordVo.getJdImg10(), report.getJdImg10())
                    && StringUtil.equals(recordVo.getZjImg13(), report.getZjImg13()) && StringUtil.equals(recordVo.getJdImg11(), report.getJdImg11())
                    && StringUtil.equals(recordVo.getZjImg14(), report.getZjImg14()) && StringUtil.equals(recordVo.getFsImg1(), report.getFsImg1())
                    && StringUtil.equals(recordVo.getComponentImg3(), report.getComponentImg3()) && StringUtil.equals(recordVo.getFsImg2(), report.getFsImg2())
                    && StringUtil.equals(recordVo.getComponentImg4(), report.getComponentImg4()) && StringUtil.equals(recordVo.getFsImg3(), report.getFsImg3())
                    && StringUtil.equals(recordVo.getOtherImgs5(), report.getOtherImgs5()) && StringUtil.equals(recordVo.getOtherImgs6(), report.getOtherImgs6())
                    && StringUtil.equals(recordVo.getSafetyDiscloseDocImg(), report.getSafetyDiscloseDocImg()) && StringUtil.equals(recordVo.getBkImg2(), report.getBkImg2())
                    && StringUtil.equals(recordVo.getKgConditionFiles(), report.getKgConditionFiles()) && StringUtil.equals(recordVo.getSupplementVideoUrl(), report.getSupplementVideoUrl())
            ) {
                sameCount++;
            }
        }
        if(sameCount==changeRecordDetailList.size()){
            return Result.fail("请至少修改一条数据");
        }*/
        List<CustomerEntryCompletionChangeRecordDetail> details=new ArrayList<>();

        //获取当前已变更次数
        User user = UserUtils.getUser();
        Date now = new Date();
        CustomerEntryCompletionChangeRecord record = new CustomerEntryCompletionChangeRecord();
        Integer maxNum = changeDao.selectMaxNum(req.getEntryId());
        if (maxNum == null) {
            record.setNum(1);
            //把第一次的存起来
            CustomerEntryCompletionChangeRecord zeroRecord = new CustomerEntryCompletionChangeRecord();
            zeroRecord.setNum(0);
            zeroRecord.setId(IdGen.uuid());
            zeroRecord.setNumber(PlantformUtils.getOrderNumber());
            zeroRecord.setEntryId(entry.getId());
            zeroRecord.setPowerStationCode(entry.getPowerStationCode());
            zeroRecord.setStatus("1");
            zeroRecord.setCreator(CrmUtils.getUserXM(user));
            zeroRecord.setCreateBy(user.getId());
            zeroRecord.setCreateTime(now);
            zeroRecord.setFilingDocumentPhoto(surveyRecord.getFilingDocumentPhoto());
            zeroRecord.setFilingDocumentCode(surveyRecord.getFilingDocumentCode());
            CrmAcceptingReportDetail oneOldReportDetail =oldReportDetailList.get(0);
            zeroRecord.setKgConditionFiles(oneOldReportDetail.getKgConditionFiles());
            zeroRecord.setSafetyWarningImg2(oneOldReportDetail.getSafetyWarningImg2());
            for(CrmAcceptingReportDetail oldReportDetail:oldReportDetailList){
                CustomerEntryCompletionChangeRecordDetail oldChngeDetail = new CustomerEntryCompletionChangeRecordDetail();
                oldChngeDetail.setNumber(zeroRecord.getNumber());
                oldChngeDetail.setEntryId(zeroRecord.getEntryId());
                oldChngeDetail.setPowerStationCode(zeroRecord.getPowerStationCode());
                oldChngeDetail.setChangeRecordId(zeroRecord.getId());
                oldChngeDetail.setProjectId(oldReportDetail.getProjectId());

                //oldChngeDetail.setHouseOwnership(oldReportDetail.getHouseOwnership());
                oldChngeDetail.setOtherImgs1(oldReportDetail.getOtherImgs1());
                oldChngeDetail.setOtherImgs2(oldReportDetail.getOtherImgs2());
                oldChngeDetail.setOtherImgs3(oldReportDetail.getOtherImgs3());
                oldChngeDetail.setOtherImgs4(oldReportDetail.getOtherImgs4());
                oldChngeDetail.setDzjc1(oldReportDetail.getDzjc1());
                oldChngeDetail.setDzjc2(oldReportDetail.getDzjc2());
                oldChngeDetail.setZjImg1(oldReportDetail.getZjImg1());
                oldChngeDetail.setZjImg2(oldReportDetail.getZjImg2());
                oldChngeDetail.setZjImg3(oldReportDetail.getZjImg3());
                oldChngeDetail.setZjImg4(oldReportDetail.getZjImg4());
                oldChngeDetail.setZjImg5(oldReportDetail.getZjImg5());
                oldChngeDetail.setComponentImg1(oldReportDetail.getComponentImg1());
                oldChngeDetail.setComponentImg2(oldReportDetail.getComponentImg2());
                oldChngeDetail.setSizeMeasureImgs(oldReportDetail.getSizeMeasureImgs());
                oldChngeDetail.setFixOtherImg(oldReportDetail.getFixOtherImg());
                oldChngeDetail.setTotalImgs(oldReportDetail.getTotalImgs());
                oldChngeDetail.setBwInnerImg(oldReportDetail.getBwInnerImg());
                oldChngeDetail.setDcImgs(oldReportDetail.getDcImgs());
                oldChngeDetail.setNbqZbImg(oldReportDetail.getNbqZbImg());
                oldChngeDetail.setDqsbAnImgOne(oldReportDetail.getDqsbAnImgOne());
                oldChngeDetail.setNbqPlateImg(oldReportDetail.getNbqPlateImg());
                oldChngeDetail.setZjPlateImg(oldReportDetail.getZjPlateImg());
                oldChngeDetail.setOpenWayVoltageImgs(oldReportDetail.getOpenWayVoltageImgs());
                oldChngeDetail.setJdImg1(oldReportDetail.getJdImg1());
                oldChngeDetail.setJdImg2(oldReportDetail.getJdImg2());
                oldChngeDetail.setJdImg3(oldReportDetail.getJdImg3());
                oldChngeDetail.setJdImg4(oldReportDetail.getJdImg4());
                oldChngeDetail.setJdImg5(oldReportDetail.getJdImg5());
                oldChngeDetail.setJdImg6(oldReportDetail.getJdImg6());
                oldChngeDetail.setFilingDocumentPhoto(surveyRecord.getFilingDocumentPhoto());
                oldChngeDetail.setFilingDocumentCode(surveyRecord.getFilingDocumentCode());
                oldChngeDetail.setZjReport(oldReportDetail.getZjReport());
                oldChngeDetail.setOtherFiles(oldReportDetail.getOtherFiles());
                oldChngeDetail.setSafetyWarningImg(oldReportDetail.getSafetyWarningImg());
                oldChngeDetail.setSafetyWarningImg2(oldReportDetail.getSafetyWarningImg2());
                oldChngeDetail.setSafetyWarningImg3(oldReportDetail.getSafetyWarningImg3());
                oldChngeDetail.setSafetyDiscloseDocImg(oldReportDetail.getSafetyDiscloseDocImg());
                oldChngeDetail.setZjImg8(oldReportDetail.getZjImg8());
                oldChngeDetail.setZjImg9(oldReportDetail.getZjImg9());
                oldChngeDetail.setZjImg10(oldReportDetail.getZjImg10());
                oldChngeDetail.setZjImg11(oldReportDetail.getZjImg11());
                oldChngeDetail.setZjImg12(oldReportDetail.getZjImg12());
                oldChngeDetail.setZjImg13(oldReportDetail.getZjImg13());
                oldChngeDetail.setZjImg14(oldReportDetail.getZjImg14());
                oldChngeDetail.setComponentImg3(oldReportDetail.getComponentImg3());
                oldChngeDetail.setComponentImg4(oldReportDetail.getComponentImg4());
                oldChngeDetail.setBkImg1(oldReportDetail.getBkImg1());
                oldChngeDetail.setBkImg2(oldReportDetail.getBkImg2());
                oldChngeDetail.setBkImg3(oldReportDetail.getBkImg3());
                oldChngeDetail.setYkImg1(oldReportDetail.getYkImg1());
                oldChngeDetail.setNbqZlcImg(oldReportDetail.getNbqZlcImg());
                oldChngeDetail.setBwJdImg(oldReportDetail.getBwJdImg());
                oldChngeDetail.setJdImg8(oldReportDetail.getJdImg8());
                oldChngeDetail.setJdImg9(oldReportDetail.getJdImg9());
                oldChngeDetail.setJdImg10(oldReportDetail.getJdImg10());
                oldChngeDetail.setJdImg11(oldReportDetail.getJdImg11());
                oldChngeDetail.setFsImg1(oldReportDetail.getFsImg1());
                oldChngeDetail.setFsImg2(oldReportDetail.getFsImg2());
                oldChngeDetail.setFsImg3(oldReportDetail.getFsImg3());
                oldChngeDetail.setOtherImgs5(oldReportDetail.getOtherImgs5());
                oldChngeDetail.setOtherImgs6(oldReportDetail.getOtherImgs6());
                oldChngeDetail.setOtherImgs7(oldChngeDetail.getOtherImgs7());
                oldChngeDetail.setOtherImgs8(oldChngeDetail.getOtherImgs8());
                oldChngeDetail.setSupplementVideoUrl(oldReportDetail.getSupplementVideoUrl());
                oldChngeDetail.setCompleteRemark(oldReportDetail.getCompleteRemark());
                oldChngeDetail.setKgConditionFiles(oldReportDetail.getKgConditionFiles());
                oldChngeDetail.setCreateTime(now);
                oldChngeDetail.setCompleteRemark(oldReportDetail.getCompleteRemark());
                details.add(oldChngeDetail);
            }
            changeDao.save(zeroRecord);
        } else {
            record.setNum(maxNum + 1);
        }
        record.setId(IdGen.uuid());
        record.setNumber(PlantformUtils.getOrderNumber());
        record.setEntryId(req.getEntryId());
        record.setPowerStationCode(entry.getPowerStationCode());
        for(CrmAcceptingReportDetail changeDetail:changeRecordDetailList){
            CustomerEntryCompletionChangeRecordDetail changeDetailRecord = new CustomerEntryCompletionChangeRecordDetail();
            changeDetailRecord.setNumber(record.getNumber());
            changeDetailRecord.setEntryId(record.getEntryId());
            changeDetailRecord.setPowerStationCode(record.getPowerStationCode());
            changeDetailRecord.setChangeRecordId(record.getId());
            changeDetailRecord.setProjectId(changeDetail.getProjectId());

            changeDetailRecord.setOtherImgs1(changeDetail.getOtherImgs1());
            changeDetailRecord.setOtherImgs2(changeDetail.getOtherImgs2());
            changeDetailRecord.setOtherImgs3(changeDetail.getOtherImgs3());
            changeDetailRecord.setOtherImgs4(changeDetail.getOtherImgs4());
            changeDetailRecord.setDzjc1(changeDetail.getDzjc1());
            changeDetailRecord.setDzjc2(changeDetail.getDzjc2());
            changeDetailRecord.setZjImg1(changeDetail.getZjImg1());
            changeDetailRecord.setZjImg2(changeDetail.getZjImg2());
            changeDetailRecord.setZjImg3(changeDetail.getZjImg3());
            changeDetailRecord.setZjImg4(changeDetail.getZjImg4());
            changeDetailRecord.setZjImg5(changeDetail.getZjImg5());
            changeDetailRecord.setComponentImg1(changeDetail.getComponentImg1());
            changeDetailRecord.setComponentImg2(changeDetail.getComponentImg2());
            changeDetailRecord.setSizeMeasureImgs(changeDetail.getSizeMeasureImgs());
            changeDetailRecord.setFixOtherImg(changeDetail.getFixOtherImg());
            changeDetailRecord.setTotalImgs(changeDetail.getTotalImgs());
            changeDetailRecord.setBwInnerImg(changeDetail.getBwInnerImg());
            changeDetailRecord.setDcImgs(changeDetail.getDcImgs());
            changeDetailRecord.setNbqZbImg(changeDetail.getNbqZbImg());
            changeDetailRecord.setDqsbAnImgOne(changeDetail.getDqsbAnImgOne());
            changeDetailRecord.setNbqPlateImg(changeDetail.getNbqPlateImg());
            changeDetailRecord.setZjPlateImg(changeDetail.getZjPlateImg());
            changeDetailRecord.setOpenWayVoltageImgs(changeDetail.getOpenWayVoltageImgs());
            changeDetailRecord.setJdImg1(changeDetail.getJdImg1());
            changeDetailRecord.setJdImg2(changeDetail.getJdImg2());
            changeDetailRecord.setJdImg3(changeDetail.getJdImg3());
            changeDetailRecord.setJdImg4(changeDetail.getJdImg4());
            changeDetailRecord.setJdImg5(changeDetail.getJdImg5());
            changeDetailRecord.setJdImg6(changeDetail.getJdImg6());
            changeDetailRecord.setFilingDocumentPhoto(req.getFilingDocumentPhoto());
            changeDetailRecord.setFilingDocumentCode(req.getFilingDocumentCode());
            changeDetailRecord.setZjReport(changeDetail.getZjReport());
            changeDetailRecord.setOtherFiles(changeDetail.getOtherFiles());
            changeDetailRecord.setSafetyWarningImg(changeDetail.getSafetyWarningImg());
            changeDetailRecord.setSafetyWarningImg2(changeDetail.getSafetyWarningImg2());
            changeDetailRecord.setSafetyWarningImg3(changeDetail.getSafetyWarningImg3());
            changeDetailRecord.setSafetyDiscloseDocImg(changeDetail.getSafetyDiscloseDocImg());
            changeDetailRecord.setZjImg8(changeDetail.getZjImg8());
            changeDetailRecord.setZjImg9(changeDetail.getZjImg9());
            changeDetailRecord.setZjImg10(changeDetail.getZjImg10());
            changeDetailRecord.setZjImg11(changeDetail.getZjImg11());
            changeDetailRecord.setZjImg12(changeDetail.getZjImg12());
            changeDetailRecord.setZjImg13(changeDetail.getZjImg13());
            changeDetailRecord.setZjImg14(changeDetail.getZjImg14());
            changeDetailRecord.setComponentImg3(changeDetail.getComponentImg3());
            changeDetailRecord.setComponentImg4(changeDetail.getComponentImg4());
            changeDetailRecord.setBkImg1(changeDetail.getBkImg1());
            changeDetailRecord.setBkImg2(changeDetail.getBkImg2());
            changeDetailRecord.setBkImg3(changeDetail.getBkImg3());
            changeDetailRecord.setYkImg1(changeDetail.getYkImg1());
            changeDetailRecord.setNbqZlcImg(changeDetail.getNbqZlcImg());
            changeDetailRecord.setBwJdImg(changeDetail.getBwJdImg());
            changeDetailRecord.setJdImg8(changeDetail.getJdImg8());
            changeDetailRecord.setJdImg9(changeDetail.getJdImg9());
            changeDetailRecord.setJdImg10(changeDetail.getJdImg10());
            changeDetailRecord.setJdImg11(changeDetail.getJdImg11());
            changeDetailRecord.setFsImg1(changeDetail.getFsImg1());
            changeDetailRecord.setFsImg2(changeDetail.getFsImg2());
            changeDetailRecord.setFsImg3(changeDetail.getFsImg3());
            changeDetailRecord.setOtherImgs5(changeDetail.getOtherImgs5());
            changeDetailRecord.setOtherImgs6(changeDetail.getOtherImgs6());
            changeDetailRecord.setOtherImgs7(changeDetail.getOtherImgs7());
            changeDetailRecord.setOtherImgs8(changeDetail.getOtherImgs8());
            changeDetailRecord.setSupplementVideoUrl(changeDetail.getSupplementVideoUrl());
            changeDetailRecord.setKgConditionFiles(changeDetail.getKgConditionFiles());
            changeDetailRecord.setCreateTime(now);
            changeDetailRecord.setCompleteRemark(changeDetail.getCompleteRemark());
            details.add(changeDetailRecord);
        }
        record.setReason(req.getCompleteRemark());
        record.setStatus("0");
        record.setCreator(CrmUtils.getUserXM(user));
        record.setCreateBy(user.getId());
        record.setCreateTime(now);
        record.setSafetyWarningImg2(req.getSafetyWarningImg2());
        record.setFilingDocumentPhoto(req.getFilingDocumentPhoto());
        record.setFilingDocumentCode(req.getFilingDocumentCode());
        record.setKgConditionFiles(req.getKgConditionFiles());
        //清空安装验收审核备注
        record.setRemarks("");
        changeDao.save(record);
        changeRecordDetailDao.save(details);

        entryMsgService.saveCustomerEntryMsg(Operator.of(user), entry.getId(), CustomerEntryConstants.ENTRY_ADD_CHANGE
                , String.format("第%d次提交电站完工信息变更申请", record.getNum()), null);

        //所有的变更审核 要走待办事项审批流程，默认审批人为原审核人员  完工信息变更审核 查询的是安装验收/完工信息变更审核人；
        String stationName = CrmUtils.getStationName(entry);
        ItemManage itemManage=itemManageDao.addItemChange(FormUtils.ENTRY_CHANGE_WGBG,entry.getId(),record.getId(),"完工信息变更("+stationName+"-"+entry.getPowerStationCode()+")-"+ DateUtils.formatDate(now,"yyyy-MM-dd HH:mm:ss"),now,user.getUserName());
        //获取最近一次 档案审核/客户信息变更审核人
        String userId=itemManageDao.getLastAuditUserId("93,96",entry.getId());
        if(StringUtil.isNotBlank(userId)){
//            itemManage.setAssignAuditUserId(userId);
            itemManage.setPreferUserId(userId);
        }
        itemManageDao.save(itemManage);
        //大代理商优先审核
        String groupName = "安装审核";
        crmPriorReviewSettingService.traderPriorityReview(entry.getCreateTraderId(),itemManage.getId(),groupName);
        return Result.ok();
    }

    public List<CustomerEntryCompletionChangeRecordDetail> getByRecordId(String recordId){
           return changeRecordDetailDao.findByChangeRecord(recordId);
    }
    /**
     * 审核通过
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result pass(SurveyAuditVo auditVo) {
        CustomerEntryCompletionChangeRecord reportChange = changeDao.get(auditVo.getId());
        if (!"0".equals(reportChange.getStatus())) {
            return Result.fail("状态已改变,刷新后重试");
        }

        Date date=new Date();
        User user = UserUtils.getUser();
        reportChange.setStatus("1");
        reportChange.setAuditor(CrmUtils.getUserXM(user));
        reportChange.setAuditBy(user.getId());
        reportChange.setAuditTime(date);
        reportChange.setRemarks(auditVo.getAuditRemark());
        changeDao.save(reportChange);

        CustomerEntry entry = entryDao.get(reportChange.getEntryId());
        CrmAcceptingReport report = reportDao.get(entry.getReceivingReportId());
        // report.setHouseOwnership(reportChange.getHouseOwnership());
        report.setOtherImgs1(reportChange.getOtherImgs1());
        report.setOtherImgs2(reportChange.getOtherImgs2());
        report.setOtherImgs3(reportChange.getOtherImgs3());
        report.setOtherImgs4(reportChange.getOtherImgs4());
        report.setDzjc1(reportChange.getDzjc1());
        report.setDzjc2(reportChange.getDzjc2());
        report.setZjImg1(reportChange.getZjImg1());
        report.setZjImg2(reportChange.getZjImg2());
        report.setZjImg3(reportChange.getZjImg3());
        report.setZjImg4(reportChange.getZjImg4());
        report.setZjImg5(reportChange.getZjImg5());
        report.setComponentImg1(reportChange.getComponentImg1());
        report.setComponentImg2(reportChange.getComponentImg2());
        report.setSizeMeasureImgs(reportChange.getSizeMeasureImgs());
        report.setFixOtherImg(reportChange.getFixOtherImg());
        report.setTotalImgs(reportChange.getTotalImgs());
        report.setBwInnerImg(reportChange.getBwInnerImg());
        report.setDcImgs(reportChange.getDcImgs());
        report.setNbqZbImg(reportChange.getNbqZbImg());
        report.setDqsbAnImgOne(reportChange.getDqsbAnImgOne());
        report.setNbqPlateImg(reportChange.getNbqPlateImg());
        report.setZjPlateImg(reportChange.getZjPlateImg());
        report.setOpenWayVoltageImgs(reportChange.getOpenWayVoltageImgs());
        report.setJdImg1(reportChange.getJdImg1());
        report.setJdImg2(reportChange.getJdImg2());
        report.setJdImg3(reportChange.getJdImg3());
        report.setJdImg4(reportChange.getJdImg4());
        report.setJdImg5(reportChange.getJdImg5());
        report.setJdImg6(reportChange.getJdImg6());
        report.setZjReport(reportChange.getZjReport());
        report.setOtherFiles(reportChange.getOtherFiles());
        report.setSafetyWarningImg(reportChange.getSafetyWarningImg());
        report.setSafetyWarningImg2(reportChange.getSafetyWarningImg2());
        report.setSafetyWarningImg3(reportChange.getSafetyWarningImg3());
        report.setSafetyDiscloseDocImg(reportChange.getSafetyDiscloseDocImg());
        report.setZjImg8(reportChange.getZjImg8());
        report.setZjImg9(reportChange.getZjImg9());
        report.setZjImg10(reportChange.getZjImg10());
        report.setZjImg11(reportChange.getZjImg11());
        report.setZjImg12(reportChange.getZjImg12());
        report.setZjImg13(reportChange.getZjImg13());
        report.setZjImg14(reportChange.getZjImg14());
        report.setComponentImg3(reportChange.getComponentImg3());
        report.setComponentImg4(reportChange.getComponentImg4());
        report.setBkImg1(reportChange.getBkImg1());
        report.setBkImg2(reportChange.getBkImg2());
        report.setBkImg3(reportChange.getBkImg3());
        report.setYkImg1(reportChange.getYkImg1());
        report.setNbqZlcImg(reportChange.getNbqZlcImg());
        report.setBwJdImg(reportChange.getBwJdImg());
        report.setJdImg8(reportChange.getJdImg8());
        report.setJdImg9(reportChange.getJdImg9());
        report.setJdImg10(reportChange.getJdImg10());
        report.setJdImg11(reportChange.getJdImg11());
        report.setFsImg1(reportChange.getFsImg1());
        report.setFsImg2(reportChange.getFsImg2());
        report.setFsImg3(reportChange.getFsImg3());
        report.setOtherImgs5(reportChange.getOtherImgs5());
        report.setOtherImgs6(reportChange.getOtherImgs6());
        report.setSupplementVideoUrl(reportChange.getSupplementVideoUrl());
        report.setCompleteRemark(reportChange.getCompleteRemark());
        report.setKgConditionFiles(reportChange.getKgConditionFiles());
        reportDao.save(report);

        CustomerEntrySurveyRecord surveyRecord = surveyRecordDao.findOneByEntryId(reportChange.getEntryId());
        surveyRecord.setFilingDocumentPhoto(reportChange.getFilingDocumentPhoto());
        surveyRecord.setHouseOwnership(reportChange.getHouseOwnership());
        surveyRecordDao.save(surveyRecord);

        //过程记录
        String msg = (StringUtil.isNotBlank(auditVo.getAuditRemark()) ? ","+ auditVo.getAuditRemark() : "");
        entryMsgService.saveCustomerEntryMsg(Operator.of(user), reportChange.getEntryId()
                , CustomerEntryConstants.ENTRY_CHANGE_PASS, String.format("第%d次完工信息变更申请，审核通过" +msg, reportChange.getNum()), null);

        //推送农户投放审查附件
        if(FormSetUtils.processSetEnable(entry.getPartnerId(), ProcessSetConstants.NODE_KEY_NAME_ZFSH)) {
            CrmCustomerIncomingApplication application = applicationDao.getByEntryId(entry.getId());
            switch(entry.getPartnerId()) {
                case PartnerConstants.PARTNER_ID_YX:
                    if(StringUtil.isNotBlank(entry.getApplyStatus()) && (Double.valueOf(entry.getApplyStatus())>16 || "15".equals(entry.getApplyStatus()) )){
                        //退回完工
                        operatingLeaseService.cancelProjectApply(application.getPartnersContractNumber(),"完工信息变更", SourceConstants.RETURN_TYPE_RETURN_TO_COMPLETION);
                        entryMsgService.saveCustomerEntryMsg(user,entry.getId(), CustomerEntryConstants.INCOMING_CANCEL,"完工信息变更导致 退回完工");
                        logger.info(">>>>>>pass 完工信息变更  entryId:{}",entry.getId());
                        Gson gson=new Gson();
                        Operator operator=Operator.of(user);
                        Result result = entryService.accessProjectApplySec(entry,operator);
                        logger.info(">>>>>>>>>>完工信息变更  againPushIncoming twoIncoming result:{}",gson.toJson(result));
                        if (!"200".equals(result.getCode())) {
                            throw new IllegalArgumentException("农户投放审查信息同步失败；"+result.getErrMsg());
                        }
                        entryService.saveByUpdate(entry);
                    }else{
                        Result result = entryService.updateSecoundFile(entry, application.getPartnersContractNumber());
                        if (!"200".equals(result.getCode())) {
                            logger.error("完工信息变更推送农户投放审查附件失败：", result.getErrMsg());
                            throw new IllegalArgumentException(result.getErrMsg());
                        }
                    }
                    break;
                case PartnerConstants.PARTNER_ID_GF:
                    if(StringUtil.isNotBlank(entry.getApplyStatus()) && (Double.parseDouble(entry.getApplyStatus())>16 || "15".equals(entry.getApplyStatus()) )){
                        guangFaManagemnetService.completionInfoUpdate(entry, reportChange, application.getInputPieceId());
                    }
            }
        }
        ItemManage itemManage= itemManageDao.getItemManagementByProcessInstanceId(reportChange.getId(),FormUtils.ENTRY_CHANGE_WGBG);
        if (itemManage != null) {
            Operator operator=Operator.of(user);
            itemTaskDetailDao.addItemTaskDetail2(FormUtils.ENTRY_CHANGE_WGBG,itemManage.getApplicationId(), itemManage.getProcessInstanceId(), itemManage.getProcessInstanceId(), ItemConstants.RESULT_OK, date, operator, "完工信息变更");
            itemManage.setStatus(ItemConstants.ITEM_STATUS_FINISH);
            itemManage.setEndTime(date);
            itemManageDao.save(itemManage);
            reviewDispatchDao.markDispatchFinish(itemManage);
        }
        return Result.ok();
    }

    /**
     * 审核通过
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result tempRemark(SurveyAuditVo auditVo) {
        CustomerEntryCompletionChangeRecord reportChange = changeDao.get(auditVo.getId());
        if (!"0".equals(reportChange.getStatus())) {
            return Result.fail("状态已改变,刷新后重试");
        }
        reportChange.setRemarks(auditVo.getAuditRemark());
        changeDao.save(reportChange);
        return Result.ok();
    }

    public Result passHR(SurveyAuditVo auditVo){
        CustomerEntryCompletionChangeRecord reportChange = changeDao.get(auditVo.getId());
        if (!"0".equals(reportChange.getStatus())) {
            return Result.fail("状态已改变,刷新后重试");
        }

        Date date=new Date();
        User user = UserUtils.getUser();
        reportChange.setStatus("1");
        reportChange.setAuditor(CrmUtils.getUserXM(user));
        reportChange.setAuditBy(user.getId());
        reportChange.setAuditTime(date);
        reportChange.setRemarks(auditVo.getAuditRemark());
        changeDao.save(reportChange);

        List<CustomerEntryCompletionChangeRecordDetail> listDetail=getByRecordId(reportChange.getId());
        Map<String,CustomerEntryCompletionChangeRecordDetail> changeDetailMap = listDetail.stream().collect(Collectors.toMap(e -> e.getProjectId(), e -> e, (a, b) -> a));

        CustomerEntry entry = entryDao.get(reportChange.getEntryId());
       // CrmAcceptingReport report = reportDao.get(entry.getReceivingReportId());
        List<CrmAcceptingReportDetail> oldReportDetailList = reportDetailDao.findByEntryId(entry.getId());
        for(CrmAcceptingReportDetail report:oldReportDetailList){
            CustomerEntryCompletionChangeRecordDetail changeDetail=changeDetailMap.get(report.getProjectId());
            report.setOtherImgs1(changeDetail.getOtherImgs1());
            report.setOtherImgs2(changeDetail.getOtherImgs2());
            report.setOtherImgs3(changeDetail.getOtherImgs3());
            report.setOtherImgs4(changeDetail.getOtherImgs4());
            report.setDzjc1(changeDetail.getDzjc1());
            report.setDzjc2(changeDetail.getDzjc2());
            report.setZjImg1(changeDetail.getZjImg1());
            report.setZjImg2(changeDetail.getZjImg2());
            report.setZjImg3(changeDetail.getZjImg3());
            report.setZjImg4(changeDetail.getZjImg4());
            report.setZjImg5(changeDetail.getZjImg5());
            report.setComponentImg1(changeDetail.getComponentImg1());
            report.setComponentImg2(changeDetail.getComponentImg2());
            report.setSizeMeasureImgs(changeDetail.getSizeMeasureImgs());
            report.setFixOtherImg(changeDetail.getFixOtherImg());
            report.setTotalImgs(changeDetail.getTotalImgs());
            report.setBwInnerImg(changeDetail.getBwInnerImg());
            report.setDcImgs(changeDetail.getDcImgs());
            report.setNbqZbImg(changeDetail.getNbqZbImg());
            report.setDqsbAnImgOne(changeDetail.getDqsbAnImgOne());
            report.setNbqPlateImg(changeDetail.getNbqPlateImg());
            report.setZjPlateImg(changeDetail.getZjPlateImg());
            report.setOpenWayVoltageImgs(changeDetail.getOpenWayVoltageImgs());
            report.setJdImg1(changeDetail.getJdImg1());
            report.setJdImg2(changeDetail.getJdImg2());
            report.setJdImg3(changeDetail.getJdImg3());
            report.setJdImg4(changeDetail.getJdImg4());
            report.setJdImg5(changeDetail.getJdImg5());
            report.setJdImg6(changeDetail.getJdImg6());
            report.setZjReport(changeDetail.getZjReport());
            report.setOtherFiles(changeDetail.getOtherFiles());
            report.setSafetyWarningImg(changeDetail.getSafetyWarningImg());
            report.setSafetyWarningImg2(changeDetail.getSafetyWarningImg2());
            report.setSafetyWarningImg3(changeDetail.getSafetyWarningImg3());
            report.setSafetyDiscloseDocImg(changeDetail.getSafetyDiscloseDocImg());
            report.setZjImg8(changeDetail.getZjImg8());
            report.setZjImg9(changeDetail.getZjImg9());
            report.setZjImg10(changeDetail.getZjImg10());
            report.setZjImg11(changeDetail.getZjImg11());
            report.setZjImg12(changeDetail.getZjImg12());
            report.setZjImg13(changeDetail.getZjImg13());
            report.setZjImg14(changeDetail.getZjImg14());
            report.setComponentImg3(changeDetail.getComponentImg3());
            report.setComponentImg4(changeDetail.getComponentImg4());
            report.setBkImg1(changeDetail.getBkImg1());
            report.setBkImg2(changeDetail.getBkImg2());
            report.setBkImg3(changeDetail.getBkImg3());
            report.setYkImg1(changeDetail.getYkImg1());
            report.setNbqZlcImg(changeDetail.getNbqZlcImg());
            report.setBwJdImg(changeDetail.getBwJdImg());
            report.setJdImg8(changeDetail.getJdImg8());
            report.setJdImg9(changeDetail.getJdImg9());
            report.setJdImg10(changeDetail.getJdImg10());
            report.setJdImg11(changeDetail.getJdImg11());
            report.setFsImg1(changeDetail.getFsImg1());
            report.setFsImg2(changeDetail.getFsImg2());
            report.setFsImg3(changeDetail.getFsImg3());
            report.setOtherImgs5(changeDetail.getOtherImgs5());
            report.setOtherImgs6(changeDetail.getOtherImgs6());
            report.setOtherImgs7(changeDetail.getOtherImgs7());
            report.setOtherImgs8(changeDetail.getOtherImgs8());
            report.setSupplementVideoUrl(changeDetail.getSupplementVideoUrl());
            report.setCompleteRemark(changeDetail.getCompleteRemark());
            report.setKgConditionFiles(changeDetail.getKgConditionFiles());
            reportDetailDao.save(report);
        }


        CustomerEntrySurveyRecord surveyRecord = surveyRecordDao.findOneByEntryId(reportChange.getEntryId());
        surveyRecord.setFilingDocumentPhoto(reportChange.getFilingDocumentPhoto());
        surveyRecord.setFilingDocumentCode(reportChange.getFilingDocumentCode());
        //surveyRecord.setHouseOwnership(reportChange.getHouseOwnership());
        surveyRecordDao.save(surveyRecord);

        //过程记录
        String msg = (StringUtil.isNotBlank(auditVo.getAuditRemark()) ? ","+ auditVo.getAuditRemark() : "");
        entryMsgService.saveCustomerEntryMsg(Operator.of(user), reportChange.getEntryId()
                , CustomerEntryConstants.ENTRY_CHANGE_PASS, String.format("第%d次完工信息变更申请，审核通过" +msg, reportChange.getNum()), null);

        ItemManage itemManage= itemManageDao.getItemManagementByProcessInstanceId(reportChange.getId(),FormUtils.ENTRY_CHANGE_WGBG);
        if (itemManage != null) {
            Operator operator=Operator.of(user);
            itemTaskDetailDao.addItemTaskDetail2(FormUtils.ENTRY_CHANGE_WGBG,itemManage.getApplicationId(), itemManage.getProcessInstanceId(), itemManage.getProcessInstanceId(), ItemConstants.RESULT_OK, date, operator, "完工信息变更");
            itemManage.setStatus(ItemConstants.ITEM_STATUS_FINISH);
            itemManage.setEndTime(date);
            itemManageDao.save(itemManage);
            reviewDispatchDao.markDispatchFinish(itemManage);
        }
        return Result.ok();
    }

    /**
     * 审核拒绝
     *
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result refuse(SurveyAuditVo auditVo) {
        CustomerEntryCompletionChangeRecord surveyChangeRecord = changeDao.get(auditVo.getId());
        if (!"0".equals(surveyChangeRecord.getStatus())) {
            return Result.fail("状态已改变,刷新后重试");
        }
        User user = UserUtils.getUser();
        Date date=new Date();
        surveyChangeRecord.setStatus("2");
        surveyChangeRecord.setAuditor(CrmUtils.getUserXM(user));
        surveyChangeRecord.setAuditBy(user.getId());
        surveyChangeRecord.setAuditTime(date);
        surveyChangeRecord.setRemarks(auditVo.getAuditRemark());
        changeDao.save(surveyChangeRecord);
        String msg = (StringUtil.isNotBlank(auditVo.getAuditRemark()) ? ","+ auditVo.getAuditRemark() : "");
        String content = String.format("第%d次完工信息变更申请，审核驳回" + msg, surveyChangeRecord.getNum());
        entryMsgService.saveCustomerEntryMsg(Operator.of(user), surveyChangeRecord.getEntryId()
                , CustomerEntryConstants.ENTRY_CHANGE_REFUSE, content, null);

        ItemManage itemManage= itemManageDao.getItemManagementByProcessInstanceId(surveyChangeRecord.getId(),FormUtils.ENTRY_CHANGE_WGBG);
        if (itemManage != null) {
            Operator operator=Operator.of(user);
            itemTaskDetailDao.addItemTaskDetail2(FormUtils.ENTRY_CHANGE_WGBG,itemManage.getApplicationId(), itemManage.getProcessInstanceId(), itemManage.getProcessInstanceId(), ItemConstants.RESULT_BACK, date, operator, "完工信息变更");
            itemManage.setStatus(ItemConstants.ITEM_STATUS_FINISH);
            itemManage.setEndTime(date);
            itemManageDao.save(itemManage);
            reviewDispatchDao.markDispatchFinish(itemManage);
        }
        return Result.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    public Result back(String id) {
        CustomerEntryCompletionChangeRecord changeRecord = changeDao.get(id);
        if (!"0".equals(changeRecord.getStatus())) {
            return Result.fail("状态已改变,刷新后重试");
        }
        User user = UserUtils.getUser();
        changeRecord.setStatus("3");
        changeRecord.setAuditor(CrmUtils.getUserXM(user));
        changeRecord.setAuditBy(user.getId());
        changeDao.save(changeRecord);

        entryMsgService.saveCustomerEntryMsg(Operator.of(user), changeRecord.getEntryId()
                , CustomerEntryConstants.ENTRY_CHANGE_CANCEL, String.format("第%d次完工信息变更申请，撤回", changeRecord.getNum()), null);

        ItemManage itemManage= itemManageDao.getItemManagementByProcessInstanceId(changeRecord.getId(),FormUtils.ENTRY_CHANGE_WGBG);
        if(itemManage!=null){
            itemManage.setStatus(ItemConstants.ITEM_STATUS_CANCEL);
            itemManage.setEndTime(new Date());
            itemManageDao.save(itemManage);
        }
        return Result.ok();
    }

}
