package com.fdkankan.platform.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.platform.order.entity.VirtualOrder;
import com.fdkankan.platform.user.request.RequestCamera;
import com.fdkankan.platform.user.request.RequestTrade;
import com.fdkankan.platform.user.request.RequestVirtualOrder;
import com.fdkankan.platform.user.vo.ResponseVirtualOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * <p>
 * 充值订单表（双目相机点数充值、消费） Mapper 接口
 * </p>
 *
 * @author 
 * @since 2021-12-27
 */
@Mapper
public interface IVirtualOrderMapper extends BaseMapper<VirtualOrder> {

    Page<ResponseVirtualOrder> getChargeListByParam(Page<ResponseVirtualOrder> page,@Param("param") RequestVirtualOrder param);

    List<VirtualOrder> getAllByParam(@Param("param")RequestCamera param);

    @Select(" SELECT IFNULL(SUM(v.amount),0) FROM t_virtual_order v LEFT JOIN t_camera c on v.camera_id = c.id  WHERE c.id = #{cameraId} and pay_status = 1 and status = #{status} ")
    Integer queryRechargeByCameraIdAndStatus(@Param("cameraId") Long cameraId, @Param("status") int status);


    @Select(" SELECT count(id) FROM t_virtual_order WHERE (DATEDIFF(trade_time, NOW())=0) AND REC_STATUS = 'A' AND pay_status = 1 AND status = 1 ")
    Integer queryOrderCountByToday();

    @Select(" SELECT IFNULL(SUM(amount),0) FROM t_virtual_order WHERE (DATEDIFF(trade_time,NOW())=0) AND REC_STATUS = 'A' AND pay_status = 1 AND status = #{status}  ")
    BigDecimal queryTurnoverByToday(@Param("status")int status);

    @Select(" SELECT IFNULL(SUM(amount),0) FROM t_virtual_order WHERE (DATE_FORMAT( trade_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )) AND REC_STATUS = 'A' AND pay_status = 1 AND status = 1  ")
    BigDecimal queryTurnoverByMonth();

    @Select(" SELECT IFNULL(SUM(amount),0) FROM t_virtual_order WHERE (PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( trade_time, '%Y%m' ) ) =1) AND REC_STATUS = 'A' AND pay_status = 1 AND status = 1  ")
    BigDecimal queryTurnoverByLastMonth();

    @Select(" SELECT count(id) FROM t_virtual_order WHERE REC_STATUS = 'A' AND pay_status = 1 AND status = 1  ")
    Integer queryAllCount();

    @Select(" SELECT IFNULL(SUM(amount),0) FROM t_virtual_order WHERE REC_STATUS = 'A' AND pay_status = 1 AND status = #{status}   ")
    BigDecimal selectAllTurnover(@Param("status")int status);

    @Select(" SELECT trade_time as tradeTime, count(id) as orderCount FROM t_virtual_order WHERE (DATE_SUB(CURDATE(), INTERVAL 12 DAY) <=date(trade_time)) AND REC_STATUS = 'A' AND pay_status = 1 AND status = #{status} group by day(create_time) ")
    List<ResponseVirtualOrder> queryTrendRechargeOrder(@Param("status")int status);

    Page<ResponseVirtualOrder> getListByParam(Page<ResponseVirtualOrder> page, @Param("param")RequestTrade param);
}
