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

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

import com.sw.mes.yb.production.entity.BlastFurnaceDetail;
import org.springframework.data.jpa.repository.Query;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.LoadingTask;

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

    @Query(value = "select * from production_loading_task where equipment_code=?1 and lot_no = ?2 and (raw_status=3 or raw_status=4) ORDER BY id desc", nativeQuery = true)
    List<LoadingTask> getFinishTask(String equipmentCode, String lotNo);

    @Query(value = " SELECT SUM(pbfd.weight) as real_weight from production_loading_task plt LEFT JOIN production_blast_furnace_detail pbfd ON plt.id = pbfd.blast_furnace_id \n" +
            "\n" +
            "WHERE (plt.raw_status = 3 OR plt.raw_status = 4) AND pbfd.material_name not like '%坩埚%' and plt.lot_no = ?1 GROUP BY pbfd.blast_furnace_id", nativeQuery = true)
    BigDecimal getPeiLiaoTotalWeightByLotNo(String lotNo);

    List<LoadingTask> findByLotNo(String lotNo);

    @Query(value = "select * from production_loading_task where sap_no = ?1 and (raw_status=3 or raw_status=4)", nativeQuery = true)
    List<LoadingTask> findBySapNo(String sapNo);

    List<LoadingTask> findByEquipmentCode(String equipmentCode);

    @Query(value = "select * from production_loading_task where equipment_code = ?1 and lot_no=?2 and raw_status=?3 ORDER BY id desc limit 1", nativeQuery = true)
    LoadingTask getByEquipmentAndStateLastTask(String equipmentCode, String lotNo, Integer rawstatus);

    @Query(value = "select * from production_loading_task where equipment_code = ?1 and lot_no=?2 and (raw_status=3 or raw_status=4) ORDER BY id desc limit 1", nativeQuery = true)
    LoadingTask getByEquipmentAndSuccessLastTask(String equipmentCode, String lotNo);

    @Query(value = "select crucible_height from production_loading_task where equipment_code = ?1 and (raw_status=3 or raw_status=4) ORDER BY id desc limit 1", nativeQuery = true)
    String getLastOneHeight(String equipmentCode);

    @Query(value = "SELECT plt.lot_no as lotNo, pbfd.batch_no as batchNo, pbfd.factory_brand as factoryBrand FROM production_blast_furnace_detail pbfd LEFT JOIN production_loading_task plt ON pbfd.blast_furnace_id = plt.id "+
            "\n" + "WHERE  plt.lot_no = ?1 AND pbfd.mark_crucible =1  AND ( plt.raw_status = 3 OR plt.raw_status = 4 )  Limit 1 ", nativeQuery = true)
    Map<String, Object>  findCrucibleInfoByLotNo(String lotNo);

    @Query(value = "select * from production_loading_task where lot_no in ?1",nativeQuery = true)
    List<LoadingTask> findByLotNoList(List<String> lotNo);

    @Query(value = "SELECT * FROM production_loading_task WHERE (raw_status=3 OR raw_status=4) AND confirm = 0 AND lot_no = ?1 ORDER BY id DESC LIMIT 1 ",nativeQuery = true)
    LoadingTask findUnconfirmedTask(String lotNo);

    @Query(value = "select * from production_loading_task where (raw_status=3 OR raw_status=4) and lot_no = ?1 ",nativeQuery = true)
    List<LoadingTask> findRealWeightByLotNo(String lotNo);

    @Query(value = "SELECT pbfd.* FROM production_loading_task plt LEFT JOIN production_blast_furnace_detail pbfd ON plt.id = pbfd.blast_furnace_id \n" +
            "WHERE ( plt.raw_status = 3 OR plt.raw_status = 4 ) AND plt.lot_no = ?1 AND plt.product_code = ?2 \n" +
            "ORDER BY plt.id DESC LIMIT 1",nativeQuery = true)
    List<BlastFurnaceDetail> findByLotNoAndProductCode(String lotNo, String code);

    @Query(value = "select * from production_loading_task where lot_no = ?1 and (raw_status=3 or raw_status=4) limit 1",nativeQuery = true)
    LoadingTask findSuccessByLotNo(String lotNo);

    @Query(value = "SELECT \n" +
            "lt.id AS taskId, \n" +
            "CONCAT('0-', fd.id) AS detailId, \n" +
            "'初始装料' AS rawType, \n" +
            "lt.sap_no AS sapNo, \n" +
            "lt.equipment_code AS equipmentCode, \n" +
            "lt.lot_no AS lotNo, \n" +
            "0 AS againNum, \n" +
            "0 AS againPailNum, \n" +
            "'0' AS pailNum, \n" +
            "'0' AS bucketNo, \n" +
            "fd.material_code AS materialCode, \n" +
            "fd.material_name AS materialName, \n" +
            "fd.batch_no AS batchNo, \n" +
            "fd.factory_brand AS factoryBrand, \n" +
            "fd.resistance_gear AS materialGear, \n" +
            "fd.material_level AS materialLevel, \n" +
            "fd.supplies_grain AS suppliesGrain, \n" +
            "fd.weight AS weight, \n" +
            "fd.user_name AS fillUser, \n" +
            "fd.install_time AS fillTime, \n" +
            "CASE\n" +
            "\tWHEN lt.confirm = 1 THEN '是' \n" +
            "\tWHEN lt.confirm = 0 THEN '否' \n" +
            "\tELSE lt.confirm \n" +
            "END confirm, \n" +
            "lt.confirm_time AS confirmTime \n" +
            "FROM production_loading_task AS lt \n" +
            "LEFT JOIN production_blast_furnace_detail AS fd ON fd.blast_furnace_id = lt.id \n" +
            "WHERE lt.raw_status = 4 \n" +
            "AND IF(?1 is NOT NULL AND LENGTH(TRIM(?1)) > 0, lt.operation_time >= ?1, 1=1) \n" +
            "AND IF(?2 is NOT NULL AND LENGTH(TRIM(?2)) > 0, lt.operation_time <= ?2, 1=1) \n" +
            "AND IF(?3 is NOT NULL AND LENGTH(TRIM(?3)) > 0, lt.equipment_code LIKE CONCAT('%', ?3, '%'), 1=1) \n" +
            "AND IF(?4 is NOT NULL AND LENGTH(TRIM(?4)) > 0, lt.lot_no LIKE CONCAT('%', ?4, '%'), 1=1) \n",nativeQuery = true)
    List<Map<String, Object>> findTaskAndDetails(String startTime, String endTime, String equipmentCode, String lotNo);

    @Query(value = "SELECT ifnull(SUM(real_weight),0) as weight FROM `production_loading_task` WHERE sap_no = ?1 and (raw_status=3 OR raw_status=4)", nativeQuery = true)
    BigDecimal findLoadingSumWeightBySapNoAndInRawStatus(String orderNo);

    @Query(value = "select * from production_loading_task where confirm_time >= ?1 and confirm_time < ?2", nativeQuery = true)
    List<LoadingTask> findByConfirmTime(String startTime, String endTime);

}
