package com.crunii.micro.domain.repo.main;

import com.crunii.micro.domain.common.BaseRepo;
import com.crunii.micro.domain.entity.main.TsmWorkerRole;
import java.util.List;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 田平 create 2019-01-28 05:01:09
 */
@Repository
public interface TsmWorkerRoleRepo extends BaseRepo<TsmWorkerRole, Integer> {


    @Query(value = "select * from tsm_worker_role where login_id=:loginId ",nativeQuery = true)
    List<TsmWorkerRole> findByLoginId(@Param("loginId") Long loginId);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM tsm_worker_role WHERE role_id = :roleId and login_id IN :loginIds", nativeQuery = true)
    void deleteAllByRoleIdAndLoginIdIn(@Param("roleId")Integer roleId , @Param("loginIds")List<Integer> loginIds);


    @Query(value = "select a.* from tsm_worker_role a left join tsm_role b on a.role_id=b.role_id where b.role_name=:roleName",nativeQuery = true)
    List<TsmWorkerRole> findByRoleName(@Param("roleName") String roleName);

    @Query(value = "select a.* from tsm_worker_role a " +
            " left join tsm_role b on a.role_id=b.role_id " +
            " left join tsm_worker c on a.login_id=c.login_id" +
            " where b.role_name=:roleName and c.department=:department",nativeQuery = true)
    List<TsmWorkerRole> findByRoleNameAndDepartment(@Param("roleName") String roleName,@Param("department") String department);

    @Query(value = "select a.* from tsm_worker_role a " +
            " left join tsm_role b on a.role_id=b.role_id " +
            " left join tsm_worker c on a.login_id=c.login_id" +
            " where b.role_name=:roleName and c.org_id=:orgId",nativeQuery = true)
    List<TsmWorkerRole> findByRoleNameAndOrg(@Param("roleName") String roleName,@Param("orgId") String orgId);
}
