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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.fb.turnstore.entity.RawMaterialReceivingDetail;
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 RawMaterialReceivingDetailDao extends BaseDao<RawMaterialReceivingDetail, Integer> {

    List<RawMaterialReceivingDetail> findByTrayNo(String trayNo);

    List<RawMaterialReceivingDetail> findByTrayNoAndState(String trayNo,int state);

    @Query(value = "SELECT mrd.* FROM fb_raw_material_receiving_detail mrd left join fb_raw_material_receiving mr on mr.tray_no = mrd.tray_no where mrd.state = ?2 and mr.has_gone = '粘方棒' and mrd.section_no = ?1 order by mrd.id desc ", nativeQuery = true)
    List<RawMaterialReceivingDetail> findBySectionNoAndState(String sectionNo, int state);

    //@Query(value = "select * from (select   frird.*, frir.out_order_no,frir.user,frir.apply_time as time from  fb_recheck_inspect_receive_detail frird left join fb_recheck_inspect_receive frir  on frird.recheck_receive_id = frir.id where frird.return_remark='综计退库' and frir.state = 1 UNION select ztii.id as id,0 as recheck_receive_id,'' as tray_no,ztii.section_no as crystal_no,cod.yb_level as yb_product_level,cod.product_level_fb as fb_product_level,ztii.product_level_experiment as exper_grade,cod.specification as specs,ztii.instore_length as length,ztii.instore_weight as weight,ztii.actual_weight as theory_weight,cod.conduction_type as conductive_type,cod.collapse_loss as collapse_loss,cod.length_slope as length_slope,cod.damage_grey_sum as crystal_grayness,cod.damage_grey_sum as fb_crystal_grayness,cod.line_scratch as line_scratch,cod.knife_scratch as scratch,cod.no_friction as no_friction,cod.size_exception as size_exception,cod.square_rod_remark as square_rod_remark,cod.checkout_name as checkout_name,'正常' as input_type,zti.type as return_remark,ztii.withdrawing_type as quit_type,zti.time as confirm_time,zti.confirm_user as confirm_user,1 as state,zti.order_no as out_order_no,zti.user as user,zti.time as time from  zhxx_turnstore_in_item ztii left join zhxx_turnstore_in zti on ztii.in_id = zti.id left join zhxx_operate_check_out_data cod on ztii.section_no = cod.fb_section_no  where  zti.return_station = '粘方棒站' and zti.state = '已确认') kk where kk.crystal_no like ?1 and kk.confirm_time >=?2 and  kk.confirm_time <=?3 order by kk.confirm_time desc\", countQuery = \"select count(1) from (select   frird.*, frir.out_order_no,frir.user,frir.apply_time as time from  fb_recheck_inspect_receive_detail frird left join fb_recheck_inspect_receive frir  on frird.recheck_receive_id = frir.id where frird.return_remark='综计退库' and frir.state = 1 UNION select ztii.id as id,0 as recheck_receive_id,'' as tray_no,ztii.section_no as crystal_no,cod.yb_level as yb_product_level,cod.product_level_fb as fb_product_level,ztii.product_level_experiment as exper_grade,cod.specification as specs,ztii.instore_length as length,ztii.instore_weight as weight,ztii.actual_weight as theory_weight,cod.conduction_type as conductive_type,cod.collapse_loss as collapse_loss,cod.length_slope as length_slope,cod.damage_grey_sum as crystal_grayness,cod.damage_grey_sum as fb_crystal_grayness,cod.line_scratch as line_scratch,cod.knife_scratch as scratch,cod.no_friction as no_friction,cod.size_exception as size_exception,cod.square_rod_remark as square_rod_remark,cod.checkout_name as checkout_name,'正常' as input_type,zti.type as return_remark,ztii.withdrawing_type as quit_type,zti.time as confirm_time,zti.confirm_user as confirm_user,1 as state,zti.order_no as out_order_no,zti.user as user,zti.time as time from  zhxx_turnstore_in_item ztii left join zhxx_turnstore_in zti on ztii.in_id = zti.id left join zhxx_operate_check_out_data cod on ztii.section_no = cod.fb_section_no  where  zti.return_station = '粘方棒站' and zti.state = '已确认') kk where kk.crystal_no like ?1 and kk.confirm_time >=?2 and  kk.confirm_time <=?3 order by kk.confirm_time desc", nativeQuery = true)
    @Query(value = "select * from " +
            "(select frird.id,frird.recheck_receive_id,frird.tray_no,frird.crystal_no,frird.yb_product_level,frird.fb_product_level,frird.exper_grade,frird.specs,frird.length,frird.weight," +
            "frird.theory_weight,frird.conductive_type,frird.collapse_loss,frird.length_slope,frird.crystal_grayness,frird.fb_crystal_grayness,frird.line_scratch,frird.scratch," +
            "frird.no_friction,frird.size_exception,frird.square_rod_remark,frird.checkout_name,frird.input_type,frird.return_remark,frird.quit_type,frird.confirm_time," +
            "frird.confirm_user,frird.state," +
            "frir.out_order_no,frir.user,frir.apply_time as time " +
            "from  fb_recheck_inspect_receive_detail frird left join fb_recheck_inspect_receive frir  on frird.recheck_receive_id = frir.id " +
            "where frird.return_remark='综计退库' and frir.state = 1 " +
            "UNION " +
            "select ztii.id as id,0 as recheck_receive_id,'' as tray_no,ztii.section_no as crystal_no,cod.yb_level as yb_product_level," +
            "cod.product_level_fb as fb_product_level,ztii.product_level_experiment as exper_grade,cod.specification as specs," +
            "ztii.instore_length as length,ztii.instore_weight as weight,ztii.actual_weight as theory_weight,cod.conduction_type as conductive_type," +
            "cod.collapse_loss as collapse_loss,cod.length_slope as length_slope,cod.damage_grey_sum as crystal_grayness," +
            "cod.damage_grey_sum as fb_crystal_grayness,cod.line_scratch as line_scratch,cod.knife_scratch as scratch," +
            "cod.no_friction as no_friction,cod.size_exception as size_exception,cod.square_rod_remark as square_rod_remark," +
            "cod.checkout_name as checkout_name,'正常' as input_type,zti.type as return_remark,ztii.withdrawing_type as quit_type," +
            "zti.time as confirm_time,zti.confirm_user as confirm_user,1 as state,zti.order_no as out_order_no,zti.user as user," +
            "zti.time as time " +
            "from  zhxx_turnstore_in_item ztii " +
            "left join zhxx_turnstore_in zti on ztii.in_id = zti.id " +
            "left join zhxx_operate_check_out_data cod on ztii.section_no = cod.fb_section_no  " +
            "where  zti.return_station = '粘方棒站' and zti.state = '已确认') kk " +
            "where (kk.crystal_no = ?1 or ?1 is null) and (kk.confirm_time >=?2 or ?2 is null) " +
            "and  (kk.confirm_time <=?3 or ?3 is null) and (kk.quit_type =?4 or ?4 is null) and (kk.state =?5 or ?5 is null)  " +
            "and (kk.time >=?6 or ?6 is null) and  (kk.time <=?7 or ?7 is null)   and  (kk.out_order_no = ?8 or ?8 is null) " +
            "order by kk.confirm_time desc",
            countQuery = "select count(1) from  \n" +
                    "(select frird.id,frird.recheck_receive_id,frird.tray_no,frird.crystal_no,frird.yb_product_level,frird.fb_product_level,frird.exper_grade,frird.specs,frird.length,frird.weight, \n" +
                    "frird.theory_weight,frird.conductive_type,frird.collapse_loss,frird.length_slope,frird.crystal_grayness,frird.fb_crystal_grayness,frird.line_scratch,frird.scratch, \n" +
                    "frird.no_friction,frird.size_exception,frird.square_rod_remark,frird.checkout_name,frird.input_type,frird.return_remark,frird.quit_type,frird.confirm_time, \n" +
                    "frird.confirm_user,frird.state, \n" +
                    "frir.out_order_no,frir.user,frir.apply_time as time  \n" +
                    "from  fb_recheck_inspect_receive_detail frird left join fb_recheck_inspect_receive frir  on frird.recheck_receive_id = frir.id  \n" +
                    "where frird.return_remark='综计退库' and frir.state = 1  \n" +
                    "UNION  \n" +
                    "select ztii.id as id,0 as recheck_receive_id,'' as tray_no,ztii.section_no as crystal_no,cod.yb_level as yb_product_level, \n" +
                    "cod.product_level_fb as fb_product_level,ztii.product_level_experiment as exper_grade,cod.specification as specs, \n" +
                    "ztii.instore_length as length,ztii.instore_weight as weight,ztii.actual_weight as theory_weight,cod.conduction_type as conductive_type, \n" +
                    "cod.collapse_loss as collapse_loss,cod.length_slope as length_slope,cod.damage_grey_sum as crystal_grayness, \n" +
                    "cod.damage_grey_sum as fb_crystal_grayness,cod.line_scratch as line_scratch,cod.knife_scratch as scratch, \n" +
                    "cod.no_friction as no_friction,cod.size_exception as size_exception,cod.square_rod_remark as square_rod_remark, \n" +
                    "cod.checkout_name as checkout_name,'正常' as input_type,zti.type as return_remark,ztii.withdrawing_type as quit_type, \n" +
                    "zti.time as confirm_time,zti.confirm_user as confirm_user,1 as state,zti.order_no as out_order_no,zti.user as user, \n" +
                    "zti.time as time  \n" +
                    "from  zhxx_turnstore_in_item ztii  \n" +
                    "left join zhxx_turnstore_in zti on ztii.in_id = zti.id  \n" +
                    "left join zhxx_operate_check_out_data cod on ztii.section_no = cod.fb_section_no   \n" +
                    "where  zti.return_station = '粘方棒站' and zti.state = '已确认') kk  \n" +
                    "where (kk.crystal_no = ?1 or ?1 is null) and (kk.confirm_time >=?2 or ?2 is null)  \n" +
                    "and  (kk.confirm_time <=?3 or ?3 is null) and (kk.quit_type =?4 or ?4 is null) and (kk.state =?5 or ?5 is null)   \n" +
                    "and (kk.time >=?6 or ?6 is null) and  (kk.time <=?7 or ?7 is null)   and  (kk.out_order_no = ?8 or ?8 is null)  \n" +
                    "order by kk.confirm_time desc", nativeQuery = true)
    Page<Map<String, Object>> getPageListInfo(String search_EQ_crystalNo, String search_GTE_confirmTime, String search_LT_confirmTime,
                                              String search_EQ_quitType, String search_EQ_state, String search_GTE_time,
                                              String search_LT_time,String search_EQ_outOrderNo,
                                              Pageable pageable);

}
