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

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

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

    @Query(value = "SELECT * FROM plan_production_order where aufnr=?1", nativeQuery = true)
    List<ProductionOrder> findByAufnr(String code);

    @Query(value = "SELECT * FROM plan_production_order ppo \n" +
            "WHERE ppo.matnr = ?1 AND ppo.gstrp < ?2  AND ppo.gltrp > ?2 AND ppo.status = 'REL' AND ppo.auart = ?3 AND ppo.field2 = ?4", nativeQuery = true)
    List<ProductionOrder> findByProductModelCode(String code, Date toDate, String orderType, String field2);

    @Query(value = "SELECT * FROM plan_production_order ppo \n" +
            " WHERE ppo.matnr = ?1 AND ppo.gstrp < ?2  AND ppo.gltrp > ?2 AND ppo.status = 'REL' AND ppo.auart = ?3 AND ppo.field2 <> ?4", nativeQuery = true)
    List<ProductionOrder> findByProductModelCodeV2(String code, Date toDate, String orderType, String field2);

    /**
     * 根据 物料类别id 查找订单
     * @param materialTypeId 物料类别id
     * @param toDate 时间
     * @param orderType 订单类型
     * @param field2 复检标识
     * @return
     */
    @Query(value = "SELECT ppo.*  FROM plan_production_order ppo " +
            " LEFT JOIN factory_product_model fpm ON ppo.product_model_id = fpm.id " +
            " WHERE fpm.material_type_id = ?1  AND ppo.gstrp < ?2  AND ppo.gltrp > ?2 AND ppo.status = 'REL' AND ppo.auart = ?3 AND ppo.field2 = ?4", nativeQuery = true)
    List<ProductionOrder> findByMaterialTypeId(Integer materialTypeId, Date toDate, String orderType, String field2);

    /**
     * 根据 物料类别id 查找订单
     * @param materialTypeId 物料类别id
     * @param toDate 时间
     * @param orderType 订单类型
     * @param field2 复检标识
     * @return
     */
    @Query(value = "SELECT ppo.*  FROM plan_production_order ppo " +
            " LEFT JOIN factory_product_model fpm ON ppo.product_model_id = fpm.id " +
            " WHERE fpm.material_type_id = ?1  AND ppo.gstrp < ?2  AND ppo.gltrp > ?2 AND ppo.status = 'REL' AND ppo.auart = ?3 AND ppo.field2 <> ?4", nativeQuery = true)
    List<ProductionOrder> findByMaterialTypeIdV2(Integer materialTypeId, Date toDate, String orderType, String field2);

    /**
     * 根据 设备加工能力 查找符合计划时间内的工单
     * @param machineCapacityId
     * @param status
     * @param productionStatus
     * @param gmtCreate
     * @return
     */
    @Query(value = "SELECT * FROM plan_production_order ppo \n" +
            "WHERE ppo.machine_capacity_id = ?1 AND ppo.`status` = ?2 AND ppo.production_status = ?3 AND ppo.gstrp <= ?4 AND ppo.gltrp >= ?4\n" +
            "ORDER BY ppo.id DESC", nativeQuery = true)
    List<ProductionOrder> findByMachineCapacityIdAndStatusAndProductionStatusAndGmtCreateTime(Integer machineCapacityId, String status, Boolean productionStatus, Date gmtCreate);

    @Query(value = "select * from plan_production_order ppo where ppo.production_status = ?1 and ppo.matnr = ?2 and (\t (ppo.gstrp > ?3 and ppo.gstrp < ?4)\n" +
            "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tor (ppo.gltrp > ?3 and ppo.gltrp < ?4)\n" +
            "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tor (ppo.gstrp <= ?3 and ppo.gltrp >= ?4)\t)", nativeQuery = true)
    List<ProductionOrder> findByStatusAndProductCodeAndTime(boolean b, String matnr, Date gstrp, Date gltrp);

    @Query(value = "select * from plan_production_order ppo where ppo.work_type = ?1 and ppo.gstrp >= ?2 and ppo.gstrp <= ?3 and if(?4 is null, 1=1, status = ?4)", nativeQuery = true)
    List<ProductionOrder> findByWorkTypeAndTimeAndStatus(Integer workType, Date gstrp, Date gltrp, String status);

    List<ProductionOrder> findByErpOrderNo(String code);

    List<ProductionOrder> findByFbOrderNo(String code);

	List<ProductionOrder> findByAufnrAndMatnr(String code, String matnr);

    @Query(value = "select ppo.field5,fmm.product_name,ppo.auart,count(1) as  count\n" +
            "from jinko_mes.plan_production_order ppo\n" +
            "left join jinko_mes.factory_material_model fmm\n" +
            "on ppo.matnr=fmm.code\n" +
            "where ppo.aufnr not in (\n" +
            "SELECT aufnr\n" +
            "FROM jinko_mes.`report_sap_goodsmvt`\n" +
            "where aufnr is not null\n" +
            "group by aufnr)\n" +
            "and ppo.status ='REL'\n" +
            "and ppo.field5 like '%拉晶一车间生产%'\n" +
            "group by ppo.field5,fmm.product_name,ppo.auart\n" +
            "UNION ALL\n" +
            "select ppo.field5,fmm.product_name,ppo.auart,count(1) as  count\n" +
            "from jinko_mes.plan_production_order ppo\n" +
            "left join jinko_mes.factory_material_model fmm\n" +
            "on ppo.matnr=fmm.code\n" +
            "where ppo.aufnr not in (\n" +
            "SELECT aufnr\n" +
            "FROM jinko_mes.`report_sap_goodsmvt`\n" +
            "where aufnr is not null\n" +
            "group by aufnr)\n" +
            "and ppo.status ='REL'\n" +
            "and ppo.field5 like '%拉晶二车间生产%'\n" +
            "group by ppo.field5,fmm.product_name,ppo.auart", nativeQuery = true)
    List<Map<String, Object>> findByWorkOrderMarginMonitor();

    @Query(value = "SELECT * FROM plan_production_order where aufnr IN(?1)", nativeQuery = true)
    List<ProductionOrder> findByAufnrs(List<String> aufnrs);

    //AND (ie.area = ?2  OR ?2 IS NULL)
    @Query(value = "select * from plan_production_order ppo where ppo.work_type = ?1 and ppo.status = ?2 ", nativeQuery = true)
    List<ProductionOrder> findByWorkTypeAndZorientationind(String workType, String status);
}
