<?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.ytpm.middle.dao.MiddleUserMapper">
    <insert id="insertOne">
        INSERT INTO yt_platform_user (
            user_id,
            nick_name,
            head_image,
            login_name,
            encrypt_pwd,
            salt,
            phone,
            last_login_time,
            account_status,
            user_type,
            registry_time,
            dept_id
        )
        VALUES
        (
            #{userId},
            #{nickName},
            #{headImage},
            #{loginName},
            #{encryptPwd},
            #{salt},
            #{phone},
            #{lastLoginTime},
            #{accountStatus},
            #{userType},
            #{registryTime},
            #{deptId}
        );
    </insert>
    <insert id="addUserRoleList">
        insert into yt_middle_user_role (user_id, role_id, create_time, create_user_id)
        values
        <foreach collection="roleIdList" separator="," item="item">
            (#{userId},#{item},now(),#{operatorId})
        </foreach>
    </insert>
    <update id="updateById">
        update yt_platform_user
        <set>
            <if test="nickName != null">
                nick_name = #{nickName},
            </if>
            <if test="headImage != null">
                head_image = #{headImage},
            </if>
            <if test="loginName != null">
                login_name = #{loginName},
            </if>
            <if test="encryptPwd != null">
                encrypt_pwd = #{encryptPwd},
            </if>
            <if test="salt != null">
                salt = #{salt},
            </if>
            <if test="phone != null">
                phone = #{phone},
            </if>
            <if test="lastLoginTime != null">
                last_login_time = #{lastLoginTime},
            </if>
            <if test="accountStatus != null">
                account_status = #{accountStatus},
            </if>
            <if test="userType != null">
                user_type = #{userType},
            </if>
            <if test="registryTime != null">
                registry_time = #{registryTime},
            </if>
            <if test="deptId != null">
                dept_id = #{deptId}
            </if>
        </set>
        where user_id = #{userId}
    </update>
    <delete id="deleteByUserId">
        delete from yt_middle_user_role where user_id = #{userId}
    </delete>

    <select id="getByLoginName" resultMap="MiddleUserMap">
        select
            u.user_id,
            u.nick_name,
            u.head_image,
            u.login_name,
            u.encrypt_pwd,
            u.salt,
            u.phone,
            u.last_login_time,
            u.account_status,
            u.user_type,
            u.registry_time,
            u.super_admin,
            u.account_status,
            d.dept_id,
            d.dept_name,
            r.role_id,
            (select role_name from yt_middle_role where role_id = r.role_id) roleName
        from yt_platform_user u
        left join yt_middle_dept d on u.dept_id = d.dept_id
        left join yt_middle_user_role r on u.user_id = r.user_id
        where u.user_type = 99 and u.account_status = 1 and u.login_name = #{userName}
    </select>
    <select id="getRoleList" resultType="com.ytpm.middle.view.MiddleRoleVO">
        select
        ymur.role_id,
        ymr.role_name,
        ymr.role_desc,
        ymr.`range`
        from yt_middle_user_role ymur
        join yt_middle_role ymr on ymur.role_id = ymr.role_id
        where ymur.user_id = #{userId}
    </select>
    <select id="getSuperAdmin" resultType="com.ytpm.middle.view.MiddleRoleVO">
        select
        ymur.role_id,
        ymr.role_name,
        ymr.role_desc,
        ymr.`range`
        from yt_middle_user_role ymur
        join yt_middle_role ymr on ymur.role_id = ymr.role_id
    </select>
    <resultMap id="MiddleUserMap" type="com.ytpm.middle.view.MiddleUserInfo">
        <id column="user_id" property="userId" />
        <result column="dept_id" property="deptId" />
        <result column="dept_name" property="deptName" />
        <result column="nick_name" property="nickName" />
        <result column="head_image" property="headImage" />
        <result column="account_status" property="accountStatus" />
        <result column="login_name" property="loginName" />
        <result column="super_admin" property="superAdmin" />
        <result column="phone" property="phone" />
        <result column="last_login_time" property="lastLoginTime" />
        <collection property="roleList" ofType="com.ytpm.middle.view.MiddleRoleVO">
            <id column="role_id" property="roleId" />
            <result column="roleName" property="roleName" />
        </collection>
    </resultMap>
    <select id="queryList" resultMap="MiddleUserMap">
        select
            ypu.user_id,
            ypu.dept_id,
            ymd.dept_name,
            ymur.role_id,
            (select role_name from yt_middle_role where role_id = ymur.role_id) roleName,
            ypu.nick_name,
            ypu.head_image,
            ypu.login_name,
            ypu.account_status,
            ypu.phone,
            ypu.super_admin,
            ypu.last_login_time
        from yt_platform_user ypu
        left join yt_middle_user_role ymur on ypu.user_id = ymur.user_id
        left join yt_middle_dept ymd on ypu.dept_id = ymd.dept_id
        where ypu.user_type = 99 and ypu.account_status = 1
        <if test=" deptId != null ">
            and ypu.dept_id = #{deptId}
        </if>
        <if test=" nickName!= null and nickName!= ''">
            and ypu.nick_name like concat('%', #{nickName} ,'%')
        </if>
        <if test="loginName != null and loginName!= ''">
            and ypu.login_name like concat('%', #{loginName} ,'%')
        </if>
        <if test="phone != null and phone!= ''">
            and ypu.phone like concat('%', #{phone} ,'%')
        </if>
        <if test="accountStatus != null">
            and ypu.account_status = #{accountStatus}
        </if>
    </select>
    <select id="getByPhone" resultType="com.ytpm.middle.view.MiddleUserInfo">
        select
            user_id, nick_name, head_image, login_name, encrypt_pwd, salt, phone, last_login_time,  account_status,  user_type,  registry_time
        from yt_platform_user
        where user_type = 99 and account_status = 1 and phone = #{phone}
    </select>
    <select id="selectPrimary" resultType="com.ytpm.middle.view.MiddleUserInfo">
        select
            user_id, nick_name, head_image, login_name, encrypt_pwd, salt, phone, last_login_time,  account_status,  user_type,  registry_time
        from yt_platform_user
        where user_id = #{userId}
    </select>
    <select id="getByIds" resultType="com.ytpm.middle.model.YtMiddleUser">
        select
        from yt_platform_user
        where user_id in
        <foreach collection="userIds" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
</mapper>