<?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.SsoUserDeptMapper">

    <resultMap id="BaseResultMap" type="com.sso.dao.entity.SsoUserDept">
      <result column="sys_code" property="sysCode" jdbcType="VARCHAR"/>
      <result column="user_id" property="userId" jdbcType="BIGINT"/>
      <result column="dept_id" property="deptId" jdbcType="BIGINT"/>
      <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
      <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
      sys_code,
      user_id,
      dept_id,
      create_time,
      update_time
    </sql>

    <select id="getDeptBySysCodeAndUserId" resultMap="BaseResultMap">
      SELECT
      <include refid="Base_Column_List"/>
      FROM sso_user_dept
      WHERE sys_code = #{sysCode,jdbcType=VARCHAR}
      AND user_id = #{userId,jdbcType=BIGINT}
      LIMIT 1
    </select>

    <insert id="insert" parameterType="com.sso.dao.entity.SsoUserDept">
      INSERT INTO sso_user_dept
       (
        sys_code,
        user_id,
        dept_id
       )
      values
       (
          #{sysCode,jdbcType=VARCHAR},
          #{userId,jdbcType=BIGINT},
          #{deptId,jdbcType=BIGINT}
       )
    </insert>

    <insert id="insertSelective" parameterType="com.sso.dao.entity.SsoUserDept">
      INSERT INTO sso_user_dept
      <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="sysCode != null">
        sys_code,
        </if>
        <if test="userId != null">
        user_id,
        </if>
        <if test="deptId != null">
        dept_id,
        </if>
        <if test="createTime != null">
        create_time,
        </if>
        <if test="updateTime != null">
        update_time,
        </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="sysCode != null">
          #{sysCode,jdbcType=VARCHAR},
        </if>
        <if test="userId != null">
          #{userId,jdbcType=BIGINT},
        </if>
        <if test="deptId != null">
          #{deptId,jdbcType=BIGINT},
        </if>
        <if test="createTime != null">
          #{createTime,jdbcType=TIMESTAMP},
        </if>
        <if test="updateTime != null">
          #{updateTime,jdbcType=TIMESTAMP},
        </if>
      </trim>
    </insert>

    <delete id="deleteBySysCodeAndUserId">
      DELETE FROM sso_user_dept
      WHERE sys_code = #{sysCode,jdbcType=VARCHAR}
      AND user_id = #{userId,jdbcType=BIGINT}
    </delete>

    <delete id="deleteBySysCodeAndDeptId">
      DELETE FROM sso_user_dept
      WHERE  sys_code = #{sysCode,jdbcType=VARCHAR}
      AND  dept_id = #{deptId,jdbcType=BIGINT}
    </delete>

    <update id="updateByPrimaryKey" parameterType="com.sso.dao.entity.SsoUserDept">
      UPDATE sso_user_dept
      SET
       sys_code = #{sysCode,jdbcType=VARCHAR},
       user_id = #{userId,jdbcType=BIGINT},
       dept_id = #{deptId,jdbcType=BIGINT},
       create_time = #{createTime,jdbcType=TIMESTAMP},
       update_time = #{updateTime,jdbcType=TIMESTAMP}
      WHERE id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateByPrimaryKeySelective" parameterType="com.sso.dao.entity.SsoUserDept">
      UPDATE sso_user_dept
      <set>
        <if test="sysCode != null">
         sys_code = #{sysCode,jdbcType=VARCHAR},
        </if>
        <if test="userId != null">
         user_id = #{userId,jdbcType=BIGINT},
        </if>
        <if test="deptId != null">
         dept_id = #{deptId,jdbcType=BIGINT},
        </if>
        <if test="createTime != null">
         create_time = #{createTime,jdbcType=TIMESTAMP},
        </if>
        <if test="updateTime != null">
         update_time = #{updateTime,jdbcType=TIMESTAMP},
        </if>
      </set>
      WHERE id = #{id,jdbcType=BIGINT}
    </update>

    <select id="countByDeptId" resultType="java.lang.Integer" parameterType="java.lang.Long">
        SELECT
         COUNT(*)
        FROM sso_user_dept
        WHERE dept_id = #{deptId,jdbcType=BIGINT}
    </select>

</mapper>
