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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.core.dto.ProductionPaintingLineDetailDto1;
import com.sw.mes.yb.pad.dto.ProductionPaintingLineDetailDto;
import com.sw.mes.yb.pad.entity.ProductionPaintingLineDetail;
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 ProductionPaintingLineDetailDao extends BaseDao<ProductionPaintingLineDetail, Long>{

    // 根据晶段编号找到对应的单晶异常信息
    @Query(value = "SELECT\n" +
            "\tcrystal_error \n" +
            "FROM\n" +
            "\tproduction_long_crystal \n" +
            "WHERE\n" +
            "\tid = (\n" +
            "\tSELECT\n" +
            "\t\tld.crystal_id \n" +
            "\tFROM\n" +
            "\t\t`production_wsm_reserve` wr\n" +
            "\t\tLEFT JOIN production_painting_line_detail ld ON wr.crystal_no = ld.NO \n" +
            "\tWHERE\n" +
            "\tld.type = 8 and wr.crystal_no = ?1)\n", nativeQuery = true)
    String findErrorMessege(String crystalNo);

    @Query(value = "SELECT * FROM `production_painting_line_detail` WHERE crystal_id = ?1 and operate_reversecut_draw_id = 0", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByCrystalId(Long crystalId);

    @Query(value = "SELECT ppld.* FROM `production_painting_line_detail` ppld left join yb_inspect_data yid on ppld.no = yid.section_no \n" +
            "WHERE ppld.type = ?1 and ppld.crystal_type = ?2 and ppld.crystal_id = ?3 and yid.grade_type = '正常'", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByTypeAndCrystalTypeAndCrystalId(Integer type, Integer crystalType, Long crystalId);


    @Query(value = "select * from production_painting_line_detail where  `no` like CONCAT('%',:no,'%') and crystal_type=0 and `type`=2 ORDER BY id DESC", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByLikeSegmentNo(@Param("tSegmentNo")String no);

    @Query(value = "select * from production_painting_line_detail where  `no` like CONCAT('%',:no,'%') and `type`=2 ORDER BY id DESC", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByLikeSegmentNoHs(@Param("tSegmentNo")String no);

    @Query(value = "select * from production_painting_line_detail where `no` like CONCAT('%',:no,'%') and  `type`=0 ORDER BY id DESC", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByLikeSegmentNoFb(@Param("tSegmentNo")String no);


    @Query(value = "select * from production_painting_line_detail where  no = ?1", nativeQuery = true)
    List<ProductionPaintingLineDetail> findBySegmentNo(String segmentNo);

    @Query(value = "select * from production_painting_line_detail where  no in ?1", nativeQuery = true)
    List<ProductionPaintingLineDetail> findBySegmentNos(List<String> segmentNo);

    @Query(value = "select no from production_painting_line_detail where crystal_id = ?1 and type = ?2 and crystal_type = ?3 order by sequence", nativeQuery = true)
    List<String> findByCrystalIdAndType(Integer crystalId,Integer type,Integer crystalType);

    @Query(value = "SELECT * FROM `production_painting_line_detail` WHERE `crystal_id` = ?1 AND `type` = '0' AND `operate_reversecut_draw_id` = '0'", nativeQuery = true)
    List<ProductionPaintingLineDetail> findType(Integer crystalId);

    @Modifying
    @Query(value = "DELETE FROM `production_painting_line_detail` WHERE operate_draw_id = ?1", nativeQuery = true)
    void deleteByOperateDrawId(Long operateDrawId);

    @Modifying
    @Query(value = "DELETE FROM `production_painting_line_detail` WHERE crystal_id = ?1", nativeQuery = true)
    void deleteByCrystalId(Long crystalId);

    // 画线id找到对应的晶棒画线编号
    @Query(value = "select no FROM `production_painting_line_detail` WHERE operate_draw_id = ?1", nativeQuery = true)
    List<String> findByOperateDrawId(Long operateDrawId);


    // 通过圆棒反切画线id 类型找到圆棒反切画线编号
    @Query(value = "select no FROM `production_painting_line_detail` WHERE operate_reversecut_draw_id = ?1 and crystal_type = 0", nativeQuery = true)
    List<String> findByOperateRecutDrawId(Long operateDrawId);


    @Modifying
    @Query(value = "DELETE FROM `production_painting_line_detail` WHERE operate_reversecut_draw_id = ?1 and crystal_type = 0", nativeQuery = true)
    void deleteByOperateRecutDrawId(Long operateRecutDrawId);


    /**
     * 通过方棒反切画线id 方棒反切画线编号
     *
     * @param operateDrawId
     * @return
     */
    @Query(value = "select no FROM `production_painting_line_detail` WHERE operate_reversecut_draw_id = ?1 and crystal_type = 1", nativeQuery = true)
    List<String> findByFbOperateRecutDrawId(Long operateDrawId);

    /**
     * 通过方棒反切画线id 删除方棒反切明细
     *
     * @param operateRecutDrawId
     */
    @Modifying
    @Query(value = "DELETE FROM `production_painting_line_detail` WHERE operate_reversecut_draw_id = ?1 and crystal_type = 1", nativeQuery = true)
    void deleteFbByOperateRecutDrawId(Long operateRecutDrawId);

    /**
     * 查询长棒划线明细
     *
     * @param id 长棒划线id
     * @return
     */
    @Query(value = "select * from production_painting_line_detail where  operate_draw_id = ?1", nativeQuery = true)
    List<ProductionPaintingLineDetail> findProductionPaintingLineDetailByOperateDrawId(Long id);

    /**
     * 查询圆棒反切明细
     * @param id 圆棒反切划线id
     * @return
     */
    @Query(value = "select * from production_painting_line_detail where  operate_reversecut_draw_id = ?1  and crystal_type = 0", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByOperateReversecutDrawId(Long id);

    /**
     * 查询方棒反切明细
     * @param operateDrawId 方棒反切划线id
     * @return
     */
    @Query(value = "select * FROM `production_painting_line_detail` WHERE operate_reversecut_draw_id = ?1 and crystal_type = 1", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByOperateRecutDrawIdFb(Long operateDrawId);

    @Modifying
    void deleteByOperateReversecutDrawIdAndCrystalType(Long id, Integer crystalType);

    ProductionPaintingLineDetail findTopByOperateDrawIdAndAndNoOrderByIdDesc(Long operateDrawId, String crystalNo);

    List<ProductionPaintingLineDetail> findByCrystalIdAndType(Long operateDrawId, int type);

    @Query(value = "select a.*  from production_painting_line_detail a join (select *  from production_painting_line_detail where no = ?1) b on a.operate_draw_id=b.operate_draw_id  and a.crystal_id = b.crystal_id where  a.type = 0 and a.crystal_type = 0 order by a.segment_num,a.id ", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByRecycledSectionNo(String recycledSectionNo);

    @Query(value = "select a.*  from production_painting_line_detail a join (select *  from production_painting_line_detail where no = ?1 and crystal_type = 0) b on a.operate_reversecut_draw_id=b.operate_reversecut_draw_id  where  a.type = 0 and a.crystal_type = 0 order by segment_num ", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByRecycledSectionNoFQ(String recycledSectionNo);

    @Query(value = "select a.*  from production_painting_line_detail a join (select *  from production_painting_line where crystal_no = ?1 order by id desc LIMIT 1) b on a.operate_draw_id=b.id where  a.type = 0 and a.crystal_type = 0 order by segment_num desc ", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByRecycledCrystalNo(String crystalNo);

    @Query(value = "select no FROM `production_painting_line_detail` WHERE no LIKE CONCAT(?1,'%') and type = 0 order by sequence", nativeQuery = true)
    List<String> findDetailByCrystalNo(String crystalNo);

    @Query(value = "select * FROM `production_painting_line_detail` WHERE no = ?1 LIMIT 1", nativeQuery = true)
    ProductionPaintingLineDetail findDetailByNo(String no);

    @Query(value = "SELECT * FROM production_painting_line_detail WHERE operate_reversecut_draw_id = ?1 AND crystal_type = ?2 and is_cut = ?3", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByOperateReversecutDrawId(Long operateReversecutDrawId, Integer crystalType, Integer isCut);

    @Query(value = "SELECT * FROM production_painting_line_detail WHERE operate_reversecut_draw_id = ?1 AND crystal_id = ?2 and type = ?3 order by id desc", nativeQuery = true)
    List<ProductionPaintingLineDetail> findByOperateReversecutDrawIdAndCrystalIdAndType(Long operateReversecutDrawId, Long crystalId, Integer type);
}
