package com.sw.mes.store.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sw.mes.store.entity.WarehouseInputBillDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * @Author wupeng
 * @Description 入库单详情
 * @Date 18:58 2023/7/27
 * @Param
 * @return
 **/
@Mapper
public interface WarehouseInputBillDetailMapper extends BaseMapper<WarehouseInputBillDetail> {

    @Select("select * from warehouse_input_bill_detail where input_bill_no = #{billNo}")
    List<WarehouseInputBillDetail> getByBillNo(@Param("billNo") String billNo);

    @Select("select * from warehouse_input_bill_detail where unique_code = #{uniqueCode}")
    WarehouseInputBillDetail getByUniqueCode(@Param("uniqueCode") String uniqueCode);

    @Select("select * from warehouse_input_bill_detail where batch_no = #{batchNo}")
    List<WarehouseInputBillDetail> getByBatchNo(@Param("batchNo") String batchNo);

    @Select("select * from warehouse_input_bill_detail where input_bill_id = #{billId}")
    List<WarehouseInputBillDetail> getByBillId(@Param("billId") Long billId);

    @Select({"<script>",
            "select * from warehouse_input_bill_detail",
            " where input_bill_id in",
            "<foreach collection='billIds' item='id' open='(' separator =',' close=')'>",
            "#{id}",
            "</foreach>",
            "group by input_bill_id",
            "</script>"
    })
    List<WarehouseInputBillDetail> getByBillIds(List<Long> billIds);
    @Select("select * from warehouse_input_bill_detail where batch_no = #{code} or traceid = #{code} or serial_no = #{code}")
    List<WarehouseInputBillDetail> findWarehouseInputBillDetailsBySerialNoOrTraceIdOrBatchNo(@Param("code") String code);
    @Select({"<script>",
            "select * from warehouse_input_bill_detail where 1=1",
            "<if test='serialNo != null and serialNo != \"\" '>",
            " and serial_no = #{serialNo}",
            "</if>",
            "<if test='traceId != null and traceId != \"\" '>",
            " and traceId = #{traceId}",
            "</if>",
            "</script>"
    })
    List<WarehouseInputBillDetail> findBatchNoBySerialNoOrTraceId(String serialNo,String traceId);
    @Select("select * from warehouse_input_bill_detail where material_model_code = #{materialCode} and status = '已入库'")
    List<WarehouseInputBillDetail> findWarehouseInputBillDetailsByMaterialCode(String materialCode);


    @Select("select DISTINCT(material_model_code) from warehouse_input_bill_detail")
    Set<String> getDistinctMaterialCode();
//    @Select("select input_bill_no from warehouse_input_bill_detail where material_model_code = #{materialCode} " +
//            "and batch_no = #{batchNo} and warehouse_code = #{warehouseCode} and type = #{type} limit 1")
//    String findDetailsByMaterialBatchNoWarehouseType(String materialCode,String batchNo,String warehouseCode,String type);
}