<?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.zehong.system.mapper.TTradeProjectMapper">
    
    <resultMap type="TTradeProject" id="TTradeProjectResult">
        <result property="tradeId"    column="trade_id"    />
        <result property="receiptNum"    column="receipt_num"    />
        <result property="relationTransactionProjectId"    column="relation_transaction_project_id"    />
        <result property="transactionDetailName"    column="transaction_detail_name"    />
        <result property="projectName"    column="project_name"    />
        <result property="applyId"    column="apply_id"    />
        <result property="applyDeptId"    column="apply_dept_id"    />
        <result property="applyDeptManagerId"    column="apply_dept_manager_id"    />
        <result property="tradeTransactor"    column="trade_transactor"    />
        <result property="tradeDeptId"    column="trade_dept_id"    />
        <result property="tradePrice"    column="trade_price"    />
        <result property="tradeCount"    column="trade_count"    />
        <result property="tradeTotal"    column="trade_total"    />
        <result property="tradeScore"    column="trade_score"    />
        <result property="tradeDeptManagerId"    column="trade_dept_manager_id"    />
        <result property="settlementTime"    column="settlement_time"    />
        <result property="dealPrice"    column="deal_price"    />
        <result property="pendingPayment"    column="pending_payment"    />
        <result property="attachmentUrl"    column="attachment_url"    />
        <result property="tradeStatus"    column="trade_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remark"    column="remark"    />
        <result property="dealRemark"    column="deal_remark"    />
        <result property="applyName" column="apply_name"/>
        <result property="tradeTransactorName" column="trade_transactor_name"/>

        <result property="applyDeptName" column="apply_dept_name"/>
        <result property="tradeDeptName" column="trade_dept_name"/>
        <result property="tradeDeptManagerName" column="trade_dept_manager_name"/>
        <result property="applyDeptManagerName" column="apply_dept_manager_name"/>
    </resultMap>

    <sql id="selectTTradeProjectVo">
       SELECT
            trade_id,
            receipt_num,
            relation_transaction_project_id,
            transaction_detail_name,
            apply_id,
            apply_dept_id,
            apply_dept_manager_id,
            trade_transactor,
            trade_dept_id,
            trade_price,
            trade_count,
            trade_total,
            trade_score,
            trade_dept_manager_id,
            settlement_time,
            deal_price,
            pending_payment,
            attachment_url,
            trade_status,
            create_time,
            update_time,
            is_del,
            remark,
            deal_remark,
            (SELECT nick_name FROM sys_user WHERE user_id = trade_transactor) AS trade_transactor_name,
           (SELECT nick_name FROM sys_user WHERE user_id = apply_id) AS apply_name,
           (SELECT dept_name FROM sys_dept WHERE dept_id = apply_dept_id) AS apply_dept_name,
           (SELECT dept_name FROM sys_dept WHERE dept_id = trade_dept_id) AS trade_dept_name,
           (SELECT nick_name FROM sys_user WHERE user_id = trade_dept_manager_id) AS trade_dept_manager_name,
           (SELECT nick_name FROM sys_user WHERE user_id = apply_dept_manager_id)AS apply_dept_manager_name
        FROM
            t_trade_project
    </sql>

    <select id="selectTTradeProjectList" parameterType="TTradeProject" resultMap="TTradeProjectResult">
        <include refid="selectTTradeProjectVo"/>
        <where>  
            <if test="receiptNum != null  and receiptNum != ''"> and receipt_num = #{receiptNum}</if>
            <if test="relationTransactionProjectId != null "> and relation_transaction_project_id = #{relationTransactionProjectId}</if>
            <if test="queryType != 'all'">
                <if test="applyId != null"> and apply_id = #{applyId}</if>
                <if test="applyDeptId != null"> and apply_dept_id = #{applyDeptId}</if>
                <if test="tradeTransactor != null"> and trade_transactor = #{tradeTransactor}</if>
                <if test="tradeDeptId != null"> and trade_dept_id = #{tradeDeptId}</if>
            </if>
            <!-- add by lizhichao 0612 -->
            <!-- 当同时拥有部门长及经办人角色时 -->
            <if test="queryType == 'all' and applyDeptId != null  and tradeTransactor == null">
                and (
                (apply_dept_id = #{applyDeptId}
                <if test="applyId != null"> and apply_id = #{applyId}</if>)
                or(trade_dept_id = #{tradeDeptId}
                <if test="tradeTransactor != null"> and trade_transactor = #{tradeTransactor}</if>)
                )
            </if>

            <if test="queryType == 'all' and applyDeptId != null  and tradeTransactor != null">
                and (
                 (apply_dept_id = #{applyDeptId}
                   <if test="applyId != null"> and apply_id = #{applyId}</if>)
                 or(trade_dept_id = #{tradeDeptId}
                  <if test="tradeTransactor != null"> and trade_transactor = #{tradeTransactor}</if>)
                )
            </if>
            <if test="applyDeptManagerId != null "> and apply_dept_manager_id = #{applyDeptManagerId}</if>
            <if test="tradePrice != null "> and trade_price = #{tradePrice}</if>
            <if test="tradeCount != null "> and trade_count = #{tradeCount}</if>
            <if test="tradeTotal != null "> and trade_total = #{tradeTotal}</if>
            <if test="tradeScore != null "> and trade_score = #{tradeScore}</if>
            <if test="tradeDeptManagerId != null "> and trade_dept_manager_id = #{tradeDeptManagerId}</if>
            <if test="settlementTime != null "> and settlement_time = #{settlementTime}</if>
            <if test="dealPrice != null "> and deal_price = #{dealPrice}</if>
            <if test="pendingPayment != null "> and pending_payment = #{pendingPayment}</if>
            <if test="attachmentUrl != null  and attachmentUrl != ''"> and attachment_url = #{attachmentUrl}</if>
            <if test="tradeStatus != null  and tradeStatus != ''"> and trade_status = #{tradeStatus}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
            <if test="queryApplyDeptId != null">and apply_dept_id = #{queryApplyDeptId}</if>
            <if test="queryTradeDeptId != null">and trade_dept_id = #{queryTradeDeptId}</if>
            <if test="transactionDetailName != null">and transaction_detail_name like concat('%', #{transactionDetailName}, '%')</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectTTradeProjectById" parameterType="Long" resultMap="TTradeProjectResult">
        <include refid="selectTTradeProjectVo"/>
        where trade_id = #{tradeId}
    </select>
        
    <insert id="insertTTradeProject" parameterType="TTradeProject" useGeneratedKeys="true" keyProperty="tradeId">
        insert into t_trade_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="receiptNum != null">receipt_num,</if>
            <if test="relationTransactionProjectId != null">relation_transaction_project_id,</if>
            <if test="transactionDetailName != null">transaction_detail_name,</if>
            <if test="applyId != null">apply_id,</if>
            <if test="applyDeptId != null">apply_dept_id,</if>
            <if test="applyDeptManagerId != null">apply_dept_manager_id,</if>
            <if test="tradeTransactor != null">trade_transactor,</if>
            <if test="tradeDeptId != null">trade_dept_id,</if>
            <if test="tradePrice != null">trade_price,</if>
            <if test="tradeCount != null">trade_count,</if>
            <if test="tradeTotal != null">trade_total,</if>
            <if test="tradeScore != null">trade_score,</if>
            <if test="tradeDeptManagerId != null">trade_dept_manager_id,</if>
            <if test="settlementTime != null">settlement_time,</if>
            <if test="dealPrice != null">deal_price,</if>
            <if test="pendingPayment != null">pending_payment,</if>
            <if test="attachmentUrl != null">attachment_url,</if>
            <if test="tradeStatus != null">trade_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="receiptNum != null">#{receiptNum},</if>
            <if test="relationTransactionProjectId != null">#{relationTransactionProjectId},</if>
            <if test="transactionDetailName != null">#{transactionDetailName},</if>
            <if test="applyId != null">#{applyId},</if>
            <if test="applyDeptId != null">#{applyDeptId},</if>
            <if test="applyDeptManagerId != null">#{applyDeptManagerId},</if>
            <if test="tradeTransactor != null">#{tradeTransactor},</if>
            <if test="tradeDeptId != null">#{tradeDeptId},</if>
            <if test="tradePrice != null">#{tradePrice},</if>
            <if test="tradeCount != null">#{tradeCount},</if>
            <if test="tradeTotal != null">#{tradeTotal},</if>
            <if test="tradeScore != null">#{tradeScore},</if>
            <if test="tradeDeptManagerId != null">#{tradeDeptManagerId},</if>
            <if test="settlementTime != null">#{settlementTime},</if>
            <if test="dealPrice != null">#{dealPrice},</if>
            <if test="pendingPayment != null">#{pendingPayment},</if>
            <if test="attachmentUrl != null">#{attachmentUrl},</if>
            <if test="tradeStatus != null">#{tradeStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTTradeProject" parameterType="TTradeProject">
        update t_trade_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="receiptNum != null">receipt_num = #{receiptNum},</if>
            <if test="relationTransactionProjectId != null">relation_transaction_project_id = #{relationTransactionProjectId},</if>
            <if test="transactionDetailName != null">transaction_detail_name = #{transactionDetailName},</if>
            <if test="applyId != null">apply_id = #{applyId},</if>
            <if test="applyDeptId != null">apply_dept_id = #{applyDeptId},</if>
            <if test="applyDeptManagerId != null">apply_dept_manager_id = #{applyDeptManagerId},</if>
            <if test="tradeTransactor != null">trade_transactor = #{tradeTransactor},</if>
            <if test="tradeDeptId != null">trade_dept_id = #{tradeDeptId},</if>
            <if test="tradePrice != null">trade_price = #{tradePrice},</if>
            <if test="tradeCount != null">trade_count = #{tradeCount},</if>
            <if test="tradeTotal != null">trade_total = #{tradeTotal},</if>
            <if test="tradeScore != null and tradeScore!=-1">trade_score = #{tradeScore},</if>
            <if test="tradeScore == -1">trade_score = null,</if>
            <if test="tradeDeptManagerId != null">trade_dept_manager_id = #{tradeDeptManagerId},</if>
            <if test="settlementTime != null">settlement_time = #{settlementTime},</if>
            <if test="dealPrice != null">deal_price = #{dealPrice},</if>
            <if test="pendingPayment != null">pending_payment = #{pendingPayment},</if>
            <if test="attachmentUrl != null">attachment_url = #{attachmentUrl},</if>
            <if test="tradeStatus != null">trade_status = #{tradeStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="dealRemark != null">deal_remark = #{dealRemark},</if>

        </trim>
        where trade_id = #{tradeId}
    </update>

    <delete id="deleteTTradeProjectById" parameterType="Long">
        delete from t_trade_project where trade_id = #{tradeId}
    </delete>

    <delete id="deleteTTradeProjectByIds" parameterType="String">
        delete from t_trade_project where trade_id in 
        <foreach item="tradeId" collection="array" open="(" separator="," close=")">
            #{tradeId}
        </foreach>
    </delete>

    <select id="getTradePendingPaymentInfo" parameterType="Long" resultMap="TTradeProjectResult">
        <include refid="selectTTradeProjectVo"/>
            t_trade_project
        WHERE trade_dept_id = #{tradeDeptId}
        AND trade_status = '3'
        AND pending_payment > 0
    </select>
    <select id="getIncomeOrPayableList" parameterType="TTradeProject" resultMap="TTradeProjectResult">
        SELECT
        trade_id,
        receipt_num,
        relation_transaction_project_id,
        apply_id,
        apply_dept_id,
        apply_dept_manager_id,
        (select transaction_project_name from t_transaction_project where transaction_project_id=relation_transaction_project_id) project_name,
        trade_transactor,
        trade_dept_id,
        trade_price,
        trade_count,
        trade_total,
        trade_score,
        trade_dept_manager_id,
        settlement_time,
        deal_price,
        pending_payment,
        attachment_url,
        trade_status,
        create_time,
        update_time,
        is_del,
        remark,
        deal_remark,
        (SELECT nick_name FROM sys_user WHERE user_id = trade_transactor) AS trade_transactor_name,
        (SELECT nick_name FROM sys_user WHERE user_id = apply_id) AS apply_name,
        (SELECT dept_name FROM sys_dept WHERE dept_id = apply_dept_id) AS apply_dept_name,
        (SELECT dept_name FROM sys_dept WHERE dept_id = trade_dept_id) AS trade_dept_name
        FROM
        t_trade_project
        <where>
            trade_status =3 and pending_payment>0
            <if test="applyDeptId != null"> and apply_dept_id = #{applyDeptId}</if>
            <if test="tradeTransactor != null"> and trade_transactor = #{tradeTransactor}</if>
            <if test="tradeDeptId != null"> and trade_dept_id = #{tradeDeptId}</if>
        </where>
        order by create_time desc
    </select>
</mapper>