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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ItemBrokerageSequenceDetail">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="brokerage_detail_id" property="brokerageDetailId" />
        <result column="sequence_id" property="sequenceId" />
        <result column="brokerage_amount" property="brokerageAmount" />
        <result column="brokerage_cate" property="brokerageCate" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        brokerage_detail_id, sequence_id, brokerage_amount, brokerage_cate
    </sql>

    <select id="xPage" parameterType="com.dayou.vo.BrokerageSequenceDetailVO" resultType="com.dayou.vo.BrokerageSequenceDetailVO">
        SELECT
            ibsd.*,
            u.NAME AS userName,
            ibd.predict_amount,
            ibd.brokerage_rule,
            ibd.brokerage_mode ,
            s.name as stageName,
            i.name as itemName,
        pc.amount as paymentAmount,
        pc.name as paymentName,
        ibs.year,
        ibs.month
        FROM
            item_brokerage_sequence_detail ibsd
                LEFT JOIN item_brokerage_detail ibd ON ibd.id = ibsd.brokerage_detail_id
                LEFT JOIN user u ON u.id = ibsd.user_id
                LEFT JOIN item_stage s ON s.id = ibd.brokerage_stage_id
                left join item_brokerage_general g on g.id = ibd.general_id
                left join item i on i.id = g.item_id
        left join payment_collection pc on ibsd.payment_id = pc.id
        left join item_brokerage_sequence ibs on ibsd.sequence_id = ibs.id
        WHERE
            ibsd.deleted = 0
          AND ibd.deleted = 0
          AND u.deleted = 0
          and g.deleted = 0
          and i.deleted = 0
          and ibs.deleted = 0
        <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.year!=null ">
            and ibs.year = #{vo.year}
        </if>
        <if test="vo!=null and vo.month!=null ">
            and ibs.month = #{vo.month}
        </if>
        <if test="vo!=null and vo.brokerageCate!=null and vo.brokerageCate!='' ">
            and ibsd.brokerage_cate = #{vo.brokerageCate}
        </if>
        <if test="vo!=null and vo.userIds!=null and vo.userIds.size!=0">
            and  ibsd.user_id in
            <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by ibsd.id DESC
    </select>

    <select id="xList" parameterType="com.dayou.vo.BrokerageSequenceDetailVO" resultType="com.dayou.vo.BrokerageSequenceDetailVO">
        SELECT
        ibsd.*,
        u.NAME AS userName,
        ibd.predict_amount,
        ibd.brokerage_rule,
        ibd.brokerage_mode ,
        s.name as stageName,
        i.name as itemName,
        pc.amount as paymentAmount,
        pc.name as paymentName,
        ibs.year,
        ibs.month
        FROM
        item_brokerage_sequence_detail ibsd
        LEFT JOIN item_brokerage_detail ibd ON ibd.id = ibsd.brokerage_detail_id
        LEFT JOIN user u ON u.id = ibsd.user_id
        LEFT JOIN item_stage s ON s.id = ibd.brokerage_stage_id
        left join item_brokerage_general g on g.id = ibd.general_id
        left join item i on i.id = g.item_id
        left join payment_collection pc on ibsd.payment_id = pc.id
        left join item_brokerage_sequence ibs on ibsd.sequence_id = ibs.id
        WHERE
        ibsd.deleted = 0
        AND ibd.deleted = 0
        AND u.deleted = 0
        and g.deleted = 0
        and i.deleted = 0
        and ibs.deleted = 0
        <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.year!=null ">
            and ibs.year = #{vo.year}
        </if>
        <if test="vo!=null and vo.month!=null ">
            and ibs.month = #{vo.month}
        </if>
        <if test="vo!=null and vo.brokerageCate!=null and vo.brokerageCate!='' ">
            and ibsd.brokerage_cate = #{vo.brokerageCate}
        </if>
        order by ibsd.id DESC
    </select>
</mapper>
