<?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.ruoyi.project.business.mapper.TbAgentMapper">

    <resultMap type="TbAgent" id="TbAgentResult">
        <result property="agentId" column="agent_id"/>
        <result property="agentNo" column="agent_no"/>
        <result property="agentName" column="agent_name"/>
        <result property="areaId" column="area_id"/>
        <result property="countryId" column="country_id"/>
        <result property="agentType" column="agent_type"/>
        <result property="hasContract" column="has_contract"/>
        <result property="parentAgentId" column="parent_agent_id"/>
        <result property="parentSalerId" column="parent_saler_id"/>
        <result property="agentLevel" column="agent_level"/>
        <result property="discount" column="discount"/>
        <result property="discountFlag" column="discount_flag"/>
        <result property="head" column="head"/>
        <result property="phone" column="phone"/>
        <result property="email" column="email"/>
        <result property="address" column="address"/>
        <result property="status" column="status"/>
        <result property="permissionChar" column="permission_char"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <!--附加-->
        <result property="parentAgentName" column="parent_agent_name"/>
        <result property="parentSalerName" column="parent_saler_name"/>
        <result property="areaName" column="area_name"/>
        <result property="countryName" column="country_name"/>
        <result property="city" column="city"/>
        <result property="firstCooperationYear" column="first_cooperation_year"/>
        <result property="origin" column="origin"/>
        <result property="protocolStartDate" column="protocol_start_date"/>
        <result property="protocolEndDate" column="protocol_end_date"/>
        <result property="authorizedArea" column="authorized_area"/>
        
        <!--<association property="agentTarget" column="agent_id" select="com.ruoyi.project.business.mapper.TbAgentTargetMapper.selectById"/>-->
        <association property="agentTarget" column="agent_id" javaType="com.ruoyi.project.business.domain.TbAgentTarget">
            <result property="targetId" column="target_id"/>
            <result property="targetYear" column="target_year"/>
            <result property="targetMoney" column="target_money"/>
            <result property="targetElevatorCount" column="target_elevator_count"/>
            <result property="targetProjectCount" column="target_project_count"/>
        </association>
    </resultMap>

    <sql id="selectTbAgentVo2">
        select T.*,
               u.user_name as parent_saler_name,
               a3.area_name
        from (select a1.agent_id,
                     a1.agent_no,
                     a1.agent_name,
                     a1.area_id,
                     a1.country_id,
                     a1.agent_type,
                     a1.parent_agent_id,
                     a2.agent_name as parent_agent_name,
                     a1.parent_saler_id,
                     a1.agent_level,
                     a1.discount,
                     a1.discount_flag,
                     a1.head,
                     a1.phone,
                     a1.email,
                     a1.address,
                     a1.status,
                     a1.permission_char,
                     a1.del_flag,
                     a1.create_time,
                     a1.create_by,
                     a1.update_time,
                     a1.update_by,
                     a1.remark
              from tb_agent a1
                       left join tb_agent a2 on a1.parent_agent_id = a2.agent_id) T
                 left join tb_area a3 on T.area_id = a3.area_id
                 left join sys_user u on T.parent_saler_id = u.user_id
    </sql>
    <sql id="selectTbAgentVo">
        select a1.agent_id,
               a1.agent_no,
               a1.agent_name,
               a1.area_id,
               a1.country_id,
               a1.city,
               a1.agent_type,
               a1.has_contract,
               a1.parent_agent_id,
               a1.parent_saler_id,
               a1.agent_level,
               a1.discount,
               a1.discount_flag,
               a1.head,
               a1.phone,
               a1.email,
               a1.address,
               a1.status,
               a1.permission_char,
               a1.del_flag,
               a1.create_time,
               a1.create_by,
               a1.update_time,
               a1.update_by,
               a1.origin,
               a1.first_cooperation_year,
               a1.protocol_start_date,
               a1.protocol_end_date,
               a1.authorized_area,
               a1.remark
        from tb_agent a1
    </sql>

    <!--resultMap="TbAgentResult"-->
    <!--<select id="selectTbAgentList" parameterType="TbAgent" resultType="TbAgent">-->
    <select id="selectTbAgentList" parameterType="TbAgent" resultMap="TbAgentResult">
        select a1.*,
        a2.agent_name as parent_agent_name,
        a3.area_name,
        u.nick_name as parent_saler_name,
        c.country_name,
        tat.target_id,
        tat.target_year,
        tat.target_money,
        tat.target_elevator_count,
        tat.target_project_count
        from 
        (<include refid="selectTbAgentVo"/>) a1
        left join tb_agent a2 on a1.parent_agent_id = a2.agent_id
        left join tb_area a3 on a1.area_id = a3.area_id
        left join sys_user u on a1.parent_saler_id = u.user_id
        left join tb_country c on a1.country_id = c.country_id
        left join tb_agent_target tat on tat.agent_id = a1.agent_id and YEAR(tat.target_year) = YEAR(CURRENT_DATE)
        INNER JOIN (SELECT p.premession_char
            FROM (SELECT @premessionStr := #{permissionChar} p) param,
            getpremessionview_chars p) permission ON permission.premession_char = a1.agent_id
        <where>
            a1.del_flag = 0
            <if test="agentIds != null and agentIds.size() > 0">
                and a1.agent_id in
                <foreach collection="agentIds" item="agentId" open="(" separator="," close=")">
                    #{agentId}
                </foreach>
            </if>
            <if test="agentId != null">and a1.agent_id = #{agentId}</if>
            <if test="agentNo != null  and agentNo != ''">and a1.agent_no like concat('%', #{agentNo}, '%')</if>
            <if test="agentName != null  and agentName != ''">and a1.agent_name like concat('%', #{agentName}, '%')</if>
            <if test="areaId != null ">and a1.area_id = #{areaId}</if>
            <if test="areaName != null and areaName != ''">and a3.area_name like concat ('%', #{areaName}, '%')</if>
            <if test="countryId != null ">and a1.country_id = #{countryId}</if>
            <if test="city != null and city != ''">and a1.city like concat ('%', #{city}, '%')</if>
            <if test="agentType != null  and agentType != ''">and a1.agent_type = #{agentType}</if>
            <if test="hasContract != null ">and a1.has_contract = #{hasContract}</if>
            <if test="parentAgentId != null ">and a1.parent_agent_id = #{parentAgentId}</if>
            <if test="parentSalerId != null ">and a1.parent_saler_id = #{parentSalerId}</if>
            <if test="agentLevel != null  and agentLevel != ''">and a1.agent_level = #{agentLevel}</if>
            <if test="discount != null ">and a1.discount = #{discount}</if>
            <if test="discountFlag != null ">and a1.discount_flag = #{discountFlag}</if>
            <if test="head != null  and head != ''">and a1.head = #{head}</if>
            <if test="phone != null  and phone != ''">and a1.phone like concat('%', #{phone}, '%')</if>
            <if test="email != null  and email != ''">and a1.email = #{email}</if>
            <if test="address != null  and address != ''">and a1.address = #{address}</if>
            <if test="status != null  and status != ''">and a1.status = #{status}</if>
            <if test="curUserPermissionChar != null  and curUserPermissionChar != ''">
                and find_in_set(#{curUserPermissionChar}, a1.permission_char)
            </if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(a1.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                AND date_format(a1.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
      
            <if test="areaIds != null and areaIds.size() > 0">
                and a1.area_id in
                <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
                    #{areaId}
                </foreach>
            </if>
            <if test="agentLevels != null and agentLevels.size() > 0">
                and a1.agent_level in
                <foreach collection="agentLevels" item="agentLevel" open="(" separator="," close=")">
                    #{agentLevel}
                </foreach>
            </if>
            <if test="origins != null and origins.size() > 0">
                and a1.origin in
                <foreach collection="origins" item="origin" open="(" separator="," close=")">
                    #{origin}
                </foreach>
            </if>
            <if test="parentAgentName != null and parentAgentName != ''">
                and a2.agent_name like concat('%', #{parentAgentName}, '%')
            </if>
            <if test="parentSalerName != null and parentSalerName != ''">
                and u.nick_name like concat('%', #{parentSalerName}, '%')
            </if>
            <if test="firstCooperationYear != null and firstCooperationYear != ''">
                and a1.first_cooperation_year = #{firstCooperationYear}
            </if>
            <if test="isThisYear">
                and tat.target_id is not null
            </if>
            <if test="protocolSsDate != null and protocolSeDate != null">
                and a1.protocol_start_date between #{protocolSsDate} and #{protocolSeDate}
            </if>
            <if test="protocolEsDate != null and protocolEeDate != null">
                and a1.protocol_end_date between #{protocolEsDate} and #{protocolEeDate}
            </if>
            <if test="beginTime != null and endTime != null">
                and a1.create_time between #{beginTime} and #{endTime}
            </if>
            <if test="authorizedArea != null and authorizedArea != ''">
                and a1.authorized_area like concat('%', #{authorizedArea}, '%')
            </if>
        
            order by a1.agent_id desc
        </where>
    </select>

    <!--<select id="selectTbAgentByAgentId" parameterType="Long" resultMap="TbAgentResult">-->
    <!--    <include refid="selectTbAgentVo"/>-->
    <!--    where agent_id = #{agentId} and del_flag = 0-->
    <!--</select>-->
    <select id="existAgentNo" resultType="java.lang.Integer">
        select 1
        from tb_agent
        where agent_no = #{agentNo}
        limit 1
    </select>
    
    <select id="existNextAgent" resultType="java.lang.Integer">
        select 1
        from tb_agent
        <where>
            and del_flag = 0
            and parent_agent_id in 
            <foreach item="agentId" collection="array" open="(" separator="," close=")">
                #{agentId}
            </foreach>
        </where>
       
    </select>

    <select id="selectCountByCountryIds" resultType="java.lang.Integer">
        select count(1) from tb_agent where del_flag = '0' and country_id in
        <foreach item="countryId" collection="array" open="(" separator="," close=")">
            #{countryId}
        </foreach>
    </select>

    <select id="selectCountByAreaIds" resultType="java.lang.Integer">
        select count(1) from tb_agent where del_flag = '0' and area_id in
        <foreach item="areaId" collection="array" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </select>
    <select id="existAgent" resultType="java.lang.Integer">
        select 1
        from tb_agent
        <where>
            and del_flag = 0
            <if test="agentNo != null">and agent_no = #{agentNo}</if>
            <if test="agentName != null">and agent_name = #{agentName}</if>
            <if test="agentId != null">and agent_id != #{agentId}</if>
        </where>
        limit 1
    </select>

    <select id="selectForAccount" resultMap="TbAgentResult">
        select distinct a1.agent_id, a1.agent_name from tb_agent a1
        inner join tc_account a on a.owner_agent = a1.agent_id
        inner join (SELECT p.premession_char FROM (SELECT @premessionStr:= #{permissionChar} p) param , getpremessionview_chars p
        ) permission on permission.premession_char = a.permission_char
        where a.del_flag = '0'
    </select>

    <select id="selectForProject" resultMap="TbAgentResult">
        select distinct a1.agent_id, a1.agent_name from tb_agent a1
        inner join tc_project p on p.owner_agent = a1.agent_id
        inner join (SELECT p.premession_char FROM (SELECT @premessionStr:= #{permissionChar} p) param , getpremessionview_chars p
        ) permission on permission.premession_char = p.permission_char
        where p.del_flag = '0'
    </select>

    <select id="selectForYear" resultMap="TbAgentResult" parameterType="string">
        select distinct agent_id,agent_name
        from tb_agent
        where del_flag = '0'
        <if test="yearParam != null and yearParam != ''">and first_cooperation_year = #{yearParam}</if>
    </select>

    <select id="selectChildAgentList" resultType="com.ruoyi.project.business.domain.TbAgent">
        SELECT *
        FROM tb_agent
        WHERE del_flag = '0' AND status = '0'
          AND REVERSE(permission_char) LIKE CONCAT(#{permissionChar}, '%')
    </select>

    <select id="selectByAreaIds" resultMap="TbAgentResult">
        select agent_id, agent_name from tb_agent
        <where>
            <if test="areaIds != null and areaIds.size() > 0">
                and area_id in
                <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
                    #{areaId}
                </foreach>
            </if>
                and del_flag = '0'
        </where>
    </select>

    <select id="selectAgentListByAreaIds" resultMap="TbAgentResult">
        select agent_id, agent_name from tb_agent
        where del_flag = '0' and area_id in
        <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </select>

    <insert id="insertTbAgent" parameterType="TbAgent" useGeneratedKeys="true" keyProperty="agentId">
        insert into tb_agent
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="agentId != null">agent_id,</if>
            <if test="agentNo != null">agent_no,</if>
            <if test="agentName != null">agent_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="countryId != null">country_id,</if>
            <if test="agentType != null">agent_type,</if>
            <if test="parentAgentId != null">parent_agent_id,</if>
            <if test="parentSalerId != null">parent_saler_id,</if>
            <if test="agentLevel != null">agent_level,</if>
            <if test="discount != null">discount,</if>
            <if test="discountFlag != null ">discount_flag,</if>
            <if test="head != null">head,</if>
            <if test="phone != null">phone,</if>
            <if test="email != null">email,</if>
            <if test="address != null">address,</if>
            <if test="status != null">status,</if>
            <if test="permissionChar != null">permission_char,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="agentId != null">#{agentId},</if>
            <if test="agentNo != null">#{agentNo},</if>
            <if test="agentName != null">#{agentName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="countryId != null">#{countryId},</if>
            <if test="agentType != null">#{agentType},</if>
            <if test="parentAgentId != null">#{parentAgentId},</if>
            <if test="parentSalerId != null">#{parentSalerId},</if>
            <if test="agentLevel != null">#{agentLevel},</if>
            <if test="discount != null">#{discount},</if>
            <if test="discountFlag != null ">#{discountFlag},</if>
            <if test="head != null">#{head},</if>
            <if test="phone != null">#{phone},</if>
            <if test="email != null">#{email},</if>
            <if test="address != null">#{address},</if>
            <if test="status != null">#{status},</if>
            <if test="permissionChar != null">#{permissionChar},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateTbAgent" parameterType="TbAgent">
        update tb_agent
        <trim prefix="SET" suffixOverrides=",">
            <if test="agentNo != null">agent_no = #{agentNo},</if>
            <if test="agentName != null">agent_name = #{agentName},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="countryId != null">country_id = #{countryId},</if>
            <if test="agentType != null">agent_type = #{agentType},</if>
            <if test="parentAgentId != null">parent_agent_id = #{parentAgentId},</if>
            parent_saler_id = #{parentSalerId},
            <if test="agentLevel != null">agent_level = #{agentLevel},</if>
            <if test="discount != null">discount = #{discount},</if>
            <if test="discountFlag != null ">discount_flag = #{discountFlag},</if>
            <if test="head != null">head = #{head},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="email != null">email = #{email},</if>
            <if test="address != null">address = #{address},</if>
            <if test="status != null">status = #{status},</if>
            <if test="permissionChar != null">permission_char = #{permissionChar},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where agent_id = #{agentId}
    </update>

    <update id="deleteTbAgentByAgentId" parameterType="Long">
        update tb_agent
        set del_flag = 1
        where agent_id = #{agentId}
    </update>

    <update id="deleteTbAgentByAgentIds" parameterType="String">
        update tb_agent
        set del_flag = 1
        where agent_id in
        <foreach item="agentId" collection="array" open="(" separator="," close=")">
            #{agentId}
        </foreach>
    </update>
    <select id="selectAgentDealLine" parameterType="int" resultType="com.ruoyi.project.business.domain.TbAgent">
        SELECT agent_no,
        agent_name,
        parent_saler_id,
        #{configValue} as configValue
        FROM tb_agent
        WHERE del_flag = '0'
          and has_contract = 1
          AND protocol_end_date is not null
          AND DATEDIFF(protocol_end_date, CURRENT_DATE) = #{configValue}
    </select>
    <select id="selectCountriesByCurUser" resultType="java.lang.String">
        select group_concat(a.country_id)
        from tb_agent a
                 inner join (SELECT p.premession_char
                             FROM (SELECT @premessionStr := #{permissionChar} p) param,
                                  getpremessionview_chars p) permission
                            on permission.premession_char = a.permission_char
        where a.del_flag = '0' and a.country_id is not null
    </select>


</mapper>