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.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.beans.BeanUtils;
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();
			BeanUtils.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());
			//单位差异
			if("是".equals(socialInfo.getFd_3add9de0be85e4()) && "实缴".equals(socialInfo.getFd_3add9dd7833db8())){
				//单位差异
				//$列表.求总和$($社保订单明细*总数(实缴单位合计)$)+ $实缴单位合计$ - $列表.求总和$($社保订单明细*总数(预估单位合计)$)
				socialInfo.setFd_3adfeb53c70f72(Double.sum(Double.sum(socialSumInfo.getSjDwSum(),socialInfo.getFd_3af9f2e9208e4e()),-socialSumInfo.getYgDwSum()));
				//个人差异
				//$列表.求总和$($社保订单明细*总数(实缴个人合计)$)+ $实缴个人合计$ - $列表.求总和$($社保订单明细*总数(预估个人合计)$)
				socialInfo.setFd_3adfeb5413fb44(Double.sum(Double.sum(socialSumInfo.getSjPersonSum(),socialInfo.getFd_3af9f303037214()),-socialSumInfo.getYgPersonSum()));
			}

			//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(socialSumInfo.getYgSum(),socialInfo.getFd_3af9f2883941b4()),-socialSumInfo.getYs()));
						//$列表.求总和$( $社保订单明细*总数(预估个人合计)$)  +  $预估个人合计$ - $列表.求总和$($社保订单明细*总数(个人应收)$)
						socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(socialSumInfo.getYgPersonSum(),socialInfo.getFd_3af9f285ee1e38()),-socialSumInfo.getPersonYs()));
						//$列表.求总和$($社保订单明细*总数(预估单位合计)$)  + $预估单位合计$ - $列表.求总和$($社保订单明细*总数(单位应收)$)
						socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(socialSumInfo.getYgDwSum(),socialInfo.getFd_3adfeb4e8064a8()),-socialSumInfo.getDwYs()));
					}else{
						if(socialSumInfo.getYgSum()>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(socialSumInfo.getSjSum(),socialInfo.getFd_3af9f3059e5b9c()),-socialSumInfo.getYs()));
							//$列表.求总和$($社保订单明细*总数(实缴个人合计)$)+ $实缴个人合计$  - $列表.求总和$($社保订单明细*总数(个人应收)$)
							socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(socialSumInfo.getSjPersonSum(),socialInfo.getFd_3af9f303037214()),-socialSumInfo.getPersonYs()));
							//列表.求总和$($社保订单明细*总数(实缴单位合计)$)+ $实缴单位合计$  - $列表.求总和$($社保订单明细*总数(单位应收)$)
							socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(socialSumInfo.getSjDwSum(),socialInfo.getFd_3af9f2e9208e4e()),-socialSumInfo.getDwYs()));
						}
					}
				}else{
					if("预估".equals(socialInfo.getFd_3add9dd7833db8())){
						//$列表.求总和$($社保订单明细*总数(预估合计)$) + $预估合计$-$列表.求总和$($社保订单明细*总数(应收)$)
						socialInfo.setFd_3adfeb7b624f06(Double.sum(Double.sum(socialSumInfo.getYgSum(),socialInfo.getFd_3af9f2883941b4()),-socialSumInfo.getYs()));
						//$列表.求总和$( $社保订单明细*总数(预估个人合计)$) + $预估个人合计$-$列表.求总和$($社保订单明细*总数(个人应收)$)
						socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(socialSumInfo.getYgPersonSum(),socialInfo.getFd_3af9f285ee1e38()),-socialSumInfo.getPersonYs()));
						//$列表.求总和$($社保订单明细*总数(预估单位合计)$) + $预估单位合计$-$列表.求总和$($社保订单明细*总数(单位应收)$)
						socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(socialSumInfo.getYgDwSum(),socialInfo.getFd_3adfeb4e8064a8()),-socialSumInfo.getDwYs()));
					}else{
						//$列表.求总和$($社保订单明细*总数(实缴合计)$)+ $实缴合计$  - $列表.求总和$($社保订单明细*总数(应收)$)
						socialInfo.setFd_3adfeb7b624f06(Double.sum(Double.sum(socialSumInfo.getSjSum(),socialInfo.getFd_3af9f3059e5b9c()),-socialSumInfo.getYs()));
						//$列表.求总和$($社保订单明细*总数(实缴个人合计)$)+ $实缴个人合计$  - $列表.求总和$($社保订单明细*总数(个人应收)$)
						socialInfo.setFd_3b35a57aee1428(Double.sum(Double.sum(socialSumInfo.getSjPersonSum(),socialInfo.getFd_3af9f303037214()),-socialSumInfo.getPersonYs()));
						//$列表.求总和$($社保订单明细*总数(实缴单位合计)$)+ $实缴单位合计$  - $列表.求总和$($社保订单明细*总数(单位应收)$)
						socialInfo.setFd_3b35a57b6e9d0a(Double.sum(Double.sum(socialSumInfo.getSjDwSum(),socialInfo.getFd_3af9f2e9208e4e()),-socialSumInfo.getDwYs()));
					}
				}
			}
			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);
			paymentIds.add(socialParam.getSocialIds());
		}
		boolean flag = this.saveBatch(socialInfoList);
		if (flag) {
			EkpSocialPushInfoVo pushInfoVo = new EkpSocialPushInfoVo();
			pushInfoVo.setUnPushInfo(socialPushList);
			pushInfoVo.setPaymentIds(paymentIds);
			return pushInfoVo;
		}
		return null;
	}

}
