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

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.app.xytjinfo.vo.ItemListReqVO;
import com.jojubanking.boot.module.system.controller.app.xytjinfo.vo.studentJfListVO;
import com.jojubanking.boot.module.system.dal.dataobject.item.ItemDO;
import com.jojubanking.boot.module.system.dal.dataobject.order.OrderDO;
import org.apache.ibatis.annotations.Mapper;
import com.jojubanking.boot.module.system.controller.admin.item.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 检查项目 Mapper
 *
 * @author admin
 */
@Mapper
public interface ItemMapper extends BaseMapperX<ItemDO> {

    default PageResult<ItemDO> selectPage(ItemPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ItemDO>()
                .eqIfPresent(ItemDO::getPackageflag, reqVO.getPackageflag())
                .eqIfPresent(ItemDO::getPackageno, reqVO.getPackageno())
                .eqIfPresent(ItemDO::getItemflag, reqVO.getItemflag())
                .eqIfPresent(ItemDO::getItemno, reqVO.getItemno())
                .likeIfPresent(ItemDO::getItemname, reqVO.getItemname())
                .eqIfPresent(ItemDO::getItemprice, reqVO.getItemprice())
                .betweenIfPresent(ItemDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ItemDO::getId));
    }

    default List<ItemDO> selectList(ItemExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ItemDO>()
                .eqIfPresent(ItemDO::getPackageflag, reqVO.getPackageflag())
                .eqIfPresent(ItemDO::getPackageno, reqVO.getPackageno())
                .eqIfPresent(ItemDO::getItemflag, reqVO.getItemflag())
                .eqIfPresent(ItemDO::getItemno, reqVO.getItemno())
                .likeIfPresent(ItemDO::getItemname, reqVO.getItemname())
                .eqIfPresent(ItemDO::getItemprice, reqVO.getItemprice())
                .betweenIfPresent(ItemDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(ItemDO::getSort));
    }

//    @Select("SELECT * FROM xytj_item b WHERE NOT EXISTS ( SELECT 1 FROM xytj_order a WHERE a.itemno = b.itemno AND a.patid = #{patid} ) and b.packageno != #{packageno} ")
//    List<ItemDO> selectItemAddtionListByOrder(@Param("patid") String patid, @Param("packageno") String packageno );

    @Select("SELECT * FROM xytj_item b WHERE NOT EXISTS ( SELECT 1 FROM xytj_order a WHERE a.itemno = b.itemno AND a.patid = #{patid} AND a.batchno = #{batchno}  and a.deleted = false) and b.packageno = #{packageno} and b.deleted = false")
    List<ItemDO> selectItemStandardListByOrder(@Param("patid") String patid, @Param("packageno") String packageno , @Param("batchno") String batchno );

    @Select("SELECT * FROM xytj_item b WHERE NOT EXISTS ( SELECT 1 FROM xytj_order a WHERE a.itemno = b.itemno AND a.patid = #{patid} ) and b.packageno = #{packageno} ")
    List<ItemDO> updateItemPriceByNo1(@Param("patid") String patid, @Param("packageno") String packageno );

    default void updateItemByChild(ItemDO itemDO){
        update(itemDO, Wrappers.lambdaUpdate(ItemDO.class)
                .eq(ItemDO::getPackageno, itemDO.getPackageno()));
    }


    default void updateItemPriceByNo(ItemDO itemDO){
        update(itemDO, Wrappers.lambdaUpdate(ItemDO.class)
                .eq(ItemDO::getItemno, itemDO.getItemno()));
    }

    default List<ItemDO> selectSimpleList(ItemListReqVO reqVO) {
        if(reqVO.getPackageno().equals("0")){
            return selectList(new LambdaQueryWrapperX<ItemDO>()
                    .eqIfPresent(ItemDO::getPackageflag, reqVO.getPackageflag())
                    .eqIfPresent(ItemDO::getPackageno, reqVO.getPackageno())
                    .eqIfPresent(ItemDO::getItemflag, reqVO.getItemflag())
                    .eqIfPresent(ItemDO::getItemno, reqVO.getItemno())
                    .likeIfPresent(ItemDO::getItemname, reqVO.getItemname())
                    .orderByDesc(ItemDO::getId));

        }else{
            return selectList(new LambdaQueryWrapperX<ItemDO>()
                    .eqIfPresent(ItemDO::getPackageflag, reqVO.getPackageflag())
                    .neIfPresent(ItemDO::getPackageno, "0")
                    .eqIfPresent(ItemDO::getItemflag, reqVO.getItemflag())
                    .eqIfPresent(ItemDO::getItemno, reqVO.getItemno())
                    .likeIfPresent(ItemDO::getItemname, reqVO.getItemname())
                    .orderByDesc(ItemDO::getId));
        }
    }

}
