package cn.iocoder.yudao.module.pay.dal.mysql.divide;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pay.controller.admin.divide.vo.DividePageReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.divide.DivideDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;

/**
 * 分账单 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface DivideMapper extends BaseMapperX<DivideDO> {

    default PageResult<DivideDO> selectPage(DividePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DivideDO>()
                .eqIfPresent(DivideDO::getChannelId, reqVO.getChannelId())
                .eqIfPresent(DivideDO::getChannelCode, reqVO.getChannelCode())
                .eqIfPresent(DivideDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(DivideDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DivideDO::getNo, reqVO.getNo())
                .eqIfPresent(DivideDO::getChannelOrderNo, reqVO.getChannelOrderNo())
                .eqIfPresent(DivideDO::getOrderId, reqVO.getOrderId())
                .betweenIfPresent(DivideDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(DivideDO::getId));
    }

    @Select("select sum(wx_amount) from member_dish_order where DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)")
    BigDecimal getToday();

    @Select("select money from member_refund_log where DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY)")
    BigDecimal getRefund();

    @Select("select wx_amount from member_card where user_id = #{userId} order by create_time desc limit 1")
    BigDecimal getWxAmount(Long userId);

    @Select("select count(1) from member_dish_order where user_id = #{userId} and order_status = '0'")
    int orderCount(Long userId);


}