package cn.iocoder.yudao.module.system.job.rental;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.rental.vo.rentalsalerecord.RentalSaleRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalCustomerDO;
import cn.iocoder.yudao.module.system.dal.dataobject.rental.RentalDepositRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.rental.RentalCustomerMapper;
import cn.iocoder.yudao.module.system.service.rental.RentalDepositRecordService;
import cn.iocoder.yudao.module.system.service.rental.RentalSaleRecordService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
@EnableScheduling
public class RentalSaleJob {

    @Resource
    private RentalSaleRecordService saleRecordService;

    @Resource
    private RentalCustomerMapper customerMapper;

    @Resource
    private RentalDepositRecordService depositRecordService;

    @XxlJob("rentalSaleJob")
    @TenantJob // --- ⚠️ 这个注解 会将租户列表拉出来 完了后逐个租户执行 定时任务需要注意
    public ReturnT<String> execute() {

        // 获取 当前日期已到达周转日期的收款记录
        List<RentalSaleRecordRespVO> saleRecordList = saleRecordService.getSaleDepositRecordList();

        List<RentalSaleRecordSaveReqVO> saleRecordSaveReqVOList = BeanUtils.toBean(saleRecordList, RentalSaleRecordSaveReqVO.class);

        // 插入销售数据
        saleRecordService.createBatch(saleRecordSaveReqVOList);

        // 映射出一个map key为客户编号 value为 saleRecordSaveReqVOList该客户金额的和
        Map<Long, BigDecimal> customerAmountMap = saleRecordSaveReqVOList.stream()
                .collect(Collectors.groupingBy(
                        RentalSaleRecordSaveReqVO::getCustomerId,
                        Collectors.reducing(BigDecimal.ZERO, RentalSaleRecordSaveReqVO::getAmount, BigDecimal::add)
                ));

        // 获取所有客户信息
        List<RentalCustomerDO> RentalCustomerDO = customerMapper.selectList();

        // 更新客户押金金额
        RentalCustomerDO = RentalCustomerDO.stream()
                .peek(customer -> {
                    BigDecimal originalAmount = customer.getAmount();
                    BigDecimal deductionAmount = customerAmountMap.getOrDefault(customer.getId(), BigDecimal.ZERO);
                    customer.setAmount(originalAmount.subtract(deductionAmount));
                })
                .collect(Collectors.toList());

        // 同步更新 客户押金金额
        customerMapper.updateBatch(RentalCustomerDO);

        List<RentalDepositRecordDO> updateList = saleRecordList.stream()
                .map(saleRecord -> new RentalDepositRecordDO()
                        .setId(saleRecord.getDepositId())
                        .setType(3))
                .collect(Collectors.toList());
        // 同步更新 租金记录表
        depositRecordService.updateRentalDepositRecordType(updateList);

        // 返回执行成功
        return ReturnT.SUCCESS;
    }
}
