
package com.tmerclub.cloud.distribution.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.multishop.bo.OrderChangeShopWalletAmountBO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.DistributionAudit;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.distribution.constant.DistributionUserIncomeStateEnum;
import com.tmerclub.cloud.distribution.model.DistributionUserIncome;
import com.tmerclub.cloud.distribution.model.DistributionUserWallet;
import com.tmerclub.cloud.distribution.model.DistributionUserWalletBill;
import com.tmerclub.cloud.distribution.service.DistributionRefundService;
import com.tmerclub.cloud.distribution.service.DistributionUserIncomeService;
import com.tmerclub.cloud.distribution.service.DistributionUserWalletBillService;
import com.tmerclub.cloud.distribution.service.DistributionUserWalletService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author cl
 * @date 2021-08-20 15:47:30
 */
@Service
public class DistributionRefundServiceImpl implements DistributionRefundService {

    private static final Logger logger = LoggerFactory.getLogger(DistributionRefundServiceImpl.class);
    @Autowired
    private DistributionUserWalletService distributionUserWalletService;
    @Autowired
    private DistributionUserWalletBillService distributionUserWalletBillService;
    @Autowired
    private DistributionUserIncomeService distributionUserIncomeService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void refundDistributionAmount(OrderChangeShopWalletAmountBO message) {
        if (Objects.isNull(message)) {
            return;
        }
        // 回退的分销佣金
        Long orderId = message.getOrderId();
        Long orderItemId = message.getOrderItemId();
        Long distributionAmount = message.getDistributionAmount();

        // 所有的订单分销流水记录查询出来
        List<DistributionUserIncome> distributionUserIncomes = distributionUserIncomeService.getByOrderId(orderId);
        if (CollUtil.isEmpty(distributionUserIncomes)) {
            logger.info("订单无分销信息，不用回退分销佣金");
            return;
        }
        List<DistributionUserIncome> returnAmountList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        // 整单退款
        if (Objects.isNull(orderItemId) || Objects.equals(Constant.ZERO_LONG, orderItemId)) {
            returnAmountList.addAll(distributionUserIncomes);
        } else {
            // 单个订单项退款
            List<DistributionUserIncome> distributionUserIncomeList = distributionUserIncomes.stream().filter(item -> Objects.equals(orderItemId, item.getOrderItemId())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(distributionUserIncomeList)) {
                returnAmountList.addAll(distributionUserIncomeList);
            }
        }
        if (CollUtil.isEmpty(returnAmountList)) {
            return;
        }
        // 处理分销订单
        List<Long> updateIncomeIds = new ArrayList<>(Constant.INITIAL_CAPACITY);

        DistributionUserWalletBill saveDistributionWalletBill = null;
        DistributionUserWallet updateWallet = null;
        for (DistributionUserIncome distributionUserIncome : returnAmountList) {
            Integer reson = distributionUserIncome.getReson();
            // 幂等， 已经回退的分销佣金，失效状态，就不用再次回退佣金了
            if (Objects.equals(DistributionAudit.INCOME_ONE.value(), reson) || Objects.equals(DistributionAudit.INCOME_THREE.value(), reson)) {
                continue;
            }
            if (Objects.isNull(updateWallet)) {
                DistributionUserWallet distributionUserWallet = distributionUserWalletService.getByDistributionUserId(distributionUserIncome.getDistributionUserId());
                if (distributionUserWallet == null) {
                    // 未找到分销员信息
                    throw new LuckException("未找到分销员信息");
                }
                // 添加钱包变动日志
                distributionUserWallet.setUnsettledAmount(distributionUserWallet.getUnsettledAmount() - distributionAmount);
                distributionUserWallet.setInvalidAmount(distributionUserWallet.getInvalidAmount() + distributionAmount);
                saveDistributionWalletBill = new DistributionUserWalletBill(distributionUserWallet, "分销订单退款失效奖励", "Distribution order refund lapse bonus", -distributionAmount, Constant.ZERO_LONG, distributionAmount, Constant.ZERO_LONG, 0);
                updateWallet = distributionUserWallet;
            }
            // 更新收入状态
            updateIncomeIds.add(distributionUserIncome.getIncomeId());
        }
        // 修改分销员分销钱包
        if (Objects.nonNull(updateWallet)) {
            distributionUserWalletService.updateWalletAmount(updateWallet);
            // 添加一条分销钱包流水
            distributionUserWalletBillService.save(saveDistributionWalletBill);
        }
        // 失效分销流水记录
        if (CollUtil.isNotEmpty(updateIncomeIds)) {
            distributionUserIncomeService.updateStateByIncomeIds(updateIncomeIds, DistributionUserIncomeStateEnum.INVALID.value(), DistributionAudit.INCOME_TWO.value());
        }

    }

}
