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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ItemBrokerageTechnicist">
        <result column="id" property="id" />
        <result column="modified" property="modified" />
        <result column="sequence_id" property="sequenceId" />
        <result column="item_id" property="itemId" />
        <result column="user_id" property="userId" />
        <result column="amount" property="amount" />
        <result column="residue_amount" property="residueAmount" />
    </resultMap>

    <resultMap id="defaultDeductionRecordsMap" type="com.dayou.vo.DeductionTechItemVO">
        <result column="id" property="id" />
        <result column="sequenceId" property="sequenceId"/>
        <result column="embody" property="embody"/>
        <result column="itemId" property="itemId"/>
        <result column="itemName" property="itemName"/>
        <result column="userId" property="userId" />
        <result column="userName" property="userName"/>
        <result column="brokerageAmount" property="brokerageAmount"/>
        <result column="residue_amount" property="residueAmount"/>
        <result column="created" property="created"/>
        <collection property="defaultDeductions" ofType="com.dayou.vo.TechDeductionVO" select="selectDefaultDeductionRecords" column="id"/>
    </resultMap>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        modified,
        sequence_id, item_id, user_id, amount, residue_amount
    </sql>

    <select id="notSettleForTechnicist" parameterType="java.lang.Long" resultType="com.dayou.entity.ItemBrokerageTechnicist">
        SELECT
            ibt.*
        FROM
            item_brokerage_technicist ibt
                LEFT JOIN item_brokerage_general ibg ON ibt.item_id = ibg.item_id
        WHERE
                (select amount from item where  id = ibt.item_id and deleted = 0 )  =
                (select sum(amount) from payment_collection where item_id = ibt.item_id)
          AND ibt.residue_amount != 0
	AND  ibt.user_id = #{userId}
    </select>

    <select id="xPage" parameterType="com.dayou.vo.DeductionTechItemVO" resultMap="defaultDeductionRecordsMap">
        SELECT
            ibt.id,
            ibt.sequence_id,
            ibt.item_id,
            ibt.user_id,
            ibt.amount AS brokerageAmount,
            ibt.residue_amount,
            ibt.created ,
            i.name as itemName,
            u.name as userName,
            ibs.embody
        FROM
            item_brokerage_technicist ibt
                LEFT JOIN item_brokerage_sequence ibs ON ibs.id = ibt.sequence_id
                LEFT JOIN item i ON i.id = ibt.item_id
                LEFT JOIN user u ON u.id = ibt.user_id
        WHERE
            ibt.deleted = 0
          AND ibs.deleted = 0
          AND ibt.user_id = #{vo.userId}
            <if test="vo.itemName!=null and vo.itemName!='' ">
               and i.name like concat ('%',#{vo.itemName},'%')
            </if>
        ORDER BY
            ibt.id DESC
    </select>
    <select id="selectDefaultDeductionRecords" parameterType="java.lang.Long" resultType="com.dayou.vo.TechDeductionVO">
        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.deduction_amount, 0 ))  AS notDeductionAmount
        FROM
            item_brokerage_technicist_deduction ibtd
                LEFT JOIN item_brokerage_sequence  ibs ON ibtd.brokerage_sequence_id = ibs.id
                LEFT JOIN user u ON u.id = ibs.user_id
        where ibtd.brokerage_technicist_id = #{id} and ibtd.deleted = 0 and ibs.deleted = 0
        order by ibtd.id DESC
    </select>

    <select id="oncePage" parameterType="com.dayou.vo.DeductionTechItemVO" resultType="com.dayou.vo.DeductionTechItemVO">
        SELECT
            ibs.id,
            u.NAME AS userName,
            i.NAME AS itemName,
            ibs.brokerage_amount AS onceAmount,
            ibs.created,
            ibs.remark
        FROM
            item_brokerage_sequence ibs
                LEFT JOIN item_brokerage_technicist_deduction ibtd ON ibtd.brokerage_sequence_id = ibs.id
                LEFT JOIN item_brokerage_technicist ibt ON ibt.id = ibtd.brokerage_technicist_id
                LEFT JOIN item i ON i.id = ibt.item_id
                LEFT JOIN user u ON u.id = ibt.user_id
        WHERE
            ibs.cate = 'ONCE_SETTLE'
          AND ibt.user_id = #{vo.userId}
        <if test="vo.itemName!=null and vo.itemName!='' ">
            and i.name like concat ('%',#{vo.itemName},'%')
        </if>
          AND ibs.deleted = 0 and ibt.deleted = 0 and ibtd.deleted = 0
    </select>
</mapper>
