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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ItemBrokerageTechnicistDeduction">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="brokerage_technicist_id" property="brokerageTechnicistId" />
        <result column="brokerage_sequence_id" property="brokerageSequenceId" />
        <result column="deduction_amount" property="deductionAmount" />
    </resultMap>

    <resultMap id="deductionRecordsMap" type="com.dayou.vo.TechDeductionVO">
        <result column="id" property="id" />
        <result column="userId" property="userId" />
        <result column="userName" property="userName"/>
        <result column="year" property="year"/>
        <result column="month" property="month"/>
        <result column="brokerageAmount" property="brokerageAmount"/>
        <result column="cate" property="cate"/>
        <result column="created" property="created"/>
        <result column="notDeductionAmount" property="notDeductionAmount"/>
        <collection property="deductionRecords" ofType="com.dayou.vo.DeductionVO" select="selectDeductionRecords" column="id"/>
    </resultMap>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        created,
        brokerage_technicist_id, brokerage_sequence_id,deduction_amount
    </sql>

    <select id="notDeductionDefaultSettle" parameterType="java.lang.Long" resultType="com.dayou.dto.DeductionDTO">
        SELECT

            ibs.id as sequenceId,
            ibs.brokerage_amount as totalAmount,
            (ibs.brokerage_amount- ifnull(ibtd.deduDone,0)) as notDeduAmount
        FROM
            item_brokerage_sequence ibs
                LEFT JOIN (select brokerage_sequence_id,sum(deduction_amount) as deduDone from item_brokerage_technicist_deduction GROUP BY brokerage_sequence_id) ibtd ON ibtd.brokerage_sequence_id = ibs.id
        WHERE
            ibs.user_id = #{userId} and ibs.cate = 'DEFAULT_AHEAD' and
            ifnull(ibtd.deduDone,0) &lt; ibs.brokerage_amount
                                      order by ibs.id ASC
    </select>

    <select id="xPage" parameterType="com.dayou.vo.TechDeductionVO" resultMap="deductionRecordsMap">
        SELECT
            ibs.id,
            u.id AS userId,
            u.NAME AS userName,
            ibs.YEAR,
            ibs.MONTH,
            ibs.cate,
            ibs.brokerage_amount AS brokerageAmount,
            ibs.created,
            (ibs.brokerage_amount - ifnull( ibtd.deduDone, 0 ))  AS notDeductionAmount
        FROM
            item_brokerage_sequence ibs
                LEFT JOIN ( SELECT brokerage_sequence_id, sum( deduction_amount ) AS deduDone FROM item_brokerage_technicist_deduction WHERE deleted = 0 GROUP BY brokerage_sequence_id ) ibtd ON ibtd.brokerage_sequence_id = ibs.id
                LEFT JOIN user u ON u.id = ibs.user_id
        WHERE
            ibs.user_id = #{vo.userId} and ibs.deleted = 0 and ibs.cate = 'DEFAULT_AHEAD'
        <if test="vo.cate!=null and vo.cate!='' ">
            and ibs.cate = #{vo.cate}
        </if>
        <if test="vo.year!=null ">
            and ibs.year = #{vo.year}
        </if>
        <if test="vo.month!=null ">
            and ibs.month = #{vo.month}
        </if>
order by ibs.id DESC
    </select>

    <select id="selectDeductionRecords" parameterType="java.lang.Long" resultType="com.dayou.vo.DeductionVO">
        SELECT
            ibtd.deduction_amount,
            ibtd.created ,
            ibt.amount as settleAmount,
            i.name as itemName,
            i.id as itemId,
            ibs.year,
            ibs.month,
            ibs.cate as settleCate
        FROM
            item_brokerage_technicist_deduction ibtd
                LEFT JOIN item_brokerage_technicist ibt on ibtd.brokerage_technicist_id = ibt.id
                left join item i on i.id = ibt.item_id
                left join item_brokerage_sequence ibs on ibs.id = ibt.sequence_id
        where ibtd.deleted = 0 and ibt.deleted =0 and ibs.deleted = 0 and ibtd.brokerage_sequence_id = #{id}
        order by ibtd.id DESC
    </select>

    <select id="poolResidueAmount" parameterType="java.lang.Long" resultType="java.math.BigDecimal">
        SELECT
            (
                    ( SELECT ifnull( sum( residue_amount ), 0 ) FROM `item_brokerage_technicist` WHERE user_id = #{userId} AND deleted = 0 ) -(
                    SELECT
                        ifnull( sum( ibs.brokerage_amount ), 0 ) -  ifnull(sum( ibtd.deductionAmount ),0)
                    FROM
                        item_brokerage_sequence ibs
                            LEFT JOIN ( SELECT brokerage_sequence_id AS seId, sum( deduction_amount ) AS deductionAmount FROM item_brokerage_technicist_deduction GROUP BY brokerage_sequence_id ) ibtd ON ibs.id = ibtd.seId
                    WHERE
                        ibs.user_id = #{userId}
                      AND ibs.cate = 'DEFAULT_AHEAD'
                      AND ibs.deleted = 0
                )
                ) AS poolResidueAmount;
    </select>
</mapper>
