This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/dao/UserDao.xml
2017-12-29 16:18:40 +08:00

420 lines
12 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="com.nis.web.dao.UserDao">
<!-- 生产环境使用-->
<cache eviction="LRU" flushInterval="60000" size="512" readOnly="true">
</cache>
<!-- 通过别名映射对象 -->
<select id="getUserById" parameterType="long" resultType="sysUser">
select
id as "id",
login_id as "loginId",
name,
identity,
photo,
email email,
create_time createTime,
status status
from sys_user where id=#{id}
</select>
<select id="getUserByLoginName" resultMap="userResultMap">
select
id,
login_id,
name,
identity,
photo,
email,
create_time,
status
from sys_user where login_id=#{loginName}
</select>
<!-- map 转为对象 -->
<resultMap type="sysUser" id="userResultMap">
<id property="id" column="id"/>
<result property="loginId" column="login_id"/>
<result property="name" column="name"/>
<result property="identity" column="identity"/>
<result property="photo" column="photo"/>
<result property="email" column="email"/>
<result property="createTime" column="create_time"/>
<result property="status" column="status"/>
</resultMap>
<resultMap type="sysUser" id="userWithRelationResultMap">
<id property="id" column="user_id"/>
<result property="loginId" column="login_id"/>
<result property="name" column="user_name"/>
<result property="identity" column="identity"/>
<result property="photo" column="photo"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="createTime" column="user_create_time"/>
<result property="status" column="user_status"/>
<result property="company.id" column="company.id"/>
<result property="company.name" column="company.name"/>
<result property="company.parent.id" column="company.parent.id"/>
<result property="company.parentIds" column="company.parentIds"/>
<result property="company.type" column="company.type"/>
<result property="company.grade" column="company.grade"/>
<result property="company.jobType" column="company.job_type"/>
<result property="entity.id" column="entity.id"/>
<result property="entity.name" column="entity.name"/>
<result property="entity.parent.id" column="entity.parent.id"/>
<result property="entity.parentIds" column="entity.parentIds"/>
<result property="entity.type" column="entity.type"/>
<result property="entity.grade" column="entity.grade"/>
<result property="entity.jobType" column="entity.job_type"/>
<result property="office.id" column="office.id"/>
<result property="office.name" column="office.name"/>
<result property="office.parent.id" column="office.parent.id"/>
<result property="office.parentIds" column="office.parentIds"/>
<result property="office.type" column="office.type"/>
<result property="office.grade" column="office.grade"/>
<result property="office.jobType" column="office.job_type"/>
<collection property="userRoleList" ofType="sysRole">
<id property="id" column="role_id"/>
<result property="name" column="role_name"/>
<result property="dataScope" column="data_scope"/>
<result property="remark" column="role_desc"/>
<result property="status" column="role_status"/>
<result property="createTime" column="role_create_time"/>
</collection>
</resultMap>
<select id="findUserByRoleId" resultType="sysUser">
SELECT
u.id,
u.login_id,
u.name,
u.identity,
u.photo,
u.password,
u.email,
u.create_time,
u.status,
c.id AS "company.id",
c.name AS "company.name",
e.id AS "entity.id",
e.name AS "entity.name",
o.id AS "office.id",
o.name AS "office.name"
FROM sys_user u
JOIN sys_inter_user_role ir ON u.id=ir.user_id and ir.role_id=#{id}
LEFT JOIN sys_office c ON c.id=u.company_id AND c.useable=1
LEFT JOIN sys_office e ON e.id=u.entity_id AND e.useable=1
LEFT JOIN sys_office o ON o.id=u.office_id AND o.useable=1
WHERE u.status=1
</select>
<!-- 通过别名映射对象 -->
<select id="findUserByOfficeId" parameterType="long" resultType="sysUser">
select
id as "id",
login_id as "loginId",
identity,
name,
photo,
email email,
create_time createTime,
status status,
company_id AS "company.id",
entity_id AS "entity.id",
office_id AS "office.id"
from sys_user where office_id=#{id}
</select>
<select id="officeIsExistOfCompany" parameterType="long" resultType="Integer">
select
count(id)
from sys_office where id=#{officeId}
and id in (select id from sys_office where parent_ids like
CONCAT((select parent_ids from sys_office where id=#{companyId}), #{companyId}, ',%'))
</select>
<sql id="userColumns">
u.id AS user_id,
u.photo,
u.login_id,
u.name AS user_name,
u.identity,
u.password,
u.email,
u.create_time AS user_create_time,
u.status AS user_status,
u.company_id AS "company.id",
c.name AS "company.name",
c.parent_id AS "company.parent.id",
c.parent_ids AS "company.parentIds",
c.type AS "company.type",
c.grade AS "company.grade",
c.job_type AS "company.job_type",
u.office_id AS "office.id",
o.name AS "office.name",
o.parent_id AS "office.parent.id",
o.parent_ids AS "office.parentIds",
o.type AS "office.type",
o.grade AS "office.grade",
o.job_type AS "office.job_type",
u.entity_id AS "entity.id",
e.name AS "entity.name",
e.parent_id AS "entity.parent.id",
e.parent_ids AS "entity.parentIds",
e.type AS "entity.type",
e.grade AS "entity.grade",
e.job_type AS "entity.job_type",
r.id AS role_id,
r.name AS role_name,
r.data_scope,
r.remark AS role_desc,
r.status AS role_status,
r.create_time AS role_create_time
</sql>
<sql id="userJoins">
LEFT JOIN sys_office c ON c.id = u.company_id AND c.useable=1
LEFT JOIN sys_office e ON e.id=u.entity_id AND e.useable=1
LEFT JOIN sys_office o ON o.id=u.office_id AND o.useable=1
LEFT JOIN sys_inter_user_role ir ON u.id=ir.user_id
LEFT JOIN sys_role r ON r.id=ir.role_id AND r.status=1
</sql>
<select id="getUserWithRelation" resultMap="userWithRelationResultMap">
SELECT
<include refid="userColumns"/>
FROM sys_user u
<include refid="userJoins"/>
WHERE u.status=#{DEL_FLAG_NORMAL}
<if test=" id != null">
AND u.id=#{id}
</if>
<if test=" loginId != null">
AND u.login_id=#{loginId}
</if>
</select>
<select id="findList" resultType="sysUser">
select
u.id,
u.login_id,
u.photo,
u.name,
u.identity,
u.email,
u.create_time,
u.status,
u.company_id AS "company.id",
c.name AS "company.name",
c.parent_id AS "company.parent.id",
c.parent_ids AS "company.parentIds",
c.type AS "company.type",
c.grade AS "company.grade",
c.job_type AS "company.job_type",
u.entity_id AS "entity.id",
e.name AS "entity.name",
e.parent_id AS "entity.parent.id",
e.parent_ids AS "entity.parentIds",
e.type AS "entity.type",
e.grade AS "entity.grade",
e.job_type AS "entity.job_type",
u.office_id AS "office.id",
o.name AS "office.name",
o.parent_id AS "office.parent.id",
o.parent_ids AS "office.parentIds",
o.type AS "office.type",
o.grade AS "office.grade",
o.job_type AS "office.job_type"
from sys_user u
LEFT JOIN sys_office c ON c.id = u.company_id AND c.useable=1
LEFT JOIN sys_office e ON e.id=u.entity_id AND e.useable=1
LEFT JOIN sys_office o ON o.id=u.office_id AND o.useable=1
WHERE u.status=#{DEL_FLAG_NORMAL}
<if test="company != null and company.id != null">
AND (c.id = #{company.id} OR c.parent_ids LIKE
<if test="dbName == 'oracle'">'%,'||#{company.id}||',%')</if>
<if test="dbName == 'mssql'">'%,'+#{company.id}+',%')</if>
<if test="dbName == 'mysql'">CONCAT('%,', #{company.id}, ',%'))</if>
</if>
<if test="entity != null and entity.id != null">
AND (e.id = #{entity.id} OR e.parent_ids LIKE
<if test="dbName == 'oracle'">'%,'||#{entity.id}||',%')</if>
<if test="dbName == 'mssql'">'%,'+#{entity.id}+',%')</if>
<if test="dbName == 'mysql'">CONCAT('%,', #{entity.id}, ',%'))</if>
</if>
<if test="office != null and office.id != null">
AND (o.id = #{office.id} OR o.parent_ids LIKE
<if test="dbName == 'oracle'">'%,'||#{office.id}||',%')</if>
<if test="dbName == 'mssql'">'%,'+#{office.id}+',%')</if>
<if test="dbName == 'mysql'">CONCAT('%,', #{office.id}, ',%'))</if>
</if>
<!-- 数据范围过滤 -->
${sqlMap.dsf}
<!-- 如果不是超级管理员,则不显示超级管理员用户 -->
<if test="!currentUser.admin">
AND u.login_id != 'admin'
</if>
<if test="loginId != null and loginId !=''">
AND u.login_id=#{loginId}
</if>
<if test="name != null and name != ''">
AND u.name like
<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
<if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
</if>
<choose>
<when test="page !=null and page.orderBy != null and page.orderBy != ''">
ORDER BY ${page.orderBy}
</when>
<otherwise>
ORDER BY u.create_time desc,u.name asc
</otherwise>
</choose>
</select>
<insert id="insert" parameterType="sysUser" useGeneratedKeys="true" keyProperty="id" >
insert into sys_user(LOGIN_ID,PHOTO,NAME,IDENTITY,EMAIL,PASSWORD,CREATE_TIME,STATUS,COMPANY_ID,ENTITY_ID,OFFICE_ID)
values(#{loginId},#{photo},#{name},#{identity},#{email},#{password},#{createTime},#{status},#{company.id},#{entity.id},#{office.id})
</insert>
<!-- 更新用户 -->
<update id="update">
UPDATE sys_user SET
login_id= #{loginId},
password = #{password},
name = #{name},
identity = #{identity},
photo = #{photo},
email = #{email},
company_id = #{company.id},
entity_id = #{entity.id},
office_id = #{office.id}
WHERE id = #{id}
</update>
<!-- 插入用户和角色关联表数据 -->
<insert id="insertUserRole">
INSERT INTO sys_inter_user_role(role_id, user_id)
<foreach collection="userRoleList" item="role" separator=" union all ">
SELECT #{role.id},#{id}
<if test="dbName != 'mssql'">
FROM dual
</if>
</foreach>
</insert>
<!-- 插入用户和部门关联表数据 -->
<insert id="insertUserOffice">
INSERT INTO sys_inter_user_office(office_id, user_id)
<foreach collection="userOfficeList" item="office" separator=" union all ">
SELECT #{office.id},#{id}
<if test="dbName != 'mssql'">
FROM dual
</if>
</foreach>
</insert>
<!-- 删除用户和角色关联表数据 -->
<delete id="deleteUserRole">
DELETE FROM sys_inter_user_role WHERE user_id = #{id}
</delete>
<!-- 删除用户所属角色,唯一对应-->
<delete id="removeUserInRole">
DELETE FROM sys_inter_user_role WHERE user_id = #{userId} and role_id = #{roleId}
</delete>
<!-- 删除用户和部门关联表数据 -->
<delete id="deleteUserOffice">
DELETE FROM sys_inter_user_office WHERE user_id = #{id}
</delete>
<!-- 逻辑删除用户 -->
<update id="delete">
UPDATE sys_user SET
status = #{DEL_FLAG_DELETE}
WHERE id = #{id}
</update>
<!-- 更新用户信息 -->
<update id="updateUserInfo">
UPDATE sys_user SET
email = #{email},
photo = #{photo}
WHERE id = #{id}
</update>
<!-- 更新用户密码 -->
<update id="updatePasswordById">
UPDATE sys_user SET
password = #{password}
WHERE id = #{id}
</update>
<select id="findDeptLeader" parameterType="Integer" resultType="map">
SELECT t3.* FROM (
SELECT t2.id,t2.name FROM sys_inter_user_office t1
LEFT JOIN sys_user t2 ON t1.USER_ID=t2.ID
WHERE t1.office_id=#{deptementId} AND t2.status=1) t3
LEFT JOIN sys_inter_user_role t4 ON t3.id=t4.user_Id
LEFT JOIN sys_role t5 ON t5.id=t4.role_id
WHERE t5.id=7 AND t5.status=1
</select>
</mapper>