package com.yifu.cloud.plus.v1.yifu.social.util;

import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.DateUtil;
import com.yifu.cloud.plus.v1.yifu.ekp.util.EkpIncomeUtil;
import com.yifu.cloud.plus.v1.yifu.ekp.vo.EkpIncomeParamManage;
import com.yifu.cloud.plus.v1.yifu.ekp.vo.EkpIncomeParamRisk;
import com.yifu.cloud.plus.v1.yifu.social.constants.SocialConstants;
import com.yifu.cloud.plus.v1.yifu.social.entity.TIncome;
import com.yifu.cloud.plus.v1.yifu.social.entity.TSendEkpError;
import com.yifu.cloud.plus.v1.yifu.social.mapper.TIncomeMapper;
import com.yifu.cloud.plus.v1.yifu.social.service.TSendEkpErrorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 主要执行人员档案的员工类型同步更新
 * @author huyc
 * @Date 2022-07-08
 * @Description 多线程的执行demo
 */
@Slf4j
@Component
public class DoJointSocialTask {

	@Autowired
	private EkpIncomeUtil ekpIncomeUtil;

	@Autowired
	private TSendEkpErrorService tSendEkpErrorService;

	@Autowired
	private TIncomeMapper incomeMapper;

    /**
     * @Description: 合同-批量更换负责人
     * @Author: huyc
     * @Date: 2022-7-8
     * @return: void
     **/
    @Async
    public void asynchronousEkpIncome(TIncome income) {
        log.info("推送收入到EKP-线程开始");
        try {
			String sendBack = this.getSendBack(income);
			if (Common.isEmpty(sendBack) || sendBack.length() != 32) {
				sendBack = this.getSendBack(income);
			}
			income.setSendTime(new Date());
			if (Common.isNotNull(sendBack) && sendBack.length() == 32) {
				income.setSendStatus(CommonConstants.ONE_STRING);
				income.setSendMonth(DateUtil.addMonth(0));
				income.setEkpId(sendBack);
				incomeMapper.updateById(income);
			} else {
				TSendEkpError error = new TSendEkpError();
				error.setCreateTime(new Date());
				error.setCreateDay(DateUtil.getThisDay());
				error.setType(CommonConstants.FOUR_STRING);
				error.setCreateUserName("收入表推送");
				error.setLinkId(income.getId());
				error.setTitle(sendBack);
				error.setNums(CommonConstants.ONE_INT);
				tSendEkpErrorService.saveError(error);
			}
        } catch (Exception e) {
			TSendEkpError error = new TSendEkpError();
			error.setCreateTime(new Date());
			error.setCreateDay(DateUtil.getThisDay());
			error.setType(CommonConstants.FOUR_STRING);
			error.setCreateUserName("收入表推送");
			error.setLinkId(income.getId());
			error.setTitle(e.getMessage());
			error.setNums(CommonConstants.ONE_INT);
			tSendEkpErrorService.saveError(error);
            log.error("推送收入到EKP错误", e);
        }
        log.info("推送收入到EKP结束");
    }

	/**
	 * @param income
	 * @Description: 判断类型发送
	 * @Author: hgw
	 * @Date: 2022/9/6 15:15
	 * @return: java.lang.String
	 **/
	private String getSendBack(TIncome income) {
		String sendBack;
		if (CommonConstants.ONE_STRING.equals(income.getFeeType())) {
			EkpIncomeParamManage sendParam = new EkpIncomeParamManage();
			this.copyToEkpManage(income, sendParam);
			sendBack = ekpIncomeUtil.sendToEkpManage(sendParam);
		} else {
			EkpIncomeParamRisk sendParam = new EkpIncomeParamRisk();
			this.copyToEkpRisk(income, sendParam);
			sendBack = ekpIncomeUtil.sendToEkpRisk(sendParam);
		}
		return sendBack;
	}

