<?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="cn.com.ty.lift.enterprise.push.mapper.PushUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.com.ty.lift.common.model.PushUserInfo">
        <id column="user_id" property="userId"/>
        <result column="username" property="userName"/>
        <result column="mobile" property="mobile"/>
        <result column="device_model" property="deviceModel"/>
        <result column="device_flag" property="deviceFlag"/>
        <result column="role_name" property="roleName"/>
    </resultMap>

    <select id="listByUserId" resultMap="BaseResultMap">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="userId != null and userId > 0">
                AND ui.user_id = #{userId}
            </if>
        </where>
    </select>

    <select id="listByUserIds" resultMap="BaseResultMap">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="userIds != null">
                AND ui.user_id IN
                <foreach collection="userIds" item="userId" open="(" separator="," close=")">
                    #{userId, jdbcType=BIGINT}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listByRoleCodes" resultMap="BaseResultMap">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="roleCodes != null">
                AND ro.code IN
                <foreach collection="roleCodes" item="roleCode" open="(" separator="," close=")">
                    #{roleCode, jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listByRoleCode" resultMap="BaseResultMap">
        SELECT
        ui.user_id,
        ui.NAME AS username,
        ua.mobile,
        ua.device_model,
        ua.device_flag,
        ro.NAME AS role_name
        FROM
        user_info ui
        LEFT JOIN user_account ua ON ui.user_id = ua.user_id
        LEFT JOIN user_role ur ON ui.user_id = ur.user_id
        LEFT JOIN role ro ON ro.id = ur.role_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                AND ur.company_id = #{mtCompanyId}
            </if>
            <if test="roleCode != null and roleCode != ''">
                AND ro.code = #{roleCode, jdbcType=VARCHAR}
            </if>
        </where>
    </select>

    <select id="pageByMtCompany" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT
            ui.user_id,
            ui.NAME AS username,
            ua.mobile,
            ua.device_model,
            ua.device_flag,
            ro.NAME AS role_name
        FROM
            user_info ui
            LEFT JOIN user_account ua ON ui.user_id = ua.user_id
            LEFT JOIN user_role ur ON ui.user_id = ur.user_id
            LEFT JOIN role ro ON ro.id = ur.role_id
            LEFT JOIN mt_company_user mcu ON mcu.user_id = ui.user_id AND mcu.mt_company_id = ur.company_id
        <where>
            <if test="mtCompanyId != null and mtCompanyId > 0">
                mcu.mt_company_id = #{mtCompanyId}
            </if>
        </where>
    </select>
</mapper>
