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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.report.entity.CrystalPullingInfoLog;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface CrystalPullingInfoLogDao extends BaseDao<CrystalPullingInfoLog, Long> {

    @Query(value = "select * from rpt_crystal_pulling_info_log where lot_no = ?1 order by id desc limit 1", nativeQuery = true)
    CrystalPullingInfoLog getOneByLotNoOrderByIdDesc(String lotNo);

    @Query(value = "select * from rpt_crystal_pulling_info_log where lot_no in ?1 order by id desc limit 1", nativeQuery = true)
    List<CrystalPullingInfoLog> getCrystalPullingInfoLogByLotNos(List<String> lotNos);

    @Query(value = "select * from rpt_crystal_pulling_info_log where device_no = ?1 order by id desc limit 1", nativeQuery = true)
    CrystalPullingInfoLog getCrystalPullingInfoLogByDeviceNo(String deviceNo);

    @Modifying
    @Transactional
    @Query(value = "delete from rpt_crystal_pulling_info_log where lot_no = ?1 ", nativeQuery = true)
    void deleteLogByLotNo(String lotNo);


    @Query(value = "select * from rpt_crystal_pulling_info_log where device_no = ?1 and id < (select id from rpt_crystal_pulling_info_log where lot_no = ?2 order by id desc limit 1)order by id desc limit 1", nativeQuery = true)
    CrystalPullingInfoLog getCrystalPullingInfoLogByDeviceNoAndLotNo(String deviceNo, String lotNo);

    @Query(value = "select sum(run_time) from rpt_crystal_pulling_info_log where stop_time >= ?1 and stop_time < ?2 and series in ?3 and lot_no in (select lot_no from production_long_crystal where gmt_create >= ?1 and gmt_create < ?2 and closure_identification = \"W\")", nativeQuery = true)
    BigDecimal getCrystalPullingInfoLogByTimeAndSeries(String startTime, String endTime, List<String> seriesList);

}
