package com.sifang.modules.customer.Service;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.dao.CrmCustomerEntryProjectDao;
import com.sifang.modules.customer.dao.CrmCustomerEntrySurveyRecordDetailDao;
import com.sifang.modules.customer.entity.CrmCustomerEntryProject;
import com.sifang.modules.customer.entity.CrmCustomerEntrySurveyRecordDetail;
import com.sifang.modules.customer.entity.CustomerEntrySurveyRecord;
import com.sifang.modules.customer.form.AddEntryReq;
import com.sifang.modules.customer.form.AddEntryReqSurveyDetail;
import com.sifang.modules.customer.form.ProjectInfoVo;
import com.sifang.modules.sys.common.persistence.Parameter;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class CrmCustomerEntrySurveyRecordDetailService {

    @Autowired
    private CrmCustomerEntryProjectDao projectDao;
    @Autowired
    private CrmCustomerEntrySurveyRecordDetailDao recordDetailDao;


    public CustomerEntrySurveyRecord setBasicInfo(AddEntryReq req, CustomerEntrySurveyRecord surveyRecord, Date now) {
        return setBasicInfo(surveyRecord, req.getDistributorId() , req.getAzimuth(), req.getAngle(), req.getBuildingHeight(),
                req.getRepaymentMethod(),req.getTenantType(), req.getDistance(),
                req.getVoltageClasses(),req.getTransformerCapacity(),req.getRestrictFlag(),
                req.getSealRoof(),req.getCompletionYear(),req.getDipAngle(),req.getSlopeGradient(),req.getPurlineInterval(),
                req.getWaterproof(),req.getSourcePollution(),req.getOpsCondition(),now
                ,req.getEwLength(),req.getNsWide(),req.getRoofArea(),req.getProjectList(),req.getAddEntryReqSurveyDetails(),req.getId());
    }

    public CustomerEntrySurveyRecord setBasicInfo(CustomerEntrySurveyRecord surveyRecord, String distributorId, String azimuth, String angle, BigDecimal buildingHeight, String repaymentMethod,
                                                   String tenantType, BigDecimal distance, BigDecimal voltageClasses, BigDecimal transformerCapacity, String restrictFlag,
                                                   String sealRoof, String completionYear, BigDecimal dipAngle, BigDecimal slopeGradient, BigDecimal purlineInterval,
                                                   String waterproof, String sourcePollution, String opsCondition, Date now, BigDecimal ewLength, BigDecimal nsWide, BigDecimal roofArea,
                                                   List<ProjectInfoVo> projectList, List<AddEntryReqSurveyDetail> addEntryReqSurveyDetails, String entryId) {
        surveyRecord.setTraderId(distributorId);
        surveyRecord.setAzimuth(azimuth);
        if (!"1".equals(azimuth)) {
            surveyRecord.setAngle(angle);
        }
        else {
            surveyRecord.setAngle("");
        }
        surveyRecord.setBuildingHeight(buildingHeight);
        surveyRecord.setRepaymentMethod(repaymentMethod);
        if("2".equals(tenantType)){
            surveyRecord.setRepaymentMethod("0");
        }
        surveyRecord.setDistance(distance);
        surveyRecord.setVoltageClasses(voltageClasses);
        surveyRecord.setTransformerCapacity(transformerCapacity);
        surveyRecord.setRestrictFlag(restrictFlag);
        surveyRecord.setSealRoof(sealRoof);
        surveyRecord.setCompletionYear(completionYear);
        surveyRecord.setDipAngle(dipAngle);
        surveyRecord.setSlopeGradient(slopeGradient);
        surveyRecord.setPurlineInterval(purlineInterval);
        surveyRecord.setWaterproof(waterproof);
        surveyRecord.setSourcePollution(sourcePollution);
        surveyRecord.setOpsCondition(opsCondition);
        surveyRecord.setCreateTime(now);
        surveyRecord.setEwLength(ewLength);
        surveyRecord.setNsWide(nsWide);
        surveyRecord.setRoofArea(roofArea);
        //先删除原来的产品信息
        projectDao.deleteByEntryId(entryId);
        List<CrmCustomerEntryProject> projects = new ArrayList<>();
        for(ProjectInfoVo projectInfoVo : projectList){
            CrmCustomerEntryProject project = new CrmCustomerEntryProject();
            project.setEntryId(entryId);
            project.setProjectId(projectInfoVo.getProjectId());
            project.setRoofType(projectInfoVo.getRoofType());
            projects.add(project);
        }
        projectDao.save(projects);
        //删除原来的勘测信息
        recordDetailDao.deleteByEntryId(entryId);
        List<CrmCustomerEntrySurveyRecordDetail> surveyRecords = new ArrayList<>();
        for(AddEntryReqSurveyDetail addEntryReqSurveyDetail :addEntryReqSurveyDetails){
            CrmCustomerEntrySurveyRecordDetail recordDetail = new CrmCustomerEntrySurveyRecordDetail();
            recordDetail.setEntryId(entryId);
            recordDetail.setProjectId(addEntryReqSurveyDetail.getProjectId());
            recordDetail.setDipAngle(addEntryReqSurveyDetail.getDipAngle());
            recordDetail.setFlyImgs(addEntryReqSurveyDetail.getFlyImgs());
            recordDetail.setRoofPhotoOne(addEntryReqSurveyDetail.getRoofPhotoOne());
            recordDetail.setRoofPhotoTwo(addEntryReqSurveyDetail.getRoofPhotoTwo());
            recordDetail.setRoofPhotoThree(addEntryReqSurveyDetail.getRoofPhotoThree());
            recordDetail.setRoofPhotoFour(addEntryReqSurveyDetail.getRoofPhotoFour());
            recordDetail.setRoofPhotoFive(addEntryReqSurveyDetail.getRoofPhotoFive());
            recordDetail.setDetailSupplementOne(addEntryReqSurveyDetail.getDetailSupplementOne());
            recordDetail.setRoofPhotoFacade(addEntryReqSurveyDetail.getRoofPhotoFacade());
            recordDetail.setRoofPhotoInterior(addEntryReqSurveyDetail.getRoofPhotoInterior());
            recordDetail.setHouseSupplementOne(addEntryReqSurveyDetail.getHouseSupplementOne());
            recordDetail.setCreateTime(now);
            recordDetail.setSurveyFiles(addEntryReqSurveyDetail.getSurveyFiles());
            recordDetail.setSurveyVideo(addEntryReqSurveyDetail.getSurveyVideo());
            recordDetail.setVrRoofOne(addEntryReqSurveyDetail.getVrRoofOne());
            recordDetail.setVrRoofTwo(addEntryReqSurveyDetail.getVrRoofTwo());
            recordDetail.setVrRoofThree(addEntryReqSurveyDetail.getVrRoofThree());
            recordDetail.setVrRoofFour(addEntryReqSurveyDetail.getVrRoofFour());
            recordDetail.setHouseOutlinePhoto(addEntryReqSurveyDetail.getHouseOutlinePhoto());
            recordDetail.setFwImg1(addEntryReqSurveyDetail.getFwImg1());
            recordDetail.setFwImg2(addEntryReqSurveyDetail.getFwImg2());
            recordDetail.setFwImg3(addEntryReqSurveyDetail.getFwImg3());
            recordDetail.setFwImg4(addEntryReqSurveyDetail.getFwImg4());
            recordDetail.setFwImg5(addEntryReqSurveyDetail.getFwImg5());
            surveyRecords.add(recordDetail);
        }
        recordDetailDao.save(surveyRecords);
        return surveyRecord;
    }

    public List<Record> getEntryDetail(String entryId) {
        return recordDetailDao.getEntryDetail(entryId);
    }
}
