package com.sw.mes.plan.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.plan.entity.FbProductionSchedule;
import org.springframework.data.jpa.repository.Query;

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

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface FbProductionScheduleDao extends BaseDao<FbProductionSchedule, Integer> {

    FbProductionSchedule findByDeviceIdAndStatus(Integer deviceId, Integer status);

    FbProductionSchedule findTopByDeviceNoAndStatusOrderByIdDesc(String deviceNo, Integer status);

    List<FbProductionSchedule> findByStatus(Integer status);

    // 更具状态及设备类别查询排程
    List<FbProductionSchedule> findByStatusAndCategory(Integer status, Integer category);

    @Query(value = "SELECT  ppo.resistance_type AS conductionType, ppo.field3 AS specification \n" +
            ",fpm.product_level AS productRank, fpm.dopant, fpm.side_length AS sideLength \n" +
            ",fpm.sticks_count AS sticksCount, fpm.resistivity, fpm.side_length_range AS sideLengthRange " +
            ",ppo.material_model_id AS fbProductModelId, ppo.id AS fbProductionOrderId, ppo.field4 AS workShop \n" +
            ",ppo.auart AS orderType, ppo.work_type AS workType, ppo.product_type AS productType, ppo.rdonlylogo \n" +
            ",ppo.zorientationind AS zorientationind \n" +
            "FROM plan_production_order ppo \n" +
            "LEFT JOIN factory_material_model fpm ON fpm.id = ppo.product_model_id \n" +
            "WHERE ppo.production_status = true AND ppo.work_type IN (3, 4) AND NOW() > ppo.gstrp AND NOW() < ppo.gltrp \n" +
            "GROUP BY ppo.id", nativeQuery = true)
    List<Map<String, Object>> getProductionScheduleData(Integer status);

//    // 根据状态及设备类别查询
//    @Query(value = "", nativeQuery = true)
//    List<Map<String, Object>> getCutOrderSchedule(Integer status, Integer category);

    @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_fb_production_schedule WHERE `status` = 0) pps ON  pps.device_id  =ie.id \n" +
            "  WHERE id.category = 3 GROUP BY area,CASE WHEN pps.id is NULL THEN 0 ELSE 1 END  ORDER BY area", nativeQuery = true)
    List<Map<String, Object>> getSchedulingInfo();

    /**
     * 圆棒匹配方棒加工订单
     *
     * @param specs          规格
     * @param conductiveType 导电型号
     * @param dopant         掺杂剂
     * @param productLevels  产品等级集合
     * @return 方棒订单号
     */
    @Query(value = "SELECT pfpo.aufnr,pfpo.id FROM (SELECT DISTINCT fb_product_model_id,fb_production_order_id FROM plan_fb_production_schedule WHERE `status` = 0) pfps" +
            "    LEFT JOIN plan_fb_production_order pfpo ON pfps.fb_production_order_id = pfpo.id" +
            "    LEFT JOIN zhxx_fb_product_model zfm  ON pfps.fb_product_model_id = zfm.id" +
            "    WHERE zfm.specs = ?1 AND zfm.dopant = ?3 AND zfm.conductive_type = ?2 AND product_level IN (?4)", nativeQuery = true)
    List<Map<String, Object>> findProductionScheduleCode(String specs, String conductiveType, String dopant, List<String> productLevels);

    @Query(value = "SELECT pfpo.aufnr,pfps.device_no,imck.yb_product_grade,imck.stick_count FROM (\n" +
            "    SELECT * FROM plan_fb_production_schedule WHERE `status` = 0 \n" +
            ") pfps \n" +
            " JOIN plan_fb_production_order pfpo ON pfps.fb_production_order_id = pfpo.ID\n" +
            " JOIN init_machine_capacity_kf imck ON pfps.device_id = imck.equipment_id WHERE pfpo.aufnr = ?1 " +
            "AND imck.yb_product_grade LIKE ?2 AND (imck.stick_count = 0 OR imck.stick_count = ?3)", nativeQuery = true)
    List<Map<String, Object>> findFbDeviceSchedule(String orderNo, String productLevel, Integer sticksType);
}
