package com.cf.imes.module.executor.dal.mysql.order;

import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsAreaRespVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsReqVO;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsStatusRespVO;
import com.cf.imes.module.executor.dal.dataobject.order.OrderDO;
import com.cf.imes.module.executor.enums.OrderDeletedEnum;
import com.cf.imes.module.executor.enums.OrderStatusEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

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

/**
 * @author Gqr
 * @since 2024/8/7 9:35
 */
@Mapper
public interface OrderStatisticsMapper extends BaseMapperX<OrderDO> {

    /**
     * 订单数量统计
     */
    default Integer selectOrderCount() {
        return Math.toIntExact(selectCount());
    }

    /**
     * 今日新增订单总数统计
     *
     */
    default Integer selectOrderCountToday() {
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<OrderDO>()
//                .eq(OrderDO::getDeleted, OrderDeletedEnum.NOT_DELETED.getStatus())
                .between(OrderDO::getCreateTime, LocalDateTime.of(LocalDate.now(), LocalTime.MIN), LocalDateTime.of(LocalDate.now(), LocalTime.MAX))));

    }

    /**
     * 今日完成订单总数统计
     *
     */
    default Integer selectOrderCountTodayFinish() {
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<OrderDO>()
                .eq(OrderDO::getDeleted, OrderDeletedEnum.NOT_DELETED.getStatus())
                .eq(OrderDO::getStatus, OrderStatusEnum.FINISH_PRODUCTION.getStatus())
                .between(OrderDO::getFinishTime, LocalDateTime.of(LocalDate.now(), LocalTime.MIN), LocalDateTime.of(LocalDate.now(), LocalTime.MAX))));
    }

    /**
     * 生产中订单总数
     *
     */
    default Integer selectOrderCountProduce() {
        return Math.toIntExact(selectCount(new LambdaQueryWrapperX<OrderDO>()
                .eq(OrderDO::getDeleted, OrderDeletedEnum.NOT_DELETED.getStatus())
                .eq(OrderDO::getStatus, OrderStatusEnum.IN_PRODUCTION.getStatus())));
    }

    /**
     * 生产中生产平方数
     *
     * @param
     * @return
     */
    Double selectOrderSquareProduce();

    /**
     * 订单状态按时间分组统计数量
     *
     * @param reqVO
     * @return
     */
    List<OrderStatisticsStatusRespVO> selectOrderStatusCountGroupBy(@Param("req") OrderStatisticsReqVO reqVO);

    /**
     * 订单按时间分组统计数量
     *
     * @param reqVO
     * @return
     */
    List<OrderStatisticsStatusRespVO> selectOrderCountGroupByOrderDate(@Param("req") OrderStatisticsReqVO reqVO);

    /**
     * 订单按时间分组统计面积
     *
     * @param reqVO
     * @return
     */
    List<OrderStatisticsAreaRespVO> selectOrderAreaGroupByOrderDate(@Param("req") OrderStatisticsReqVO reqVO);
}
