package com.sw.mes.inspect.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.inspect.entity.YbInspectData;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

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

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


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

    @Query(value = "SELECT * FROM `yb_inspect_data` WHERE crystal_no = ?1 ORDER BY id DESC ", nativeQuery = true)
    List<YbInspectData> getInspectDatasByCrystalNo(String sectionNo);

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

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

    @Query(value = "SELECT * FROM `yb_inspect_data` WHERE section_no IN ?1 ORDER BY ware_house_length DESC", nativeQuery = true)
    List<YbInspectData> findBySections(List<String> sectionNoList);

    @Query(value = "select * from yb_inspect_data where section_no = ?1 ORDER BY id DESC LIMIT 2", nativeQuery = true)
    List<Map<String, Object>> findBySectionNoMap(String sectionNo);

    @Modifying
    @Query(value = "UPDATE yb_inspect_data SET re_check_type = case when (re_check_type REGEXP '[^0-9.]')=0 then re_check_type + 1 else 1 end," +
            " order_type= ?2, checkout_name = ?3 WHERE section_no IN (?1) ", nativeQuery = true)
    Integer updateOrderTypeBySectionNoList(List<String> sectionNoList, String orderType, String checkoutName);

    @Modifying
    @Query(value = "UPDATE yb_inspect_data SET order_type= ?2, checkout_name = ?3 WHERE section_no IN (?1) ", nativeQuery = true)
    Integer updateOrderTypeBySectionNoListNoType(List<String> sectionNoList, String orderType, String checkoutName);

    @Query(value = "SELECT* from yb_inspect_data  WHERE section_no IN ( select section_no FROM production_segment_detail WHERE crystal_no = (SELECT DISTINCT crystal_no FROM `production_segment_detail`  WHERE section_no = ?1))", nativeQuery = true)
    List<YbInspectData> findByCrystalNo(String sectionNo);

    @Query(value = "SELECT* from yb_inspect_data  WHERE crystal_no = ?1 ", nativeQuery = true)
    List<YbInspectData> findByCrystalNo1(String sectionNo);

    @Modifying
    @Query(value = "UPDATE yb_inspect_data SET sticks_count = ?2,spell_bar_length = ?3,section_type = ?4,section_compose=?5 WHERE section_no IN (?1) ", nativeQuery = true)
    Integer updateSticksCountFbSectionNoList(List<String> sectionNoList, Integer sticksCount, BigDecimal totalLength, String sectionType, String SectionNoGroup);

    @Query(value = " SELECT section_no FROM yb_inspect_data WHERE create_time > '2022-03-22 13:26:32' ORDER BY id", nativeQuery = true)
    List<String> findByAllSectionNo();

    /**
     * 根据单晶（长棒）编号查询第一根晶段检验数据
     *
     * @param crystalNo
     * @return
     */
    @Query(value = " SELECT * FROM yb_inspect_data where crystal_no=?1 and piecewise_num=1 ", nativeQuery = true)
    List<YbInspectData> findFirstInfo(String crystalNo);

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

    /**
     * 修改检验数据库是否反切字段
     *
     * @param isCutBack 是否反切
     * @param sectionNo 晶段
     */
    @Modifying
    @Query(value = "UPDATE yb_inspect_data SET is_cut_back = ?1 WHERE section_no = ?2 ", nativeQuery = true)
    void updateIsCutBack(int isCutBack, String sectionNo);

    /**
     * 重新匹配订单，修改检验数据中的值
     *
     * @param sectionNo 晶段
     * @param orderNo   新订单号
     */
    @Modifying
    @Query(value = "UPDATE yb_inspect_data SET order_no = ?2 WHERE section_no = ?1 ", nativeQuery = true)
    void updateOrderNoBySectionNo(String sectionNo, String orderNo);

    /**
     * 重新匹配订单，修改检验数据中的值
     *
     * @param sectionNo    晶段
     * @param orderNo      新订单号
     * @param materialCode 新物料号
     */
    @Modifying
    @Query(value = "UPDATE yb_inspect_data SET order_no = ?2,material_code=?3 WHERE section_no = ?1 ", nativeQuery = true)
    void updateOrderNoAndModelCodeBySectionNo(String sectionNo, String orderNo, String materialCode);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out_data SET sticks_count = ?2,spell_bar_length=?3,section_type=?4,section_compose=?5 WHERE fb_section_no in (?1) ", nativeQuery = true)
    void updateSticksCountFbSectionNoListFromYb(List<String> sectionNos, int i, Object o, String sectionType, List<String> sectionNos1);

    /**
     * 重新计算单晶上所有为切断的短棒的平均直径
     *
     * @param sectionNo 单晶编号
     */
    @Modifying
    @Query(value = "UPDATE yb_inspect_data,(SELECT AVG(avg_diameter) AS avg_diameter FROM yb_inspect_data WHERE crystal_no = ?1 AND is_cut_back = 0 GROUP BY crystal_no)t SET all_avg_diameter = t.avg_diameter\n" +
            "  WHERE crystal_no = ?1", nativeQuery = true)
    void recalculateAllAvgDiameter(String sectionNo);

    @Query(value = "select * from yb_inspect_data where lot_no = ?1 and again_num = ?2 and t_resistivity is not null",nativeQuery = true)
    List<YbInspectData> findByLotNoAndAgainNum(String lotNo, Integer againNum);


    @Query(value = "SELECT * FROM `yb_inspect_data` WHERE LOCATE(?1, section_no) > 0 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    YbInspectData findBySectionNoLike(String sectionNo);

    @Query(value = "SELECT * FROM `yb_inspect_data` WHERE crystal_no = ?1 AND oxygen_carbon_mark = ?2 ORDER BY piecewise_num ASC,id DESC LIMIT 1 ", nativeQuery = true)
    YbInspectData findByCrystalNoAndOxygenCarbonMarkAsc(String crystalNo, Integer oxygenCarbonMark);

    @Query(value = "SELECT * FROM `yb_inspect_data` WHERE crystal_no = ?1 AND oxygen_carbon_mark = ?2 ORDER BY piecewise_num DESC,id DESC LIMIT 1 ", nativeQuery = true)
    YbInspectData findByCrystalNoAndOxygenCarbonMarkDescd(String crystalNo, Integer oxygenCarbonMark);

    @Query(value = "SELECT ifnull(SUM(theory_weight),0) as weight FROM `yb_inspect_data` WHERE order_no = ?1 and is_cut_back = ?2", nativeQuery = true)
    BigDecimal findSumWeightByOrderNoAndIsCutBack(String orderNo, int backCut);

    @Query(value = "SELECT plc.is_done FROM `yb_inspect_data` yid LEFT JOIN production_long_crystal plc ON plc.crystal_no = yid.crystal_no \n" +
            "WHERE yid.section_no = ?1 order by plc.id desc limit 1 ", nativeQuery = true)
    Integer findLongCrystalByCrystalNo(String sectionNo);
    @Query(value = "SELECT * FROM `yb_inspect_data` WHERE section_no like ?1 LIMIT 1", nativeQuery = true)
    YbInspectData findByLikeSectionNo(String sectionNo);

    @Query(value = "SELECT * FROM `yb_inspect_data` WHERE crystal_no in  ?1 ", nativeQuery = true)
    List<YbInspectData> findByCrystalNo2(List<String> ybCrystalNoList);
}
