package com.yami.shop.distribution.platform.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.enums.AuditStatus;
import com.yami.shop.bean.enums.EnterpriseApplyType;
import com.yami.shop.bean.enums.ShopWalletChangeReason;
import com.yami.shop.bean.event.EnterprisePayFailEvent;
import com.yami.shop.bean.event.EnterprisePaySuccessEvent;
import com.yami.shop.bean.model.EnterprisePay;
import com.yami.shop.bean.model.ShopWallet;
import com.yami.shop.bean.model.ShopWithdrawCash;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Arith;
import com.yami.shop.distribution.comment.Enums.DistributionWithdrawCashStateEnum;
import com.yami.shop.distribution.comment.dto.DistributionUserWalletDto;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.model.DistributionUserWallet;
import com.yami.shop.distribution.comment.model.DistributionUserWalletBill;
import com.yami.shop.distribution.comment.model.DistributionWithdrawCash;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.distribution.comment.service.DistributionUserWalletBillService;
import com.yami.shop.distribution.comment.service.DistributionUserWalletService;
import com.yami.shop.distribution.comment.service.DistributionWithdrawCashService;
import com.yami.shop.service.ShopWalletLogService;
import com.yami.shop.service.ShopWalletService;
import com.yami.shop.service.ShopWithdrawCashService;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * 企业支付事件监听
 */
@Component("distributionWithdrawEnterprisePayListener")
@AllArgsConstructor
public class EnterprisePayListener {

    private final DistributionWithdrawCashService distributionWithdrawCashService;
    private final DistributionUserWalletService distributionUserWalletService;
    private final DistributionUserService distributionUserService;
    private final DistributionUserWalletBillService distributionUserWalletBillService;


    @EventListener(EnterprisePaySuccessEvent.class)
    public void distributionWithdrawEventHandle(EnterprisePaySuccessEvent event) {
        EnterprisePay enterprisePay = event.getEnterprisePay();
        if (!Objects.equals(enterprisePay.getType(), EnterpriseApplyType.DISTRIBUTION_WITHDRAW.value())) {
            return;
        }
        // 更新提现状态
        DistributionWithdrawCash distributionWithdrawCash = distributionWithdrawCashService.getById(enterprisePay.getBizId());
        distributionWithdrawCash.setState(DistributionWithdrawCashStateEnum.CASH_SUCCESS.getValue());
        distributionWithdrawCash.setUpdateTime(new Date());
        distributionWithdrawCashService.updateById(distributionWithdrawCash);
    }


    @EventListener(EnterprisePayFailEvent.class)
    public void shopWithdrawFailEventHandle(EnterprisePayFailEvent event) {
        EnterprisePay enterprisePay = event.getEnterprisePay();
        //商户提现失败
        if (!Objects.equals(EnterpriseApplyType.DISTRIBUTION_WITHDRAW.value(), enterprisePay.getType())) {
            return;
        }
        DistributionWithdrawCash distributionWithdrawCash = distributionWithdrawCashService.getById(enterprisePay.getBizId());
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(enterprisePay.getUserId(), Constant.PLATFORM_SHOP_ID);
        DistributionUserWallet distributionUserWallet = distributionUserWalletService.getOne(new LambdaQueryWrapper<DistributionUserWallet>()
                .eq(DistributionUserWallet::getDistributionUserId, distributionUser.getDistributionUserId()));
        // 更新状态并将金额回退到分销员钱包中
        distributionWithdrawCash.setState(DistributionWithdrawCashStateEnum.CASH_FAIL.getValue());
        // 增加用户可提现金额
        distributionUserWallet.setSettledAmount(Arith.add(distributionUserWallet.getSettledAmount(), distributionWithdrawCash.getAmount()));

        //增加钱包流水记录
        distributionUserWalletBillService.save(new DistributionUserWalletBill(distributionUserWallet, "提现失败", 0.0, distributionWithdrawCash.getAmount(), 0.0, 0.0, 0));
        distributionUserWalletService.updateById(distributionUserWallet);
    }

}