	/**
	 * @param income    收入
	 * @param sendParam 目标格式
	 * @Description: 转化格式，传到EKP
	 * @Author: hgw
	 * @Date: 2022/9/5 16:57
	 * @return: void
	 **/
	private void copyToEkpManage(TIncome income, EkpIncomeParamManage sendParam) {
		// 项目编码
		sendParam.setFd_3adfef5e5b9d34(income.getDeptNo());
		// 项目名称
		sendParam.setFd_3adfef5eb6691c(income.getDeptName());
		// 单号
		sendParam.setFd_3adfef7f5d2b52("");
		// 客户编码
		sendParam.setFd_3adfef80202ab2(income.getUnitNo());
		// 客户名称
		sendParam.setFd_3adfef7fc1c886(income.getUnitName());
		// 类型：单选（人，人次，比例，固定值，总额）
		// feeMode收费方式：1按比例2金额-人数3金额-人次
		if (CommonConstants.ONE_STRING.equals(income.getFeeMode())) {
			sendParam.setFd_3adda3037e3bda("比例");
		} else if (CommonConstants.TWO_STRING.equals(income.getFeeMode())) {
			sendParam.setFd_3adda3037e3bda("人数");
		} else {
			sendParam.setFd_3adda3037e3bda("人次");
		}
		// 产生来源：单选（社保，工资，商险）
		// sourceType1社保2公积金3商险4薪资
		if (CommonConstants.THREE_STRING.equals(income.getSourceType())) {
			sendParam.setFd_3adda320daef5a("商险");
		} else if (CommonConstants.FOUR_STRING.equals(income.getSourceType())) {
			sendParam.setFd_3adda320daef5a("工资");
		} else {
			sendParam.setFd_3adda320daef5a("社保");
		}
		// 姓名
		sendParam.setFd_3adfef948da848(income.getEmpName());
		// 身份证号
		sendParam.setFd_3adfef94dcdbb4(income.getEmpIdcard());
		// 生成月份
		if (Common.isEmpty(income.getCreateMonth()) || income.getCreateMonth().length() != 6) {
			sendParam.setFd_3ae0c23b2e9a92(SocialConstants.YEAR_1970);
		} else {
			sendParam.setFd_3ae0c23b2e9a92(income.getCreateMonth().substring(0, 4)
					+ CommonConstants.CENTER_SPLIT_LINE_STRING + income.getCreateMonth().substring(4, 6));
		}
		// 结算月份
		if (Common.isEmpty(income.getPayMonth()) || income.getPayMonth().length() != 6) {
			sendParam.setFd_3ae0c23cb3fccc(SocialConstants.YEAR_1970);
		} else {
			sendParam.setFd_3ae0c23cb3fccc(income.getPayMonth().substring(0, 4)
					+ CommonConstants.CENTER_SPLIT_LINE_STRING + income.getPayMonth().substring(4, 6));
		}
		// 管理费、风险金金额
		sendParam.setFd_3adfef963bae28(String.valueOf(income.getMoney()));
		// 应收
		sendParam.setFd_3adfefa3daae72(String.valueOf(income.getMoney()));
		// 结算状态
		sendParam.setFd_3adfefaaa3bbd0("未结算");
		// 收款状态
		sendParam.setFd_3adfefaaef583e("未收");
		// 结算单号
		sendParam.setFd_3aead3c68b1078("");
		// 收款单号
		sendParam.setFd_3aeae58b14691c("");
		// 管理费id
		sendParam.setFd_3b13dae9bd70f8(income.getId());
		// 薪酬申请编号
		sendParam.setFd_3b3bf13759b850(income.getApplyNo()==null?"":income.getApplyNo());
	}

	private void copyToEkpRisk(TIncome income, EkpIncomeParamRisk sendParam) {
		// 项目编码
		sendParam.setFd_3adfef5e5b9d34(income.getDeptNo());
		// 项目名称
		sendParam.setFd_3adfef5eb6691c(income.getDeptName());
		// 单号
		sendParam.setFd_3adfef7f5d2b52("");
		// 客户编码
		sendParam.setFd_3adfef80202ab2(income.getUnitNo());
		// 客户名称
		sendParam.setFd_3adfef7fc1c886(income.getUnitName());
		// 类型：单选（人，人次，比例，固定值，总额）
		// feeMode收费方式：1按比例2金额-人数3金额-人次
		if (CommonConstants.ONE_STRING.equals(income.getFeeMode())) {
			sendParam.setFd_3adda3037e3bda("比例");
		} else if (CommonConstants.TWO_STRING.equals(income.getFeeMode())) {
			sendParam.setFd_3adda3037e3bda("人数");
		} else {
			sendParam.setFd_3adda3037e3bda("人次");
		}
		// 产生来源：单选（社保，工资，商险）
		// sourceType1社保2公积金3商险4薪资
		if (CommonConstants.THREE_STRING.equals(income.getSourceType())) {
			sendParam.setFd_3adda320daef5a("商险");
		} else if (CommonConstants.FOUR_STRING.equals(income.getSourceType())) {
			sendParam.setFd_3adda320daef5a("工资");
		} else {
			sendParam.setFd_3adda320daef5a("社保");
		}
		// 姓名
		sendParam.setFd_3adfef948da848(income.getEmpName());
		// 身份证号
		sendParam.setFd_3adfef94dcdbb4(income.getEmpIdcard());
		// 生成月份
		if (Common.isEmpty(income.getCreateMonth()) || income.getCreateMonth().length() != 6) {
			sendParam.setFd_3ae0c3032cffc8(SocialConstants.YEAR_1970);
		} else {
			sendParam.setFd_3ae0c3032cffc8(income.getCreateMonth().substring(0, 4)
					+ CommonConstants.CENTER_SPLIT_LINE_STRING + income.getCreateMonth().substring(4, 6));
		}
		// 结算月份
		if (Common.isEmpty(income.getPayMonth()) || income.getPayMonth().length() != 6) {
			sendParam.setFd_3ae0c3044e5958(SocialConstants.YEAR_1970);
		} else {
			sendParam.setFd_3ae0c3044e5958(income.getPayMonth().substring(0, 4)
					+ CommonConstants.CENTER_SPLIT_LINE_STRING + income.getPayMonth().substring(4, 6));
		}
		// 管理费、风险金金额
		sendParam.setFd_3adfef963bae28(String.valueOf(income.getMoney()));
		// 应收
		sendParam.setFd_3adfefa3daae72(String.valueOf(income.getMoney()));
		// 结算状态
		sendParam.setFd_3adfefaaa3bbd0("未结算");
		// 收款状态
		sendParam.setFd_3adfefaaef583e("未收");
		// 结算单号
		sendParam.setFd_3aead7204bb594("");
		// 收款单号
		sendParam.setFd_3aeae59b70fe5a("");
		// 风险金id
		sendParam.setFd_3b13dac4c03022(income.getId());
		// 薪酬申请编号
		sendParam.setFd_3b3bf0e75ef6c4(income.getApplyNo()==null?"":income.getApplyNo());
	}
}
