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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.vo.BrokerageSequenceVO">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="user_id" property="userId" />
        <result column="userName" property="userName" />
        <result column="brokerage_amount" property="brokerageAmount" />
        <result column="embody" property="embody" />
        <result column="remark" property="remark" />
        <result column="cate" property="cate"/>
        <collection property="details" ofType="com.dayou.vo.BrokerageSequenceDetailVO" select="selectSequenceDetail" column="id"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        user_id, brokerage_amount, embody, remark
    </sql>

    <select id="getPage" parameterType="com.dayou.vo.BrokerageSequenceVO" resultMap="BaseResultMap">
        select ibs.*,u.name as userName from item_brokerage_sequence ibs left join user u on u.id = ibs.user_id
        where u.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.cate!=null and vo.cate!='' ">
            and ibs.cate =#{vo.cate}
        </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.userIds!=null and vo.userIds.size!=0">
            and  ibs.user_id in
            <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by ibs.id DESC
    </select>

    <select id="selectSequenceDetail" parameterType="java.lang.Long" resultType="com.dayou.vo.BrokerageSequenceDetailVO">
        SELECT
            ibsd.*,
            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
        FROM
            item_brokerage_sequence_detail ibsd
                LEFT JOIN item_brokerage_detail ibd ON ibd.id = ibsd.brokerage_detail_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

        WHERE
            ibsd.deleted = 0
          AND ibd.deleted = 0
          and g.deleted = 0
          and i.deleted = 0
          and ibsd.sequence_id = #{id}
    </select>

    <select id="deduction" parameterType="com.dayou.vo.BrokerageSequenceVO" resultType="com.dayou.vo.BrokerageSequenceVO">
        SELECT
        ibs.id,
        ibs.created,
        (case ibs.cate when 'DEFAULT_AHEAD' then
        - ibs.brokerage_amount when 'ONCE_SETTLE' then -ibs.brokerage_amount   else ibs.brokerage_amount end) brokerageAmount,
        ibs.embody,
        ibs.cate,
        u.NAME AS userName  from item_brokerage_sequence ibs left join user u on u.id = ibs.user_id
        where u.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.cate!=null and vo.cate!='' ">
            and ibs.cate =#{vo.cate}
        </if>
        <if test="vo!=null and vo.userIds!=null and vo.userIds.size!=0">
            and  ibs.user_id in
            <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by ibs.id DESC
    </select>

    <select id="xList" parameterType="com.dayou.vo.BrokerageSequenceVO" resultType="com.dayou.vo.BrokerageSequenceVO">
        select ibs.*,u.name as userName from item_brokerage_sequence ibs left join user u on u.id = ibs.user_id
        where u.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.cate!=null and vo.cate!='' ">
            and ibs.cate =#{vo.cate}
        </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.userIds!=null and vo.userIds.size!=0">
            and  ibs.user_id in
            <foreach collection="vo.userIds" open="(" close=")" separator="," item="userId">
                #{userId}
            </foreach>
        </if>
        order by ibs.id DESC
    </select>
</mapper>
