package cn.lihongjie.coal.empSalaryItem.repository;

import cn.lihongjie.coal.base.dao.BaseRepository;
import cn.lihongjie.coal.empSalaryItem.entity.EmpSalaryItemEntity;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface EmpSalaryItemRepository extends BaseRepository<EmpSalaryItemEntity> {
    long countByNameContainsAndOrganizationId(String name, String organizationId);

    long countByNameContainsAndOrganizationIdAndIdNot(
            String name, String organizationId, String id);

    @Query(
            value =
                    """
                            select md5(string_agg(t.f, ',')) from (
                                                    select f from (
                                                    select code ,formula f from t_emp_salary_item where organization_id = :organizationId 
                                                    union all 
                                                    select code,  item_expression f from t_emp_salary_sys_item 
                                                ) as t order by code asc) as t
                                                    """,
            nativeQuery = true)
    String computeCacheKey(@Param("organizationId") String organizationId);
}
