<?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.model.entity.user.info.UserInfoSearchEntity"
            resultType="com.crm.rely.backend.model.pojo.view.UserInfoView">
        SELECT user_info.*,
        user_group.`name` AS group_name,
        user_group.en_name AS group_en_name,
        user_role.`name` as role_name
        FROM user_info
        LEFT JOIN user_group ON user_group.id= user_info.group_id
        LEFT JOIN user_role ON user_role.id =user_info.role_id
        where user_info.role_id!=-1
        <if test="name!=null and name!=''">
            and user_info.name like binary concat('%',#{name},'%')
        </if>
        <if test="username!=null and username!=''">
            and user_info.username like binary concat('%',#{username},'%')
        </if>
        <if test="email!=null and email!=''">
            and user_info.email like binary concat('%',#{email},'%')
        </if>
        <if test="ibNo!=null and ibNo!=''">
            and user_info.ib_no like binary 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="endDate != null">
            AND #{endDate}>user_info.add_time
        </if>
        order by id desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="searchByPageCount" parameterType="com.crm.rely.backend.model.entity.user.info.UserInfoSearchEntity"
            resultType="integer">
        SELECT count(user_info.id)
        FROM user_info
        where user_info.role_id!=-1
        <if test="name!=null and name!=''">
            and user_info.name like binary concat('%',#{name},'%')
        </if>
        <if test="username!=null and username!=''">
            and user_info.username like binary concat('%',#{username},'%')
        </if>
        <if test="email!=null and email!=''">
            and user_info.email like binary concat('%',#{email},'%')
        </if>
        <if test="ibNo!=null and ibNo!=''">
            and user_info.ib_no like binary 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="endDate != null">
            AND #{endDate}>user_info.add_time
        </if>

    </select>
    <select id="getOne" resultType="com.crm.rely.backend.model.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
        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
        where user_info.id = #{id}
    </select>

    <select id="findAllByRoleCode"
            parameterType="String"
            resultType="com.crm.rely.backend.model.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}
    </select>

    <select id="getAllAgent"
            resultType="com.crm.rely.backend.model.pojo.table.UserInfoTable">
        select sales.id, sales.`name`, sales.email, sales.ib_no
        from user_info sales

    </select>

    <select id="getSuperAdminInfo" resultType="com.crm.rely.backend.model.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.model.pojo.table.UserInfoTable">
        select *
        from user_info
        where stamp like binary concat(#{stamp}
            , '%')
          and stamp != binary #{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>
        <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>
</mapper>