<?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.dayou.mapper.ItemBrokerageDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ItemBrokerageDetail">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="general_id" property="generalId" />
        <result column="user_id" property="userId" />
        <result column="predict_amount" property="predictAmount" />
        <result column="actual_amount" property="actualAmount" />
        <result column="advance_amount" property="advanceAmount" />
        <result column="brokerage_rule" property="brokerageRule" />
        <result column="brokerage_mode" property="brokerageMode" />
        <result column="brokerage_stage_id" property="brokerageStageId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        general_id, user_id, predict_amount, actual_amount, advance_amount, brokerage_rule, brokerage_mode,brokerage_stage_id
    </sql>

    <select id="getPage" parameterType="com.dayou.vo.BrokerageDetailVO" resultType="com.dayou.vo.BrokerageDetailVO">
        SELECT
            ibd.*,
            u.name as userName,
            i.name as itemName,
            i.amount,
            s.name as stageName,
        (case ibd.brokerage_rule when 'LAND_MARKETER_RULE' then '市场人员'
            when 'LAND_SUPERVISOR_RULE' then '营销主管'
            when 'LAND_MANAGER_RULE' then '营销经理'
            when 'LAND_OTHER_RULE' then '参与人员' end) as userType
        FROM
            item_brokerage_detail ibd
                LEFT JOIN item_brokerage_general ibg ON ibg.id = ibd.general_id
                LEFT JOIN item i ON i.id = ibg.item_id
                LEFT JOIN user u ON u.id = ibd.user_id
                LEFT JOIN item_stage s on s.id = ibd.brokerage_stage_id
        where ibd.deleted = 0 and ibg.deleted = 0 and i.deleted = 0
    <if test="vo!=null and vo.generalId!=null">
        and ibd.general_id = #{vo.generalId}
    </if>
    <if test="vo!=null and vo.userName!=null and vo.userName!='' ">
        and u.name like concat ('%',#{vo.userName},'%')
    </if>
    <if test="vo!=null and vo.itemName!=null and vo.itemName!='' ">
        and i.name like concat ('%',#{vo.itemName},'%')
    </if>
    <if test="vo!=null and vo.brokerageRule!=null and vo.brokerageRule!='' ">
        and ibd.brokerage_rule =  #{vo.brokerageRule}
    </if>
    <if test="vo!=null and vo.brokerageMode!=null and vo.brokerageMode!='' ">
        and ibd.brokerage_mode =  #{vo.brokerageMode}
    </if>
        <if test="vo!=null and vo.userIds!=null and vo.userIds.size!=0">
            and  ibd.user_id in
            <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by ibd.id DESC
    </select>

    <select id="xList" parameterType="com.dayou.vo.BrokerageDetailVO" resultType="com.dayou.vo.BrokerageDetailVO">
        SELECT
        ibd.*,
        u.name as userName,
        i.name as itemName,
        i.amount,
        s.name as stageName,
        (case ibd.brokerage_rule when 'LAND_MARKETER_RULE' then '市场人员'
        when 'LAND_SUPERVISOR_RULE' then '营销主管'
        when 'LAND_MANAGER_RULE' then '营销经理'
        when 'LAND_OTHER_RULE' then '参与人员' end) as userType
        FROM
        item_brokerage_detail ibd
        LEFT JOIN item_brokerage_general ibg ON ibg.id = ibd.general_id
        LEFT JOIN item i ON i.id = ibg.item_id
        LEFT JOIN user u ON u.id = ibd.user_id
        LEFT JOIN item_stage s on s.id = ibd.brokerage_stage_id
        where ibd.deleted = 0 and ibg.deleted = 0 and i.deleted = 0
        <if test="vo!=null and vo.generalId!=null">
            and ibd.general_id = #{vo.generalId}
        </if>
        <if test="vo!=null and vo.userName!=null and vo.userName!='' ">
            and u.name like concat ('%',#{vo.userName},'%')
        </if>
        <if test="vo!=null and vo.itemName!=null and vo.itemName!='' ">
            and i.name like concat ('%',#{vo.itemName},'%')
        </if>
        <if test="vo!=null and vo.brokerageRule!=null and vo.brokerageRule!='' ">
            and ibd.brokerage_rule =  #{vo.brokerageRule}
        </if>
        <if test="vo!=null and vo.brokerageMode!=null and vo.brokerageMode!='' ">
            and ibd.brokerage_mode =  #{vo.brokerageMode}
        </if>
        <if test="vo!=null and vo.userIds!=null and vo.userIds.size!=0">
            and  ibd.user_id in
            <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by ibd.id DESC
    </select>

    <select id="getDetailForMarketer" parameterType="java.util.List" resultType="com.dayou.entity.ItemBrokerageDetail">
        select * from item_brokerage_detail where brokerage_mode = 'PERSONAL' and brokerage_rule in ('LAND_SUPERVISOR_RULE','LAND_MARKETER_RULE') and deleted = 0
            and general_id in (
                <foreach collection="list" item="item" index="index" separator=",">
                    #{item}
                </foreach>
                )
    </select>
</mapper>
