package cn.iocoder.yudao.module.member.dal.mysql.storeorderdetail;

import java.time.LocalDateTime;
import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.member.controller.admin.screen.vo.GoodsVO;
import cn.iocoder.yudao.module.member.controller.app.storeorder.vo.AppStoreOrderDetailVo;
import cn.iocoder.yudao.module.member.dal.dataobject.storeorderdetail.StoreOrderDetailDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.member.controller.admin.storeorderdetail.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 商品订单详情 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface StoreOrderDetailMapper extends BaseMapperX<StoreOrderDetailDO> {

    default PageResult<StoreOrderDetailDO> selectPage(StoreOrderDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<StoreOrderDetailDO>()
                .eqIfPresent(StoreOrderDetailDO::getOrderId, reqVO.getOrderId())
                .eqIfPresent(StoreOrderDetailDO::getGoodsId, reqVO.getGoodsId())
                .eqIfPresent(StoreOrderDetailDO::getCustomPrice, reqVO.getCustomPrice())
                .eqIfPresent(StoreOrderDetailDO::getNumber, reqVO.getNumber())
                .betweenIfPresent(StoreOrderDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(StoreOrderDetailDO::getId));
    }


    @Select("select od.custom_price,od.number,od.weight,od.goods_name,sg.img from member_store_order_detail od left join member_store_goods sg on od.goods_id = sg.goods_id  where od.order_id = #{orderId}")
    List<AppStoreOrderDetailVo> selectByOrderId(Integer orderId);

    @Select("<script>" +
            "select od.goods_name,sum(od.number) as `number`,sum(od.number*od.custom_price) as price from member_store_order_detail od\n" +
            "left join member_store_order so on  od.order_id = so.order_id\n" +
            "where so.carteen_id = #{storeId} and od.create_time BETWEEN #{startTime} AND #{endTime}\n" +
            "<if test='type == 1'>" +
            "  AND DATE_FORMAT(od.create_time, '%H:%i:%s') BETWEEN '00:00:00' AND '09:59:59' " +
            "</if>" +
            "<if test='type == 2'>" +
            "  AND DATE_FORMAT(od.create_time, '%H:%i:%s') BETWEEN '10:00:00' AND '15:59:59' " +
            "</if>" +
            "<if test='type == 3'>" +
            "  AND DATE_FORMAT(od.create_time, '%H:%i:%s') BETWEEN '16:00:00' AND '23:59:59' " +
            "</if>" +
            "group by od.goods_name" +
            "</script>")
    List<GoodsVO> selectGoodsSale(@Param("storeId") Long storeId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime, @Param("type") Integer type);
}