package com.sifang.modules.customer.dao;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.entity.CrmYxIncomingProcess;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.persistence.BaseDao;
import com.sifang.modules.sys.util.SqlBuilder;
import com.sifang.modules.sys.common.persistence.Parameter;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

import java.util.Objects;

@Repository
public class CrmYxIncomingProcessDao extends BaseDao<CrmYxIncomingProcess> {

    /**
     * 添加进件进程
     * @param entryId
     * @param type 事项 1预审 2进件 3合同 4完工资料提交 5并网资料提交
     * @param itemStatus 事项状态
     * @param interfaceStatus 接口状态 0 成功 1失败
     * @param auditStatus 审核状态 0.待审核 1.审核中 2.审核通过 3.审核不通过
     * @param remarks 审核反馈
     */
    public static void addIncomingProcess(String entryId,String type,String itemStatus,String interfaceStatus,String auditStatus, String remarks) {
        Record process = findIncomingProcess(entryId,type);
        if (process == null || StringUtil.isBlank(process.getStr("id"))) {
            SqlBuilder builder = new SqlBuilder();
            builder.line("INSERT INTO crm_yx_incoming_process(id,entry_id,`type`,submit_time,interface_status");
            if (StringUtil.isNotBlank(itemStatus)) {
                builder.line(",item_status");
            }
            if (StringUtil.isNotBlank(auditStatus)) {
                builder.line(",audit_status");
                builder.line(",audit_time");
            }
            if("推送".equals(remarks)){
                remarks="";
            }
            if (StringUtil.isNotBlank(remarks)) {
                builder.line(",remarks");
            }

            builder.line(") VALUES(MD5(UUID()),?,?,NOW(),?",entryId,type,interfaceStatus);
            if (StringUtil.isNotBlank(itemStatus)) {
                builder.line(",?",itemStatus);
            }
            if (StringUtil.isNotBlank(auditStatus)) {
                builder.line(",?",auditStatus);
                builder.line(",NOW()");
            }
            if (StringUtil.isNotBlank(remarks)) {
                builder.line(",?",remarks);
            }
            builder.line(")");
            Db.update(builder.build(),builder.params());
        } else {
            SqlBuilder builder = new SqlBuilder();
            builder.line("UPDATE crm_yx_incoming_process SET");
            if ("1".equals(process.getStr("interface_status"))) {
                builder.line("submit_time=now(),interface_status=? ",interfaceStatus);
            } else {
                builder.line("interface_status=0");
            }
            if(!"推送".equals(remarks)){
                if (!Objects.equals(auditStatus,process.getStr("audit_status"))) {
                    builder.line(",audit_status=?",auditStatus);
                    if ("2".equals(auditStatus) || "3".equals(auditStatus)) {
                        builder.line(",audit_time=NOW()");
                    }
                }
            }
            if (StringUtil.isNotBlank(itemStatus)) {
                builder.line(",item_status=?",itemStatus);
            }
            if("推送".equals(remarks)){
                builder.line(",submit_time=NOW(),audit_time=NULL,audit_status=NULL  ");
                remarks="";
            }else if("退回".equals(remarks) || "取消".equals(remarks)){
                builder.line(",submit_time=NULL,audit_time=NULL,audit_status=NULL,remarks=NULL ");
                remarks="";
            }
            if (StringUtil.isNotBlank(remarks)) {
                builder.line(",remarks=?",remarks);
            }else if("2".equals(auditStatus)){
                builder.line(",remarks=?","");
            }
            builder.line("WHERE id=?",process.getStr("id"));
            Db.update(builder.build(),builder.params());
        }

    }

    public static String findIncomingProcessId(String entryId,String type) {
        return Db.queryStr("select id from crm_yx_incoming_process where entry_id=? and type=?",entryId,type);
    }

    public static Record findIncomingProcess(String entryId, String type) {
        return Db.findFirst("select * from crm_yx_incoming_process where entry_id=? and type=?",entryId,type);
    }

    public static void deleteIncomingProcess(String entryId){
          Db.update("delete from crm_yx_incoming_process where entry_id=? and type in ('3','4','5')",entryId);
    }


    public CrmYxIncomingProcess getProcess(String entryId,String type){
        String sql = "from CrmYxIncomingProcess  WHERE entryId =:p1 and type=:p2 ";
        CrmYxIncomingProcess process = getByHql(sql, new Parameter(entryId,type));
        return process;
    }

    public void deleteLog(String entryId){
        SQLQuery sqlQuery = getSession().createSQLQuery("DELETE FROM crm_yx_incoming_process WHERE type in('3','4','5') and entry_id=:entryId");
        sqlQuery.setParameter("entryId", entryId);
        sqlQuery.executeUpdate();
    }

    public static void returnIncomingProcess(String entryId,String types){
        Db.update("delete from crm_yx_incoming_process where entry_id=? and type in ("+StringUtil.joinInSql(types.split(","))+")",entryId);
    }
}
