<?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.TbAgentTargetMapper">

    <resultMap type="TbAgentTarget" id="TbAgentTargetResult">
        <result property="targetId" column="target_id"/>
        <result property="agentId" column="agent_id"/>
        <result property="targetYear" column="target_year"/>
        <result property="targetMoney" column="target_money"/>
        <result property="actualMoney" column="actual_money"/>
        <result property="targetElevatorCount" column="target_elevator_count"/>
        <result property="actualElevatorCount" column="actual_elevator_count"/>
        <result property="targetProjectCount" column="target_project_count"/>
        <result property="actualProjectCount" column="actual_project_count"/>
        <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"/>
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.agent.AgentTargetRes" id="TbAgentTargetResResult">
        <result property="targetId" column="target_id"/>
        <result property="agentId" column="agent_id"/>
        <result property="targetYear" column="target_year"/>
        <result property="targetMoney" column="target_money"/>
        <result property="actualMoney" column="actual_money"/>
        <result property="targetElevatorCount" column="target_elevator_count"/>
        <result property="actualElevatorCount" column="actual_elevator_count"/>
        <result property="targetProjectCount" column="target_project_count"/>
        <result property="actualProjectCount" column="actual_project_count"/>
        <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="agentNo" column="agent_no"/>
        <result property="agentName" column="agent_name"/>
        <result property="areaId" column="area_id"/>
        <result property="agentType" column="agent_type"/>
        <result property="agentLevel" column="agent_level"/>
        
    </resultMap>

    <sql id="selectTbAgentTargetVo">
        select t1.target_id,
               t1.agent_id,
               t1.target_year,
               t1.target_money,
               t1.actual_money,
               t1.target_elevator_count,
               t1.actual_elevator_count,
               t1.target_project_count,
               /*t1.actual_project_count,*/
               t1.del_flag,
               t1.create_time,
               t1.create_by,
               t1.update_time,
               t1.update_by,
               t1.remark,
               t1.currency,
               t2.agent_no,
               t2.agent_name,
               t2.area_id,
               t2.agent_type,
               t2.agent_level,
               (select count(1)
                from tc_project tp
                where tp.owner_agent = t1.agent_id
                  and YEAR(tp.create_time) = YEAR(t1.target_year)) as actual_project_count
        from tb_agent_target t1
                 left join tb_agent t2 on t1.agent_id = t2.agent_id
    </sql>

    <select id="selectTbAgentTargetList" parameterType="com.ruoyi.project.business.domain.vo.agent.AgentTargetReq"
            resultType="com.ruoyi.project.business.domain.vo.agent.AgentTargetRes">
        <include refid="selectTbAgentTargetVo"/>
        INNER JOIN (SELECT p.premession_char
        FROM (SELECT @premessionStr := #{permissionChar} p) param,
        getpremessionview_chars p) permission ON permission.premession_char = t1.agent_id
        <where>
            <if test="targetIds != null and targetIds.size() > 0">
                and t1.target_id in
                <foreach collection="targetIds" item="targetId" open="(" close=")" separator=",">
                    #{targetId}
                </foreach>
            </if>
            <if test="agentId != null ">and t1.agent_id = #{agentId}</if>
            <if test="agentNo != null and agentNo != ''">and t2.agent_no like concat('%',#{agentNo},'%')</if>
            <if test="agentName != null and agentName != ''">and t2.agent_name like concat('%',#{agentName},'%')</if>
            <if test="areaIds != null and areaIds.size() > 0">
                and t2.area_id in
                <foreach collection="areaIds" item="areaId" open="(" close=")" separator=",">
                    #{areaId}
                </foreach>
            </if>
            <if test="agentTypes != null and agentTypes.size() > 0">
                and t2.agent_type in
                <foreach collection="agentTypes" item="type" open="(" close=")" separator=",">
                    #{type}
                </foreach>
            </if>

            <if test="agentLevels != null and agentLevels.size() > 0">
                and t2.agent_level in
                <foreach collection="agentLevels" item="agentLevel" open="(" close=")" separator=",">
                    #{agentLevel}
                </foreach>
            </if>


            <if test="targetYear != null ">and target_year = #{targetYear}</if>
            <if test="targetMoney != null ">and target_money = #{targetMoney}</if>
            <if test="actualMoney != null ">and actual_money = #{actualMoney}</if>
            <if test="targetElevatorCount != null ">and target_elevator_count = #{targetElevatorCount}</if>
            <if test="actualElevatorCount != null ">and actual_elevator_count = #{actualElevatorCount}</if>
            <if test="targetProjectCount != null  and targetProjectCount != ''">and target_project_count =
                #{targetProjectCount}
            </if>
            <if test="actualProjectCount != null ">and actual_project_count = #{actualProjectCount}</if>
            <if test="currencies != null and currencies.size() > 0">
                and t1.currency in
                <foreach collection="currencies" item="currency" open="(" close=")" separator=",">
                    #{currency}
                </foreach>
            </if>
        </where>
        order by t1.target_id desc
    </select>

</mapper>