package com.sw.mes.yb.pc.dao;


import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.pc.entity.StoreMixInfo;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
* @Author liru
* @Description：
* @Date:
* @Modified By:
*/
public interface StoreMixInfoDao extends BaseDao<StoreMixInfo , Integer> {

    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_data_batch_info SET experiment_name=?2,experiment_value=?3 WHERE lot_no=?1 ", nativeQuery = true)
    void updateBatchInfo(String lotNo, String testTypeName, String testTypeValue);

    @Query(value = "SELECT product_model_name FROM zhxx_data_batch_info WHERE lot_no=?1 ", nativeQuery = true)
    String getProductModelNameByLotNo(String lotNo);

    @Query(value = "SELECT * FROM store_mix_info WHERE code=?1 ORDER BY create_time DESC ", nativeQuery = true)
    List<StoreMixInfo> findByCode(String code);

    @Query(value = "SELECT COUNT(1) FROM store_mix_info WHERE code=?1 ", nativeQuery = true)
    Long countByCode(String code);

    @Query(value = "SELECT COUNT(1) FROM store_mix_info WHERE lot_no=?1 ", nativeQuery = true)
    Long countByLotNo(String lotNo);

    @Query(value = "SELECT id FROM store_mix_info WHERE lot_no=?1 ", nativeQuery = true)
    Long getIdByLotNo(String lotNo);

    @Modifying
    @Transactional
    @Query(value = "UPDATE store_mix_info SET lot_no =?2 WHERE lot_no=?1 ", nativeQuery = true)
    void updatelotNo(String oldCode, String newCode);

    @Modifying
    @Transactional
    @Query(value = "UPDATE store_mix_info SET code =?2 WHERE code=?1 ", nativeQuery = true)
    void updateCode(String oldCode, String newCode);

    @Modifying
    @Query(value = "UPDATE store_mix_info SET lot_no =?2 , device_no =?3 WHERE lot_no=?1 ", nativeQuery = true)
    void updatelotNoAndDeviceNo(String oldCode, String newCode, String newDeviceNo);


    @Query(value = "call procedure_report_pre_materials(:lotNo,:device,:series,:ggSupplier,:date1,:date2,:start,:rows)", nativeQuery = true)
    List<Map<String,Object>> reportPreMaterials(@Param("lotNo") String lotNo, @Param("device") String device,
                                                @Param("series") String series, @Param("ggSupplier") String ggSupplier,
                                                @Param("date1") String date1, @Param("date2") String date2,
                                                @Param("start") Long start, @Param("rows") Integer rows);

    @Query(value = "call procedure_count_pre_materials(:lotNo,:device,:series,:ggSupplier,:date1,:date2)", nativeQuery = true)
    List<Map<String,Object>> countPreMaterials(@Param("lotNo") String lotNo, @Param("device") String device,
                                               @Param("series") String series, @Param("ggSupplier") String ggSupplier,
                                               @Param("date1") String date1, @Param("date2") String date2);

    @Query(value = "call procedure_materials_use(:device,:modelName,:date1,:date2,:start,:rows)", nativeQuery = true)
    List<Map<String,Object>> materialsUse(@Param("device") String device, @Param("modelName") String modelName,
                                          @Param("date1") String date1, @Param("date2") String date2,
                                          @Param("start") Long start, @Param("rows") Integer rows);

    @Query(value = "call procedure_count_materials_use(:device,:modelName,:date1,:date2)", nativeQuery = true)
    List<Map<String,Object>> countMaterialsUse(@Param("device") String device, @Param("modelName") String modelName,
                                               @Param("date1") String date1, @Param("date2") String date2);

    @Query(value = "SELECT * FROM store_mix_info WHERE lot_no=?1 ORDER BY create_time DESC ", nativeQuery = true)
    List<StoreMixInfo> findByLotNo(String lotNo);

    @Query(value = "SELECT sap_order_no FROM store_mix_info WHERE device_no=?1 ORDER BY create_time DESC limit 1 ", nativeQuery = true)
    String findSapOrderNoByDeviceNo(String name);

    @Query(value = "SELECT lot_no FROM store_mix_info WHERE device_no=?1 ORDER BY create_time DESC limit 1 ", nativeQuery = true)
    String findLastByDeviceNo(String deviceNo);

}
