UserMapper.xml 11.6 KB
<?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.huaheng.pc.system.user.mapper.UserMapper">

    <resultMap type="com.huaheng.pc.system.user.domain.User" id="UserResult">
        <id property="id" column="id"/>
        <result property="deptId" column="deptId"/>
        <result property="loginName" column="loginName"/>
        <result property="userName" column="userName"/>
        <result property="email" column="email"/>
        <result property="phoneNumber" column="phoneNumber"/>
        <result property="sex" column="sex"/>
        <result property="avatar" column="avatar"/>
        <result property="password" column="password"/>
        <result property="salt" column="salt"/>
        <result property="enable" column="enable"/>
        <result property="deleted" column="deleted"/>
        <result property="loginIp" column="loginIp"/>
        <result property="loginDate" column="loginDate"/>
        <result property="createBy" column="createBy"/>
        <result property="createTime" column="createTime"/>
        <result property="updateBy" column="updateBy"/>
        <result property="updateTime" column="updateTime"/>
        <result property="station" column="station"/>
        <result property="remark" column="remark"/>
        <association property="dept" column="deptId" resultMap="deptResult"
                     javaType="com.huaheng.pc.system.dept.domain.Dept"/>
    </resultMap>

    <resultMap id="deptResult" type="com.huaheng.pc.system.dept.domain.Dept">
        <id property="id" column="deptId"/>
        <result property="parentId" column="parentId"/>
        <result property="deptName" column="deptName"/>
        <result property="orderNum" column="orderNum"/>
        <result property="enable" column="dept_enable"/>
    </resultMap>

    <sql id="selectUserVo">
        select u.id,
               u.deptId,
               u.loginName,
               u.userName,
               u.email,
               u.phoneNumber,
               u.sex,
               u.avatar,
               u.password,
               u.salt,
               u.enable,
               u.deleted,
               u.loginIp,
               u.loginDate,
               u.createTime,
               u.remark,
               u.station,
               d.id     AS deptId,
               d.parentId,
               d.deptName,
               d.orderNum,
               d.enable as dept_enable
        from sys_user u
                 left join sys_dept d on u.deptId = d.id
                 left join sys_user_role ur on u.id = ur.userId
    </sql>

    <select id="selectUserList" resultMap="UserResult">
        select u.id, u.deptId, u.loginName, u.userName, u.email, u.phoneNumber, u.password, u.sex, u.avatar, u.salt,
        u.enable, u.deleted,u.station,
        u.loginIp, u.loginDate, u.createBy, u.createTime, u.updateTime, u.updateBy, u.remark, d.deptName
        from sys_user u
        left join sys_dept d on u.deptId = d.id
        WHERE u.deleted = false
        <if test="loginName != null and loginName != ''">
            AND u.loginName like concat('%', #{loginName}, '%')
        </if>
        <if test="userName != null and userName != ''">
            AND u.userName like concat('%', #{userName}, '%')
        </if>
        <if test="enable != null">
            AND u.enable = #{enable}
        </if>
        <if test="phoneNumber != null and phoneNumber != ''">
            AND u.phoneNumber like concat('%', #{phoneNumber}, '%')
        </if>
        <if test="params != null and params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(u.createTime,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
        </if>
        <if test="params != null and params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
            AND date_format(u.createTime,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
        </if>
        <if test="deptId != null and deptId != 0">
            AND (u.deptId = #{deptId} OR u.deptId IN ( SELECT t.id FROM sys_dept t WHERE FIND_IN_SET
            (#{deptId},ancestors) ))
        </if>
    </select>

    <select id="selectUserByLoginName" resultMap="UserResult">
        <include refid="selectUserVo"/>
        where u.loginName = #{userName}
    </select>


    <select id="selectUserByPhoneNumber" resultMap="UserResult">
        <include refid="selectUserVo"/>
        where u.phoneNumber = #{phoneNumber}
    </select>

    <select id="selectUserByEmail" resultMap="UserResult">
        <include refid="selectUserVo"/>
        where u.email = #{email}
    </select>

    <select id="checkLoginNameUnique" resultType="java.lang.Integer">
        select count(1)
        from sys_user
        where loginName = #{loginName}
    </select>

    <select id="checkPhoneUnique" resultMap="UserResult">
        select id, phoneNumber
        from sys_user
        where phoneNumber = #{phoneNumber}
    </select>

    <select id="checkEmailUnique" resultMap="UserResult">
        select id, email
        from sys_user
        where email = #{email}
    </select>

    <select id="selectUserById" resultMap="UserResult">
        <include refid="selectUserVo"/>
        where u.id = #{id}
    </select>

    <select id="selectmen" resultType="com.huaheng.pc.system.user.domain.User">
        select id,
               deptId,
               loginName,
               userName,
               email,
               phoneNumber,
               password,
               sex,
               avatar,
               salt,
               enable,
               deleted,
               loginIp,
               loginDate,
               createBy,
               createTime,
               updateTime,
               updateBy,
               remark
        from sys_user
        where loginName = #{loginName}
    </select>

    <delete id="deleteUserById">
        delete
        from sys_user
        where id = #{id}
    </delete>

    <delete id="deleteUserByIds">
        update sys_user set deleted = true where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="updateUser">
        update sys_user
        <set>
            <if test="deptId != null and deptId != 0">deptId = #{deptId},</if>
            <if test="loginName != null and loginName != ''">loginName = #{loginName},</if>
            <if test="userName != null and userName != ''">userName = #{userName},</if>
            <if test="email != null and email != ''">email = #{email},</if>
            <if test="phoneNumber != null and phoneNumber != ''">phoneNumber = #{phoneNumber},</if>
            <if test="sex != null and sex != ''">sex = #{sex},</if>
            <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="salt != null and salt != ''">salt = #{salt},</if>
            <if test="enable != null ">enable = #{enable},</if>
            <if test="loginIp != null and loginIp != ''">loginIp = #{loginIp},</if>
            <if test="loginDate != null">loginDate = #{loginDate},</if>
            <if test="updateBy != null and updateBy != ''">updateBy = #{updateBy},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            updateTime = sysdate()
        </set>
        <where>
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="loginName != null">
                AND loginName = #{loginName}
            </if>
        </where>
    </update>
    <!--where id = #{id}-->

    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
        insert into sys_user(
        <if test="deptId != null and deptId != 0">deptId,</if>
        <if test="loginName != null and loginName != ''">loginName,</if>
        <if test="userName != null and userName != ''">userName,</if>
        <if test="email != null and email != ''">email,</if>
        <if test="phoneNumber != null and phoneNumber != ''">phoneNumber,</if>
        <if test="sex != null and sex != ''">sex,</if>
        <if test="password != null and password != ''">password,</if>
        <if test="salt != null and salt != ''">salt,</if>
        <if test="enable != null ">enable,</if>
        <if test="createBy != null and createBy != ''">createBy,</if>
        <if test="remark != null and remark != ''">remark,</if>
        createTime
        )values(
        <if test="deptId != null and deptId != ''">#{deptId},</if>
        <if test="loginName != null and loginName != ''">#{loginName},</if>
        <if test="userName != null and userName != ''">#{userName},</if>
        <if test="email != null and email != ''">#{email},</if>
        <if test="phoneNumber != null and phoneNumber != ''">#{phoneNumber},</if>
        <if test="sex != null and sex != ''">#{sex},</if>
        <if test="password != null and password != ''">#{password},</if>
        <if test="salt != null and salt != ''">#{salt},</if>
        <if test="enable != null ">#{enable},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        sysdate()
        )
    </insert>

    <select id="getWarehouseByUserId" resultType="java.util.HashMap">
        SELECT DISTINCT warehouse.code, warehouse.name
        FROM sys_user_role
                 INNER JOIN sys_role ON sys_user_role.roleId = sys_role.id AND
                                        sys_user_role.userId = #{userId, jdbcType=INTEGER} AND
                                        sys_role.enable = TRUE AND sys_role.deleted = FALSE
                 INNER JOIN warehouse ON sys_role.warehouseCode = warehouse.code AND warehouse.enable = TRUE AND
                                         warehouse.deleted = FALSE
    </select>

    <select id="getWarehouseByUserCode" resultType="java.util.HashMap">
        SELECT DISTINCT warehouse.code, warehouse.name
        FROM sys_user_role
                 INNER JOIN sys_user ON sys_user_role.userId = sys_user.id AND
                                        sys_user.loginName = #{loginName,jdbcType=VARCHAR} AND
                                        sys_user.enable = TRUE AND sys_user.DELETED = FALSE
                 INNER JOIN sys_role ON sys_user_role.roleId = sys_role.id AND sys_user_role.userId = sys_user.Id AND
                                        sys_role.enable = TRUE
                 INNER JOIN warehouse ON sys_role.warehouseCode = warehouse.code AND warehouse.enable = TRUE
    </select>

    <select id="getWarehouseByUserName" resultType="java.util.HashMap">
        SELECT r.`name`, r.code
        FROM sys_user u
                 LEFT JOIN sys_user_warehouse ur ON u.id = ur.userId
                 LEFT JOIN warehouse r ON ur.warehouseCode = r.code
        WHERE u.loginName = #{loginName,jdbcType=VARCHAR}
          AND r.deleted = FALSE
    </select>

    <update id="insertupdateTime">
        update sys_user
        set updateTime = #{date}
        where loginName = #{cPersonCode}
    </update>
    <update id="bindUserStation">
        update sys_user
        set station = #{stationCode}
        where id = #{userId}
    </update>
    <select id="checkWarehouseCodeAndUserName" resultType="java.util.Map">
        SELECT r.`name`, r.code
        FROM sys_user u
                 LEFT JOIN sys_user_warehouse ur ON u.id = ur.userId
                 LEFT JOIN warehouse r ON ur.warehouseCode = r.code
        WHERE u.loginName = #{loginName,jdbcType=VARCHAR}
          AND r.`code` = #{warehouseCode,jdbcType=VARCHAR}
    </select>

</mapper>