package com.sifang.modules.customer.dao;

import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.entity.CustomerEntrySurveyRecord;
import com.sifang.modules.customer.form.AddEntryReq;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Repository
public class CustomerEntrySurveyRecordDao extends BaseDao<CustomerEntrySurveyRecord> {

    /**
     * 查询勘测记录
     */
    public CustomerEntrySurveyRecord findOneByEntryId(String entryId) {
        return getByHql("from CustomerEntrySurveyRecord where entryId = :p1", new Parameter(entryId));
    }

    public CustomerEntrySurveyRecord setBasicInfo(AddEntryReq req, CustomerEntrySurveyRecord surveyRecord, Date now) {
        return setBasicInfo(surveyRecord, req.getDistributorId(), req.getRoofType(), req.getAzimuth(), req.getAngle(), req.getBuildingHeight(),
                req.getRepaymentMethod(),req.getTenantType(), req.getDistance(), req.getFlyImgs(), req.getRoofPhotoOne(),req.getRoofPhotoTwo(),
                req.getRoofPhotoThree(),req.getRoofPhotoFour(),req.getRoofPhotoFive(),req.getHouseOwnership(),req.getRoofPhotoFacade(),
                req.getRoofPhotoInterior(),req.getFilingDocumentPhoto2(),req.getFilingDocumentPhoto3(),req.getHouseSupplementOne(),
                req.getDetailSupplementOne(),req.getVoltageClasses(),req.getTransformerCapacity(),req.getRestrictFlag(),
                req.getSealRoof(),req.getCompletionYear(),req.getDipAngle(),req.getSlopeGradient(),req.getPurlineInterval(),
                req.getWaterproof(),req.getSourcePollution(),req.getOpsCondition(),now,req.getSurveyVideo(),req.getSurveyFiles()
                ,req.getEwLength(),req.getNsWide(),req.getRoofArea(),req.getVrRoofOne(),req.getVrRoofTwo(),req.getVrRoofThree(),req.getVrRoofFour()
                ,req.getHouseOutlinePhoto(),req.getFwImg1(),req.getFwImg2(),req.getFwImg3(),req.getFwImg4(),req.getFwImg5(),req.getRoofFaceThickness()
                ,req.getStructureType(),req.getSiteSlope(),req.getGroundType(),req.getGeologicalCondition(),req.getFenceHeight());
    }

