<?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.RechargeMapper">
    <resultMap id="baseResultMap" type="com.jkcredit.invoice.hub.model.po.recharge.RechargePo">
        <id column="id" property="id"/>
        <result column="app_key" property="appKey"/>
        <result column="company" property="company"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="contact_name" property="contactName"/>
        <result column="telephone" property="telephone"/>
        <result column="price" property="price"/>
        <result column="before_balance" property="beforeBalance"/>
        <result column="money" property="money"/>
        <result column="user_id" property="userId"/>
        <result column="role_name" property="roleName"/>
    </resultMap>

    <select id="getRechargeRecords" resultMap="baseResultMap">
        select recharge.id, user.app_key, user.company, user.create_time, user.update_time, user.status,
        user.contact_name, user.telephone, user.price, recharge.before_balance, recharge.money, recharge.user_id,
        role.role_name from h_recharge recharge
        left join h_user user on recharge.user_id = user.id
        left join(select distinct userRolePermission.user_id, userRolePermission.role_id from h_user_role_permission
        userRolePermission left join h_user user on user.id = userRolePermission.user_id) permission on user.id =
        permission.user_id
        left join h_role role on permission.role_id = role.id
        <where>
            <if test="query.appKey != null and query.appKey != ''">
                and user.app_key = #{query.appKey}
            </if>
            <if test="query.company != null and query.company != ''">
                and user.company like CONCAT('%',#{query.company},'%')
            </if>
        </where>
    </select>
</mapper>
