<?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.TbAgentHistoryMapper">
    
    <resultMap type="TbAgentHistory" id="TbAgentHistoryResult">
        <result property="agentId"    column="agent_id"    />
        <result property="id"    column="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="city"    column="city"    />
        <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="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="origin"    column="origin"    />
        <result property="firstCooperationYear"    column="first_cooperation_year"    />
        <result property="protocolStartDate"    column="protocol_start_date"    />
        <result property="protocolEndDate"    column="protocol_end_date"    />
        <result property="authorizedArea"    column="authorized_area"    />

        <result property="createName"    column="create_name"    />

        <!--附加-->
        <result property="parentAgentName" column="parent_agent_name"/>
        <result property="parentSalerName" column="parent_saler_name"/>
        <result property="areaName" column="area_name"/>
    </resultMap>

    <sql id="selectTbAgentHistoryVo">
        select ah.agent_id,
               ah.id,
               ah.agent_no,
               ah.agent_name,
               ah.area_id,
               ah.country_id,
               ah.city,
               ah.agent_type,
               ah.has_contract,
               ah.parent_agent_id,
               ah.parent_saler_id,
               ah.agent_level,
               ah.discount,
               ah.head,
               ah.phone,
               ah.email,
               ah.address,
               ah.status,
               ah.permission_char,
               ah.del_flag,
               ah.create_time,
               ah.create_by,
               ah.update_time,
               ah.update_by,
               ah.remark,
               ah.origin,
               ah.first_cooperation_year,
               ah.protocol_start_date,
               ah.protocol_end_date,
               ah.authorized_area
        from tb_agent_history as ah
    </sql>

    <select id="selectTbAgentHistoryList" parameterType="TbAgentHistory" resultMap="TbAgentHistoryResult">
        select ah.*,
               u.nick_name as parent_saler_name,
                u2.nick_name as create_name,
               a2.agent_name as parent_agent_name,
               a3.area_name
        from
        (<include refid="selectTbAgentHistoryVo"/>) ah
        left join tb_agent a2 on ah.parent_agent_id = a2.agent_id
        left join tb_area a3 on ah.area_id = a3.area_id
        LEFT JOIN sys_user u ON ah.parent_saler_id = u.user_id
        LEFT JOIN sys_user u2 ON u2.user_id = ah.create_by
        INNER JOIN (SELECT p.premession_char
                        FROM (SELECT @premessionStr := #{permissionChar} p) param,
                        getpremessionview_chars p) permission ON permission.premession_char = ah.agent_id
        <where>
            ah.del_flag = 0
            <if test="agentIds != null and agentIds.size() > 0">
                and ah.agent_id in
                <foreach collection="agentIds" item="agentId" open="(" separator="," close=")">
                    #{agentId}
                </foreach>
            </if>
            <if test="agentNo != null  and agentNo != ''"> and ah.agent_no like concat('%', #{agentNo}, '%')</if>
            <if test="agentName != null  and agentName != ''"> and ah.agent_name like concat('%', #{agentName}, '%')</if>
            <if test="areaIds != null and areaIds.size() > 0">
                and ah.area_id in
                <foreach collection="areaIds" item="areaId" open="(" separator="," close=")">
                    #{areaId}
                </foreach>
            </if>

            <if test="countryId != null  and countryId != ''"> and ah.country_id = #{countryId}</if>
            <if test="city != null  and city != ''"> and ah.city = #{city}</if>
            <if test="agentType != null  and agentType != ''"> and ah.agent_type = #{agentType}</if>
            <if test="hasContract != null "> and ah.has_contract = #{hasContract}</if>
            <if test="parentAgentId != null "> and ah.parent_agent_id = #{parentAgentId}</if>
            <if test="parentSalerId != null "> and ah.parent_saler_id = #{parentSalerId}</if>
            <if test="agentLevels != null and agentLevels.size() > 0">
                and ah.agent_level in
                <foreach collection="agentLevels" item="agentLevel" open="(" separator="," close=")">
                    #{agentLevel}
                </foreach>
            </if>
            <if test="discount != null "> and ah.discount = #{discount}</if>
            <if test="head != null  and head != ''"> and ah.head = #{head}</if>
            <if test="phone != null  and phone != ''"> and ah.phone  like concat('%', #{phone}, '%')</if>
            <if test="email != null  and email != ''"> and ah.email like concat('%',#{email},'%') </if>
            <if test="address != null  and address != ''"> and ah.address like concat('%', #{address},'%')</if>
            <if test="status != null  and status != ''"> and ah.status = #{status}</if>
            <if test="permissionChar != null  and permissionChar != ''"> and find_in_set(#{permissionChar}, ah.permission_char)</if>
            <if test="origins != null and origins.size() > 0">
                and ah.origin in
                <foreach collection="origins" item="origin" open="(" separator="," close=")">
                    #{origin}
                </foreach>
            </if>
            <if test="firstCooperationYear != null  and firstCooperationYear != ''"> and ah.first_cooperation_year = #{firstCooperationYear}</if>
            <if test="protocolSsDate != null and protocolSeDate != null">and ah.protocol_start_date between #{protocolSsDate} and #{protocolSeDate}</if>
            <if test="protocolEsDate != null and protocolEeDate != null">and ah.protocol_end_date between #{protocolEsDate} and #{protocolEeDate}</if>
            <if test="beginTime != null and endTime != null">and ah.create_time between #{beginTime} and #{endTime}</if>
            <if test="authorizedArea != null and authorizedArea != ''">and ah.authorized_area like concat('%', #{authorizedArea}, '%')</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="createName != null and createName != ''">
                and u2.nick_name like concat('%', #{createName}, '%')
            </if>
        </where>
        order by ah.id desc
    </select>
    
    <select id="selectTbAgentHistoryById" parameterType="Long" resultMap="TbAgentHistoryResult">
        <include refid="selectTbAgentHistoryVo"/>
        where ah.id = #{id}
    </select>
        
    <insert id="insertTbAgentHistory" parameterType="TbAgentHistory" useGeneratedKeys="true" keyProperty="id">
        insert into tb_agent_history
        <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="city != null">city,</if>
            <if test="agentType != null">agent_type,</if>
            <if test="hasContract != null">has_contract,</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="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>
            <if test="origin != null">origin,</if>
            <if test="firstCooperationYear != null">first_cooperation_year,</if>
            <if test="protocolStartDate != null">protocol_start_date,</if>
            <if test="protocolEndDate != null">protocol_end_date,</if>
            <if test="authorizedArea != null">authorized_area,</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="city != null">#{city},</if>
            <if test="agentType != null">#{agentType},</if>
            <if test="hasContract != null">#{hasContract},</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="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>
            <if test="origin != null">#{origin},</if>
            <if test="firstCooperationYear != null">#{firstCooperationYear},</if>
            <if test="protocolStartDate != null">#{protocolStartDate},</if>
            <if test="protocolEndDate != null">#{protocolEndDate},</if>
            <if test="authorizedArea != null">#{authorizedArea},</if>
         </trim>
    </insert>

    <update id="updateTbAgentHistory" parameterType="TbAgentHistory">
        update tb_agent_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="agentId != null">agent_id = #{agentId},</if>
            <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="city != null">city = #{city},</if>
            <if test="agentType != null">agent_type = #{agentType},</if>
            <if test="hasContract != null">has_contract = #{hasContract},</if>
            <if test="parentAgentId != null">parent_agent_id = #{parentAgentId},</if>
            <if test="parentSalerId != null">parent_saler_id = #{parentSalerId},</if>
            <if test="agentLevel != null">agent_level = #{agentLevel},</if>
            <if test="discount != null">discount = #{discount},</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>
            <if test="origin != null">origin = #{origin},</if>
            <if test="firstCooperationYear != null">first_cooperation_year = #{firstCooperationYear},</if>
            <if test="protocolStartDate != null">protocol_start_date = #{protocolStartDate},</if>
            <if test="protocolEndDate != null">protocol_end_date = #{protocolEndDate},</if>
            <if test="authorizedArea != null">authorized_area = #{authorizedArea},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTbAgentHistoryById" parameterType="Long">
        delete from tb_agent_history where id = #{id}
    </delete>

    <delete id="deleteTbAgentHistoryByIds" parameterType="String">
        delete from tb_agent_history where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>