package com.zfire.mall.manager.logic.engin.deposit.bacth;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.zfire.mall.manager.bean.wallet.CustomerWalletAmountBean;
import com.zfire.mall.manager.enums.ExamineStatusEnum;
import com.zfire.mall.manager.enums.OrderTypeEnum;
import com.zfire.mall.manager.logic.wallet.WalletLogic;
import com.zfire.mall.manager.plus.entity.DepositManage;
import com.zfire.mall.manager.plus.entity.DepositManageCheckRecord;
import com.zfire.mall.manager.plus.entity.DepositManageItem;
import com.zfire.mall.manager.plus.service.DepositManageItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ExamineHomeLogic {

    @Transactional(rollbackFor = Exception.class)
    public void handleSingleDeposit(DepositManageItemService depositManageItemService, WalletLogic walletLogic,
                                    DepositManage depositManage, String confirmBy, String confirmName,
                                    DateTime confirmTime) throws InterruptedException {

        List<CustomerWalletAmountBean> walletAmountBeans = new ArrayList<>();

        List<DepositManageItem> depositManageItems = depositManageItemService.lambdaQuery()
                .eq(DepositManageItem::getParentId, depositManage.getDepositManageId())
                .list();
        // 退押处理
        String enginName = depositManage.getEnginOrderType().equals(OrderTypeEnum.HOME.getKey()) ? "家用" : "商用";
        BigDecimal zero = new BigDecimal("0");
        for (DepositManageItem depositManageItem : depositManageItems) {
            if (depositManageItem.getDepositAmount().compareTo(zero) > 0) {
                walletAmountBeans.add(new CustomerWalletAmountBean(depositManageItem.getCustomerWalletId(), depositManageItem.getDepositAmount(), "", enginName + "工程订单退押金"
                        , depositManage.getCreateTime(), depositManage.getRefEnginRecordNo() + "|" + depositManage.getRefEnginRecordNo(), 4, "add"));
            }
        }

        new DepositManageCheckRecord()
                .setDepositManageId(depositManage.getDepositManageId())
                .setApplyNote(depositManage.getApplyNote())
                .setSubmitBy(confirmBy)
                .setSubmitName(confirmName)
                .setSubmitTime(confirmTime)
                .setCheckNote("批量审核")
                .setCheckBy(confirmBy)
                .setCheckName(confirmName)
                .setCheckDate(confirmTime)
                .setIsRefundDeposit(true)
                .insert();

        depositManage.setIsRefundDeposit(true)
                .setExamineStatus(ExamineStatusEnum.OK.toString())
                .setSubmitBy(confirmBy)
                .setSubmitName(confirmName)
                .setSubmitTime(confirmTime)
                .setCheckBy(confirmName)
                .setCheckDate(confirmTime)
                .setConfirmBy(confirmBy)
                .setConfirmName(confirmName)
                .setConfirmTime(confirmTime);

        depositManage.updateById();

        if (CollectionUtil.isNotEmpty(walletAmountBeans)) {
            walletLogic.doWalletAmount(walletAmountBeans);
        }
    }
}
