package com.sw.mes.plan.dao;


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

import java.util.Date;
import java.util.List;

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

    FbProductionOrder findByAufnr(String code);

    @Query(value = "SELECT * FROM plan_fb_production_order ppo \n" +
            " LEFT JOIN zhxx_fb_product_model fpm ON ppo.fb_product_model_id = fpm.id\n" +
            " WHERE fpm.material_type_id = ?1 AND ppo.product_type = ?2 AND ppo.gstrp < ?3  AND ppo.gltrp > ?3 AND ppo.`status` = 'REL' ", nativeQuery = true)
    List<FbProductionOrder> findByFbProductModelMaterialTypeId(Integer id, String productType, Date toDate);

    @Query(value = "SELECT * FROM plan_fb_production_order ppo \n" +
            " LEFT JOIN zhxx_fb_product_model fpm ON ppo.fb_product_model_id = fpm.id\n" +
            " WHERE fpm.material_type_id = ?1 AND ppo.product_type = ?2 AND ppo.gstrp < ?3  AND ppo.gltrp > ?3 AND ppo.`status` = 'REL'", nativeQuery = true)
    List<FbProductionOrder> findByFbProductModelMaterialTypeIdV2(Integer id, String productType, Date toDate);

    @Query(value = "select * from plan_fb_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<FbProductionOrder> findByStatusAndProductCodeAndTime(boolean b, String matnr, Date gstrp, Date gltrp);

    @Query(value = "select * from plan_production_order where auart in (?1) and status = ?2 and gstrp <= CURRENT_TIMESTAMP and CURRENT_TIMESTAMP <= gltrp ", nativeQuery = true)
    List<FbProductionOrder> findByGstrpAndGltrpAndStatusAndAuart(List<String> list, String status);
}
