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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.ProductionCircleRecipeDetail;
import io.swagger.models.auth.In;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

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

    @Query(value = "SELECT * FROM production_circle_recipe_detail WHERE recipe_name = (?1) and recipe_code = ?2 and version = ?3", nativeQuery = true)
    List<ProductionCircleRecipeDetail> getRecipeDetailsById(String recipeName,String recipeCode,Integer version);

    @Query(value = "SELECT recipe_purpose from production_circle_recipe_detail WHERE recipe_name = (?1) and recipe_code = ?2 and version = ?3 GROUP BY recipe_purpose", nativeQuery = true)
    List<String> getRecipePurpose(String recipeName,String recipeCode,Integer version);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM production_circle_recipe_detail WHERE recipe_name=?1 and recipe_code = ?2 and version = ?3", nativeQuery = true)
    void deleteHistory(String recipeName,String recipeCode, Integer version);

    @Modifying
    @Transactional
    @Query(value = "select * FROM production_circle_recipe_detail WHERE recipe_name=?1 and recipe_code = ?2 and version = ?3 and recipe_purpose = ?4 AND priority = 1", nativeQuery = true)
    List<ProductionCircleRecipeDetail> getRecipeDetailsByIdAndPurpose(String recipeName, String recipeCode, Integer version, String recipePurpose);

    @Query(value = "SELECT * FROM production_circle_recipe_detail WHERE recipe_id = ?1 and  recipe_purpose LIKE ?2 and (priority is null or priority = 1) group by recipe_kind", nativeQuery = true)
    List<ProductionCircleRecipeDetail> getProductionCircleRecipeDetailsByParam(Long recipeId, String againNum);

    @Query(value = "SELECT distinct(supplies_name) FROM production_circle_recipe_detail WHERE recipe_id = ?1 and  recipe_purpose LIKE ?2", nativeQuery = true)
    List<String> getRecipeMaterialTypeNamesByParam(Long recipeId, String againNum);

    @Query(value = "SELECT * FROM production_circle_recipe_detail WHERE recipe_id = ?1 and  recipe_purpose = ?2 and (priority is null or priority = 1) group by recipe_kind", nativeQuery = true)
    List<ProductionCircleRecipeDetail> getProductionCircleRecipeDetailsByParamAccurate(Long recipeId, String againNum);

    @Query(value = "SELECT distinct(supplies_name) FROM production_circle_recipe_detail WHERE recipe_id = ?1 and  recipe_purpose = ?2", nativeQuery = true)
    List<String> getRecipeMaterialTypeNamesByParamAccurate(Long recipeId, String againNum);

    @Query(value = "SELECT * FROM production_circle_recipe_detail WHERE recipe_id = ?1 and  recipe_purpose LIKE ?2 order by recipe_kind,priority", nativeQuery = true)
    List<ProductionCircleRecipeDetail> getProductionRecipeDetailsByParam(Long recipeId, String againNum);

    @Query(value = "SELECT distinct(supplies_type) FROM production_circle_recipe_detail WHERE  if(ifnull(?1,'') != '',supplies_type LIKE CONCAT('%',?1,'%'),1=1)", nativeQuery = true)
    List<String> getSuppliesTypeByParam(String suppliesType);

}
