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

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

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

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface TurnstoreTrayItemDao extends BaseDao<TurnstoreTrayItem, Integer> {
    @Modifying
    @Query(value = "DELETE FROM  zhxx_turnstore_tray_item WHERE tray_id=?1", nativeQuery = true)
    void deleteByTrayId(Integer trayId);

    @Query(value = "SELECT * FROM  zhxx_turnstore_tray_item WHERE tray_id=?1", nativeQuery = true)
    List<TurnstoreTrayItem> getItemsByTrayId(Integer trayId);

    // 获取圆棒物料卡的信息
    @Query(value = " SELECT\n" +
            "        tti.id ,\n" +
            "        tti.tray_id AS trayId,\n" +
            "        tti.tray_no AS trayNo,\n" +
            "        tti.section_no AS sectionNo,\n" +
            "        tti.product_level_second AS productLevelSecond,\n" +
            "        tti.product_level_experiment AS productLevelExperiment,\n" +
            "        tti.product_type_second AS productTypeSecond,\n" +
            "        tti.instore_length AS instoreLength,\n" +
            "        tti.instore_weight AS instoreWeight,\n" +
            "        tti.actual_length AS actualLength,\n" +
            "        tti.actual_weight AS actualWeight,\n" +
            "        tti.fine_cut_length AS fineCutLength,\n" +
            "        tti.collapse_line AS collapseLine,\n" +
            "        tti.location_name AS locationName,\n" +
            "        tti.user ,\n" +
            "        tti.time ,\n" +
            "        tti.reason,\n" +
            "        tti.remark,\n" +
            "        tti.section_type AS sectionType,\n" +
            "        tti.is_to_wms AS isToWms,\n" +
            "        tti.to_wms_time AS toWmsTime,\n" +
            "        smn.new_material_number AS materialNumber ,\n" +
            "        zdi.slopet AS slopeT,\n" +
            "        zdi.slopew AS slopeW,\n" +
            "        zdi.fine_diameter AS fineDiameter,\n" +
            "        zdi.edge_bark AS edgeBark ,\n" +
            "        zdi.type," +
            "zdi.conductive_type AS conductiveType " +
            "    FROM\n" +
            "        zhxx_turnstore_tray_item tti  \n" +
            "    LEFT JOIN\n" +
            "        sap_section_material_number smn \n" +
            "            ON tti.section_no = smn.section_no  \n" +
            "    LEFT JOIN\n" +
            "        yb_inspect_data zdi \n" +
            "            ON zdi.section_no = smn.section_no \n" +
            "    WHERE\n" +
            "        tti.tray_id =?1 GROUP BY tti.id ", nativeQuery = true)
    List<Map<String, Object>> getItemsDtoByTrayId(Integer trayId);


    @Query(value = "SELECT * FROM  zhxx_turnstore_tray_item WHERE tray_no=?1", nativeQuery = true)
    List<TurnstoreTrayItem> getItemsByTrayNo(String trayNo);

    @Query(value = "SELECT id,section_no,tray_no FROM  zhxx_turnstore_tray_item WHERE section_no IN (?1) ORDER BY time DESC", nativeQuery = true)
    List<Map<String, Object>> getBySectionNos(List<String> sectionNoList);

    @Query(value = "SELECT id,section_no,remark,edge_bark,collapse_line,slopet,slopew,fine_diameter,fine_cut_length,distortion_big,sinton_head1,sinton_head2,sinton_tail1,sinton_tail2 FROM yb_inspect_data WHERE section_no IN(?1) ", nativeQuery = true)
    List<Map<String, Object>> getInspectDataBySectionNos(List<String> sectionNoList);

    @Modifying
    @Query(value = "UPDATE sap_inspect_info SET status = 2,end_time=NOW() WHERE section_no IN (?1)", nativeQuery = true)
    void updateBySectionNos(List<String> sectionNos);

    @Query(value = "SELECT * FROM  zhxx_turnstore_tray_item WHERE section_no=?1 AND (state=0 OR state=1)", nativeQuery = true)
    List<TurnstoreTrayItem> findBySectionNo(String crystalNo);

    @Query(value = "SELECT * FROM  zhxx_turnstore_tray_item WHERE section_no=?1 AND state=?2", nativeQuery = true)
    List<TurnstoreTrayItem> findBySectionNoAndState(String crystalNo, int state);

    @Query(value = "SELECT * FROM  zhxx_turnstore_tray_item WHERE tray_no=?1 AND section_no=?2", nativeQuery = true)
    List<TurnstoreTrayItem> findByTrayNoAndSectionNo(String trayNo, String sectionNo);

    @Query(value = "SELECT * FROM  zhxx_turnstore_tray_item WHERE section_no=?1 AND (state= 9 or has_gone='退料')", nativeQuery = true)
    List<TurnstoreTrayItem> findBySectionNoValidate(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);

    @Query(value = "SELECT  * FROM zhxx_turnstore_tray_item WHERE operation_time < '2022-03-31 18:08:34' ORDER BY id LIMIT 400", nativeQuery = true)
    List<TurnstoreTrayItem> getData();

    @Modifying
    @Query(value = "UPDATE zhxx_turnstore_tray_item SET order_no = ?2 WHERE id = ?1 ", nativeQuery = true)
    void updateOrderNoById(Integer id, String orderNo);
}
