package com.sifang.modules.customer.Service;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.customer.dao.CrmAcceptingReportDao;
import com.sifang.modules.customer.dao.CustomerEntryDao;
import com.sifang.modules.customer.dao.CustomerEntryMsgDao;
import com.sifang.modules.customer.entity.CrmAcceptingReport;
import com.sifang.modules.customer.entity.CustomerEntry;
import com.sifang.modules.customer.entity.CustomerEntryMsg;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.dao.UserDao;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.SpringUtils;
import com.sifang.modules.sys.util.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class CustomerEntryMsgService extends BaseService {

    @Autowired
    CustomerEntryMsgDao customerEntryMsgDao;

    @Autowired
    CustomerEntryDao customerEntryDao;

    @Autowired
    CrmAcceptingReportDao reportDao;


   public void saveCustomerEntryMsg(Operator operator, String entryId, short type, String otherMsg,String opera){
        CustomerEntryMsg msg = new CustomerEntryMsg();
       msg.setCustomerEntryId(entryId);
       opera = findName(operator);
       msg.setOperator(opera);
        msg.setOperatorId(operator.getUserId());
        msg.setOperateTime(new Date());
        msg.setType(String.valueOf(type));

        String content = getMsgByType(type);
        if(StringUtil.isNotBlank(content) && StrKit.notBlank(otherMsg)){
            content += "；" + otherMsg;
        }else if(StrKit.notBlank(otherMsg)){
          content=otherMsg;
        }else if(StringUtil.isBlank(content) && StringUtil.isBlank(otherMsg)){
            content="";
        }
        msg.setContent(content);
        customerEntryMsgDao.save(msg);
    }

    /**
     * 新增操作类型
     */
    public void saveCustomerEntryMsg(Operator operator, String customerentryId, short type) {

        CustomerEntryMsg msg = new CustomerEntryMsg();
        msg.setCustomerEntryId(customerentryId);
    /*    User user= UserUtils.getUser();
        if(user == null){
             UserDao userDao = SpringUtils.getBean(UserDao.class);
            if (StrKit.isBlank(userId)) {
                throw new BusinessException("userId不能为空");
            }
             user = userDao.get(userId);
            if (user == null) {
                user = userDao.getById(userId);
            }
        }

        String newName = UserUtils.findName(user);*/
        String opera = findName(operator);
        msg.setOperator(opera);
        msg.setOperatorId(operator.getUserId());
        msg.setOperateTime(new Date());
        msg.setType(String.valueOf(type));
        String content = getMsgByType(type);
        msg.setContent(content);
        customerEntryMsgDao.save(msg);
    }

    /**
     * 操作类型
     */
    public void saveCustomerEntryMsg(User user, String customerentryId, short type, String content) {

        CustomerEntryMsg msg = new CustomerEntryMsg();
        msg.setCustomerEntryId(customerentryId);
        // User user= UserUtils.getUser();
        // String newName = UserUtils.findName(user);
        String opera = UserUtils.findName(user);
        msg.setOperator(opera);
        msg.setOperatorId(user.getId());
        msg.setOperateTime(new Date());
        msg.setType(String.valueOf(type));
        // content = name+","+content;
        msg.setContent(content);
        customerEntryMsgDao.save(msg);
    }

    public void saveCustomerEntryMsg(String customerId,String content){
        CustomerEntryMsg msg = new CustomerEntryMsg();
        msg.setCustomerEntryId(customerId);
        msg.setOperator("");
        msg.setOperatorId("");
        msg.setOperateTime(new Date());
        msg.setType(String.valueOf(CustomerEntryConstants.SIGN_TENANT_DONE));
        // content = name+","+content;
        msg.setContent(content);
        customerEntryMsgDao.save(msg);
    }

    /**
     * 操作类型
     */
    public void saveEntryMsg(Operator operator, String customerentryId, short type, String content) {

        CustomerEntryMsg msg = new CustomerEntryMsg();
        msg.setCustomerEntryId(customerentryId);
        msg.setOperator(operator.getUserName());
        msg.setOperatorId(operator.getUserId());
        msg.setOperateTime(new Date());
        msg.setType(String.valueOf(type));
        msg.setContent(content);
        customerEntryMsgDao.save(msg);
    }

    private String getMsgByType(short type){
        String content = "";
        //1.添加 2.修改 3.审核 4.下单 5.收货 6.安装 7.并网 8.交付 9. 归档
        switch (type) {
            case CustomerEntryConstants
                    .ADVANCE_TYPE_OPT_ADD:
                content += "录入信息";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_AUDIT:
                content += "修改";
                break;
          /*  case CustomerEntryConstants.ADVANCE_TYPE_OPT_CONFIRM:
                content += "审核通过";
                break;*/
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_XIADAN:
                content += "下单";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_SHOUHUO:
                content += "收货";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_CANCEL_AUDIT:
                content += "完工";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_BINGW:
                content += "并网验收";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_JIOAFU:
                content += "交付";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_GUIDANG:
                content += "归档";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_DELETE:
                content += "删除信息";
                break;
         /*   case CustomerEntryConstants.ADVANCE_TYPE_OPT_FAILEDS:
                content += "审核驳回";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_PASS:
                content += "验收通过";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_BACK:
                content += "验收驳回";
                break;*/
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_IN_DOC_BACK:
                content += "归档驳回";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_DJFRETURN:
                content += "待交付驳回";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_SQCS:
                content += "申请重审";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_SHSQCHTG:
                content += "审核申请重审通过";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_SHSQCHBTG:
                content += "审核申请重审驳回";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_SQBG:
                content += "申请整改";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_TWENTY:
                content += "审核申请整改通过";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_TWENTY_ONE:
                content += "审核申请整改驳回";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_INCOMING_TYPE_SUBMIT:
                content += "提交进件申请";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_INCOMING_TYPE_APPROVE:
                content += "进件平台审批通过";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_INCOMING_TYPE_FAIL:
                content += "进件平台审批失败";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_TWO_JJ_TYPE_FAIL:
                content += "农户投放审查 审批拒绝";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_TWO_JJ_TYPE_APPROVE:
                content += "农户投放审查 审批通过";
                break;
            case  CustomerEntryConstants.ENTRY_HR_JDBCZL:
                content += "建档补充资料";
                break;
                case  CustomerEntryConstants.ENTRY_HR_BWBCZL:
                content += "补充并网资料";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_UPLOAD_CONTRACT:
                content += "上传发用电合同";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_QSWC:
                content += "签署完成";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_UPLOAD_BILL:
                content += "上传发票";
                break;
           /* case CustomerEntryConstants.ADVANCE_TYPE_FIX_ACCEPT_REFUSE:
                content += "安装验收驳回";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_FIX_ACCEPT_PASS:
                content += "安装验收通过";
                break;*/
            case CustomerEntryConstants.ADVANCE_TYPE_MERGE_TEMP:
                content += "并网暂存";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_MERGE_SUBMIT:
                content += "并网提交";
                break;
       /*     case CustomerEntryConstants.ADVANCE_TYPE_MERGE_ACCEPT_REFUSE:
                content += "并网验收驳回";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_MERGE_ACCEPT_PASS:
                content += "并网验收通过";
                break;*/
            case CustomerEntryConstants.SIGN_INVITE:
                content += "邀请签约";
                break;
            case CustomerEntryConstants.SIGN_TENANT_DONE:
                content +="农户签约";
                break;
            case CustomerEntryConstants.SIGN_TENANT_REFUSE:
                content +="农户拒签";
                break;
            case CustomerEntryConstants.SIGN_COMPANY_DONE:
                content +="公司签约";
                break;
            case CustomerEntryConstants.SIGN_REVOKE:
                content +="合同作废";
                break;
            case CustomerEntryConstants.ENTRY_DESIGN_BACK:
                content +="设计驳回勘测";
                break;
            case CustomerEntryConstants.INCOMING_CANCEL_PASS:
                content +="终止进件审核通过";
                break;
            case CustomerEntryConstants.INCOMING_CANCEL_REFUSE:
                content +="终止进件审核不通过";
                break;
          /*  case CustomerEntryConstants.ADVANCE_TYPE_OPT_GWANSH:
                content +="资方安装验收";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_GWBWSH:
                content +="资方并网验收";
                break;
            case CustomerEntryConstants.ADVANCE_TYPE_OPT_ANSH:
                content +="工程部完工验收";
                break;*/
         /*   case CustomerEntryConstants.ACCEPTANCE_REVIEW_PASS:
                content +="工程部负责人审核通过";
                break;*/
            /*case CustomerEntryConstants.ACCEPTANCE_REVIEW_REFUSE:
                content +="工程部负责人审核驳回";
                break;*/
          /*  case CustomerEntryConstants.MERGE_ACCEPTANCE_PASS:
                content +="验收审核通过";
                break;
            case CustomerEntryConstants.MERGE_ACCEPTANCE_REFUSE:
                content +="验收审核驳回";
                break;*/
            default:

        }
        return content;
    }

    public List<Record> listOptRecordByEntryId(String entryId,String traderId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select m.*")
                .line("from crm_customer_entry_msg m");
//            if(StringUtil.isNotBlank(traderId)){
//                builder.line(" INNER JOIN sys_user u ON u.id = m.operator_id ");
//            }
        builder.line("where customer_entry_id=?", entryId);
//        if (StringUtil.isNotBlank(traderId)) {
//            builder.line(" and u.user_type IN ('6','7')");
//        }
        builder.line("order by operate_time desc");
        return Db.find(builder.build(), builder.params());
    }
    public List<Record> listOptRecordByEntryId2(String entryId){
        SqlBuilder builder = new SqlBuilder();
        builder.line("select m.*")
                .line("from crm_customer_entry_msg m");
        builder.line("where customer_entry_id=?", entryId);
        builder.line("order by operate_time desc");
        return Db.find(builder.build(), builder.params());
    }

    public static String findName(Operator operator) {
        try {
            if (operator == null) {
                return "";
            }
            if (User.USER_TYPE_KJ.equals(operator.getUserType())) {
                return operator.getUserName();
            } else if (User.USER_TYPE_ADVISOR.equals(operator.getUserType())) {
                String traderId = CrmUtils.getTraderId(operator.getUserId());
                String traderName = CrmUtils.getTraderNameById(traderId);
                return traderName + " " + UserUtils.findTraderRoleName(operator.getUserId()) + " " + operator.getUserName();
            } else if (StringUtil.equalsAny(operator.getUserType(), User.USER_TYPE_YYS, User.USER_TYPE_PTYG)) {
                String departName = CrmUtils.getEmpDepartName(operator.getUserId());
                String otherName = departName + " " + UserUtils.findRoleNameByEmp(operator.getUserId()) + " " + operator.getUserName();
                return otherName;
            }
        } catch (Exception e) {
            return operator.getUserName();
        }
        return operator.getUserName();
    }

}
