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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.ProductionCircleWeightDetail;
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 ProductionCircleWeightDetailDao extends BaseDao<ProductionCircleWeightDetail, Integer>{

    @Query(value = "SELECT * FROM production_circle_weight_detail WHERE weight_name = (?1) and weight_code = ?2 and version = ?3", nativeQuery = true)
    List<ProductionCircleWeightDetail> getRecipeWeightDetail(String weightName,String weightCode, Integer version);

    @Query(value = "SELECT recipe_purpose from production_circle_weight_detail WHERE weight_name = (?1) and weight_code = ?2 and version = ?3 GROUP BY recipe_purpose", nativeQuery = true)
    List<String> getRecipeWeight(String weightName,String weightCode, Integer version);

    @Modifying
    @Transactional
    @Query(value = "UPDATE production_circle_weight_detail SET `del`=1 WHERE weight_name=?1 and weight_code = ?2 and version = ?3", nativeQuery = true)
    void deleteHistory(String weightName, String weightCode, Integer version);

    @Query(value = "SELECT * FROM production_circle_weight_detail WHERE weight_name = (?1) and weight_code = ?2 and version = ?3 and recipe_kind = 'sygg' limit 1", nativeQuery = true)
    ProductionCircleWeightDetail getRecipeWeightDetailByKind(String weightName, String weightCode, Integer version);

    @Query(value = "SELECT * FROM `production_circle_weight_detail` WHERE weight_name=?1 and weight_code =?2 and version =?3 and find_in_set(?4,recipe_purpose) and gross =?5", nativeQuery = true)
    List<ProductionCircleWeightDetail> getWeightByRecipePurpose(String weightName, String weightCode, Integer version, String recipePurpose, Integer weight);

    @Query(value = "SELECT * FROM production_circle_weight_detail WHERE weight_id = ?1 and  recipe_purpose LIKE ?2", nativeQuery = true)
    List<ProductionCircleWeightDetail> getProductionCircleWeightDetailsByParam(Long weightId, String againNum);
}
