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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ItemBrokerageGeneral">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="item_id" property="itemId" />
        <result column="marketer_brokerage_amount" property="marketerBrokerageAmount" />
        <result column="marketer_advance_brokerage_able" property="marketerAdvanceBrokerageAble" />
        <result column="marketer_advance_brokerage_done" property="marketerAdvanceBrokerageDone" />
        <result column="participant_predict_brokerage_amount" property="participantPredictBrokerageAmount" />
        <result column="participant_actual_brokerage_amount" property="participantActualBrokerageAmount" />
        <result column="participant_advance_status" property="participantAdvanceStatus" />
        <result column="brokerage_predict_amount" property="brokeragePredictAmount" />
        <result column="brokerage_actual_amount" property="brokerageActualAmount" />
        <result column="brokerage_rate" property="brokerageRate" />
        <result column="participant_status" property="participantStatus" />
        <result column="marketer_status" property="marketerStatus" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        item_id, marketer_brokerage_amount, marketer_advance_brokerage_able, marketer_advance_brokerage_done, participant_predict_brokerage_amount, participant_actual_brokerage_amount, participant_advance_status, brokerage_predict_amount, brokerage_actual_amount, brokerage_rate,
            participant_status,marketer_status
    </sql>

    <select id="aheadPage" parameterType="com.dayou.vo.BrokerageGeneralVO" resultType="com.dayou.vo.BrokerageGeneralVO">
        SELECT
            ibg.id,
            ibg.marketer_brokerage_amount,
            ibg.marketer_advance_brokerage_able as marketerAdvanceBrokerageAble,
            ibg.brokerage_rate,
            ibg.marketer_advance_brokerage_done,
            ibg.participant_predict_brokerage_amount,
            ibg.participant_actual_brokerage_amount,
            ibg.brokerage_predict_amount,
            ibg.brokerage_actual_amount,
            ibg.participant_status,
            ibg.marketer_status,
            dd1.name AS cateName,
            dd2.name AS businessSourceName,
            i.id as itemId,
            i.deleted,
            i.business_no,
            i.oa_no,
            i.name,
            i.client_unit,
            i.client_name,
            u.name as client_manager,
            i.amount
        FROM
            item_brokerage_general ibg
                LEFT JOIN item i ON i.id = ibg.item_id
                LEFT JOIN dict_data dd1 ON i.cate = dd1.id
                LEFT JOIN dict_data dd2 ON i.business_source = dd2.id
                LEFT JOIN user u on u.id = i.user_id
        WHERE
           i.deleted = 0
          AND ibg.deleted = 0
          AND dd1.deleted = 0
          AND dd2.deleted = 0
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.marketerAdvanceBrokerageAble!=null">
            and ibg.marketer_advance_brokerage_able = #{itemBrokerageGeneral.marketerAdvanceBrokerageAble}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.marketerAdvanceBrokerageDone!=null">
            and ibg.marketer_advance_brokerage_done = #{itemBrokerageGeneral.marketerAdvanceBrokerageDone}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.name!=null and itemBrokerageGeneral.name!='' ">
            and i.name like concat ('%',#{itemBrokerageGeneral.name},'%')
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.businessNo!=null and itemBrokerageGeneral.businessNo!='' ">
            and i.business_no like concat ('%',#{itemBrokerageGeneral.businessNo},'%')
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.clientManager!=null and itemBrokerageGeneral.clientManager!='' ">
            and u.name like concat ('%',#{itemBrokerageGeneral.clientManager},'%')
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.participantStatus!=null and itemBrokerageGeneral.participantStatus!='' ">
            and ibg.participant_status = #{itemBrokerageGeneral.participantStatus}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.marketerStatus!=null and itemBrokerageGeneral.marketerStatus!='' ">
            and ibg.marketer_status=#{itemBrokerageGeneral.marketerStatus}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.userIds!=null and itemBrokerageGeneral.userIds.size!=0">
            and  i.user_id in
            <foreach collection="itemBrokerageGeneral.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        ORDER BY
            ibg.id DESC
    </select>

    <select id="getByItemId" parameterType="java.lang.Long" resultType="com.dayou.entity.ItemBrokerageGeneral">
        select * from item_brokerage_general where item_id= #{itemId} and deleted = 0
    </select>

    <select id="xList" parameterType="com.dayou.vo.BrokerageGeneralVO" resultType="com.dayou.vo.BrokerageGeneralVO">
        SELECT
        ibg.id,
        ibg.marketer_brokerage_amount,
        ibg.marketer_advance_brokerage_able as marketerAdvanceBrokerageAble,
        ibg.brokerage_rate,
        ibg.marketer_advance_brokerage_done,
        ibg.participant_predict_brokerage_amount,
        ibg.participant_actual_brokerage_amount,
        ibg.brokerage_predict_amount,
        ibg.brokerage_actual_amount,
        ibg.participant_status,
        ibg.marketer_status,
        dd1.name AS cateName,
        dd2.name AS businessSourceName,
        i.id as itemId,
        i.deleted,
        i.business_no,
        i.oa_no,
        i.name,
        i.client_unit,
        i.client_name,
        u.name as client_manager,
        i.amount
        FROM
        item_brokerage_general ibg
        LEFT JOIN item i ON i.id = ibg.item_id
        LEFT JOIN dict_data dd1 ON i.cate = dd1.id
        LEFT JOIN dict_data dd2 ON i.business_source = dd2.id
        WHERE
        i.deleted = 0
        AND ibg.deleted = 0
        AND dd1.deleted = 0
        AND dd2.deleted = 0
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.marketerAdvanceBrokerageAble!=null">
            and ibg.marketer_advance_brokerage_able = #{itemBrokerageGeneral.marketerAdvanceBrokerageAble}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.marketerAdvanceBrokerageDone!=null">
            and ibg.marketer_advance_brokerage_done = #{itemBrokerageGeneral.marketerAdvanceBrokerageDone}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.name!=null and itemBrokerageGeneral.name!='' ">
            and i.name like concat ('%',#{itemBrokerageGeneral.name},'%')
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.businessNo!=null and itemBrokerageGeneral.businessNo!='' ">
            and i.business_no like concat ('%',#{itemBrokerageGeneral.businessNo},'%')
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.clientManager!=null and itemBrokerageGeneral.clientManager!='' ">
            and u.name like concat ('%',#{itemBrokerageGeneral.clientManager},'%')
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.participantStatus!=null and itemBrokerageGeneral.participantStatus!='' ">
            and ibg.participant_status = #{itemBrokerageGeneral.participantStatus}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.marketerStatus!=null and itemBrokerageGeneral.marketerStatus!='' ">
            and ibg.marketer_status=#{itemBrokerageGeneral.marketerStatus}
        </if>
        <if test="itemBrokerageGeneral!=null and itemBrokerageGeneral.userIds!=null and itemBrokerageGeneral.userIds.size!=0">
            and  i.user_id in
            <foreach collection="itemBrokerageGeneral.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        ORDER BY
        ibg.id DESC
    </select>
</mapper>