    public CustomerEntrySurveyRecord setBasicInfo(CustomerEntrySurveyRecord surveyRecord, String distributorId,
                                                  String roofType, String azimuth, String angle, BigDecimal buildingHeight,
                                                  String repaymentMethod, String tenantType, BigDecimal distance,
                                                  String flyImgs, String roofPhotoOne, String roofPhotoTwo, String roofPhotoThree,
                                                  String roofPhotoFour, String roofPhotoFive, String houseOwnership,
                                                  String roofPhotoFacade,String roofPhotoInterior,String filingDocumentPhoto2,
                                                  String filingDocumentPhoto3,String houseSupplementOne,String detailSupplementOne,
                                                  BigDecimal voltageClasses,BigDecimal transformerCapacity,String restrictFlag,
                                                  String sealRoof,String completionYear,BigDecimal dipAngle,BigDecimal slopeGradient,
                                                  BigDecimal purlineInterval,String waterproof,String sourcePollution,
                                                  String opsCondition,Date now,String surveyVideo,String surveyFiles,BigDecimal ewLength,
                                                  BigDecimal nsWide,BigDecimal roofArea,String vrRoofOne,String vrRoofTwo,String vrRoofThree,
                                                  String vrRoofFour,String houseOutlinePhoto,String fwImg1,String fwImg2,String fwImg3,String fwImg4,String fwImg5
                                                  ,String RoofFaceThickness,String StructureType,String SiteSlope,String GroundType,String GeologicalCondition
                                                  ,String FenceHeight) {
        surveyRecord.setTraderId(distributorId);
        surveyRecord.setRoofType(roofType);
        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.setFlyImgs(flyImgs);
        surveyRecord.setRoofPhotoOne(roofPhotoOne);
        surveyRecord.setRoofPhotoTwo(roofPhotoTwo);
        surveyRecord.setRoofPhotoThree(roofPhotoThree);
        surveyRecord.setRoofPhotoFour(roofPhotoFour);
        surveyRecord.setRoofPhotoFive(roofPhotoFive);
//        if(StringUtil.isNotBlank(houseOwnership)){
//            surveyRecord.setHouseOwnership(houseOwnership);
//        }
        surveyRecord.setHouseOwnership(houseOwnership);
        surveyRecord.setRoofPhotoFacade(roofPhotoFacade);
        surveyRecord.setRoofPhotoInterior(roofPhotoInterior);
        surveyRecord.setFilingDocumentPhoto2(filingDocumentPhoto2);
        surveyRecord.setFilingDocumentPhoto3(filingDocumentPhoto3);
        surveyRecord.setHouseSupplementOne(houseSupplementOne);
        surveyRecord.setDetailSupplementOne(detailSupplementOne);
        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.setSurveyVideo(surveyVideo);
        surveyRecord.setSurveyFiles(surveyFiles);
        surveyRecord.setEwLength(ewLength);
        surveyRecord.setNsWide(nsWide);
        surveyRecord.setRoofArea(roofArea);
        surveyRecord.setVrRoofOne(vrRoofOne);
        surveyRecord.setVrRoofTwo(vrRoofTwo);
        surveyRecord.setVrRoofThree(vrRoofThree);
        surveyRecord.setVrRoofFour(vrRoofFour);
        surveyRecord.setHouseOutlinePhoto(houseOutlinePhoto);
        surveyRecord.setFwImg1(fwImg1);
        surveyRecord.setFwImg2(fwImg2);
        surveyRecord.setFwImg3(fwImg3);
        surveyRecord.setFwImg4(fwImg4);
        surveyRecord.setFwImg5(fwImg5);

        surveyRecord.setRoofFaceThickness(RoofFaceThickness);
        surveyRecord.setStructureType(StructureType);
        surveyRecord.setSiteSlope(SiteSlope);
        surveyRecord.setGroundType(GroundType);
        surveyRecord.setGeologicalCondition(GeologicalCondition);
        surveyRecord.setFenceHeight(FenceHeight);
        return surveyRecord;
    }

    public StringBuilder appendMsg(StringBuilder sf, CustomerEntrySurveyRecord surveyRecord, AddEntryReq req) {
        return appendMsg(sf, surveyRecord, req.getRoofType(), req.getDistance());
    }

    public StringBuilder appendMsg(StringBuilder sf, CustomerEntrySurveyRecord surveyRecord,
                                   String roofType, BigDecimal distance) {
        if (!Objects.equals(surveyRecord.getRoofType(), roofType)) {
            if (StringUtil.isBlank(roofType)) {
                sf.append("清除房屋类型：修改前：").append(roofType).append(";");
            }
            else if (StringUtil.isBlank(surveyRecord.getRoofType())) {
                sf.append("填写房屋类型：").append(roofType).append(";");
            }
            else {
                sf.append("修改房屋类型：修改前：").append(surveyRecord.getRoofType())
                        .append(" ,修改后 ：").append(roofType).append(";");
            }
        }
        surveyRecord.setRoofType(roofType);
        if ((distance == null && surveyRecord.getDistance() == null)){

        } else if ((distance != null && surveyRecord.getDistance() == null) ||
                (distance == null && surveyRecord.getDistance() != null) ||
                (distance.compareTo(surveyRecord.getDistance()) != 0) ) {
            if (distance == null) {
                sf.append("清除估算并网距离：修改前：").append(surveyRecord.getDistance()).append(";");
            }
            else if (surveyRecord.getDistance() == null) {
                sf.append("填写估算并网距离：").append(distance).append(";");
            }
            else {
                sf.append("估算并网距离：修改前：").append(surveyRecord.getDistance())
                        .append(" ,修改后 ：").append(distance).append(";");
            }
        }
        surveyRecord.setDistance(distance);
        return sf;
    }
}
