package com.ankaibei.workFlow.webadmin.ankaibei.mapper;

import com.ankaibei.workFlow.webadmin.ankaibei.entity.RelevanceProjectInfoEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.criteria.CriteriaBuilder;
import javax.transaction.Transactional;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @author lwq
 * @version 1.0
 * @description:
 * @date 2022/10/9 13:44
 */
public interface RelevanceProjectInfoMapper extends JpaRepository<RelevanceProjectInfoEntity, Integer>, JpaSpecificationExecutor<RelevanceProjectInfoEntity> {

    /**
     * 根据项目组id查询关联的人员
     */
    @Query(value = "SELECT p.user_id userId FROM relevance_project_info p LEFT JOIN work_sys_user w " +
            " on p.user_id =w.user_id WHERE w.deleted_flag=1 AND p.project_team_id= ?1 ",
            countQuery = "SELECT p.user_id userId FROM relevance_project_info p LEFT JOIN work_sys_user w " +
                    " on p.user_id =w.user_id WHERE w.deleted_flag=1 AND p.project_team_id= ?1 ", nativeQuery = true)
    Page<Map<String, Integer>> findByProjectTeamIdEquals(String projectTeamId, Pageable page);

    /**
     * 根据项目组id查询关联的人员
     */
    @Query(value = "SELECT p.user_id userId FROM relevance_project_info p LEFT JOIN work_sys_user w " +
            "on p.user_id =w.user_id WHERE w.deleted_flag=1 AND p.project_team_id= ?1 ",
            countQuery = "SELECT p.user_id userId FROM relevance_project_info p LEFT JOIN work_sys_user w " +
                    "on p.user_id =w.user_id WHERE w.deleted_flag=1 AND p.project_team_id= ?1 ", nativeQuery = true)
    List<String> findByProjectTeamIdEquals(String projectTeamId);

    /**
     * 根据用户id查询关联的项目组
     */
    @Query(value = "SELECT p.project_team_id projectTeamId FROM relevance_project_info p WHERE p.user_id = ?1 ",
            countQuery = "SELECT count(1) FROM relevance_project_info p WHERE p.user_id = ?1 ", nativeQuery = true)
    List<String> findByUserId(String userId);

    /**
     * 根据用户id 项目组id查询该用户是否存在这个项目组
     */
    @Query(value = "SELECT count(1) FROM relevance_project_info p WHERE p.user_id = ?1 and p.project_team_id =?2 ", nativeQuery = true)
    int findByuserIdbyrojectTeamid(String userId, Integer projectTeamId);

    /**
     * 移除项目组人员
     */
    @Transactional
    @Modifying
    @Query(value = "DELETE  FROM relevance_project_info  WHERE project_team_id=?1 AND user_id =?2 ", nativeQuery = true)
    void removeUser(Integer projectTeamId, Long user);
}
