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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.pc.entity.BatchInfo;
import org.springframework.data.jpa.repository.Query;

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

public interface BatchInfoDAO extends BaseDao<BatchInfo, Long> {

    @Query(value = "SELECT lot_no FROM zhxx_data_batch_info WHERE device_no=(?1) ORDER BY start_time LIMIT ?2", nativeQuery = true)
    List<String> findLotNoByDeviceNoLimit(String deviceNo, Integer count);

    @Query(value = "SELECT * FROM zhxx_data_batch_info WHERE device_no=?1 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    BatchInfo findLotNoByDeviceNoDescOne(String deviceNo);

    @Query(value = "SELECT * FROM zhxx_data_batch_info WHERE lot_no=?1 ", nativeQuery = true)
    List<BatchInfo> findByLotNo(String lotNo);

    @Query(value = "SELECT COUNT(1) FROM zhxx_data_batch_info WHERE lot_no=?1 ", nativeQuery = true)
    int countByLotNo(String lotNo);

    @Query(value = "SELECT COUNT(1) FROM zhxx_data_batch_info WHERE device_no=?1 AND batch_date=?2", nativeQuery = true)
    int countByDeviceNoAndBatchDate(String deviceNo, String batchDate);

    @Query(value = "SELECT IFNULL(MAX(times),0) FROM zhxx_data_batch_info WHERE device_no=?1 AND batch_date=?2", nativeQuery = true)
    int getTimesByDeviceNoAndBatchDate(String deviceNo, String batchDate);

    @Query(value = "SELECT MAX(lot_no) FROM zhxx_data_batch_info WHERE device_no=?1 ", nativeQuery = true)
    String getCurrentLotNo(String deviceNo);


    @Query(value = "select *,a.zzWeight+a.ttzzWeight+a.ttWeight+a.jdflWeight+a.gdlWeight+a.mglWeight+a.tWeight+a.wWeight as 'hslWeight',\n" +
            "zzpWeight+bcpWeight as 'sumOutput'\n" +
            "from (\n" +
            "select batch.lot_no lotNo,batch.product_model_name productModelName,\n" +
            "dl.start_time startTime,\n" +
            "ifnull(crystal.weight,0) weight,\n" +
            "ifnull(zzCount,0) zzCount,\n" +
            "ifnull(zzWeight,0) zzWeight,\n" +
            "ifnull(ttzzWeight,0) ttzzWeight,\n" +
            "ifnull(ttWeight,0) ttWeight,\n" +
            "ifnull(jdflWeight,0) jdflWeight,\n" +
            "ifnull(tWeight,0) tWeight,\n" +
            "ifnull(wWeight,0) wWeight,\n" +
            "ifnull(pot.gdlWeight,0) gdlWeight,\n" +
            "ifnull(pot.mglWeight,0) mglWeight,\n" +
            "ifnull(draw.pieceCount,0) pieceCount,\n" +
            "ifnull(inspect.bcpWeight,0) bcpWeight,\n" +
            "ifnull(inspect.zzpWeight,0) zzpWeight\n" +
            "from zhxx_data_batch_info batch left join zhxx_agv_charge dl\n" +
            "on batch.lot_no=dl.lot_no\n" +
            "left join\n" +
            "(select lot_no,sum(crystal_weight) weight from zhxx_data_crystal group by lot_no ) crystal \n" +
            "on batch.lot_no=crystal.lot_no\n" +
            "\n" +
            "left join\n" +
            "(select lot_no,\n" +
            "sum(case waste_type when '粘渣' then weight else 0 end ) 'zzWeight',\n" +
            "count(case waste_type when '粘渣' then 1 else null end ) 'zzCount',\n" +
            "sum(case waste_type when '提头粘渣' then weight else 0 end ) 'ttzzWeight',\n" +
            "sum(case waste_type when '提头' then weight else 0 end ) 'ttWeight',\n" +
            "sum(case waste_type when '晶段废料' then weight else 0 end ) 'jdflWeight'\n" +
            " from zhxx_operate_waste GROUP BY lot_no,waste_type) waste\n" +
            "on batch.lot_no=waste.lot_no\n" +
            "\n" +
            "left join \n" +
            "( select \n" +
            " lot_no,\n" +
            " sum(case  right(reclaim_cut_no,1) when 'T' then weight else 0 end ) 'tWeight',\n" +
            " sum(case  right(reclaim_cut_no,1) when 'W' then weight else 0 end ) 'wWeight'\n" +
            "from  zhxx_operate_reclaim_cut group by lot_no) cut\n" +
            "on batch.lot_no=cut.lot_no\n" +
            "\n" +
            "left join\n" +
            "(select lot_no,\n" +
            "sum(case scrap_type when '锅底料' then weight else 0 end ) 'gdlWeight',\n" +
            "sum(case scrap_type when '焖埚料' then weight else 0 end ) 'mglWeight'\n" +
            "from zhxx_operate_reclaim_pot GROUP BY lot_no) pot \n" +
            "on batch.lot_no=pot.lot_no\n" +
            "\n" +
            "left join \n" +
            "( select lot_no,sum(piece_count)pieceCount from zhxx_operate_drawline GROUP BY lot_no) draw\n" +
            "on batch.lot_no=draw.lot_no\n" +
            "\n" +
            "left join \n" +
            "(select lot_no,\n" +
            "sum(case section_type when '周转品' then actual_weight else 0 end ) 'zzpWeight',\n" +
            "sum(case section_type when '半成品' then actual_weight else 0 end ) 'bcpWeight'\n" +
            "from yb_inspect_data  group by lot_no) inspect \n" +
            "on batch.lot_no=inspect.lot_no\n" +
            "\n" +
            "left join\n" +
            "(select store_code,sum(current_count) weight from mobile_charge GROUP BY store_code) charge\n" +
            "on batch.lot_no=charge.store_code) a  WHERE  a.lotNo LIKE concat('%',?1,'%')  and  a.productModelName LIKE concat('%',?2,'%') " +
            " LIMIT ?3,?4 \n" , nativeQuery = true)
    List<Map<String,Object>>  materialBalance(String lotNo, String productModelName, Integer start, Integer row);

    @Query(value = "select count(*)count \n" +
            "from zhxx_data_batch_info batch left join zhxx_agv_charge dl\n" +
            "on batch.lot_no=dl.lot_no where batch.lot_no LIKE concat('%',?1,'%') AND batch.product_model_name LIKE concat('%',?2,'%') ", nativeQuery = true)
    List<Map<String,Object>>  countMaterialBalance(String lotNo, String productModelName);

    @Query(value = "select *,a.zzWeight+a.ttzzWeight+a.ttWeight+a.jdflWeight+a.gdlWeight+a.mglWeight+a.tWeight+a.wWeight as 'hslWeight',\n" +
            "zzpWeight+bcpWeight as 'sumOutput'\n" +
            "from (\n" +
            "select batch.lot_no lotNo,batch.product_model_name productModelName,\n" +
            "dl.start_time startTime,\n" +
            "ifnull(crystal.weight,0) weight,\n" +
            "ifnull(zzCount,0) zzCount,\n" +
            "ifnull(zzWeight,0) zzWeight,\n" +
            "ifnull(ttzzWeight,0) ttzzWeight,\n" +
            "ifnull(ttWeight,0) ttWeight,\n" +
            "ifnull(jdflWeight,0) jdflWeight,\n" +
            "ifnull(tWeight,0) tWeight,\n" +
            "ifnull(wWeight,0) wWeight,\n" +
            "ifnull(pot.gdlWeight,0) gdlWeight,\n" +
            "ifnull(pot.mglWeight,0) mglWeight,\n" +
            "ifnull(draw.pieceCount,0) pieceCount,\n" +
            "ifnull(inspect.bcpWeight,0) bcpWeight,\n" +
            "ifnull(inspect.zzpWeight,0) zzpWeight\n" +
            "from zhxx_data_batch_info batch left join zhxx_agv_charge dl\n" +
            "on batch.lot_no=dl.lot_no\n" +
            "left join\n" +
            "(select lot_no,sum(crystal_weight) weight from zhxx_data_crystal group by lot_no ) crystal \n" +
            "on batch.lot_no=crystal.lot_no\n" +
            "\n" +
            "left join\n" +
            "(select lot_no,\n" +
            "sum(case waste_type when '粘渣' then weight else 0 end ) 'zzWeight',\n" +
            "count(case waste_type when '粘渣' then 1 else null end ) 'zzCount',\n" +
            "sum(case waste_type when '提头粘渣' then weight else 0 end ) 'ttzzWeight',\n" +
            "sum(case waste_type when '提头' then weight else 0 end ) 'ttWeight',\n" +
            "sum(case waste_type when '晶段废料' then weight else 0 end ) 'jdflWeight'\n" +
            " from zhxx_operate_waste GROUP BY lot_no,waste_type) waste\n" +
            "on batch.lot_no=waste.lot_no\n" +
            "\n" +
            "left join \n" +
            "( select \n" +
            " lot_no,\n" +
            " sum(case  right(reclaim_cut_no,1) when 'T' then weight else 0 end ) 'tWeight',\n" +
            " sum(case  right(reclaim_cut_no,1) when 'W' then weight else 0 end ) 'wWeight'\n" +
            "from  zhxx_operate_reclaim_cut group by lot_no) cut\n" +
            "on batch.lot_no=cut.lot_no\n" +
            "\n" +
            "left join\n" +
            "(select lot_no,\n" +
            "sum(case scrap_type when '锅底料' then weight else 0 end ) 'gdlWeight',\n" +
            "sum(case scrap_type when '焖埚料' then weight else 0 end ) 'mglWeight'\n" +
            "from zhxx_operate_reclaim_pot GROUP BY lot_no) pot \n" +
            "on batch.lot_no=pot.lot_no\n" +
            "\n" +
            "left join \n" +
            "( select lot_no,sum(piece_count)pieceCount from zhxx_operate_drawline GROUP BY lot_no) draw\n" +
            "on batch.lot_no=draw.lot_no\n" +
            "\n" +
            "left join \n" +
            "(select lot_no,\n" +
            "sum(case section_type when '周转品' then actual_weight else 0 end ) 'zzpWeight',\n" +
            "sum(case section_type when '半成品' then actual_weight else 0 end ) 'bcpWeight'\n" +
            "from yb_inspect_data  group by lot_no) inspect \n" +
            "on batch.lot_no=inspect.lot_no\n" +
            "\n" +
            "left join\n" +
            "(select store_code,sum(current_count) weight from mobile_charge GROUP BY store_code) charge\n" +
            "on batch.lot_no=charge.store_code) a  WHERE  a.lotNo LIKE concat('%',?1,'%')  and  a.productModelName LIKE concat('%',?2,'%') " +
            " AND a.startTime>=?3 AND a.startTime<?4 LIMIT ?5,?6 \n" , nativeQuery = true)
    List<Map<String,Object>>  materialBalance(String lotNo, String productModelName, String date1, String date2, Integer start, Integer row);

    @Query(value = "select count(*) count \n" +
            "from zhxx_data_batch_info batch left join zhxx_agv_charge dl\n" +
            "on batch.lot_no=dl.lot_no where batch.lot_no LIKE concat('%',?1,'%') AND batch.product_model_name LIKE concat('%',?2,'%') " +
            " AND dl.start_time>=?3 AND dl.start_time<?4  ", nativeQuery = true)
    List<Map<String,Object>>  countMaterialBalance(String lotNo, String productModelName, String date1, String date2);

    List<BatchInfo> findByUid(String uid);

    @Query(value = "SELECT b.* FROM zhxx_data_batch_info b JOIN store_output_apply a ON b.uid=a.code WHERE a.id=?1 ", nativeQuery = true)
    List<BatchInfo> findByUidJoinStoreApplyId(Long storeOutputApplyId);
}
