package com.yifu.cloud.plus.v1.ekp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.ekp.entity.EkpSocialInfo;
import com.yifu.cloud.plus.v1.ekp.mapper.EkpSocialInfoMapper;
import com.yifu.cloud.plus.v1.ekp.service.EkpSocialInfoService;
import com.yifu.cloud.plus.v1.ekp.vo.EkpDeptInfoVo;
import com.yifu.cloud.plus.v1.ekp.vo.EkpSocialPushInfoVo;
import com.yifu.cloud.plus.v1.ekp.vo.EkpSocialSumInfoVo;
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.vo.EkpPushSocialParam;
import com.yifu.cloud.plus.v1.yifu.social.entity.TPaymentSocialPush;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
//社保明细表
 *
//@author huyc
//@date 2024-02-29 11:21:56
 */
@Log4j2
@Service
public class EkpSocialInfoServiceImpl extends ServiceImpl<EkpSocialInfoMapper, EkpSocialInfo> implements EkpSocialInfoService {

	/**
	//@Description: 社保明细推送至ekp
	//@Author: huyc
	//@Date: 2024-2-29
	//@return: void
	 **/
	public EkpSocialPushInfoVo pushSocialInfoToEkp(List<EkpPushSocialParam> unPushList) {

		//插入数据集合
		List<EkpSocialInfo> socialInfoList = new ArrayList<>();
		//社保推送记录表
		List<TPaymentSocialPush> socialPushList = new ArrayList<>();
		//社保id集合 用于更新社保状态
		List<String> paymentIds = new ArrayList<>();

		//获取ekp所有项目信息
		List<EkpDeptInfoVo> deptInfoVoList = baseMapper.getAllEkpDeptInfo();
		Map<String,EkpDeptInfoVo> map = new HashMap<>();
		EkpDeptInfoVo deptInfo;
		EkpSocialSumInfoVo socialSumInfo;
		if (Optional.ofNullable(deptInfoVoList).isPresent()) {
			map = deptInfoVoList.stream().collect(Collectors.toMap(EkpDeptInfoVo::getDeptNo, k->k));
		}

		for (EkpPushSocialParam socialParam : unPushList) {
			EkpSocialInfo socialInfo = new EkpSocialInfo();
			//对象信息赋值
			copyProperties(socialParam,socialInfo);

			//社保信息
			deptInfo = map.get(socialInfo.getFd_3adfe8c70d3fd4());
			socialInfo.setFd_3b16e2f436ff98_text(deptInfo.getDeptName());
			socialInfo.setFd_3b16e2f436ff98(deptInfo.getFdId());
			//实际结算月份
			if("生成月本月数据".equals(deptInfo.getSocialType())){
				socialInfo.setFd_3adfe8cff746bc(socialInfo.getFd_3adfe8cb96c41e());
			}else if("生成月上月数据".equals(deptInfo.getSocialType())){
				socialInfo.setFd_3adfe8cff746bc(DateUtil.dateToString(DateUtil.addMonthByDate(DateUtil.stringToDate(socialInfo.getFd_3adfe8cb96c41e(),"yyyy-MM"),-1)));
			}else if("生成月次月数据".equals(deptInfo.getSocialType())){
				socialInfo.setFd_3adfe8cff746bc(DateUtil.dateToString(DateUtil.addMonthByDate(DateUtil.stringToDate(socialInfo.getFd_3adfe8cb96c41e(),"yyyy-MM"),1)));
			}
			//是否全部结算
			if("全量未结算数据".equals(deptInfo.getSocialType())){
				socialInfo.setFd_3b1354c3add8c2(CommonConstants.IS_TRUE);
			}else {
				socialInfo.setFd_3b1354c3add8c2(CommonConstants.IS_FALSE);
			}
			//获取社保订单明细合计
			socialSumInfo = baseMapper.getCostSumInfo(socialInfo.getFd_3adfe8c7e4cf7a(),socialInfo.getFd_3adfe8cb96c41e());
			//预估合计
			Double ygSum = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getYgSum();
			//预估个人合计
			Double ygPersonSum = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getYgPersonSum();
			//实缴合计
			Double sjSum = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getSjSum();
			//预估单位合计
			Double ygDwSum = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getYgDwSum();
			//实缴单位合计
			Double sjDwSum = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getSjDwSum();
			//实缴个人合计
			Double sjPersonSum = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getSjPersonSum();
			//应收
			Double ys = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getYs();
			//个人应收
			Double personYs = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getPersonYs();
			//单位应收
			Double dwYs = Common.isEmpty(socialSumInfo) ? 0: socialSumInfo.getDwYs();
			//单位差异
			if("是".equals(socialInfo.getFd_3add9de0be85e4()) && "实缴".equals(socialInfo.getFd_3add9dd7833db8())){
				//单位差异
				//$列表.求总和$($社保订单明细*总数(实缴单位合计)$)+ $实缴单位合计$ - $列表.求总和$($社保订单明细*总数(预估单位合计)$)
				socialInfo.setFd_3adfeb53c70f72(Double.sum(Double.sum(sjDwSum,socialInfo.getFd_3af9f2e9208e4e()),-ygDwSum));
				//个人差异
				//$列表.求总和$($社保订单明细*总数(实缴个人合计)$)+ $实缴个人合计$ - $列表.求总和$($社保订单明细*总数(预估个人合计)$)
				socialInfo.setFd_3adfeb5413fb44(Double.sum(Double.sum(sjPersonSum,socialInfo.getFd_3af9f303037214()),-ygPersonSum));
			}

			//Fd_3adfeb7b624f06应收；Fd_3b35a57aee1428个人应收；fd_3b35a57b6e9d0a单位应收
			if("是".equals(deptInfo.getIsBpo())){
				//应收
				socialInfo.setFd_3adfeb7b624f06(CommonConstants.ZERO_INTEGER.doubleValue());
				//个人应收
				socialInfo.setFd_3b35a57aee1428(CommonConstants.ZERO_INTEGER.doubleValue());
				//单位应收
				socialInfo.setFd_3b35a57b6e9d0a(CommonConstants.ZERO_INTEGER.doubleValue());
			}else{
				if("实缴".equals(deptInfo.getSettleType())){
					if("预估".equals(socialInfo.getFd_3add9dd7833db8())){
						//$列表.求总和$($社保订单明细*总数(预估合计)$)  + $预估合计$ - $列表.求总和$($社保订单明细*总数(应收)$)
						socialInfo.setFd_3adfeb7b624f06(Double.sum(Double.sum(ygSum,socialInfo.getFd_3af9f2883941b4()),-ys));
						//$列表.求总和$($社保订单明细*总数(预估个人合计)$)  +  $预估个人合计$ - $列表.求总和$($社保订单明细*总数(个人应收)$)
						socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(ygPersonSum,socialInfo.getFd_3af9f285ee1e38()),-personYs));
						//$列表.求总和$($社保订单明细*总数(预估单位合计)$)  + $预估单位合计$ - $列表.求总和$($社保订单明细*总数(单位应收)$)
						socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(ygDwSum,socialInfo.getFd_3adfeb4e8064a8()),-dwYs));
					}else{
						if(ygSum > 0){
							//应收
							socialInfo.setFd_3adfeb7b624f06(CommonConstants.ZERO_INTEGER.doubleValue());
							//个人应收
							socialInfo.setFd_3b35a57aee1428(CommonConstants.ZERO_INTEGER.doubleValue());
							//单位应收
							socialInfo.setFd_3b35a57b6e9d0a(CommonConstants.ZERO_INTEGER.doubleValue());
						}else{
							//$列表.求总和$($社保订单明细*总数(实缴合计)$)+ $实缴合计$  - $列表.求总和$($社保订单明细*总数(应收)$)
							socialInfo.setFd_3adfeb7b624f06(Double.sum(Double.sum(sjSum,socialInfo.getFd_3af9f3059e5b9c()),-ys));
							//$列表.求总和$($社保订单明细*总数(实缴个人合计)$)+ $实缴个人合计$  - $列表.求总和$($社保订单明细*总数(个人应收)$)
							socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(sjPersonSum,socialInfo.getFd_3af9f303037214()),-personYs));
							//列表.求总和$($社保订单明细*总数(实缴单位合计)$)+ $实缴单位合计$  - $列表.求总和$($社保订单明细*总数(单位应收)$)
							socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(sjDwSum,socialInfo.getFd_3af9f2e9208e4e()),-dwYs));
						}
					}
				}else{
					if("预估".equals(socialInfo.getFd_3add9dd7833db8())){
						//$列表.求总和$($社保订单明细*总数(预估合计)$) + $预估合计$-$列表.求总和$($社保订单明细*总数(应收)$)
						socialInfo.setFd_3adfeb7b624f06(Double.sum(Double.sum(ygSum,socialInfo.getFd_3af9f2883941b4()),-ys));
						//$列表.求总和$( $社保订单明细*总数(预估个人合计)$) + $预估个人合计$-$列表.求总和$($社保订单明细*总数(个人应收)$)
						socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(ygPersonSum,socialInfo.getFd_3af9f285ee1e38()),-personYs));
						//$列表.求总和$($社保订单明细*总数(预估单位合计)$) + $预估单位合计$-$列表.求总和$($社保订单明细*总数(单位应收)$)
						socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(ygDwSum,socialInfo.getFd_3adfeb4e8064a8()),-dwYs));
					}else{
						//$列表.求总和$($社保订单明细*总数(实缴合计)$)+ $实缴合计$  - $列表.求总和$($社保订单明细*总数(应收)$)
						socialInfo.setFd_3adfeb7b624f06(Double.sum(Double.sum(sjSum,socialInfo.getFd_3af9f3059e5b9c()),-ys));
						//$列表.求总和$($社保订单明细*总数(实缴个人合计)$)+ $实缴个人合计$  - $列表.求总和$($社保订单明细*总数(个人应收)$)
						socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(sjPersonSum,socialInfo.getFd_3af9f303037214()),-personYs));
						//$列表.求总和$($社保订单明细*总数(实缴单位合计)$)+ $实缴单位合计$  - $列表.求总和$($社保订单明细*总数(单位应收)$)
						socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(sjDwSum,socialInfo.getFd_3af9f2e9208e4e()),-dwYs));
					}
				}
			}
			if("预估".equals(socialInfo.getFd_3add9dd7833db8())){
				//应支出
				socialInfo.setFd_3adfeb7bd97464(CommonConstants.ZERO_INTEGER.doubleValue());
				//个人应支
				socialInfo.setFd_3b35a5b03100c4(CommonConstants.ZERO_INTEGER.doubleValue());
				//单位应支
				socialInfo.setFd_3b35a5b0b04d54(CommonConstants.ZERO_INTEGER.doubleValue());
			}else{
				//应支出
				socialInfo.setFd_3adfeb7bd97464(socialInfo.getFd_3af9f3059e5b9c());
				//个人应支
				socialInfo.setFd_3b35a5b03100c4(socialInfo.getFd_3af9f303037214());
				//单位应支
				socialInfo.setFd_3b35a5b0b04d54(socialInfo.getFd_3af9f2e9208e4e());
			}
			//创建时间
			socialInfo.setCreateTime(DateUtil.getCurrentDateTime());
			socialInfoList.add(socialInfo);
			TPaymentSocialPush a = new TPaymentSocialPush();
			a.setPaymentIds(socialParam.getSocialIds());
			a.setId(socialParam.getFd_3b0afbe1f94a08());
			socialPushList.add(a);
			if (CommonConstants.ZERO_STRING.equals(socialParam.getYgFlag())) {
				paymentIds.add(socialParam.getSocialIds());
			}else {
				paymentIds.add(socialParam.getFd_3b0afbe1f94a08());
			}
		}
		boolean flag = this.saveBatch(socialInfoList);
		if (flag) {
			//推送成功保存社保明细合并的数据对应关系
			EkpSocialPushInfoVo pushInfoVo = new EkpSocialPushInfoVo();
			pushInfoVo.setUnPushInfo(socialPushList);
			pushInfoVo.setPaymentIds(paymentIds);
			return pushInfoVo;
		}
		return null;
	}

	private void copyProperties(EkpPushSocialParam socialParam,EkpSocialInfo socialInfo) {
		//订单类型
		socialInfo.setFd_3add9dd7833db8(socialParam.getFd_3add9dd7833db8());
		//与工资合并结算
		socialInfo.setFd_3add9e1a670144(socialParam.getFd_3add9e1a670144());
		//是否有预估
		socialInfo.setFd_3add9de0be85e4(socialParam.getFd_3add9de0be85e4());
		//是否为BPO业务
		socialInfo.setFd_3b178e3ba5e258(socialParam.getFd_3b178dfcf9e3e6());
		//创建人姓名
		socialInfo.setFd_3b438e61af0a56(socialParam.getFd_3b438e33f37378());
		//项目编码
		socialInfo.setFd_3adfe8c70d3fd4(socialParam.getFd_3adfe8c70d3fd4());
		//项目名称
		socialInfo.setFd_3adfe8c8468e54(socialParam.getFd_3adfe8c8468e54());
		//单号
		socialInfo.setFd_3adfe95c169c48(socialParam.getFd_3adfe95c169c48());
		//客户编码
		socialInfo.setFd_3adfe8c73cb5a4(socialParam.getFd_3adfe8c73cb5a4());
		//客户名称
		socialInfo.setFd_3adfe8c81a0e42(socialParam.getFd_3adfe8c81a0e42());
		//社保户
		socialInfo.setFd_3aeafa25916e82(socialParam.getFd_3aeafa25916e82());
		//姓名
		socialInfo.setFd_3adfe8c79989d4(socialParam.getFd_3adfe8c79989d4());
		//身份证号
		socialInfo.setFd_3adfe8c7e4cf7a(socialParam.getFd_3adfe8c7e4cf7a());
		//我司到款单位
		socialInfo.setFd_3b0194c7336de0(socialParam.getFd_3b01953871b8be());
		//生成月份
		socialInfo.setFd_3adfe8cb96c41e(socialParam.getFd_3adfe8cb96c41e());
		//缴纳月份
		socialInfo.setFd_3adfe8cf632700(socialParam.getFd_3adfe8cf632700());
		//社保ID
		socialInfo.setFd_3b0b716e771e06(socialParam.getFd_3b0afbe1f94a08());
		//薪酬申请编号
		socialInfo.setFd_3b3cab5f343a90(socialParam.getFd_3b3cab77923f44());
		//缴纳地
		socialInfo.setFd_3b5cc5e697079a(socialParam.getFd_3b5cc58d1a70fe());
		//社保合集ID
		socialInfo.setFd_3b6495a5ad6e10(socialParam.getSocialIds());
		//预估单位养老
		socialInfo.setFd_3af9f257b1b586(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ec80a9de7a()) ? null : Double.valueOf(socialParam.getFd_3af9ec80a9de7a()));
		//预估单位医疗
		socialInfo.setFd_3af9f25b851e94(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9eba5899c90()) ? null : Double.valueOf(socialParam.getFd_3af9ec80a9de7a()));
		//预估单位失业
		socialInfo.setFd_3af9f25f5e9cc4(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9eba5f6e19e()) ? null : Double.valueOf(socialParam.getFd_3af9eba5f6e19e()));
		//预估单位工伤
		socialInfo.setFd_3af9f2617e530a(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9eba684f592()) ? null : Double.valueOf(socialParam.getFd_3af9eba684f592()));
		//预估单位生育
		socialInfo.setFd_3af9f263e094c6(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9eba71c0138()) ? null : Double.valueOf(socialParam.getFd_3af9eba71c0138()));
		//预估单位补缴利息
		socialInfo.setFd_3af9f267a03bd6(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9eba7c3da5e()) ? null : Double.valueOf(socialParam.getFd_3af9eba7c3da5e()));
		//预估单位大病救助
		socialInfo.setFd_3af9f26a3cae94(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9eba863c0ee()) ? null : Double.valueOf(socialParam.getFd_3af9eba863c0ee()));
		//预估单位合计
		socialInfo.setFd_3adfeb4e8064a8(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3adfeb4e8064a8()) ? null : Double.valueOf(socialParam.getFd_3adfeb4e8064a8()));
		//预估个人养老
		socialInfo.setFd_3af9f27a3974e6(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ebbd791662()) ? null : Double.valueOf(socialParam.getFd_3af9ebbd791662()));
		//预估个人医疗
		socialInfo.setFd_3af9f27ce947ac(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ebbdd9797e()) ? null : Double.valueOf(socialParam.getFd_3af9ebbdd9797e()));
		//预估个人失业
		socialInfo.setFd_3af9f27efdd912(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ebbe29ce1c()) ? null : Double.valueOf(socialParam.getFd_3af9ebbe29ce1c()));
		//预估个人补缴利息
		socialInfo.setFd_3af9f281651734(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ebbecc4aa8()) ? null : Double.valueOf(socialParam.getFd_3af9ebbecc4aa8()));
		//预估个人大病救助
		socialInfo.setFd_3af9f283a6f288(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ebbf3e8be2()) ? null : Double.valueOf(socialParam.getFd_3af9ebbf3e8be2()));
		//预估个人合计
		socialInfo.setFd_3af9f285ee1e38(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3adfeb52a4d2e2()) ? null : Double.valueOf(socialParam.getFd_3adfeb52a4d2e2()));
		//预估合计
		socialInfo.setFd_3af9f2883941b4(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ed7e813b86()) ? null : Double.valueOf(socialParam.getFd_3af9ed7e813b86()));
		//实缴单位养老
		socialInfo.setFd_3af9f2d8df88c0(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3938170a()) ? null : Double.valueOf(socialParam.getFd_3af9ee3938170a()));
		//实缴单位医疗
		socialInfo.setFd_3af9f2db1e5e16(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee39dea6a8()) ? null : Double.valueOf(socialParam.getFd_3af9ee39dea6a8()));
		//实缴单位失业
		socialInfo.setFd_3af9f2dd5d8fb8(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3a46b7e6()) ? null : Double.valueOf(socialParam.getFd_3af9ee3a46b7e6()));
		//实缴单位工伤
		socialInfo.setFd_3af9f2e03295dc(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3aa9c84a()) ? null : Double.valueOf(socialParam.getFd_3af9ee3aa9c84a()));
		//实缴单位生育
		socialInfo.setFd_3af9f2e237f794(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3afb34c2()) ? null : Double.valueOf(socialParam.getFd_3af9ee3afb34c2()));
		//实缴单位补缴利息
		socialInfo.setFd_3af9f2e45a5d78(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3b5ddae8()) ? null : Double.valueOf(socialParam.getFd_3af9ee3b5ddae8()));
		//实缴单位合计
		socialInfo.setFd_3af9f2e9208e4e(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3c0bf286()) ? null : Double.valueOf(socialParam.getFd_3af9ee3c0bf286()));
		//实缴个人养老
		socialInfo.setFd_3af9f2ec516c30(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3e513962()) ? null : Double.valueOf(socialParam.getFd_3af9ee3e513962()));
		//实缴个人医疗
		socialInfo.setFd_3af9f2f8290f24(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3e066d48()) ? null : Double.valueOf(socialParam.getFd_3af9ee3e066d48()));
		//实缴个人失业
		socialInfo.setFd_3af9f2fa79ee72(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3db44d96()) ? null : Double.valueOf(socialParam.getFd_3af9ee3db44d96()));
		//实缴个人补缴利息
		socialInfo.setFd_3af9f2fcde9158(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3d634946()) ? null : Double.valueOf(socialParam.getFd_3af9ee3d634946()));
		//实缴个人大病救助
		socialInfo.setFd_3af9f2ff2c9f42(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3d0ba3b6()) ? null : Double.valueOf(socialParam.getFd_3af9ee3d0ba3b6()));
		//实缴个人合计
		socialInfo.setFd_3af9f303037214(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3cb6d4fa()) ? null : Double.valueOf(socialParam.getFd_3af9ee3cb6d4fa()));
		//实缴合计
		socialInfo.setFd_3af9f3059e5b9c(CommonConstants.EMPTY_STRING.equals(socialParam.getFd_3af9ee3c6bfc74()) ? null : Double.valueOf(socialParam.getFd_3af9ee3c6bfc74()));
		//结算状态
		socialInfo.setFd_3add9ea428879a(socialParam.getFd_3add9ea428879a());
		//收款状态
		socialInfo.setFd_3add9eaeed2560("未收");
		//付款状态
		socialInfo.setFd_3add9eed23894a("未付");
	}

}
