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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.plan.dto.ProductionOrderCountDto;
import com.sw.mes.yb.plan.entity.ProductionSchedule;
import com.sw.mes.yb.production.entity.LongCrystal;
import org.springframework.data.jpa.repository.Query;

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

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

    ProductionSchedule findByDeviceIdAndStatus(Long deviceId, Integer status);


    List<ProductionSchedule> findByStatus(Integer status);

    @Query(value = "SELECT  ppo.product_model_id productModelId,ppo.id productionOrderId,ppo.auart orderType " +
            ",ppo.machine_capacity_id AS machineCapacityId,ppo.rdonlylogo,ppo.menge demandCount " +
            ",sum( IFNULL(pps.count,0))sumCount,IFNULL(counts,0) AS counts, ppo.field4 AS workShop FROM plan_production_order ppo \n" +
            " LEFT JOIN ( SELECT pps.production_order_id,SUM(count) AS count,count(pps.production_order_id)counts FROM plan_production_schedule pps " +
            "             WHERE pps.`status` != 3 GROUP BY pps.production_order_id) AS pps ON ppo.id = pps.production_order_id \n" +
            "WHERE ppo.production_status = true AND ppo.work_type = 2 AND NOW() > ppo.gstrp AND NOW() < ppo.gltrp \n" +
            "GROUP BY ppo.id ", nativeQuery = true)
    List<Map<String, Object>> getProductionScheduleData();

    @Query(value = "SELECT ie.area,CASE WHEN pps.id is NULL THEN 0 ELSE 1 END AS type,count(1) AS counts FROM init_equipment ie \n" +
            "  LEFT JOIN init_device id ON ie.device_id = id.id \n" +
            "  LEFT JOIN (SELECT * FROM plan_production_schedule WHERE `status` = 0) pps ON  pps.device_id  =ie.id \n" +
            "  WHERE id.category = 1 GROUP BY area,CASE WHEN pps.id is NULL THEN 0 ELSE 1 END  ORDER BY area", nativeQuery = true)
    List<Map<String, Object>> getSchedulingInfo();

    ProductionSchedule findTopByDeviceIdAndStatusOrderByIdDesc(Long equipmentId, Integer status);

    List<ProductionSchedule> findByDeviceNoAndStatusInOrderByIdDesc(String equipmentCode, List<Integer> status);

    @Query(value = "SELECT * FROM plan_production_schedule WHERE  `status` = ?1", nativeQuery = true)
    List<ProductionSchedule> findByStatus1(Integer status);

    @Query(value = "SELECT * FROM plan_production_schedule WHERE  `status` = 1 AND lot_no = ?1 ORDER BY id DESC", nativeQuery = true)
    List<ProductionSchedule> findByLotNo(String lotNo);

    @Query(value = "SELECT * FROM plan_production_schedule WHERE `status` != 1 AND device_no = ?1 ORDER BY create_time DESC limit 10", nativeQuery = true)
    List<ProductionSchedule> findAllByLotNo(String lotNo);

    @Query(value = "SELECT * FROM `plan_production_schedule` WHERE lot_no = ?1 limit 1", nativeQuery = true)
    ProductionSchedule findDataByLotNo(String lotNo);

    @Query(value = "SELECT delivery_no FROM plan_production_schedule WHERE  `status` != 3  AND lot_no IS NOT NULL  AND device_no = ?1 ORDER BY id DESC limit 4", nativeQuery = true)
    List<String> findByDeviceNoAndStatus(String deviceNo);

    @Query(value = "SELECT distinct lot_no  FROM plan_production_schedule WHERE  `status` in (1,4,5)  AND lot_no IS NOT NULL  AND device_no = ?1 ORDER BY create_time DESC limit 1", nativeQuery = true)
    String queryLotNosByDeviceNo(String deviceNo);

    @Query(value = "SELECT * FROM plan_production_schedule WHERE  `status` = 1 AND delivery_no = ?1 ORDER BY id DESC", nativeQuery = true)
    List<ProductionSchedule> findByLotNoC(String lotNo);

    @Query(value = "SELECT * FROM `plan_production_schedule` WHERE production_order_id = ?1 ", nativeQuery = true)
    List<ProductionSchedule> findByOderId(Long orderId);

    @Query(value = "SELECT delivery_no FROM plan_production_schedule WHERE device_no = ?1 AND lot_no IS NOT null AND lot_no != '' ORDER BY id DESC LIMIT 3", nativeQuery = true)
    List<String> findTop3LotNoByEquipmentCode(String equipmentCode);

    @Query(value = "SELECT * FROM `plan_production_schedule` WHERE device_id = ?1 AND `status` IN ?2 ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
    ProductionSchedule findByDeviceAndStatusList(Long deviceId, List<Integer> statusList);

    @Query(value = "select * from plan_production_schedule where device_no = ?1 and lot_no IS NOT null AND lot_no != '' ORDER BY create_time DESC LIMIT 1 ", nativeQuery = true)
    ProductionSchedule findByEquipmentCode(String code);

    @Query(value = "SELECT * FROM `plan_production_schedule` WHERE device_no = ?1 AND `status` IN ?2 ORDER BY create_time DESC LIMIT 1 ", nativeQuery = true)
    ProductionSchedule findDataByDeviceNo(String deviceNo,List<Integer> status);
    @Query(value = "SELECT * FROM `plan_production_schedule` WHERE device_no = ?1 AND status in (1,2,5) ORDER BY create_time DESC LIMIT 1 ", nativeQuery = true)
    ProductionSchedule findByDeviceNo(String furnaceCode);

    @Query(value = "SELECT * FROM plan_production_schedule WHERE  `status` != 3 AND production_order_id = ?1 ", nativeQuery = true)
    List<ProductionSchedule> findNormalListByOrderId(Long orderId);
    @Query(value = "SELECT * FROM plan_production_schedule WHERE id = ?1", nativeQuery = true)
    ProductionSchedule findPlanProductionById(Long id);

    @Query(value = "select * from plan_production_schedule where device_no = ?1 and status =?2 and lot_no IS NOT null AND lot_no != '' ORDER BY create_time DESC LIMIT 1 ", nativeQuery = true)
    ProductionSchedule findCloseByEquipmentCode(String code, int status);
}
