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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.ProductionCircleRecipe;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

    @Modifying
    @Transactional
    @Query(value = "UPDATE production_circle_recipe SET `status`=?2 WHERE id =?1 ", nativeQuery = true)
    void updateStatus(Long id, int i);

    @Query(value = "select w.* from production_circle_dosing_list l left join production_circle_recipe w on " +
            "l.recipe_id = w.id where l.product_name = ?1 and l.status = 1 limit 1", nativeQuery = true)
    ProductionCircleRecipe getRecipeByProductCode(String productCode);

    @Query(value = "select * from production_circle_recipe where id = ?1", nativeQuery = true)
    ProductionCircleRecipe getRecipeWeightById(Long id);

    @Query(value = "select recipe_name from production_circle_recipe where status = 1 and `del`=0 GROUP BY recipe_name", nativeQuery = true)
    List<String> recipeNameListAll();

    @Modifying
    @Query(value = "UPDATE production_circle_recipe_detail SET `del`=1 WHERE recipe_name=?1 and recipe_code=?2 and version=?3",nativeQuery = true)
    void deleteAll(String recipeName, String recipeCode, Integer version);

    @Modifying
    @Query(value = "UPDATE production_circle_recipe SET `del`=?2 WHERE id =?1 ", nativeQuery = true)
	void deleteDel(Long id, int del);

    @Query(value = "select * from production_circle_recipe where (recipe_name=?1 or recipe_code=?2) and `del`=0 limit 1", nativeQuery = true)
    ProductionCircleRecipe getRecipeByProductCodeOrRecipeName(String recipeName, String recipeCode);

    @Query(value = "select max(version) from production_circle_recipe where recipe_name=?1 and recipe_code=?2", nativeQuery = true)
    Integer getNewestVersion(String recipeName, String recipeCode);

    @Query(value = "select count(*) from production_circle_recipe where (recipe_name=?1 or recipe_code=?2) and status = 1 and `del`=0 ", nativeQuery = true)
    Integer getRecipeByCodeOrName(String recipeName, String recipeCode);
}
