<?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.jkcredit.invoice.hub.mapper.UserMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="baseResultMap" type="com.jkcredit.invoice.hub.model.po.user.UserPo">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="app_key" property="appKey"/>
        <result column="app_secret" property="appSecret"/>
        <result column="company" property="company"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="activated" property="activated"/>
        <result column="user_type" property="userType"/>
        <result column="status" property="status"/>
        <result column="contact_name" property="contactName"/>
        <result column="telephone" property="telephone"/>
        <result column="price" property="price"/>
        <result column="role_name" property="roleName"/>
        <result column="balance" property="balance"/>
    </resultMap>

    <sql id="userSql">
        id,
        username,
        password,
        app_key,
        app_secret,
        company,
        create_time,
        update_time,
        activated,
        user_type,
        status,
        contact_name,
        telephone,
        price
    </sql>
    <select id="getUserByUserName" resultMap="baseResultMap">
        select
        <include refid="userSql"/>
        from
        h_user
        where
        username = #{userName}
    </select>

    <select id="getUsersPage" resultMap="baseResultMap">
        select
        user.id,
        user.username,
        user.app_key,
        user.company,
        user.contact_name,
        user.telephone,
        user.price,
        user.status,
        user.create_time,
        balance.balance,
        roleName.role_name
        from
        h_user user left join h_user_balance balance on user.id = balance.user_id
        left join (
        select distinct user_id, role.role_name from h_user_role_permission permission
        left join h_role role on permission.role_id = role.id) roleName on user.id = roleName.user_id
        <where>
            and user.activated = 1
            <if test="query.username != null and query.username != ''">
                and user.username like CONCAT('%',#{query.username},'%')
            </if>
            <if test="query.appKey != null and query.appKey != ''">
                and user.app_key like CONCAT('%',#{query.appKey},'%')
            </if>
            <if test="query.company != null and query.company != ''">
                and user.company = #{query.company}
            </if>
        </where>
        order by user.create_time desc
    </select>

    <select id="getUserById" resultMap="baseResultMap">
        select
        <include refid="userSql"/>
        from
        h_user
        where
        id = #{id}
    </select>

    <select id="getUserByAppKey" resultMap="baseResultMap">
        select
            user.id, user.app_key, user.app_secret, user.price, balance.balance
        from
            h_user user
        left join
            h_user_balance balance
        on
            user.id = balance.user_id
        where
            activated = 1 and status = 1 and app_key = #{appKey}
    </select>
</mapper>
