<?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.sso.dao.mapper.SsoSystemManagerMapper">

    <resultMap id="BaseResultMap" type="com.sso.dao.entity.SsoSystemManager">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="sys_code" property="sysCode" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="del_flag" property="delFlag" jdbcType="INTEGER"/>
        <result column="create_by" property="createBy" jdbcType="VARCHAR"/>
        <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="SysMgmtResultMap" type="com.sso.dao.dto.SsoUserMgmtDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="user_id" property="userId" jdbcType="BIGINT"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="nick_name" property="nickName" jdbcType="VARCHAR"/>
        <result column="real_name" property="realName" jdbcType="VARCHAR"/>
        <result column="sex" property="sex" jdbcType="INTEGER"/>
        <result column="phone" property="phone" jdbcType="VARCHAR"/>
        <result column="email" property="email" jdbcType="VARCHAR"/>
        <result column="user_status" property="userStatus" jdbcType="INTEGER"/>
        <result column="relation_status" property="relationStatus" jdbcType="INTEGER"/>
        <result column="create_by" property="createBy" jdbcType="VARCHAR"/>
        <result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
      id,
      user_id,
      sys_code,
      status,
      del_flag,
      create_by,
      update_by,
      create_time,
      update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT
        <include refid="Base_Column_List"/>
        FROM sso_system_manager
        WHERE id = #{id,jdbcType=BIGINT}
    </select>

    <select id="getBySysCodeAndUserId" resultMap="BaseResultMap" >
        SELECT
        <include refid="Base_Column_List"/>
        FROM sso_system_manager
        WHERE del_flag = 0
        AND user_id = #{userId,jdbcType=BIGINT}
        AND sys_code = #{sysCode,jdbcType=VARCHAR}
    </select>

    <select id="countBySysCodeAndUserId" resultType="java.lang.Integer">
        SELECT
         COUNT(*)
        FROM sso_system_manager
        WHERE del_flag = 0
        AND user_id = #{userId,jdbcType=BIGINT}
        AND sys_code = #{sysCode,jdbcType=VARCHAR}
    </select>

    <select id="countSysMgmtUser" resultType="java.lang.Integer" parameterType="com.sso.dao.query.UserPageQuery">
        SELECT
        COUNT(*)
        FROM sso_system_manager a
        INNER JOIN sso_user b on a.user_id=b.user_id
        <where>
            <if test="sysCode != null">
                AND a.sys_code =#{sysCode,jdbcType=VARCHAR}
            </if>
            <if test="usernameLike != null">
                AND b.username LIKE CONCAT(#{usernameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="nickNameLike != null">
                AND b.nick_name LIKE CONCAT(#{nickNameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="realNameLike != null">
                AND b.real_name LIKE CONCAT(#{realNameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="phoneLike != null">
                AND b.phone LIKE CONCAT(#{phoneLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="userStatus != null and userStatus !=-1">
                AND b.status = #{userStatus,jdbcType=INTEGER}
            </if>
            <if test="relationStatus != null and relationStatus !=-1">
                AND a.status = #{relationStatus,jdbcType=INTEGER}
            </if>
            <if test="delFlag != null">
                AND a.del_flag = #{delFlag,jdbcType=INTEGER}
            </if>
        </where>
    </select>

    <select id="listPageSysMgmtUser" resultMap="SysMgmtResultMap" parameterType="com.sso.dao.query.UserPageQuery">
        SELECT
        a.id,
        b.user_id,
        b.username,
        b.nick_name,
        b.real_name,
        b.sex,
        b.phone,
        b.email,
        b.`status` user_status,
        a.`status` relation_status,
        a.create_by,
        a.create_time,
        a.update_by,
        a.update_time
        FROM sso_system_manager a
        INNER JOIN sso_user b on a.user_id=b.user_id
        <where>
            <if test="sysCode != null">
                AND a.sys_code =#{sysCode,jdbcType=VARCHAR}
            </if>
            <if test="usernameLike != null">
                AND b.username LIKE CONCAT(#{usernameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="nickNameLike != null">
                AND b.nick_name LIKE CONCAT(#{nickNameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="realNameLike != null">
                AND b.real_name LIKE CONCAT(#{realNameLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="phoneLike != null">
                AND b.phone LIKE CONCAT(#{phoneLike,jdbcType=VARCHAR},'%')
            </if>
            <if test="userStatus != null and userStatus !=-1">
                AND b.status = #{userStatus,jdbcType=INTEGER}
            </if>
            <if test="relationStatus != null and relationStatus !=-1">
                AND a.status = #{relationStatus,jdbcType=INTEGER}
            </if>
            <if test="delFlag != null">
                AND a.del_flag = #{delFlag,jdbcType=INTEGER}
            </if>
        </where>
        ORDER BY a.id desc
        LIMIT #{startRow}, #{pageSize}
    </select>

    <insert id="insert" parameterType="com.sso.dao.entity.SsoSystemManager">
      INSERT INTO sso_system_manager
       (
        id,
        user_id,
        sys_code,
        status,
        del_flag,
        create_by,
        update_by,
        create_time,
        update_time
       )
      values
       (
          #{id,jdbcType=BIGINT},
          #{userId,jdbcType=BIGINT},
          #{sysCode,jdbcType=VARCHAR},
          #{status,jdbcType=INTEGER},
          #{delFlag,jdbcType=INTEGER},
          #{createBy,jdbcType=VARCHAR},
          #{updateBy,jdbcType=VARCHAR},
          #{createTime,jdbcType=TIMESTAMP},
          #{updateTime,jdbcType=TIMESTAMP}
       )
    </insert>

    <insert id="insertSelective" parameterType="com.sso.dao.entity.SsoSystemManager">
        INSERT INTO sso_system_manager
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="sysCode != null">
                sys_code,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=BIGINT},
            </if>
            <if test="sysCode != null">
                #{sysCode,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                #{status,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                #{delFlag,jdbcType=INTEGER},
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>

    <update id="updateById" parameterType="com.sso.dao.entity.SsoSystemManager">
        UPDATE sso_system_manager
        <set>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag,jdbcType=INTEGER},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR},
            </if>
        </set>
        WHERE id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateBySysCodeAndUserId" parameterType="com.sso.dao.entity.SsoSystemManager">
        UPDATE sso_system_manager
        <set>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag,jdbcType=INTEGER},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR},
            </if>
        </set>
        WHERE del_flag = 0
        AND user_id = #{userId,jdbcType=BIGINT}
        AND sys_code = #{sysCode,jdbcType=VARCHAR}
    </update>

</mapper>
