package com.ruoyi.bst.reconciliationDate.service.impl;

import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.bonus.domain.BonusQuery;
import com.ruoyi.bst.bonus.domain.enums.BonusStatus;
import com.ruoyi.bst.bonus.service.BonusDashboard;
import com.ruoyi.bst.bonusRefund.domain.BonusRefundQuery;
import com.ruoyi.bst.bonusRefund.service.BonusRefundDashboard;
import com.ruoyi.bst.channel.domain.vo.ChannelNameVO;
import com.ruoyi.bst.pay.domain.PayQuery;
import com.ruoyi.bst.pay.service.PayDashboard;
import com.ruoyi.bst.reconciliationDate.domain.ReconciliationDate;
import com.ruoyi.bst.reconciliationDate.domain.ReconciliationDateQuery;
import com.ruoyi.bst.reconciliationDate.domain.ReconciliationDateVO;
import com.ruoyi.bst.reconciliationDate.mapper.ReconciliationDateMapper;
import com.ruoyi.bst.reconciliationDate.service.ReconciliationDateService;
import com.ruoyi.bst.reconciliationDate.utils.ReconciliationDateUtil;
import com.ruoyi.bst.refund.domain.RefundQuery;
import com.ruoyi.bst.refund.service.RefundDashboard;
import com.ruoyi.common.utils.MathUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 渠道对账Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@Service
@Slf4j
public class ReconciliationDateServiceImpl implements ReconciliationDateService
{
    @Autowired
    private ReconciliationDateMapper reconciliationDateMapper;

    @Autowired
    private PayDashboard payDashboard;

    @Autowired
    private RefundDashboard refundDashboard;

    @Autowired
    private BonusDashboard bonusDashboard;

    @Autowired
    private BonusRefundDashboard bonusRefundDashboard;

    /**
     * 查询渠道对账
     *
     * @param id 渠道对账主键
     * @return 渠道对账
     */
    @Override
    public ReconciliationDateVO selectReconciliationDateById(Long id)
    {
        return reconciliationDateMapper.selectReconciliationDateById(id);
    }

    /**
     * 查询渠道对账列表
     *
     * @param reconciliationDate 渠道对账
     * @return 渠道对账
     */
    @Override
    public List<ReconciliationDateVO> selectReconciliationDateList(ReconciliationDateQuery reconciliationDate)
    {
        return reconciliationDateMapper.selectReconciliationDateList(reconciliationDate);
    }

    /**
     * 新增渠道对账
     *
     * @param reconciliationDate 渠道对账
     * @return 结果
     */
    @Override
    public int insertReconciliationDate(ReconciliationDate reconciliationDate)
    {
        return reconciliationDateMapper.insertReconciliationDate(reconciliationDate);
    }

    /**
     * 修改渠道对账
     *
     * @param reconciliationDate 渠道对账
     * @return 结果
     */
    @Override
    public int updateReconciliationDate(ReconciliationDate reconciliationDate)
    {
        return reconciliationDateMapper.updateReconciliationDate(reconciliationDate);
    }

    /**
     * 批量删除渠道对账
     *
     * @param ids 需要删除的渠道对账主键
     * @return 结果
     */
    @Override
    public int deleteReconciliationDateByIds(Long[] ids)
    {
        return reconciliationDateMapper.deleteReconciliationDateByIds(ids);
    }

    /**
     * 删除渠道对账信息
     *
     * @param id 渠道对账主键
     * @return 结果
     */
    @Override
    public int deleteReconciliationDateById(Long id)
    {
        return reconciliationDateMapper.deleteReconciliationDateById(id);
    }

    @Override
    public int insertReconciliationDate(LocalDate date, ChannelNameVO channel, Long areaId) {
        if (date == null || channel == null || areaId == null || channel.getChannelId() == null) {
            return 0;
        }
        ReconciliationDate po = new ReconciliationDate();
        po.setDate(date);
        po.setChannelId(channel.getChannelId());
        po.setChannelName(channel.getName());
        po.setAreaId(areaId);

        // 查询支付
        PayQuery payQuery = new PayQuery();
        payQuery.setPayDate(date);
        payQuery.setChannelId(channel.getChannelId());
        payQuery.setAreaId(areaId);
        // 渠道成本
        po.setChannelCost(payDashboard.selectSumOfChannelCost(payQuery));
        // 骑行订单支付金额
        po.setOrderPayAmount(payDashboard.selectSumofOrderPayedAmount(payQuery));

        // 查询退款
        RefundQuery refundQuery = new RefundQuery();
        refundQuery.setCreateDate(date);
        refundQuery.setChannelId(channel.getChannelId());
        refundQuery.setPayAreaId(areaId);
        // 骑行订单退款金额
        po.setOrderRefundAmount(refundDashboard.selectSumofOrderRefundedAmount(refundQuery));

        // 骑行订单实收
        po.setOrderActualAmount(MathUtils.subtractDecimal(po.getOrderPayAmount(), po.getOrderRefundAmount()));
        // 支付总金额
        po.setPayAmount(po.getOrderPayAmount());
        // 退款总金额
        po.setRefundAmount(po.getOrderRefundAmount());
        // 实收总金额
        po.setActualAmount(po.getOrderActualAmount());

        // 查询分成
        BonusQuery bonusQuery = new BonusQuery();
        bonusQuery.setCreateDate(date);
        bonusQuery.setChannelId(channel.getChannelId());
        bonusQuery.setAreaId(areaId);
        bonusQuery.setStatusList(BonusStatus.valid());
        // 用户分成
        po.setUserBonus(bonusDashboard.selectSumOfUserAmount(bonusQuery));
        // 平台分成
        po.setPlatformBonus(bonusDashboard.selectSumOfPlatformAmount(bonusQuery));

        // 查询分成退款
        BonusRefundQuery bonusRefundQuery = new BonusRefundQuery();
        bonusRefundQuery.setCreateDate(date);
        bonusRefundQuery.setBonusChannelId(channel.getChannelId());
        bonusRefundQuery.setBonusAreaId(areaId);
        // 用户分成退款
        po.setUserBonusRefund(bonusRefundDashboard.selectSumOfUserBonusRefund(bonusRefundQuery));
        // 平台分成退款
        po.setPlatformBonusRefund(bonusRefundDashboard.selectSumOfPlatformBonusRefund(bonusRefundQuery));

        // 用户实收
        po.setUserActualBonus(MathUtils.subtractDecimal(po.getUserBonus(), po.getUserBonusRefund()));
        // 平台实收
        po.setPlatformActualBonus(MathUtils.subtractDecimal(po.getPlatformBonus(), po.getPlatformBonusRefund()));
        // 实收总金额
        po.setActualBonus(MathUtils.addDecimal(po.getUserActualBonus(), po.getPlatformActualBonus()));
        // 差额
        po.setDifference(MathUtils.subtractDecimal(po.getActualAmount(), po.getActualBonus()));

        if (ReconciliationDateUtil.isEmpty(po)) {
            log.error("渠道对账数据为空，date={}, channel={}, areaId={}", date, channel, areaId);
            return 0;
        }

        return this.insertReconciliationDate(po);
    }

}
