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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.pad.entity.SectionNoDetail;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

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

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE section_no = ?1 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    List<SectionNoDetail> findBySectionNo(String sectionNo);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE section_no like CONCAT('%',:sectionNo,'%')  ORDER BY id DESC ", nativeQuery = true)
    List<SectionNoDetail> findByLikeSectionNo(@Param("sectionNo") String sectionNo);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE section_no in (?1) ", nativeQuery = true)
    List<SectionNoDetail> findBySectionNos(List<String> sectionNos);

    @Query(value = "SELECT COUNT(1) FROM production_segment_detail WHERE crystal_no = ?1 AND parent_id = 0", nativeQuery = true)
    Integer findSegmentNumByNo(String segmentNo);

    @Query(value = "SELECT piece_no_head FROM `production_segment_detail` WHERE section_no = ?1 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    String findPieNoHeadBySectionNo(String sectionNo);

    @Query(value = "SELECT piece_no_end FROM `production_segment_detail` WHERE section_no = ?1 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    String findPieNoEndBySectionNo(String sectionNo);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE (piece_no_head =?1  or piece_no_end = ?1) AND section_type != ?2 ORDER BY id DESC ", nativeQuery = true)
    List<SectionNoDetail> findSegmentNoBypieceNo(String pieceNo, String sectionType);

    List<SectionNoDetail> findByCrystalIdAndParentIdOrderById(Long goodsId, Long i);

    List<SectionNoDetail> findByParentId(Long parentId);

    @Modifying
    void deleteBySectionNo(String segmentNos);

    @Modifying
    void deleteBySectionNoIn(List<String> segmentNos);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_id = ?1 AND segment_num = ?2 AND parent_id = ?3 ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
    SectionNoDetail findByCrystalIdAndSegmentNumAndParentId(Long crystalId, Integer segmentNum, long parentId);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_no = ?1 ORDER BY id DESC ", nativeQuery = true)
    List<SectionNoDetail> findByCrystalNo(String crystalNo);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_no = ?1 AND section_posiotion = ?2 ORDER BY id DESC ", nativeQuery = true)
    SectionNoDetail findByCrystalNoAndSectionPosiotion(String crystalNo, Integer sectionPosiotion);


    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_no = ?1 AND section_posiotion > ?2 ORDER BY id DESC ", nativeQuery = true)
    List<SectionNoDetail> findInfoByCrystalNoAndSectionPosiotion(String crystalNo, Integer sectionPosiotion);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_no = ?1 AND segment_num < ?2 AND (piece_no_head is not null AND piece_no_end  is not null) ORDER BY segment_num DESC,parent_id desc, id DESC LIMIT 1 ", nativeQuery = true)
    SectionNoDetail findByCrystalNoAndSegmentNumLessThan(String crystalNo, Integer segmentNum);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_no = ?1 AND segment_num > ?2 AND (piece_no_head is not null AND piece_no_end  is not null) ORDER BY segment_num,parent_id desc, id DESC LIMIT 1 ", nativeQuery = true)
    SectionNoDetail findByCrystalNoAndSegmentNumGreaterThan(String crystalNo, Integer segmentNum);


    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_no = ?1 AND oxygen_carbon_mark = ?2 AND section_type != '回收料' AND piece_no_head  IS NOT NULL ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
    SectionNoDetail findHeadByCrystalNoAndOxygenCarbonMark(String crystalNo, Integer oxygenCarbonMark);

    @Query(value = "SELECT * FROM `production_segment_detail` WHERE crystal_no = ?1 AND oxygen_carbon_mark = ?2 AND section_type != '回收料' AND piece_no_end  IS NOT NULL ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
    SectionNoDetail findTailByCrystalNoAndOxygenCarbonMark(String crystalNo, Integer oxygenCarbonMark);
}
