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

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

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

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

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

    @Query(value = "select * from production_again_task where equipment_code = ?1 and lot_no=?2 and (raw_status=0 or raw_status=1 or raw_status=3 or raw_status=4) ORDER BY id desc limit 1", nativeQuery = true)
    AgainTask getByEquipmentLastTask(String equimentCode, String lotNo);

    @Query(value = "select * from production_again_task where lot_no=?2 and again_num < ?1 and raw_status = 4 ORDER BY again_num desc limit 1", nativeQuery = true)
    AgainTask getByLotNoAndAgainNum(Integer againNum, String lotNo);

    List<AgainTask> findByLotNo(String lotNo);

    List<AgainTask> findByLotNoAndAgainNum(String lotNo, Integer againNum);

    @Query(value = "select * from production_again_task where equipment_code = ?1 and lot_no=?2 and raw_type=?3 ORDER BY id desc limit 1", nativeQuery = true)
    AgainTask getByEquipmentAndStateLastTask(String equimentCode, String lotNo, Integer rawStatus);

    @Query(value = "select * from production_again_task where equipment_code = ?1 and lot_no=?2 and (raw_type=2 or raw_type=5) ORDER BY id desc limit 1", nativeQuery = true)
    AgainTask getByRepeatOpenTask(String equipmentCode, String lotNo);

    @Query(value = "select * from production_again_task where lot_no = ?1 and again_num = ?2 and (raw_status=3 or raw_status=4) ", nativeQuery = true)
    List<AgainTask> findSuccessByLotNoAndAgainNum(String lotNo, Integer againNum);

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

    @Query(value = "select * from production_again_task where equipment_code = ?1 and again_num = ?2 and (raw_status=3 or raw_status=4) ", nativeQuery = true)
    List<AgainTask> findSuccessByEquipmentCodeAndAgainNum(String equipmentCode, Integer againNum);

    AgainTask findByEquipmentCode(String code);

    @Query(value = "select * from production_again_task where lot_no = ?1 and (raw_status=3 or raw_status=4)", nativeQuery = true)
    List<AgainTask> findSuccessByLotNo(String lotNo);

    @Query(value = "SELECT pifd.weight ,pifd.product_code from production_input_furnace_detail pifd  where pifd.again_task_detail_id = (SELECT patd.id FROM production_again_task pak\n" +
            "\tLEFT JOIN production_again_task_detail patd ON pak.id = patd.again_task_id " +
            "WHERE ( pak.raw_status = 3 OR pak.raw_status = 4 ) \n" +
            "\tAND pak.lot_no = ?1 \n" +
            "\tAND pak.product_code = ?2 ORDER BY patd.id desc limit 1)", nativeQuery = true)
    List<InputFurnaceDetail> findByLotNoAndProductCode(String lotNo, String code);


    @Query(value = "select * from production_again_task where lot_no = ?1 and (raw_status=3 or raw_status=4) order by again_num desc limit 1 ", nativeQuery = true)
    AgainTask findCompletedTopByLotNo(String lotNo);

    @Query(value = "select * from production_again_task where equipment_code = ?1 and (raw_status=3 or raw_status=4) order by lot_no desc, again_num desc limit 1 ", nativeQuery = true)
    AgainTask findCompletedTopByEquipmentCode(String equipmentCode);

    @Query(value = "select * from production_again_task where lot_no = ?1 and again_num = ?2 and (raw_status=3 or raw_status=4) order by id desc limit 1 ", nativeQuery = true)
    AgainTask findCompletedByLotNoAndAgainNum(String lotNo, Integer againNum);


    /**
     * 氧监控推送
     *
     * @return
     */
    @Query(value = "SELECT\n" +
            "\tyid.again_num,\n" +
            "\tAVG( yid.oxygen_content_head ) AS oxygen_content_head,\n" +
            "\tie.series,\n" +
            "\tm.`name` \n" +
            "FROM\n" +
            "\tjinko_mes.`yb_inspect_data` yid\n" +
            "\tLEFT JOIN jinko_mes.init_equipment ie ON yid.device_no = ie.`code`\n" +
            "\tLEFT JOIN jinko_mes.factory_model m ON ie.series = m.CODE \n" +
            "WHERE\n" +
            "\tyid.checkout_name = '一次检验' AND yid.update_time > ?1 AND yid.update_time < ?2\n" +
            "GROUP BY\n" +
            "\tagain_num", nativeQuery = true)
    List<Map<String, Object>> oxygenMonitor(String startTime, String endTime);

    // 根据炉次号
    @Query(value = "select * from production_again_task where lot_no = ?1 and again_num = ?2 and raw_status not in(2, 5) ", nativeQuery = true)
    List<AgainTask> findNormalByLotNoAndAgainNum(String lotNo, Integer againNum);
}
