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.Common;
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) {
				TInsuranceDetail byId = tInsuranceDetailService.getById(tInsuranceEkp.getDetailId());
				String defaultSettleId = byId.getDefaultSettleId();
				if (tInsuranceEkp.getDefaultSettleId().equals(defaultSettleId)){
					//ekp结算信息
					SettleVo settleVo = tInsuranceDetailService.getInsuranceDetailSettleStatus(tInsuranceEkp.getDetailId(), tInsuranceEkp.getDefaultSettleId());
					//商险结算信息
					TInsuranceSettle insuranceSettle = tInsuranceSettleService.getById(tInsuranceEkp.getDefaultSettleId());
					//预估推送状态
					Integer isEstimatePush = insuranceSettle.getIsEstimatePush();
					//实缴推送状态
					Integer isActualPush = insuranceSettle.getIsActualPush();
					//实缴结算状态
					String actualStatus = settleVo.getActualStatus();
					//预缴结算状态
					String estimateStatus = settleVo.getEstimateStatus();
					Integer pushType = tInsuranceEkp.getPushType();
					//如果当前需要重发预估保费的数据已发送，则过滤掉执行下一条数据
					if(CommonConstants.ONE_INT == pushType && CommonConstants.ONE_INT == isEstimatePush){
						tInsuranceEkp.setResendFlag(CommonConstants.THREE_INT);
						failList.add(tInsuranceEkp);
						continue;
					}
					//如果当前需要重发实际保费的数据已发送，则过滤掉执行下一条数据
					if(CommonConstants.TWO_INT == pushType && CommonConstants.ONE_INT == isActualPush){
						tInsuranceEkp.setResendFlag(CommonConstants.THREE_INT);
						failList.add(tInsuranceEkp);
						continue;
					}
					EkpInteractiveParam ekpParam = new EkpInteractiveParam();
					BeanCopyUtils.copyProperties(tInsuranceEkp,ekpParam);
					//如果预估未结算
					LambdaUpdateWrapper<TInsuranceSettle> updateWrapper = new LambdaUpdateWrapper<>();
					String s = null;
					if(InsurancesConstants.SETTLE_ZERO.equals(estimateStatus)){
						//实缴未结算
						if(InsurancesConstants.SETTLE_ZERO.equals(actualStatus)){
							//如果是新增预估保费，则推送预估保费新增
							if(CommonConstants.ONE_INT == pushType){
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)){
									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){
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)) {
									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){
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)) {
									updateWrapper.eq(TInsuranceSettle::getId, tInsuranceEkp.getDefaultSettleId())
											.set(TInsuranceSettle::getIsActualPush, CommonConstants.ONE_INT)
											.set(TInsuranceSettle::getActualPushTime, LocalDateTime.now());
									tInsuranceSettleService.update(updateWrapper);
									LambdaUpdateWrapper<TInsuranceDetail> detailLambdaUpdate = new LambdaUpdateWrapper<>();
									detailLambdaUpdate.eq(TInsuranceDetail::getId, tInsuranceEkp.getDetailId())
											.set(TInsuranceDetail::getSettleMonth, CommonConstants.ONE_INT);
									tInsuranceSettleService.update(updateWrapper);
								}
							}
							//如果是作废信息，则推送作废信息
							if(CommonConstants.FOUR_INT == pushType){
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
							}
							if(CommonConstants.FIVE_INT == pushType || CommonConstants.SIX_INT == pushType){
								tInsuranceEkp.setResendFlag(CommonConstants.TWO_INT);
								failList.add(tInsuranceEkp);
								continue;
							}
							//成功则更新，不成功则跳出循环
							if(StringUtils.isNotBlank(s)) {
								tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
								updateById(tInsuranceEkp);
							}else{
								break;
							}
						}
						//实缴结算中，已结算
						if(InsurancesConstants.SETTLE_ONE.equals(actualStatus) || InsurancesConstants.SETTLE_TWO.equals(actualStatus)){
							//如果是作废，则推送实缴红冲
							if(CommonConstants.FOUR_INT == pushType){
								BigDecimal actualPremium = ekpParam.getActualPremium();
								ekpParam.setActualPremium(actualPremium.negate());
								ekpParam.setInteractiveType(InsurancesConstants.CORRECT_SETTLE_BILL);
								ekpParam.setSettleType(InsurancesConstants.ACTUAL_SETTLE_BILL);
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)) {
									tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
									updateById(tInsuranceEkp);
								}else{
									break;
								}
							}
							if(CommonConstants.SIX_INT == pushType){
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)) {
									tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
									updateById(tInsuranceEkp);
								}else{
									break;
								}
							}else{
								//推送预估新增，实缴新增，变更结算信息，作废,则不进行重发
								tInsuranceEkp.setResendFlag(CommonConstants.TWO_INT);
								updateById(tInsuranceEkp);
							}
						}
					}else{
						//如果实缴未结算
						if(InsurancesConstants.SETTLE_ZERO.equals(actualStatus)){
							//如果是作废信息，则推送预估红冲信息
							if(CommonConstants.FOUR_INT == pushType){
								BigDecimal estimatePremium = ekpParam.getEstimatePremium();
								ekpParam.setActualPremium(estimatePremium.negate());
								ekpParam.setInteractiveType(InsurancesConstants.CORRECT_SETTLE_BILL);
								ekpParam.setSettleType(InsurancesConstants.ESTIMATE_SETTLE_BILL);
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)) {
									tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
									updateById(tInsuranceEkp);
								}else{
									break;
								}
							} else if(CommonConstants.FIVE_INT == pushType){
								//如果是预估红冲信息，则推送预估红冲信息
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)) {
									tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
									updateById(tInsuranceEkp);
								}else{
									break;
								}
							}else{
								tInsuranceEkp.setResendFlag(CommonConstants.TWO_INT);
								updateById(tInsuranceEkp);
							}
						}else{
							//如果是实缴红冲信息，则推送实缴红冲信息
							if(CommonConstants.SIX_INT == pushType){
								BigDecimal actualPremium = ekpParam.getActualPremium();
								ekpParam.setActualPremium(actualPremium.negate());
								ekpParam.setInteractiveType(InsurancesConstants.CORRECT_SETTLE_BILL);
								ekpParam.setSettleType(InsurancesConstants.ACTUAL_SETTLE_BILL);
								s = ekpInsuranceUtil.sendToEkp(ekpParam);
								if(StringUtils.isNotBlank(s)) {
									tInsuranceEkp.setResendFlag(CommonConstants.ONE_INT);
									updateById(tInsuranceEkp);
								}else{
									break;
								}
							}else{
								tInsuranceEkp.setResendFlag(CommonConstants.TWO_INT);
								updateById(tInsuranceEkp);
							}
						}
					}
				}else{
					//结算id与当前不一致则不进行重发
					tInsuranceEkp.setResendFlag(CommonConstants.THREE_INT);
					failList.add(tInsuranceEkp);
				}
			}
			updateBatchById(failList);
			return R.ok("发送成功");
		}else{
			return R.failed("无需要发送的信息");
		}
	}
}
