wg-basic/service-department/src/main/resources/mybatis/mapper/department-user-mapper.xml

532 lines
17 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="ink.wgink.service.department.dao.IDepartmentUserDao">
<resultMap id="departmentUserDTO" type="ink.wgink.pojo.dtos.department.DepartmentUserDTO">
<result column="department_id" property="departmentId"/>
<result column="user_id" property="userId"/>
<result column="user_sort" property="userSort"/>
<result column="user_username" property="userUsername"/>
<result column="user_name" property="userName"/>
<result column="user_phone" property="userPhone"/>
<result column="user_email" property="userEmail"/>
<result column="user_avatar" property="userAvatar"/>
<result column="user_type" property="userType"/>
<result column="user_state" property="userState"/>
</resultMap>
<!-- 建表 -->
<update id="createTable">
CREATE TABLE IF NOT EXISTS `sys_department_user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`department_id` char(36) NOT NULL,
`user_id` char(36) NOT NULL,
`user_sort` varchar(255) DEFAULT 'ZZZ-000',
PRIMARY KEY (`id`),
KEY `user_id_idx` (`user_id`) USING BTREE,
KEY `department_id_idx` (`department_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</update>
<!-- 新增组织用户 -->
<insert id="save" parameterType="map">
INSERT INTO sys_department_user(
department_id,
user_id
) VALUES(
#{departmentId},
#{userId}
)
</insert>
<!-- 删除组织用户 -->
<delete id="delete" parameterType="map">
DELETE FROM
sys_department_user
<where>
<if test="departmentId != null and departmentId != ''">
AND
department_id = #{departmentId}
</if>
<if test="departmentIds != null and departmentIds.size > 0">
AND
department_id IN
<foreach collection="departmentIds" index="index" open="(" separator="," close=")">
#{departmentIds[${index}]}
</foreach>
</if>
<if test="userId != null and userId != ''">
AND
user_id = #{userId}
</if>
<if test="userIds != null and userIds.size > 0">
AND
user_id IN
<foreach collection="userIds" index="index" open="(" separator="," close=")">
#{userIds[${index}]}
</foreach>
</if>
</where>
</delete>
<!-- 更新组织用户排序 -->
<update id="updateSort" parameterType="map">
UPDATE
sys_department_user
SET
user_sort = #{userSort}
WHERE
department_id = #{departmentId}
AND
user_id = #{userId}
</update>
<!-- 机构用户列表 -->
<select id="list" parameterType="map" resultMap="departmentUserDTO">
SELECT
department_id,
user_id,
user_sort
FROM
sys_department_user
<where>
<if test="departmentId != null and departmentId != ''">
department_id = #{departmentId}
</if>
<if test="departmentIds != null and departmentIds.size > 0">
AND
department_id IN
<foreach collection="departmentIds" index="index" open="(" separator="," close=")">
#{departmentIds[${index}]}
</foreach>
</if>
<if test="userIds != null and userIds.size > 0">
AND
user_id IN
<foreach collection="userIds" index="index" open="(" separator="," close=")">
#{userIds[${index}]}
</foreach>
</if>
</where>
<if test="order != null and order != ''">
ORDER BY
<trim suffixOverrides=",">
<if test="order == 'userSort'">
user_sort <if test="sort != null and sort != ''"><if test="sort == 'asc'">ASC</if><if test="sort == 'desc'">DESC</if></if>,
</if>
</trim>
<trim suffixOverrides=",">
<if test="order == 'userSort'">
user_sort <if test="sort != null and sort != ''"><if test="sort == 'asc'">ASC</if><if test="sort == 'desc'">DESC</if></if>,
</if>
</trim>
</if>
</select>
<!-- 用户ID列表 -->
<select id="listUserId" parameterType="map" resultType="java.lang.String">
SELECT
user_id
FROM
sys_department_user
<where>
<if test="departmentId != null and departmentId != ''">
department_id = #{departmentId}
</if>
<if test="departmentIds != null and departmentIds.size > 0">
AND
department_id IN
<foreach collection="departmentIds" index="index" open="(" separator="," close=")">
#{departmentIds[${index}]}
</foreach>
</if>
<if test="userIds != null and userIds.size > 0">
AND
user_id IN
<foreach collection="userIds" index="index" open="(" separator="," close=")">
#{userIds[${index}]}
</foreach>
</if>
</where>
GROUP BY
user_id, user_sort
ORDER BY
user_sort
</select>
<!-- 用户ID列表 -->
<select id="listGroupUserId" parameterType="map" resultType="java.lang.String">
SELECT
user_id
FROM
sys_department_user
WHERE
1 = 1
GROUP BY
user_id
</select>
<!-- 部门ID列表 -->
<select id="listDepartmentId" parameterType="map" resultType="java.lang.String">
SELECT
department_id
FROM
sys_department_user
<where>
<if test="userId != null and userId != ''">
user_id = #{userId}
</if>
<if test="userIds != null and userIds.size > 0">
AND
user_id IN (
<foreach collection="userIds" index="index" open="(" separator="," close=")">
#{userIds[${index}]}
</foreach>
)
</if>
</where>
</select>
<!-- 用户列表 -->
<select id="listUser" parameterType="map" resultMap="ink.wgink.service.user.dao.IUserDao.userDTO">
SELECT
user_id,
user_username,
user_name,
user_phone,
user_email,
user_type,
user_state
FROM
sys_user t1
WHERE
t1.is_delete = 0
AND
t1.user_username != 'admin'
<if test="keywords != null and keywords != ''">
AND (
user_username LIKE CONCAT('%', #{keywords}, '%')
OR
user_name LIKE CONCAT('%', #{keywords}, '%')
OR
user_phone LIKE CONCAT('%', #{keywords}, '%')
OR
user_email LIKE CONCAT('%', #{keywords}, '%')
)
</if>
<if test="userType != null">
AND
t1.user_type = #{userType}
</if>
<if test="userState != null">
AND
t1.user_state = #{userState}
</if>
<if test="excludeUserType != null and excludeUserType != ''">
AND
t1.user_type != #{excludeUserType}
</if>
<if test="departmentId != null and departmentId != ''">
AND
t1.user_id IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id = #{departmentId}
)
</if>
<if test="departmentIds != null and departmentIds.size > 0">
AND
t1.user_id IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id IN
<foreach collection="departmentIds" index="index" open="(" separator="," close=")">
#{departmentIds[${index}]}
</foreach>
)
</if>
<if test="excludeDepartmentId != null and excludeDepartmentId != ''">
AND
t1.user_id NOT IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id = #{excludeDepartmentId}
)
</if>
<if test="excludeDepartmentIds != null and excludeDepartmentIds.size > 0">
AND
t1.user_id NOT IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id IN
<foreach collection="excludeDepartmentIds" index="index" open="(" separator="," close=")">
#{excludeDepartmentIds[${index}]}
</foreach>
)
</if>
</select>
<!-- 用户列表 -->
<select id="listUserPO" parameterType="map" resultMap="ink.wgink.service.user.dao.IUserDao.userPO">
SELECT
user_id,
user_password,
user_username,
user_name,
user_phone,
user_email,
user_ukey,
user_ukey_electronic_secret_key,
user_type,
user_state,
user_expired_date,
user_avatar,
user_longitude,
user_latitude,
last_login_address,
LEFT(last_login_time, 19) last_login_time,
login_type,
LEFT(gmt_password_modified, 19) gmt_password_modified,
remarks,
LEFT(gmt_create, 19) gmt_create
FROM
sys_user t1
WHERE
t1.is_delete = 0
<if test="userType != null">
AND
t1.user_type = #{userType}
</if>
<if test="userState != null">
AND
t1.user_state = #{userState}
</if>
<if test="departmentId != null and departmentId != ''">
AND
t1.user_id IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id = #{departmentId}
)
</if>
<if test="departmentIds != null and departmentIds.size > 0">
AND
t1.user_id IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id IN
<foreach collection="departmentIds" index="index" open="(" separator="," close=")">
#{departmentIds[${index}]}
</foreach>
)
</if>
<if test="excludeDepartmentId != null and excludeDepartmentId != ''">
AND
t1.user_id NOT IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id = #{excludeDepartmentId}
)
</if>
<if test="excludeDepartmentIds != null and excludeDepartmentIds.size > 0">
AND
t1.user_id NOT IN (
SELECT
user_id
FROM
sys_department_user st1
WHERE
st1.department_id IN
<foreach collection="excludeDepartmentIds" index="index" open="(" separator="," close=")">
#{excludeDepartmentIds[${index}]}
</foreach>
)
</if>
</select>
<!-- 同部门用户ID列表 -->
<select id="listSameDepartmentUserId" parameterType="map" resultType="java.lang.String">
SELECT
t1.user_id
FROM
sys_department_user t1
WHERE
t1.department_id IN (
SELECT
department_id
FROM
sys_department_user st1
<where>
<if test="userId != null and userId != ''">
user_id = #{userId}
</if>
<if test="userIds != null and userIds.size > 0">
AND
user_id IN
<foreach collection="userIds" index="index" open="(" separator="," close=")">
#{userIds[${index}]}
</foreach>
</if>
</where>
)
</select>
<!-- 部门用户列表 -->
<select id="listDepartmentUser" parameterType="map" resultMap="departmentUserDTO">
SELECT
t1.user_id,
t1.user_username,
t1.user_name,
t1.user_phone,
t1.user_email,
t1.user_avatar,
t1.user_type,
t1.user_state,
jt1.department_id,
jt1.user_sort
FROM
sys_user t1
INNER JOIN
sys_department_user jt1
ON
t1.user_id = jt1.user_id
WHERE
t1.is_delete = 0
AND
t1.user_name != 'admin'
<if test="departmentId != null and departmentId != ''">
AND
jt1.department_id = #{departmentId}
</if>
<if test="departmentIds != null and departmentIds.size > 0">
AND
jt1.department_id IN
<foreach collection="departmentIds" index="index" open="(" separator="," close=")">
#{departmentIds[${index}]}
</foreach>
</if>
<choose>
<when test="sort != null and (sort == 'userSort')">
ORDER BY
<if test="sort == 'userSort'">
user_sort ${order}
</if>
</when>
</choose>
</select>
<!-- 部门列表 -->
<select id="listDepartment" parameterType="map" resultMap="ink.wgink.service.department.dao.IDepartmentDao.departmentDTO">
SELECT
t1.department_id,
t1.department_parent_id,
t1.department_name,
t1.department_name_en,
t1.department_name_other,
t1.department_no,
t1.department_summary,
t1.department_code,
t1.department_logo,
t1.department_logo_hover,
t1.department_type,
t1.department_state,
t1.department_fax,
t1.department_tel,
t1.department_address,
t1.department_master,
t1.department_duty,
t1.department_area_code,
t1.department_area_name,
t1.department_longitude,
t1.department_latitude,
t1.department_order
FROM
sys_department t1
WHERE
t1.is_delete = 0
AND
t1.department_id IN (
SELECT
st1.department_id
FROM
sys_department_user st1
<where>
<if test="userId != null and userId != ''">
st1.user_id = #{userId}
</if>
</where>
)
</select>
<!-- 部门列表 -->
<select id="listDepartmentPO" parameterType="map" resultMap="ink.wgink.service.department.dao.IDepartmentDao.departmentPO">
SELECT
t1.department_id,
t1.department_parent_id,
t1.department_name,
t1.department_name_en,
t1.department_name_other,
t1.department_no,
t1.department_summary,
t1.department_code,
t1.department_logo,
t1.department_logo_hover,
t1.department_type,
t1.department_state,
t1.department_fax,
t1.department_tel,
t1.department_address,
t1.department_master,
t1.department_duty,
t1.department_area_code,
t1.department_area_name,
t1.department_longitude,
t1.department_latitude,
t1.department_order
FROM
sys_department t1
WHERE
t1.is_delete = 0
AND
t1.department_id IN (
SELECT
st1.department_id
FROM
sys_department_user st1
<where>
<if test="userId != null and userId != ''">
st1.user_id = #{userId}
</if>
<if test="userIds != null and userIds.size > 0">
AND
st1.user_id IN
<foreach collection="userIds" index="index" open="(" separator="," close=")">
#{userIds[${index}]}
</foreach>
</if>
</where>
)
</select>
</mapper>