package com.sifang.modules.customer.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CrmCustomerIncomingApplication;
import com.sifang.modules.customer.entity.CrmYxIncomingLog;
import com.sifang.modules.customer.form.QueryDocumentRes;
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 com.sifang.modules.sys.entity.Operator;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Repository
public class CrmYxIncomingLogDao extends BaseDao<CrmYxIncomingLog> {

    //添加进件日志
    public void addCrmYxIncomingLog(String entryId, String partnersContractNumber, String type, String remarks, Operator operator){
          CrmYxIncomingLog log=new CrmYxIncomingLog();
          log.setEntryId(entryId);
          log.setPartnersContractNumber(partnersContractNumber);
          log.setType(type);
          log.setRemarks(remarks);
          if(operator!=null){
              log.setCreateBy(operator.getUserId());
              log.setCreateName(operator.getUserName());
          }
          log.setCreateTime(new Date());
          save(log);
    }

    //如果有重复日志  那么更新之前的日志
    public void saveCrmYxIncomingLog(QueryDocumentRes res, String entryId, String partnersContractNumber, String type, String remarks){
        //获取最新的日志
        CrmYxIncomingLog latestLog=getLatestLog(entryId);
        if(latestLog==null || !Objects.equals(res.getProjectStatus(),latestLog.getProjectStatus()) || !Objects.equals(res.getFarmerSignStatus(),latestLog.getFarmerSignStatus()) || !Objects.equals(res.getReason(),latestLog.getReason())){
            CrmYxIncomingLog log=new CrmYxIncomingLog();
            log.setEntryId(entryId);
            log.setPartnersContractNumber(partnersContractNumber);
            log.setType(type);
            log.setRemarks(remarks);
            log.setReason(res.getReason());
            log.setProjectStatus(res.getProjectStatus());
            log.setFarmerSignStatus(res.getFarmerSignStatus());
            log.setFarmerSignDetailsStatus(res.getFarmerSignDetailsStatus());
            log.setCreateTime(new Date());
            save(log);
        }else{
           /* latestLog.setCreateTime(new Date());
            save(latestLog);*/
        }
    }

    public CrmYxIncomingLog getLatestLog(String entryId){
        String id=Db.queryStr(" SELECT id FROM crm_yx_incoming_log WHERE entry_id =?  AND type = '0'  ORDER BY create_time DESC LIMIT 1 ",entryId);
        if(StringUtil.isBlank(id)){
            return null;
        }else {
            return getByHql("FROM CrmYxIncomingLog WHERE id =:p1 ", new Parameter(id));
        }
    }

    public List<Record> getLogByEntryId(String entryId){
         return Db.find(" SELECT l.id,l.type,l.entry_id,l.partners_contract_number,l.type,l.remarks,l.create_name, " +
                 "DATE_FORMAT(l.create_time,'%Y-%m-%d %H:%i:%s') as createTime from crm_yx_incoming_log l where entry_id=? order by l.create_time asc ",entryId);
    }

    public String getYsContent(String result,String remarks){
        String content = "";
        //预审结果:NEW-预审待审批 APPROVING-预审审批中 APPROVED-预审通过 REJECT-预审拒绝
        if(StringUtil.isBlank(result)){
            return content;
        }
        switch (result) {
            case "NEW":
                content = "预审待审批";
                break;
            case "APPROVING":
                content = "预审审批中";
                break;
            case "APPROVED":
                content = "预审通过";
                break;
            case "REJECTED":
                content = "预审拒绝"+(StringUtil.isNotBlank(remarks)?",拒绝原因："+remarks:"");
                break;
            default:
        }
        content=content+"；";
        return content;
    }

    /**
     *
     * @param projectStatus NEW-待审批 APPROVING - 审批中 APPROVED - 审批通过 REJECTED - 审批拒绝  CLOSED - 取消 APPROVED_RETURN - 退回（农户投放审查退回到风险审查） CANCEL - 撤回（越秀业务经理撤回流程） @param farmerSignStatus
     * @return   farmerSignStatus NEW-待审批  APPROVING-审批中 APPROVED-审批通过 REJECTED-审批拒绝  CANCEL - 撤回（越秀业务经理撤回流程） WAITING-等待推送/补充并网及投放信息
     */
    public String getDjContent(String projectStatus,String farmerSignStatus,String remarks, CrmCustomerIncomingApplication incomingApplication){
        StringBuffer sb=new StringBuffer();
        if(!"APPROVED".equals(incomingApplication.getProjectStatus())) {  // 风险审查状态
            if(StringUtil.isNotBlank(projectStatus)){
                sb.append("风险审查状态 ：");
                String content="";
                switch (projectStatus) {
                    case "NEW":
                        content = "待审批";
                        break;
                    case "APPROVING":
                        content = "审批中";
                        break;
                    case "APPROVED":
                        content = "审批通过";
                        break;
                    case "REJECTED":
                        content = "审批拒绝"+(StringUtil.isNotBlank(remarks)?",拒绝原因："+remarks:"");
                        break;
                    case "CLOSED":
                        content = "取消";
                        break;
                    case "APPROVED_RETURN":
                        content = "退回"+(StringUtil.isNotBlank(remarks)?",退回原因："+remarks:"");
                        break;
                    default:
                }
                content=content+"；";
                sb.append(content);
            }
        }else{
            if(StringUtil.isNotBlank(farmerSignStatus)){
                sb.append("  农户投放审查状态 ：");
                String content="";
                switch (farmerSignStatus) {
                    case "NEW":
                        content = "待审批";
                        break;
                    case "APPROVING":
                        content = "审批中";
                        break;
                    case "APPROVED":
                        content = "审批通过";
                        break;
                    case "REJECTED":
                        content = "审批拒绝"+(StringUtil.isNotBlank(remarks)?",拒绝原因："+remarks:"");
                        break;
                    case "WAITING":
                        content = "等待推送/补充并网及投放信息"+(StringUtil.isNotBlank(remarks)?",拒绝原因："+remarks:"");
                        break;
                    default:
                }
                content=content+"；";
                sb.append(content);
            }
        }


        return sb.toString();
    }

    public String getIncomingStatusStr(String projectStatus,String farmerSignStatus){
        StringBuffer sb=new StringBuffer();
        if(StringUtil.isNotBlank(projectStatus)){
            sb.append("风险审查状态 ：");
            String content="";
            switch (projectStatus) {
                case "NEW":
                    content = "待审批";
                    break;
                case "APPROVING":
                    content = "审批中";
                    break;
                case "APPROVED":
                    content = "审批通过";
                    break;
                case "REJECTED":
                    content = "审批拒绝";
                    break;
                case "CLOSED":
                    content = "取消";
                    break;
                case "APPROVED_RETURN":
                    content = "退回";
                    break;
                default:
            }
            sb.append(content);
            sb.append("；");
        }
        if(StringUtil.isNotBlank(farmerSignStatus)){
            sb.append(" 农户投放审查状态 ：");
            String content="";
            switch (farmerSignStatus) {
                case "NEW":
                    content = "待审批";
                    break;
                case "APPROVING":
                    content = "审批中";
                    break;
                case "APPROVED":
                    content = "审批通过";
                    break;
                case "REJECTED":
                    content = "审批拒绝";
                    break;
                case "CANCEL":
                    content = "撤回";
                    break;
                case "WAITING":
                    content = "等待推送/补充并网及投放信息";
                    break;
                default:
            }
            sb.append(content);
        }
        return sb.toString();
    }
}
