package com.jojubanking.boot.module.system.dal.mysql.order;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.jojubanking.boot.framework.mybatis.core.mapper.BaseMapperX;
import com.jojubanking.boot.module.system.controller.admin.order.vo.OrderSumRespVO;
import com.jojubanking.boot.module.system.dal.dataobject.order.OrderDO;
import org.apache.ibatis.annotations.Mapper;
import com.jojubanking.boot.module.system.controller.admin.order.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 检查项目订单 Mapper
 *
 * @author admin
 */
@Mapper
public interface OrderMapper extends BaseMapperX<OrderDO> {

    default PageResult<OrderDO> selectPage(OrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrderDO>()
                .eqIfPresent(OrderDO::getBatchno, reqVO.getBatchno())
                .eqIfPresent(OrderDO::getPatid, reqVO.getPatid())
                .eqIfPresent(OrderDO::getOrderno, reqVO.getOrderno())
                .eqIfPresent(OrderDO::getPackageflag, reqVO.getPackageflag())
                .eqIfPresent(OrderDO::getPackageno, reqVO.getPackageno())
                .eqIfPresent(OrderDO::getItemflag, reqVO.getItemflag())
                .eqIfPresent(OrderDO::getItemno, reqVO.getItemno())
                .eqIfPresent(OrderDO::getPaystatus, reqVO.getPaystatus())
                .likeIfPresent(OrderDO::getName, reqVO.getName())
                .likeIfPresent(OrderDO::getNationid, reqVO.getNationid())
                .likeIfPresent(OrderDO::getSchoolName, reqVO.getSchoolName())
                .likeIfPresent(OrderDO::getClassName, reqVO.getClassName())
                .likeIfPresent(OrderDO::getItemname, reqVO.getItemname())
                .likeIfPresent(OrderDO::getZxksname, reqVO.getZxksname())
                .likeIfPresent(OrderDO::getKdksname, reqVO.getKdksname())
                .eqIfPresent(OrderDO::getItemprice, reqVO.getItemprice())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId));
    }

    default List<OrderDO> selectList(OrderExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<OrderDO>()
                .eqIfPresent(OrderDO::getBatchno, reqVO.getBatchno())
                .eqIfPresent(OrderDO::getPatid, reqVO.getPatid())
                .eqIfPresent(OrderDO::getOrderno, reqVO.getOrderno())
                .eqIfPresent(OrderDO::getPackageflag, reqVO.getPackageflag())
                .eqIfPresent(OrderDO::getPackageno, reqVO.getPackageno())
                .eqIfPresent(OrderDO::getItemflag, reqVO.getItemflag())
                .eqIfPresent(OrderDO::getItemno, reqVO.getItemno())
                .eqIfPresent(OrderDO::getPaystatus, reqVO.getPaystatus())
                .likeIfPresent(OrderDO::getSchoolName, reqVO.getSchoolName())
                .likeIfPresent(OrderDO::getClassName, reqVO.getClassName())
                .likeIfPresent(OrderDO::getItemname, reqVO.getItemname())
                .likeIfPresent(OrderDO::getZxksname, reqVO.getZxksname())
                .likeIfPresent(OrderDO::getKdksname, reqVO.getKdksname())
                .eqIfPresent(OrderDO::getItemprice, reqVO.getItemprice())
                .betweenIfPresent(OrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(OrderDO::getId));
    }


        default void updateOrderByPay(OrderDO tjOrderDO){
        if(tjOrderDO.getPackageno().equals("0")){
            //属于可选项目
            update(tjOrderDO, Wrappers.lambdaUpdate(OrderDO.class)
                    .eq(OrderDO::getPackageno, tjOrderDO.getPackageno())
                    .eq(OrderDO::getPatid, tjOrderDO.getPatid())
                    .eq(OrderDO::getItemname, tjOrderDO.getItemname())
                    .eq(OrderDO::getBatchno, tjOrderDO.getBatchno()));
        }
        else{
            //属于必选包，直接按包编号更新
            update(tjOrderDO, Wrappers.lambdaUpdate(OrderDO.class)
                    .eq(OrderDO::getPackageno, tjOrderDO.getPackageno())
                    .eq(OrderDO::getPatid, tjOrderDO.getPatid())
                    .eq(OrderDO::getBatchno, tjOrderDO.getBatchno()));
        }
    }

    default void updateOrderByNotify(OrderDO tjOrderDO){
            update(tjOrderDO, Wrappers.lambdaUpdate(OrderDO.class)
                    .eq(OrderDO::getOrderno, tjOrderDO.getOrderno()));
    }

    @Select("update xytj_order set deleted = true WHERE DATE(create_time) = CURDATE() - INTERVAL 1 DAY  AND TIME(create_time) < '24:00:00' and (paystatus != '1' or  paystatus IS NULL)")
    void updateOrderAfter24();

    @Select("select * from xytj_order WHERE DATE(create_time) = CURDATE() - INTERVAL 1 DAY  AND TIME(create_time) < '24:00:00' and (paystatus = '0') group by orderno")
    List<OrderDO> selectOrderAfter24();

    @Select("<script>" +
            "SELECT itemname, batchno, school_name as schoolName, class_name as className, sex, count(*) as patCount, ROUND(SUM(itemprice),2) as priceSum FROM xytj_order" +
            "<where>"+
            "<if test='itemname != null and itemname != \"\"'> and itemname = #{itemname}</if>" +
            "<if test='batchno != null and batchno != \"\"'> and batchno = #{batchno}</if>" +
            "<if test='schoolName != null and schoolName != \"\"'> and school_name = #{schoolName}</if>" +
            "<if test='className != null and className != \"\"'> and class_name = #{className}</if>" +
            "<if test='sex != null'> and sex = #{sex}</if>" +
            "<if test='dateRangeStart != null and dateRangeStart != \"\"'> and create_time &gt;= #{dateRangeStart}</if>" + // 大于>
            "<if test='dateRangeEnd != null and dateRangeEnd != \"\"'> and create_time &lt;= #{dateRangeEnd}</if>" +  // 小于<
            " and paystatus = '1'" +
            " and deleted = false" +
            "</where>"+
            " GROUP BY itemname" +
            "</script>")
    List<OrderSumRespVO> getSumList(@Param("batchno") String batchno, @Param("schoolName") String schoolName, @Param("className") String className,
                                    @Param("itemname") String itemname, @Param("sex") Integer sex,
                                    @Param("dateRangeStart") String dateRangeStart, @Param("dateRangeEnd") String dateRangeEnd);

}
