<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.sys.dao.UserDAO">
    <resultMap id="UserMap" type="com.jpsoft.shinestar.modules.sys.entity.User">
        <id property="id" column="id_"/>
        <result property="userName" column="user_name"/>
        <result property="companyId" column="company_id" />
        <result property="companyName" column="company_name" />
        <result property="password" column="password_"/>
        <result property="realName" column="real_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="openId" column="open_id"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.sys.entity.User">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into sys_user
	    (id_,company_id,user_name,password_,real_name,create_time,update_time,
	    del_flag,create_by,update_by,open_id)
		values
		(
            #{id,jdbcType=VARCHAR}
            ,#{companyId,jdbcType=VARCHAR}
            ,#{userName,jdbcType=VARCHAR}
            ,#{password,jdbcType=VARCHAR}
            ,#{realName,jdbcType=VARCHAR}
            ,#{createTime,jdbcType= TIMESTAMP }
            ,#{updateTime,jdbcType= TIMESTAMP }
            ,#{delFlag,jdbcType= NUMERIC }
            ,#{createBy,jdbcType=VARCHAR}
            ,#{updateBy,jdbcType=VARCHAR}
            ,#{openId,jdbcType=VARCHAR}
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from sys_user where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.sys.entity.User">
        update sys_user
        <set>
            <if test="userName!=null">
                user_name=#{userName,jdbcType=VARCHAR},
            </if>
            <if test="companyId!=null">
                company_id=#{companyId,jdbcType=VARCHAR},
            </if>
            <if test="password!=null">
                password_=#{password,jdbcType=VARCHAR},
            </if>
            <if test="realName!=null">
                real_name=#{realName,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="openId!=null">
                open_id=#{openId,jdbcType=VARCHAR},
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="UserMap">
        select * from sys_user where id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from sys_user where id_=#{0}
    </select>
    <select id="list" resultMap="UserMap">
        select * from sys_user
    </select>
    <select id="findByCompanyId" parameterType="string" resultMap="UserMap">
        select * from sys_user where company_id = #{0} and  del_flag = 0
    </select>

    <select id="findByCompanyList" resultMap="UserMap">
        select * from sys_user
        <where>
            del_flag = 0
            <if test="searchParams.companyList!=null">
                <foreach collection="searchParams.companyList" index="index" item="item" open="and company_id in("
                         separator="," close=")">
                    #{item.id}
                </foreach>
            </if>
        </where>
    </select>
    <select id="findByUserName" parameterType="string" resultMap="UserMap">
        select * from sys_user where user_name=#{userName} and del_flag=0 limit 1
    </select>
    <select id="search" parameterType="hashmap" resultMap="UserMap">
        <![CDATA[
			select a.*,b.name_ as company_name
			from sys_user a LEFT JOIN base_company_info b ON a.company_id = b.id_
			where a.del_flag = 0
		]]>
        <if test="searchParams.userName != null">
            and a.user_name like #{searchParams.userName}
        </if>
        <if test="searchParams.realName != null">
            and a.real_name like #{searchParams.realName}
        </if>
        <if test="searchParams.companyId != null">
            and a.company_id = #{searchParams.companyId}
        </if>
        <if test="searchParams.roleId != null">
            and a.id_ in (select c.user_id from sys_user_role c where c.role_id = #{searchParams.roleId})
        </if>
        <if test="searchParams.noRoleId != null">
            AND a.id_ NOT IN ( SELECT user_id FROM sys_user_role WHERE del_flag = 0 AND role_id = #{searchParams.noRoleId} )
        </if>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="hasRole" parameterType="string" resultType="long">
        select count(*) from sys_user_role ur,sys_role r
        where ur.role_id = r.id_
        and ur.user_id = #{userId} and r.name_=#{roleName}
    </select>
    <select id="findByOpenId" resultMap="UserMap">
        select * from sys_user
        where open_id=#{0} and del_flag=0
        limit 1
    </select>
</mapper>