package com.softwarebr.nunu.dao.go;

import com.brframework.commondb.core.CommonRepository;
import com.softwarebr.nunu.entity.go.PolicyLabel;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @date 2020-02-19 16:28:24
 * @author lilin
 */

@Repository
public interface PolicyLabelDao extends CommonRepository<Long, PolicyLabel> {

    /**
     * 根据政策ID删除标签关系
     * @param policyId
     */
    void deleteByPolicyId(Long policyId);

    /**
     * 查询政策分类标签
     * @param policyId
     * @return
     */
    List<PolicyLabel> findByPolicyId(Long policyId);

    /**
     * 二级标签筛选政策
     * @param childIds 二级标签id
     * @param childTotal 当前标签数
     * @return
     */
    @Query(value = "SELECT * FROM go_policy_label WHERE child_id IN (:childIds) GROUP BY policy_id HAVING COUNT(id)>=:childTotal", nativeQuery = true)
    List<PolicyLabel> findPolicyLabelByChildId(@Param("childIds") List<Long> childIds, @Param("childTotal") Integer childTotal);

}