package com.sw.mes.store.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sw.mes.store.dto.WarehouseInventoryDto;
import com.sw.mes.store.dto.AvailableInventoryDto;
import com.sw.mes.store.entity.WarehouseInventory;
import com.sw.mes.store.entity.WarehouseQueryParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
@Mapper
public interface WarehouseInventoryMapper extends BaseMapper<WarehouseInventory> {
    @Select("select * from warehouse_inventory where del = 0 and unique_code = #{uniqueCode}")
    WarehouseInventory getByUniqueCode(@Param("uniqueCode") String uniqueCode);

    @Select("select * from warehouse_inventory where del = 0 and batch_no = #{batchNo}")
    List<WarehouseInventory> getByBatchNo(@Param("batchNo") String batchNo);

    @Select("select * from warehouse_inventory where del = 0 and material_model_code = #{code}")
    List<WarehouseInventory> getByMaterialCode(@Param("code") String code);

    @Select("select * from warehouse_inventory where del = 0 and material_model_id = #{materialId} and warehouse_id = #{warehouseId}")
    WarehouseInventory getByMaterialAndWarehouse(@Param("materialId") Long materialId, @Param("warehouseId") Long warehouseId);

    @Select("select * from warehouse_inventory where del = 0 and warehouse_id = #{warehouseId}")
    List<WarehouseInventory> getByWarehouseId(@Param("warehouseId") Long warehouseId);

    @Select({
            "<script>",
            "select * from warehouse_inventory where del = 0 and warehouse_id in (",
            "<foreach collection='typeIds' index='index' item='warehouseId' separator=',' >",
            "#{warehouseId}",
            "</foreach>",
            ") limit 1</script>"
    })
    List<WarehouseInventory> getByWarehouseIds(@Param("typeIds") List<Long> typeIds);

    @Select({"<script>",
            "select * from warehouse_inventory where del = 0 and material_model_code = #{materialCode}",
            "<if test='batchNo != null and batchNo != \"\" '>",
            " and batch_no = #{batchNo}",
            "</if>",
            "<if test='warehouseId != null and warehouseId != \"\" '>",
            " and warehouse_id = #{warehouseId}",
            "</if>",
            "</script>"
    })
    List<WarehouseInventory> getByMaterialAndBatchNoAndWarehouseIds(@Param("batchNo") String batchNo, @Param("materialCode") String materialCode, @Param("warehouseId") Long warehouseId);
    @Select("select * from warehouse_inventory where del = 0 and material_model_code = #{materialCode} and batch_no = #{batchNo} and warehouse_id = #{warehouseId}")
    List<WarehouseInventory> getByMaterialAndBatchNoAndWarehouseId(@Param("batchNo") String batchNo, @Param("materialCode") String materialCode, @Param("warehouseId") Long warehouseId);
    @Select({"<script>",
            "select * from warehouse_inventory where del = 0 and material_model_code = #{materialCode}",
            "<if test='batchNo != null and batchNo != \"\" '>",
            " and batch_no = #{batchNo}",
            "</if>",
            "<if test='warehouseCode != null and warehouseCode != \"\" '>",
            " and warehouse_code = #{warehouseCode}",
            "</if>",
            "</script>"
    })
    List<WarehouseInventory> getByMaterialAndBatchNoAndWarehouseCodes(@Param("batchNo") String batchNo, @Param("materialCode") String materialCode, @Param("warehouseCode") String warehouseCode);
    @Select("select * from warehouse_inventory where del = 0 and material_model_code = #{materialCode} and batch_no = #{batchNo} and warehouse_code = #{warehouseCode}")
    List<WarehouseInventory> getByMaterialAndBatchNoAndWarehouseCode(@Param("batchNo") String batchNo, @Param("materialCode") String materialCode, @Param("warehouseCode") String warehouseCode);
    @Select("select * from warehouse_inventory where del = 0 and batch_no = #{batchNo} and material_model_code != #{materialCode}")
    List<WarehouseInventory> getByMaterialAndBatchNo(@Param("batchNo") String batchNo, @Param("materialCode") String materialCode);

    Page<WarehouseInventory> groupByMaterial(IPage<WarehouseInventory> page, @Param("param") WarehouseQueryParam param);
    List<WarehouseInventory> findAllWarehouseInventory(@Param("param") WarehouseQueryParam param);
    @Select({
            "<script>",
            "select * from warehouse_inventory where del = 0 and batch_no = #{batchNo} and material_model_code in (",
            "<foreach collection='materialCodes' index='index' item='code' separator=',' >",
            "#{code}",
            "</foreach>",
            ")</script>"
    })
    List<WarehouseInventory> getByBatchNoAndMaterialCodes(@Param("batchNo") String batchNo, @Param("materialCodes") List<String> materialCodes);

    List<AvailableInventoryDto> getAvailableInventorys(@Param("materialCodes") List<String> materialCodes);

    WarehouseInventory getByBatchWarehouseId(@Param("batchNo") String batchNo,@Param("warehouseId") Long warehouseId);
    WarehouseInventory getByBatchWarehouseCode(@Param("batchNo") String batchNo,@Param("warehouseCode") String warehouseCode);


    void updateSafetyStockAndResidenceTimeAndTimeUnitByMaterialCode(@Param("safetyStock") String safetyStock,@Param("residenceTime") Integer residenceTime,@Param("timeUnit") String timeUnit,@Param("materialModelCode") String materialModelCode);

    List<String> getUniqueCodesByBatchNo(@Param("batchNo")String batchNo);
    List<String> findBatchNoBySerialNoOrTraceId(@Param("serialNo")String serialNo, @Param("traceId")String traceId);
    List<WarehouseInventory> findWarehouseInventoryByMaterialCode(@Param("materialCode") String materialCode);
    @Select("select * from warehouse_inventory where crystal_no = #{crystalNo} order by id desc limit 1")
    WarehouseInventory getByCrystalNo(String crystalNo);
}