package com.sw.mes.fb.aio.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.fb.aio.entity.EapCutCraft;
import com.sw.mes.fb.aio.entity.EapEvolutionRecord;
import com.sw.mes.fb.aio.entity.EapGrindingMachine;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

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

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface EapCutCraftDao extends BaseDao<EapCutCraft, Integer> {
    //2022年2月9日15:56:46由于去划线记录表中去到无用的数据，先不用
    //@Query(value = "SELECT c.round_SN,c.round_length,c.equip_no,TIMESTAMPDIFF( MINUTE, c.start_cut, end_cut ) AS manHour," + "p.is_cut,p.crystal_type,l.user_real_name,p.sequence +1 as crystal_id,r.line_num,w.start_time as start_cut,w.end_time as " + "end_cut,w.in_user_name as cut_up_operator,w.out_user_name as cut_operator FROM interface_eap_cut_craft c " + "LEFT JOIN  production_painting_line r ON r.crystal_no = c.round_SN LEFT JOIN production_painting_line_detail" + " p ON r.id = p.operate_draw_id LEFT JOIN factory_class_monitor_model_log l ON c.shift = l.team_name LEFT JOIN " + "produce_wip_storage w on c.round_SN = w.processing_order_code WHERE w.process_name = '截断' and (p.crystal_type = ?1  OR ?1 IS NULL)" + "AND c.start_cut between ?2 and ?3 AND c.end_cut between ?4 AND ?5 and c.round_SN LIKE ?6 and c.equip_no LIKE ?7" + " ORDER BY c.id DESC", countQuery = "SELECT count(1) FROM interface_eap_cut_craft c LEFT JOIN  production_painting_line r ON " + "r.crystal_no = c.round_SN LEFT JOIN production_painting_line_detail p ON r.id = p.operate_draw_id " + "LEFT JOIN factory_class_monitor_model_log l ON c.shift = l.team_name LEFT JOIN produce_wip_storage " + "w on c.round_SN = w.processing_order_code WHERE w.process_name = '截断'and c.start_cut between ?2 and ?3 AND c.end_cut between ?4 " + "AND ?5 and c.round_SN LIKE ?6 and c.equip_no LIKE ?7", nativeQuery = true)
    @Query(value = "SELECT c.round_SN,c.round_length,c.equip_no,c.start_cut,c.end_cut,c.cut_up_operator,c.cut_operator,TIMESTAMPDIFF( MINUTE, c.start_cut, end_cut ) AS man_hour,c.is_cut,c.crystal_type,c.knife_num,c.line_num,c.slice_num,c.user_real_name,c.theory_weight,c.cause,c.normal_num,c.reverse_num,created_time, plc.wind_up,plc.side_length_range, plc.closure_identification, o.aufnr orderNo FROM interface_eap_cut_craft c \n" +
            "LEFT JOIN production_long_crystal plc ON plc.crystal_no = LEFT(c.round_sn, 12)  \n" +
            "LEFT JOIN plan_fb_production_schedule s ON c.equip_no = s.device_no LEFT JOIN plan_production_order o ON s.fb_production_order_id = o.id\n" +
            "WHERE c.start_cut between ?1 and ?2 AND c.end_cut between ?3 AND ?4 and c.round_SN LIKE ?5 and c.equip_no LIKE ?6 and c.crystal_type LIKE ?7 \n" +
            "AND c.start_cut BETWEEN o.gstrp AND o.gltrp ORDER BY c.id DESC", countQuery = "SELECT count(1) FROM interface_eap_cut_craft c \n" +
            "LEFT JOIN production_long_crystal plc ON plc.crystal_no = LEFT(c.round_sn, 12)  \n" +
            "LEFT JOIN plan_fb_production_schedule s ON c.equip_no = s.device_no LEFT JOIN plan_production_order o ON s.fb_production_order_id = o.id\n" +
            "WHERE c.start_cut between ?1 and ?2 AND c.end_cut between ?3 AND ?4 and c.round_SN LIKE ?5 and c.equip_no LIKE ?6 and c.crystal_type LIKE ?7 \n" +
            "AND c.start_cut BETWEEN o.gstrp AND o.gltrp ORDER BY c.id DESC", nativeQuery = true)
    Page<Map<String, Object>> getPageList(String search_gte_startCut, String search_lt_startCut, String search_gte_endCut, String search_lt_endCut, String type, String searchGteStartCut,String crystalType, Pageable pageable);

    @Query(value = "SELECT * from interface_eap_cut_craft WHERE round_SN=?1 ", nativeQuery = true)
    EapCutCraft getCutCraftByRoundSn(String roundSn);

    @Query(value = "SELECT  count(1) as line_num,sum(case when p.type=0 then 1 else 0 end) as knife_num,sum(case when p.type=1 then 1 else 0 end) as slice_num,1 as is_cut,r.valid_length FROM production_painting_line r JOIN production_painting_line_detail p ON r.id = p.operate_draw_id WHERE r.crystal_no = ?1", nativeQuery = true)
    Map<String,Object> getInfo(String round_SN);

    @Query(value = "SELECT * FROM interface_eap_cut_craft c where c.id >=?1 and c.id <=?2 order by id desc", nativeQuery = true)
    List<EapCutCraft> findlist(Long staId,Long endId);

    /**
     * 查最新的一条加工记录
     *
     * @param roundSn 晶段编号
     * @return
     */
    EapCutCraft findTopByRoundSnOrderByIdDesc(String roundSn);

    /**
     * 查最新的一条加工记录
     *
     * @param roundSn 晶段编号
     * @return
     */
    EapCutCraft findTopByRoundSnAndIsAutoOrderByIdDesc(String roundSn, Integer isAuto);

    @Query(value = "SELECT b.*,t.machine_no,t.type,t.life,t.created_time as replaceTime from interface_eap_gold_break b " + "LEFT JOIN interface_eap_ingredients_times t on b.equip_no = t.machine_no WHERE (b.equip_type = ?1  OR ?1 IS NULL)" + "AND t.created_time between ?2 and ?3 and b.equip_no LIKE ?4 and b.sn LIKE ?5" + " ORDER BY b.id DESC", countQuery = "SELECT count(1) from interface_eap_gold_break b " + "LEFT JOIN interface_eap_ingredients_times t on b.equip_no = t.machine_no WHERE (b.equip_type = ?1  OR ?1 IS NULL)" + "AND t.created_time between ?2 and ?3 and b.equip_no LIKE ?4 and b.sn LIKE ?5", nativeQuery = true)
    Page<Map<String, Object>> getGoldBreakListPage(String search_eq_equipType, String search_lt_startTime, String search_lt_endTime, String s, String s1, Pageable pageable);

    @Query(value = "SELECT f.machine_name,f.return_reason_code,f.head_dia1,f.tail_dia1,f.head_dia2,f.tail_dia2,f.head_dia3,f.tail_dia3,f.head_dia4,f.tail_dia4,f.head_deviation13,f.tail_deviation13,f.head_deviation24,f.tail_deviation24,f.created_time,r.sn as lot_no,r.spec,r.pn_type,r.side_len,r.product_rank,r.split_joint,r.is_assign,r.equip_no as equip,r.trimming_man as operator,r.start_trimming,r.end_trimming,r.trimming_hours,r.tension,r.s_linear_speed,r.s_cutting_speed,r.s_trimming_flow,r.actual_qty,r.created_time AS r_created_time,g.created_time as u_createdTime FROM (SELECT * FROM interface_eap_evolution_record GROUP BY sn) r LEFT JOIN interface_eap_retreat_feedback f ON r.sn = f.lot_no LEFT JOIN interface_eap_evolution_blanking g on g.sn = r.sn WHERE r.start_trimming between ?1 and ?2 AND r.end_trimming between ?3 AND ?4 and r.equip_no LIKE ?5 and r.sn LIKE ?6 AND r.equip_no = g.equip_no ORDER BY r.id DESC", countQuery = "SELECT count(1) FROM  (SELECT * FROM interface_eap_evolution_record GROUP BY sn) r LEFT JOIN interface_eap_retreat_feedback f ON r.sn = f.lot_no LEFT JOIN interface_eap_evolution_blanking g on g.sn = r.sn  WHERE r.start_trimming between ?1 and ?2 AND r.end_trimming between ?3 AND ?4 and r.equip_no LIKE ?5 and r.sn LIKE ?6 AND r.equip_no = g.equip_no", nativeQuery = true)
    Page<Map<String, Object>> specificationListPage(String search_gte_startTrimming, String search_lt_startTrimming, String search_gte_endTrimming, String search_lt_endTrimming, String search_like_equip, String search_like_lotNo, Pageable pageable);

    @Query(value = "SELECT a.id hid,a.sn lotNo,a.equip_no equip,b.created_time startTrimming,a.created_time endTrimming ,d.actual_length length,d.theory_weight weight,d.product_level productRank, o.aufnr orderNo FROM interface_eap_evolution_blanking a LEFT JOIN interface_eap_evolution_rack b ON a.sn = b.sn\n" +
            "LEFT JOIN plan_fb_production_schedule s ON a.equip_no = s.device_no LEFT JOIN plan_production_order o ON s.fb_production_order_id = o.id\n" +
            "LEFT JOIN yb_inspect_data d ON a.sn = d.section_no WHERE a.created_time > ?1 AND a.created_time < ?2 AND a.sn LIKE ?3 AND a.equip_no LIKE?4\n" +
            "AND b.created_time BETWEEN o.gstrp AND o.gltrp order by b.created_time desc"
            ,countQuery = "SELECT count(a.id) FROM interface_eap_evolution_blanking a LEFT JOIN interface_eap_evolution_rack b ON a.sn = b.sn\n" +
            "LEFT JOIN plan_fb_production_schedule s ON a.equip_no = s.device_no LEFT JOIN plan_production_order o ON s.fb_production_order_id = o.id\n" +
            "LEFT JOIN yb_inspect_data d ON a.sn = d.section_no WHERE a.created_time > ?1 AND a.created_time < ?2 AND a.sn LIKE ?3 AND a.equip_no LIKE?4\n" +
            "AND b.created_time BETWEEN o.gstrp AND o.gltrp"
            ,nativeQuery = true)
    Page<Map<String, Object>> specificationListPageV2(String search_gte_startTrimming, String search_lt_startTrimming, String search_like_lotNo, String search_like_equip, Pageable pageable);


    @Query(value = "SELECT a.*,b.created_time as startKfTime  FROM interface_eap_grinding_blanking  a left join ( select  sn,equip_no,max(created_time) as created_time from  interface_eap_evolution_rack GROUP BY sn,equip_no )b on a.equip_no=b.equip_no and a.sn=b.sn where  a.equip_no like ?1 and a.sn like ?2  ", countQuery = "SELECT count(1) FROM interface_eap_grinding_blanking  a left join ( select  sn,equip_no,max(created_time) as created_time from  interface_eap_evolution_rack GROUP BY sn,equip_no )b on a.equip_no=b.equip_no and a.sn=b.sn where  a.equip_no like ?1 and a.sn like ?2 ",nativeQuery = true)
    Page<Map<String, Object>> grindingReportListPage(String no, String sn, Pageable pageable);

    @Query(value = "SELECT a.*, b.created_time AS startKfTime,o.aufnr as orderNo FROM interface_eap_grinding_blanking a LEFT JOIN ( SELECT sn, equip_no, max( created_time ) AS created_time FROM interface_eap_evolution_rack GROUP BY sn, equip_no ) b ON a.sn = b.sn \n" +
            "LEFT JOIN interface_eap_evolution_blanking g ON g.sn = a.sn\n" +
            "LEFT JOIN plan_fb_production_schedule s ON g.equip_no = s.device_no LEFT JOIN plan_production_order o ON s.fb_production_order_id = o.id\n" +
            "WHERE a.equip_no LIKE ?1 AND a.sn LIKE ?2 AND a.created_time > ?3 AND a.created_time < ?4 AND a.created_time BETWEEN o.gstrp AND o.gltrp"
            ,countQuery = "SELECT count(1) FROM interface_eap_grinding_blanking a LEFT JOIN ( SELECT sn, equip_no, max( created_time ) AS created_time FROM interface_eap_evolution_rack GROUP BY sn, equip_no ) b ON a.sn = b.sn \n" +
            "LEFT JOIN interface_eap_evolution_blanking g ON g.sn = a.sn\n" +
            "LEFT JOIN plan_fb_production_schedule s ON g.equip_no = s.device_no LEFT JOIN plan_production_order o ON s.fb_production_order_id = o.id\n" +
            "WHERE a.equip_no LIKE ?1 AND a.sn LIKE ?2 AND a.created_time > ?3 AND a.created_time < ?4 AND a.created_time BETWEEN o.gstrp AND o.gltrp"
            ,nativeQuery = true)
    Page<Map<String, Object>> grindingReportListPageV2(String no, String sn,String startTime,String endTime, Pageable pageable);


    @Query(value = "SELECT c.round_SN,o.valid_length as round_length,c.equip_no,c.start_cut,c.end_cut,c.cut_up_operator,c.cut_operator,TIMESTAMPDIFF( MINUTE, c.start_cut, end_cut ) AS manHour,o.is_cut,o.crystal_type,o.knifeNum,o.line_num,d.sliceNum,l.user_real_name FROM interface_eap_cut_craft c LEFT JOIN factory_class_monitor_model_log l ON c.shift = l.team_name LEFT JOIN (SELECT p.is_cut,p.crystal_type,r.valid_length,r.line_num,r.crystal_no,COUNT( r.crystal_no ) AS knifeNum FROM production_painting_line r LEFT JOIN production_painting_line_detail p ON r.id = p.operate_draw_id WHERE p.type IN ( 0, 1, 4 ) GROUP BY r.crystal_no) o ON o.crystal_no = c.round_SN LEFT JOIN (SELECT COUNT( a.crystal_no ) AS sliceNum,a.crystal_no FROM production_painting_line a LEFT JOIN production_painting_line_detail s ON a.id = s.operate_draw_id WHERE s.type = 1 GROUP BY a.crystal_no) d on d.crystal_no = c.round_SN WHERE c.start_cut between ?1 and ?2 AND c.end_cut between ?3 AND ?4 and c.round_SN LIKE ?5 and c.equip_no LIKE ?6" + " ORDER BY c.id DESC", countQuery = "SELECT count(1) from interface_eap_cut_craft c LEFT JOIN factory_class_monitor_model_log l ON c.shift = l.team_name LEFT JOIN (SELECT p.is_cut,p.crystal_type,r.valid_length,r.line_num,r.crystal_no,COUNT( r.crystal_no ) AS knifeNum FROM production_painting_line r LEFT JOIN production_painting_line_detail p ON r.id = p.operate_draw_id WHERE p.type IN ( 0, 1, 4 ) GROUP BY r.crystal_no) o ON o.crystal_no = c.round_SN LEFT JOIN (SELECT COUNT( a.crystal_no ) AS sliceNum,a.crystal_no FROM production_painting_line a LEFT JOIN production_painting_line_detail s ON a.id = s.operate_draw_id WHERE s.type = 1 GROUP BY a.crystal_no) d on d.crystal_no = c.round_SN WHERE c.start_cut between ?1 and ?2 AND c.end_cut between ?3 AND ?4 and c.round_SN LIKE ?5 and c.equip_no LIKE ?6", nativeQuery = true)
    List<Map> getCutRecordList(String search_gte_startCut, String search_lt_startCut, String search_gte_endCut, String search_lt_endCut, String s, String s1);

    @Query(value = "SELECT * FROM interface_eap_evolution_record c where c.id >=?1 and c.id <=?2  and spec is null", nativeQuery = true)
    List<EapEvolutionRecord> findEapEvolutionRecordlist(Long staId, Long endId);

    @Query(value = "SELECT * FROM interface_eap_grinding_machine c where c.id >=?1 and c.id <=?2  and spec is null", nativeQuery = true)
    List<EapGrindingMachine> findEapGrindingMachinelist(Long staId, Long endId);
}
