package com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.ekp.util.EkpInsuranceUtil;
import com.yifu.cloud.plus.v1.yifu.insurances.constants.InsurancesConstants;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceDetail;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceEkp;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceSettle;
import com.yifu.cloud.plus.v1.yifu.insurances.mapper.insurances.TInsuranceEkpMapper;
import com.yifu.cloud.plus.v1.yifu.insurances.service.ekp.EkpSettleService;
import com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.TInsuranceDetailService;
import com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.TInsuranceEkpService;
import com.yifu.cloud.plus.v1.yifu.insurances.service.insurance.TInsuranceSettleService;
import com.yifu.cloud.plus.v1.yifu.insurances.util.BeanCopyUtils;
import com.yifu.cloud.plus.v1.yifu.insurances.util.BigDecimalUtils;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.EkpInteractiveParam;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.SettleVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【t_insurance_ekp(ekp交互失败存储表)】的数据库操作Service实现
* @createDate 2022-08-23 17:02:49
*/
@Service
@Slf4j
public class TInsuranceEkpServiceImpl extends ServiceImpl<TInsuranceEkpMapper, TInsuranceEkp> implements TInsuranceEkpService {

	@Resource
	EkpInsuranceUtil eKPInsuranceUtil;

	@Resource
	TInsuranceSettleService tInsuranceSettleService;
	@Resource
	@Lazy
	TInsuranceDetailService tInsuranceDetailService;

	@Resource
	@Lazy
	EkpSettleService ekpSettleService;


	@Override
	public R resend() {
		List<TInsuranceEkp> ekpRefundList = this.baseMapper.getEkpRefundList();
		List<TInsuranceEkp> failList = new ArrayList<>();
		List<TInsuranceEkp> successLit = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(ekpRefundList)){
			for (TInsuranceEkp tInsuranceEkp : ekpRefundList) {
				//结算信息
				SettleVo settleVo = tInsuranceDetailService.getInsuranceDetailSettleStatus(tInsuranceEkp.getDetailId(), tInsuranceEkp.getDefaultSettleId());
				//实缴结算状态
				String actualStatus = settleVo.getActualStatus();
				//预缴结算状态
				String estimateStatus = settleVo.getEstimateStatus();
				Integer pushType = tInsuranceEkp.getPushType();
				LambdaQueryWrapper<TInsuranceSettle> queryWrapper = new LambdaQueryWrapper<>();
				queryWrapper.eq(TInsuranceSettle :: getId,tInsuranceEkp.getDefaultSettleId());
				TInsuranceSettle insuranceSettle = tInsuranceSettleService.getOne(queryWrapper);
				//如果当前需要重发预估保费的数据已发送，则过滤掉执行下一条数据
				if(CommonConstants.ONE_INT == pushType && CommonConstants.ONE_INT == insuranceSettle.getIsEstimatePush()){
					tInsuranceEkp.setResendFlag(CommonConstants.THREE_INT);
					failList.add(tInsuranceEkp);
					continue;
				}
				//如果当前需要重发实际保费的数据已发送，则过滤掉执行下一条数据
				if(CommonConstants.TWO_INT == pushType && CommonConstants.ONE_INT == insuranceSettle.getIsActualPush()){
					tInsuranceEkp.setResendFlag(CommonConstants.THREE_INT);
					failList.add(tInsuranceEkp);
					continue;
				}
				EkpInteractiveParam ekpParam = new EkpInteractiveParam();
				BeanCopyUtils.copyProperties(tInsuranceEkp,ekpParam);
				if(actualStatus.equals(InsurancesConstants.SETTLE_ZERO)){
					String s = eKPInsuranceUtil.sendToEkp(ekpParam);
					//如果发送成功
					if (StringUtils.isNotBlank(s)){
						//预估则更新预估保费
						if(CommonConstants.ONE_INT == pushType){
							LambdaUpdateWrapper<TInsuranceSettle> updateWrapper = new LambdaUpdateWrapper<>();
							updateWrapper.eq(TInsuranceSettle :: getId,tInsuranceEkp.getDefaultSettleId())
									.set(TInsuranceSettle :: getIsEstimatePush,CommonConstants.ONE_INT)
									.set(TInsuranceSettle :: getEstimatePushTime, LocalDateTime.now());
							tInsuranceSettleService.update(updateWrapper);
						}
						//预估则更新预估保费
						if(CommonConstants.TWO_INT == pushType){
							LambdaUpdateWrapper<TInsuranceSettle> updateWrapper = new LambdaUpdateWrapper<>();
							updateWrapper.eq(TInsuranceSettle :: getId,tInsuranceEkp.getDefaultSettleId())
									.set(TInsuranceSettle :: getIsActualPush,CommonConstants.ONE_INT)
									.set(TInsuranceSettle :: getActualPushTime, LocalDateTime.now());
							tInsuranceSettleService.update(updateWrapper);
						}
						//结算月则变更结算月
						if(CommonConstants.THREE_INT == pushType){
							LambdaUpdateWrapper<TInsuranceDetail> updateWrapper = new LambdaUpdateWrapper<>();
							updateWrapper.eq(TInsuranceDetail :: getId,tInsuranceEkp.getDetailId())
									.set(TInsuranceDetail :: getSettleMonth,tInsuranceEkp.getSettleMonth())
									.set(TInsuranceDetail :: getUpdateTime, LocalDateTime.now());
							tInsuranceDetailService.update(updateWrapper);
						}
						tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
						successLit.add(tInsuranceEkp);
					}else{
						break;
					}

				}else{
					//如果为作废信息，已结算和结算中发送红冲数据
					if(CommonConstants.FOUR_INT == pushType){
						//预估保费
						BigDecimal estimatePremium = ekpParam.getEstimatePremium();
						//实际保费
						BigDecimal actualPremium = ekpParam.getActualPremium();
						if(!BigDecimalUtils.isNullOrZero(estimatePremium)){
							ekpParam.setEstimatePremium(estimatePremium.negate());
						}
						if(!BigDecimalUtils.isNullOrZero(actualPremium)){
							ekpParam.setActualPremium(actualPremium.negate());
						}
						ekpParam.setInteractiveType(CommonConstants.ONE_INT);
						String s = eKPInsuranceUtil.sendToEkp(ekpParam);
						if(StringUtils.isNotBlank(s)){
							tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
							successLit.add(tInsuranceEkp);
						}else{
							break;
						}
					//如果为红冲数据则正常发送
					}else if(CommonConstants.FIVE_INT == pushType || CommonConstants.SIX_INT == pushType){
						String s = eKPInsuranceUtil.sendToEkp(ekpParam);
						if(StringUtils.isNotBlank(s)){
							tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
							successLit.add(tInsuranceEkp);
						}else{
							break;
						}
					}else{
						tInsuranceEkp.setResendFlag(CommonConstants.TWO_INT);
						failList.add(tInsuranceEkp);
					}
				}
			}
			updateBatchById(successLit);
			updateBatchById(failList);
			return R.ok("发送成功");
		}else{
			return R.failed("无需要发送的信息");
		}
	}
}
