<?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.crm.manager.dao.mapper.UserInfoDao">
    <select id="getStampById" parameterType="Long" resultType="String">
        select stamp
        from user_info
        where id = #{id}
    </select>

    <select id="getStampListByIds" parameterType="java.util.List" resultType="String">
        select stamp
        from user_info
        where id in
        <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="searchByPageList" parameterType="com.crm.rely.backend.core.entity.user.info.UserInfoSearchEntity"
            resultType="com.crm.rely.backend.core.pojo.view.UserInfoView">
        SELECT user_info.*,
        user_group.`name` AS group_name,
        user_group.en_name AS group_en_name,
        agent_level.`name` AS agent_level_name,
        agent_level.en_name AS agent_level_en_name,
        user_role.`name` as role_name,
        area.area_name as area_name,
        coun.`name` countryName,coun.`en_name` countryEnName
        FROM user_info
        LEFT JOIN user_group ON user_group.id= user_info.group_id
        LEFT JOIN agent_level ON agent_level.id=user_info.agent_level
        LEFT JOIN user_role ON user_role.id =user_info.role_id
        LEFT JOIN sys_area area ON area.id =user_info.area_id
        LEFT JOIN sys_country coun ON coun.code =user_info.country
        <include refid="selectCondition">
        </include>
        order by user_info.add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="searchByPageCount" parameterType="com.crm.rely.backend.core.entity.user.info.UserInfoSearchEntity"
            resultType="integer">
        SELECT count(user_info.id)
        FROM user_info
        <include refid="selectCondition">
        </include>
    </select>
    <sql id="selectCondition">
        where user_info.role_id!=-1
        <if test="name!=null and name!=''">
            and user_info.name like concat('%',#{name},'%')
        </if>
        <if test="username!=null and username!=''">
            and user_info.username like concat('%',#{username},'%')
        </if>
        <if test="email!=null and email!=''">
            and user_info.email like concat('%',#{email},'%')
        </if>
        <if test="ibNo!=null and ibNo!=''">
            and user_info.ib_no like concat('%',#{ibNo},'%')
        </if>
        <if test="roleId!=null">
            and user_info.role_id =#{roleId}
        </if>
        <if test="valid!=null">
            and user_info.valid =#{valid}
        </if>
        <if test="startDate != null">
            AND user_info.add_time>#{startDate}
        </if>
        <if test="verified != null">
            AND user_info.verified>#{verified}
        </if>
        <if test="secretKey != null">
            <if test="secretKey == 0">
                AND (user_info.secret_key is null or user_info.secret_key='')
            </if>
            <if test="secretKey == 1">
                AND user_info.secret_key is not null and user_info.secret_key!=''
            </if>
        </if>
        <if test="endDate != null">
            AND #{endDate}>user_info.add_time
        </if>
        <if test="country!=null and country!=''">
            and user_info.country = #{country}
        </if>
    </sql>

    <select id="getOne" resultType="com.crm.rely.backend.core.pojo.view.UserInfoView">
        SELECT user_info.*,
               user_group.`name`   AS group_name,
               user_group.en_name  AS group_en_name,
               agent_level.`name`  AS agent_level_name,
               agent_level.en_name AS agent_level_en_name,
               user_role.`name`    as role_name,
               user_role.department_id,
               area.area_name
        FROM user_info
                 LEFT JOIN user_group ON user_group.id = user_info.group_id
                 LEFT JOIN agent_level ON agent_level.id = user_info.agent_level
                 LEFT JOIN user_role ON user_role.id = user_info.role_id
                 LEFT JOIN sys_area area ON area.id = user_info.area_id
        where user_info.id = #{id}
    </select>

    <select id="findAllByRoleCode"
            resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select sales.id, sales.`name`, sales.email, sales.ib_no
        from user_info sales
        left join user_role role on sales.role_id = role.id
        where role.`code` = #{roleCode}
        and sales.valid=1
        <if test="pId != null">
            and sales.pid = #{pId}
        </if>
    </select>

    <select id="getAllByLeader"
            resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        <!--        select sales.id, sales.`name`, sales.email, sales.ib_no-->
        <!--        from user_info sales-->
        <!--                 left join user_role role on sales.role_id = role.id-->
        <!--        where role.`code` = 'ROLE_SALE'-->
        <!--          and sales.leader_id = sales.id-->
        <!--          and sales.valid = 1-->

        select sales.id, sales.`name`, sales.email, sales.ib_no
        from user_info sales
        left join user_role role on sales.role_id = role.id
        where role.`code` ='ROLE_SALE' and ifnull(sales.leader_id,0) != sales.id
        and sales.sales_type=2
        and sales.valid=1 ;
    </select>
    <select id="getSuperAdminInfo" resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select *
        from user_info
        where role_id is null limit 0,1
    </select>

    <select id="findAllByStamp" parameterType="String" resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select *
        from user_info
        where stamp like binary concat(#{stamp}
            , '%')
          and stamp != binary #{stamp}
    </select>

    <select id="findAllAndSelfByStamp" parameterType="String"
            resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select *
        from user_info
        where stamp like binary concat(#{stamp}
            , '%')
    </select>

    <select id="findAllSalesAndSelfByStamp" parameterType="String"
            resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select ui.*
        from user_info ui
                 inner join user_role ur on ui.role_id = ur.id
        where ur.`code` = 'ROLE_SALE'
          and ui.stamp like binary concat(#{stamp}
            , '%')
    </select>

    <select id="getMaxStampByPid" parameterType="Long" resultType="String">
        select MAX(binary stamp)
        from user_info
        where pid = #{pid}
    </select>

    <update id="updateUserStampByBatch" parameterType="java.util.List">
        update user_info
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="stamp = case " suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.stamp}
                </foreach>
            </trim>
            <trim prefix="p_stamp = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.pStamp}
                </foreach>
            </trim>
            <trim prefix="id_stamp = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.idStamp}
                </foreach>
            </trim>
        </trim>
        <where>
            and id in
            <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
                #{item.id}
            </foreach>
        </where>
    </update>

    <select id="findStampById" parameterType="Long" resultType="String">
        select stamp
        from user_info
        where id = #{id}
    </select>

    <update id="updateShareByStamp">
        update user_info sales
        set sales.sales_share = #{share}
        where stamp like binary concat(#{stamp}
            , '%')
    </update>

    <select id="findFirstByIbNo" parameterType="String" resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select *
        from user_info
        where ib_no = #{ibNo} limit 0,1
    </select>

    <select id="existsByAreaId" parameterType="Long" resultType="Integer">
        select 1
        from user_info
        where area_id = #{areaId} limit 1
    </select>

    <update id="batchUpdateIdStamp" parameterType="java.util.List">
        update user_info
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="id_stamp = case" suffix="end,">
                <foreach collection="list" item="item" index="idx">
                    when id=#{item.id} then #{item.idStamp}
                </foreach>
            </trim>
        </trim>
        <where>
            and id in
            <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
                #{item.id}
            </foreach>
        </where>
    </update>


    <select id="findAllByIbNoIn" parameterType="java.util.List"
            resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select id,ib_no
        from user_info
        where ib_no in
        <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <update id="updateUserBalanceById">
        update user_info
        set balance = #{balance}
        where id = #{id}
    </update>

    <select id="getDataById" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select *
        from user_info
        where id = #{id}
    </select>

    <select id="getDataByRoleId" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select id, pid, ib_no, `name`, ib_no_temp
        from user_info
        where role_id = #{roleId}
        order by pid asc
    </select>

    <select id="getDataByEmail" parameterType="String" resultType="com.crm.rely.backend.core.pojo.table.UserInfoTable">
        select id, pid, ib_no, `name`
        from user_info
        where email = #{email}
    </select>

    <select id="getEntryUserCount" parameterType="com.crm.rely.backend.core.entity.user.info.UserInfoSearchEntity"
            resultType="integer">
        SELECT count(a.id) FROM user_info a
        LEFT JOIN user_role b ON a.role_id = b.id
        where b.code = 'ROLE_SALE'
        <if test="name!=null and name!=''">
            and a.name like concat('%',#{name},'%')
        </if>
        <if test="email!=null and email!=''">
            and a.email like concat('%',#{email},'%')
        </if>
        <if test="ibNo!=null and ibNo!=''">
            and a.ib_no like concat('%',#{ibNo},'%')
        </if>
    </select>

    <select id="getEntryUserList" parameterType="com.crm.rely.backend.core.entity.user.info.UserInfoSearchEntity"
            resultType="com.crm.rely.backend.core.pojo.view.UserInfoView">
        SELECT a.id,a.email, a.ib_no, a.name ,a.username, a.ib_no_temp,a.valid,
        b.name role_name, c.`name` group_name,c.en_name group_en_name, d.area_name
        FROM user_info a
        LEFT JOIN user_role b ON a.role_id = b.id
        LEFT JOIN user_group c ON a.group_id= c.id
        LEFT JOIN sys_area d ON a.area_id= d.id
        where b.code = 'ROLE_SALE' and valid = 1
        <if test="name!=null and name!=''">
            and a.name like concat('%',#{name},'%')
        </if>
        <if test="email!=null and email!=''">
            and a.email like concat('%',#{email},'%')
        </if>
        <if test="ibNo!=null and ibNo!=''">
            and a.ib_no like concat('%',#{ibNo},'%')
        </if>
        order by a.id desc
        <if test="type!=null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="getUserList" parameterType="com.crm.rely.backend.core.entity.user.info.UserInfoListEntity"
            resultType="com.crm.rely.backend.core.pojo.view.UserInfoView">
        SELECT a.id,a.email, a.ib_no, a.name ,a.username, a.ib_no_temp,a.valid
        FROM user_info a
        <where>
            <if test="email!=null and email!=''">
                and a.email like concat(#{email},'%')
            </if>
            <if test="ibNo!=null and ibNo!=''">
                and a.ib_no like concat(,#{ibNo},'%')
            </if>
        </where>
        order by a.id desc
    </select>

    <select id="findAddIbByIbNoIn" parameterType="java.util.List" resultType="String">
        select ib_no
        from user_info
        where role_id = -1 and ib_no in
        <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

</mapper>