<?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.css.simulation.resource.server.infra.db.mysql.mapper.SystemUserMapper" >


    <select id="getUserInfo" parameterType="java.lang.String" resultType="api.common.pojo.vo.system.UserVO">
        SELECT
            id,
            username,
            nickname,
            photo,
            phone,
            company,
            role_code,
            use_type
        FROM system_user WHERE id = #{userId,jdbcType=VARCHAR}
    </select>

    <!-- 列表查询共用字段-->
    <sql id="pageColumn">
        u.id,
        u.username,
        u.nickname,
        u.photo,
        u.phone,
        u.company,
        u.role_code,
        u.use_type,
        u.visible,
        u.create_time,
        t.username as createUserName
    </sql>
    <!-- 列表查询共用查询条件-->
    <sql id="pageWhere">
        <if test="username != null and username != ''">
            and u.username like CONCAT('%',#{username,jdbcType=VARCHAR},'%')
        </if>
        <if test="nickname != null and nickname != ''">
            and u.nickname like CONCAT('%',#{nickname,jdbcType=VARCHAR},'%')
        </if>
        <if test="company != null and company != ''">
            and u.company like CONCAT('%',#{company,jdbcType=VARCHAR},'%')
        </if>
        <if test="roleCode != null and roleCode != ''">
            and u.role_code = #{roleCode,jdbcType=VARCHAR}
        </if>
        <if test="visible != null and visible != ''">
            and u.visible = #{visible,jdbcType=VARCHAR}
        </if>
    </sql>

    <select id="getUserPageLista" parameterType="api.common.pojo.param.system.UserParam" resultType="api.common.pojo.vo.system.UserVO">
        SELECT
           <include refid="pageColumn"></include>
        FROM system_user u
        JOIN system_user t ON u.create_user_id = t.id
        WHERE u.is_deleted = '0' AND u.role_code &lt;&gt; '0' and u.role_code &lt;&gt; '3'
        <include refid="pageWhere"></include>
        ORDER BY u.modify_time DESC
    </select>

    <select id="getUserPageListb" parameterType="api.common.pojo.param.system.UserParam" resultType="api.common.pojo.vo.system.UserVO">
        SELECT
        <include refid="pageColumn"></include>
        FROM system_user u
        JOIN system_user t ON u.create_user_id = t.id
        WHERE u.is_deleted = '0' AND u.role_code = '2'
        <include refid="pageWhere"></include>
        ORDER BY u.modify_time DESC
    </select>

    <select id="getUserPageListc" parameterType="api.common.pojo.param.system.UserParam" resultType="api.common.pojo.vo.system.UserVO">
        SELECT
        <include refid="pageColumn"></include>
        FROM system_user u
        JOIN system_user t ON u.create_user_id = t.id
        WHERE u.is_deleted = '0' AND u.role_code = '3'
        <include refid="pageWhere"></include>
        and u.create_user_id = #{createUserId,jdbcType=VARCHAR}
        ORDER BY u.modify_time DESC
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(*)
        from system_user
        where is_deleted = '0'
    </select>

    <select id="checkUsername" parameterType="api.common.pojo.po.system.UserPO" resultType="api.common.pojo.po.system.UserPO">
        select
          id
        from system_user
        where is_deleted = '0'
        and username = #{username,jdbcType=VARCHAR}
        <if test="id != null and id != ''">
            and id &lt;&gt; #{id,jdbcType=VARCHAR}
        </if>
    </select>

    <select id="checkPassword" parameterType="api.common.pojo.vo.system.UserVO" resultType="api.common.pojo.vo.system.UserVO">
        select
        id
        from system_user
        where is_deleted = '0'
        and password = #{password,jdbcType=VARCHAR}
        and id = #{id,jdbcType=VARCHAR}
    </select>

    <insert id="insert" parameterType="api.common.pojo.po.system.UserPO">
        INSERT INTO system_user(
            id,
            username,
            password,
            nickname,
            photo,
            phone,
            company,
            role_code,
            use_type,
            visible,
            create_time,
            modify_time,
            create_user_id,
            modify_user_id,
            is_deleted
        )
        VALUES(
            #{id,jdbcType=VARCHAR},
            #{username,jdbcType=VARCHAR},
            #{password,jdbcType=VARCHAR},
            #{nickname,jdbcType=VARCHAR},
            #{photo,jdbcType=VARCHAR},
            #{phone,jdbcType=VARCHAR},
            #{company,jdbcType=VARCHAR},
            #{roleCode,jdbcType=VARCHAR},
            #{useType,jdbcType=VARCHAR},
            #{visible,jdbcType=VARCHAR},
            #{createTime,jdbcType=TIMESTAMP},
            #{modifyTime,jdbcType=TIMESTAMP},
            #{createUserId,jdbcType=VARCHAR},
            #{modifyUserId,jdbcType=VARCHAR},
            #{isDeleted,jdbcType=VARCHAR}
        )
    </insert>

    <update id="update" parameterType="api.common.pojo.po.system.UserPO">
        update system_user set
            username = #{username,jdbcType=VARCHAR},
            nickname = #{nickname,jdbcType=VARCHAR},
            <!--photo = #{photo,jdbcType=VARCHAR},-->
            phone = #{phone,jdbcType=VARCHAR},
            company = #{company,jdbcType=VARCHAR},
            <!--visible = #{visible,jdbcType=VARCHAR},-->

        modify_time = #{modifyTime,jdbcType=TIMESTAMP},
        modify_user_id = #{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <update id="saveDefaultPassword" parameterType="api.common.pojo.po.system.UserPO">
        update system_user set
            password = #{password,jdbcType=VARCHAR},
            modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <update id="saveVisible" parameterType="api.common.pojo.po.system.UserPO">
        update system_user set
            visible = #{visible,jdbcType=VARCHAR},
            modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <update id="savePhone" parameterType="api.common.pojo.po.system.UserPO">
        update system_user set
            nickname = #{nickname,jdbcType=VARCHAR},
            phone = #{phone,jdbcType=VARCHAR},
            modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <update id="savePassword" parameterType="api.common.pojo.po.system.UserPO">
        update system_user set
            password = #{password,jdbcType=VARCHAR},
            modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            modify_user_id = #{modifyUserId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <select id="getSubUser" parameterType="java.lang.String" resultType="api.common.pojo.po.system.UserPO">
        select * from system_user
        where is_deleted = '0' AND create_user_id = #{createUserId}
    </select>
    <select id="selectCreateUserIdById" resultType="java.lang.String">
        select create_user_id from system_user where id = #{id}
    </select>
    <select id="selectRoleCodeById" resultType="java.lang.String">
        select role_code from system_user where id = #{id}
    </select>
</mapper>
