package com.sifang.modules.customer.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
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.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.util.SqlBuilder;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.xml.ws.Action;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class CrmCustomerEntrySurveyRecordDetailDao extends BaseDao<CrmCustomerEntrySurveyRecordDetail> {

    public List<CrmCustomerEntrySurveyRecordDetail> findByEntryId(String entryId) {
        return find("from CrmCustomerEntrySurveyRecordDetail where entryId = :p1  order by projectId asc   ", new Parameter(entryId));
    }

    public void deleteByEntryId(String entryId){
        SQLQuery sqlQuery = getSession().createSQLQuery("DELETE FROM crm_customer_entry_survey_record_detail WHERE entry_id=:mainId");
        sqlQuery.setParameter("mainId", entryId);
        sqlQuery.executeUpdate();
    }

    public List<Record> getEntryDetail(String entryId) {
        SqlBuilder builder = new SqlBuilder();
        builder.line("select s.assembly_specs as assemblySpecs,s.id as projectId,s.code as projectCode,s.name as projectName,s.pay_cycle as payCycle,s.filing_type as filingType,p.roof_type as roofType");
        builder.line(",d.fly_imgs as flyImgs,d.roof_photo_one as roofPhotoOne,d.roof_photo_two as roofPhotoTwo,d.roof_photo_three as roofPhotoThree");
        builder.line(",d.roof_photo_four as roofPhotoFour,d.roof_photo_five as roofPhotoFive,d.detail_supplement_one as detailSupplementOne,d.roof_photo_facade as roofPhotoFacade,d.roof_photo_interior as roofPhotoInterior ");
        builder.line(",d.house_supplement_one as houseSupplementOne,d.survey_video as surveyVideo,d.survey_files as surveyFiles,d.vr_roof_one as vrRoofOne");
        builder.line(",d.vr_roof_two as vrRoofTwo,d.vr_roof_three as vrRoofThree,d.vr_roof_four as vrRoofFour,d.house_outline_photo as houseOutlinePhoto,d.fw_img1 as fwImg1,d.fw_img2,d.fw_img3,d.fw_img4,d.fw_img5");
        builder.line(",d.fw_img1 as fwImg1,d.fw_img2 as fwImg2,d.fw_img3 as fwImg3,d.fw_img4 as fwImg4,d.fw_img5 as fwImg5,d.dip_angle as dipAngle");
        builder.line("  from crm_customer_entry_survey_record_detail d");
        builder.line(" left join crm_customer_entry_project p  on p.entry_id=d.entry_id and p.project_id=d.project_id ");
        builder.line(" left join crm_project_setting s  on s.id=d.project_id ");
        builder.line("where 1=1 and d.entry_id = ? ",entryId);
        builder.line(" order by d.project_id  asc ");
        return Db.find(builder.build(), builder.params());
    }
}
