<?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.roadm.mapper.SaleMapper">
    <select id="listPageQuerySale" resultType="cn.roadm.bo.UserInfo">
        select user_name                                         as userName,
               name,
               last_login_ip                                     as lastLoginIp,
               DATE_FORMAT(last_login_time, '%Y-%m-%d %H:%i:%S') as lastLoginTime
        from sms_user_info
        <where>
            ROLE_ID = 40 and status = 1
            <if test="userId != null and userId != ''">
                and USER_NAME = #{userId}
            </if>
            <if test="userName != null and userName != ''">
                and NAME like CONCAT('%', #{userName}, '%')
            </if>
            <if test="selfId != null and selfId != ''">
                and USER_NAME = #{selfId}
            </if>
        </where>
    </select>

    <select id="queryAllSelaByManager" resultType="string">
        select a.NAME as userName
        from sms_user_info a,
             sale_manager b
        <where>
            b.seller = a.NAME
                    and a.STATUS = 1
            <if test="managerId != null and managerId != ''">
                and b.managerId = #{managerId}
            </if>
        </where>
    </select>

    <select id="queryAllSela" resultType="java.util.Map">
        select USER_NAME as userId, NAME as userName
        from sms_user_info
        where ROLE_ID in (42,520)
          and STATUS = 1;
    </select>

    <delete id="deleteAllSela">
        delete from sale_manager where managerId = #{userId}
    </delete>

    <insert id="addSelaByManager">
        insert into sale_manager values
        <foreach collection="list" separator="," item="item">
            (#{userId}, #{item})
        </foreach>
    </insert>

    <select id="listPageQueryBusiness" resultType="cn.roadm.bo.UserInfo">
        select user_name                                         as userName,
        name,
        last_login_ip                                     as lastLoginIp,
        DATE_FORMAT(last_login_time, '%Y-%m-%d %H:%i:%S') as lastLoginTime
        from sms_user_info
        <where>
            ROLE_ID = 50 and status = 1
            <if test="userId != null and userId != ''">
                and USER_NAME = #{userId}
            </if>
            <if test="userName != null and userName != ''">
                and NAME like CONCAT('%', #{userName}, '%')
            </if>
            <if test="selfId != null and selfId != ''">
                and USER_NAME = #{selfId}
            </if>
        </where>
    </select>

    <select id="queryAllBusiness" resultType="java.util.Map">
        select USER_NAME as userId, NAME as userName
        from sms_user_info
        where ROLE_ID in (52,520) and STATUS = 1;
    </select>

    <select id="queryAllBusinessByManager" resultType="java.lang.String">
        select a.USER_NAME
        from sms_user_info a, business_manager b
        <where>
            b.business = a.USER_NAME
            and a.STATUS = 1
            and b.managerId = #{userId}
        </where>
    </select>

    <select id="queryAllBusinessByManagerMap" resultType="java.util.Map">
        select a.USER_NAME as userId, a.name as userName
        from sms_user_info a, business_manager b
        <where>
            b.business = a.USER_NAME
            and a.STATUS = 1
            and b.managerId = #{userId}
        </where>
    </select>

    <delete id="deleteAllBusiness">
        delete from business_manager where managerId = #{userId}
    </delete>

    <insert id="addBusinessByManager">
        insert into business_manager values
        <foreach collection="list" separator="," item="item">
            (#{managerId}, #{item})
        </foreach>
    </insert>

    <select id="queryAllBusinessMap" resultType="java.util.Map">
        select distinct a.USER_NAME as userId, a.NAME as userName
        from sms_user_info a, business_manager b
        <where>
            b.business = a.USER_NAME
            and a.STATUS = 1
            <if test="managerId != null and managerId != ''">
                and b.managerId = #{managerId}
            </if>
        </where>
    </select>

    <select id="queryAllSelaName" resultType="java.lang.String">
        select NAME from sms_user_info
        where ROLE_ID in (42,520)
          and STATUS = 1;
    </select>
</mapper>
