package com.ruoyi.ss.dividendDetail.service.impl;

import com.ruoyi.common.utils.DateUtils;
//import com.ruoyi.system.domain.agent.RlAgentVO;
import com.ruoyi.ss.dividendDetail.domain.DividendDetail;
import com.ruoyi.ss.dividendDetail.domain.DividendDetailQuery;
import com.ruoyi.ss.dividendDetail.domain.DividendDetailVO;
//import com.ruoyi.system.domain.order.RlOrder;
//import com.ruoyi.system.domain.userExt.RlUserExt;
import com.ruoyi.ss.dividendDetail.mapper.DividendDetailMapper;
import com.ruoyi.ss.dividendDetail.service.IDividendDetailService;
import com.ruoyi.system.domain.vo.InCome;
import com.ruoyi.system.domain.vo.IndexAdminVo;
import lombok.extern.slf4j.Slf4j;
        import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 分账明细Service业务层处理
 *
 * @author qzz
 * @date 2024-10-10
 */
@Slf4j
@Service
public class DividendDetailServiceImpl implements IDividendDetailService
{
    @Resource
    private DividendDetailMapper dividendDetailMapper;

    /**
     * 查询分账明细
     *
     * @param id 分账明细主键
     * @return 分账明细
     */
    @Override
    public DividendDetail selectRlDividendDetailById(Long id)
    {
        return dividendDetailMapper.selectRlDividendDetailById(id);
    }

    /**
     * 查询分账明细列表
     *
     * @param rlDividendDetail 分账明细
     * @return 分账明细
     */
    @Override
//    @DataScope(agentAlias = "a",userAlias = "u")
    public List<DividendDetailVO> selectRlDividendDetailList(DividendDetailQuery rlDividendDetail)
    {
        return dividendDetailMapper.selectRlDividendDetailList(rlDividendDetail);
    }

    /**
     * 根据订单号查询分账明细列表
     * @param orderNo
     * @return
     */
    @Override
    public List<DividendDetailVO> selectRlDividendDetailListByOrderNo(String orderNo) {
        return dividendDetailMapper.selectRlDividendDetailListByOrderNo(orderNo);
    }

    /**
     * 新增分账明细
     *
     * @param rlDividendDetail 分账明细
     * @return 结果
     */
    @Override
    public int insertRlDividendDetail(DividendDetail rlDividendDetail)
    {
        rlDividendDetail.setCreateTime(DateUtils.getNowDate());
        return dividendDetailMapper.insertRlDividendDetail(rlDividendDetail);
    }

    /**
     * 修改分账明细
     *
     * @param rlDividendDetail 分账明细
     * @return 结果
     */
    @Override
    public int updateRlDividendDetail(DividendDetail rlDividendDetail)
    {
        return dividendDetailMapper.updateRlDividendDetail(rlDividendDetail);
    }

    /**
     * 批量删除分账明细
     *
     * @param ids 需要删除的分账明细主键
     * @return 结果
     */
    @Override
    public int deleteRlDividendDetailByIds(Long[] ids)
    {
        return dividendDetailMapper.deleteRlDividendDetailByIds(ids);
    }

    /**
     * 删除分账明细信息
     *
     * @param id 分账明细主键
     * @return 结果
     */
    @Override
    public int deleteRlDividendDetailById(Long id)
    {
        return dividendDetailMapper.deleteRlDividendDetailById(id);
    }



    /**
     * 更新退款金额
     *
     * @param id 分成明细id
     * @param dividendAmount 分账金额（为了计算结算金额）
     * @param userRefundAmount 用户退款金额
     * @return 结果
     */
    @Override
    public int updateRefundAmount(Long id, BigDecimal dividendAmount, BigDecimal userRefundAmount) {
        DividendDetail rlDividendDetail = new DividendDetail();
        rlDividendDetail.setRefundAmount(userRefundAmount);
        rlDividendDetail.setSettlementAmount(dividendAmount.subtract(userRefundAmount));
        rlDividendDetail.setId(id);
        return dividendDetailMapper.updateRlDividendDetail(rlDividendDetail);
    }

    /**
     * 查询近7天收入
     * @param merchantId 商户id
     * @return 结果
     */
    @Override
    public List<InCome> select7DaysIncome(Long merchantId) {
        return dividendDetailMapper.select7DaysIncome(merchantId);
    }

    /**
     * 查询近30天收入
     * @param merchantId 商户id
     * @return 结果
     */
    @Override
    public List<InCome> select30DaysIncome(Long merchantId) {
        return dividendDetailMapper.select30DaysIncome(merchantId);
    }

    /**
     * 根据时间查询收入
     * @param query 查询条件
     * @return 结果
     */
    @Override
    public List<InCome> selectIncome(DividendDetailQuery query) {
        return dividendDetailMapper.selectIncome(query);
    }

    /**
     * 查询收入
     * @return 结果
     */
    @Override
    public BigDecimal getIncome(DividendDetailQuery query) {
        return dividendDetailMapper.getIncome(query);
    }


}
