package com.sifang.modules.customer.dao;


import com.sifang.modules.customer.entity.CrmCustomerIncomingApplicationInsurance;
import com.sifang.modules.insurance.entity.CrmCustomerEntryInsurance;
import com.sifang.modules.insurance.entity.CrmCustomerNoSystemEntryInsurance;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.common.persistence.Parameter;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.common.utils.StringUtil;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CrmCustomerIncomingApplicationInsuranceDao extends BaseDao<CrmCustomerIncomingApplicationInsurance> {

    public CrmCustomerIncomingApplicationInsurance getByEntryId(String entryId){
        List<CrmCustomerIncomingApplicationInsurance> list = getByEntryIds(entryId);
        if (list.isEmpty()) {
            return null;
        }
        for (CrmCustomerIncomingApplicationInsurance i : list) {
            if (i.getInsuranceAmount() != null) {
                return i;
            }
        }
        return list.get(0);
    }
    public List<CrmCustomerIncomingApplicationInsurance> getByEntryIds(String entryId){
        return find(" from CrmCustomerIncomingApplicationInsurance where entryId =:p1 ",new Parameter(entryId));
    }
    public void deleteRecord(String entryId){
        SQLQuery sqlQuery = getSession().createSQLQuery("DELETE FROM crm_customer_incoming_application_insurance WHERE entry_id =:entryId");
        sqlQuery.setParameter("entryId", entryId);
        sqlQuery.executeUpdate();
    }

    public void updateEntityInfo(CrmCustomerEntryInsurance insurance) {
        List<CrmCustomerIncomingApplicationInsurance> applicationInsurances = getByEntryIds(insurance.getEntryId());
        CrmCustomerIncomingApplicationInsurance applicationInsurance = null;
        CrmCustomerIncomingApplicationInsurance applicationInsuranceGet0 = null;
        if(!applicationInsurances.isEmpty()){
            applicationInsuranceGet0 = applicationInsurances.get(0);
            if(StringUtil.isBlank(applicationInsurances.get(0).getInsuranceType())){
                applicationInsurance = applicationInsurances.get(0);
            }else {
                applicationInsurance = applicationInsurances.stream().filter(s -> dealInsuranceType(insurance.getInsuranceType()).equals(s.getInsuranceType())).findFirst().orElse(null);
            }
        }
        if(applicationInsurance==null){
            applicationInsurance = new CrmCustomerIncomingApplicationInsurance();
        }
        applicationInsurance.setEntryId(insurance.getEntryId());
        applicationInsurance.setInsuranceId(insurance.getId());
        applicationInsurance.setInsuranceName(insurance.getInsuranceName());
        applicationInsurance.setInsuranceAmount(insurance.getInsuranceMoney());
        applicationInsurance.setInsuranceType(dealInsuranceType(insurance.getInsuranceType()));
        applicationInsurance.setInsuranceCompany(insurance.getInsuranceCompanyName());
        applicationInsurance.setInsuranceNumber(insurance.getNumber());
        applicationInsurance.setInsurancePolicy(insurance.getDownUrl());
        if(insurance.getStartTime()!=null)
        applicationInsurance.setInsuranceDateFrom(DateUtils.formatDate(insurance.getStartTime(), "yyyyMMdd"));
        if(insurance.getEndTime()!=null)
        applicationInsurance.setInsuranceDateTo(DateUtils.formatDate(insurance.getEndTime(), "yyyyMMdd"));
        applicationInsurance.setFirstBeneficiary(insurance.getInsurant());
        applicationInsurance.setInsurancePolicyName("保单");
        applicationInsurance.setBwtsFile(applicationInsuranceGet0!=null?applicationInsuranceGet0.getBwtsFile():null);
        applicationInsurance.setBwtsFilename(applicationInsuranceGet0!=null?applicationInsuranceGet0.getBwtsFilename():null);
        applicationInsurance.setBwFile(applicationInsuranceGet0!=null?applicationInsuranceGet0.getBwFile():null);
        applicationInsurance.setFilingCardFile(applicationInsuranceGet0!=null?applicationInsuranceGet0.getFilingCardFile():null);
        this.save(applicationInsurance);
    }

    public void updateNoSysEntityInfo(CrmCustomerNoSystemEntryInsurance insurance) {
        List<CrmCustomerIncomingApplicationInsurance> applicationInsurances = getByEntryIds(insurance.getRequestNo());
        CrmCustomerIncomingApplicationInsurance applicationInsurance = null;
        CrmCustomerIncomingApplicationInsurance applicationInsuranceGet0 = null;
        if(!applicationInsurances.isEmpty()){
            applicationInsuranceGet0 = applicationInsurances.get(0);
            if(StringUtil.isBlank(applicationInsurances.get(0).getInsuranceType())){
                applicationInsurance = applicationInsurances.get(0);
            }else {
                applicationInsurance = applicationInsurances.stream().filter(s -> dealInsuranceType(insurance.getInsuranceType()).equals(s.getInsuranceType())).findFirst().orElse(null);
            }
        }
        if(applicationInsurance==null){
            applicationInsurance = new CrmCustomerIncomingApplicationInsurance();
        }
        applicationInsurance.setEntryId(insurance.getRequestNo());
        applicationInsurance.setInsuranceId(insurance.getId());
        applicationInsurance.setInsuranceName(insurance.getInsuranceName());
        applicationInsurance.setInsuranceAmount(insurance.getInsuranceMoney());
        applicationInsurance.setInsuranceType(dealInsuranceType(insurance.getInsuranceType()));
        applicationInsurance.setInsuranceCompany(insurance.getInsuranceCompanyName());
        applicationInsurance.setInsuranceNumber(insurance.getNumber());
        applicationInsurance.setInsurancePolicy(insurance.getDownUrl());
        if(insurance.getStartTime()!=null)
            applicationInsurance.setInsuranceDateFrom(DateUtils.formatDate(insurance.getStartTime(), "yyyyMMdd"));
        if(insurance.getEndTime()!=null)
            applicationInsurance.setInsuranceDateTo(DateUtils.formatDate(insurance.getEndTime(), "yyyyMMdd"));
        applicationInsurance.setFirstBeneficiary(insurance.getInsurant());
        applicationInsurance.setInsurancePolicyName("保单");
        applicationInsurance.setBwtsFile(applicationInsuranceGet0!=null?applicationInsuranceGet0.getBwtsFile():null);
        applicationInsurance.setBwtsFilename(applicationInsuranceGet0!=null?applicationInsuranceGet0.getBwtsFilename():null);
        applicationInsurance.setBwFile(applicationInsuranceGet0!=null?applicationInsuranceGet0.getBwFile():null);
        applicationInsurance.setFilingCardFile(applicationInsuranceGet0!=null?applicationInsuranceGet0.getFilingCardFile():null);
        this.save(applicationInsurance);
    }

    public void deleteByInsuranceId(String insuranceId){
        SQLQuery sqlQuery = getSession().createSQLQuery("DELETE FROM crm_customer_incoming_application_insurance WHERE insurance_id =:insuranceId");
        sqlQuery.setParameter("insuranceId", insuranceId);
        sqlQuery.executeUpdate();
    }

    public String dealInsuranceType(String entryInsuranceType){
        if (StringUtil.isBlank(entryInsuranceType)) {
            return entryInsuranceType;
        }
        try {
            int type = Integer.parseInt(entryInsuranceType);
            return String.valueOf(type+1);
        }
        catch (Exception e) {
            return entryInsuranceType;
        }
    }

}
