<?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.ytpm.middle.dao.FinanceMapper">
    <insert id="addItem">
        insert into yt_finance_payment_item
            (item_id, item_name, item_amount, unit, count, create_time, create_user_id, available)
        values
            (#{itemId},#{itemName},#{itemAmount},#{unit},#{count},#{createTime},#{createUserId},#{available});
    </insert>
    <insert id="addPaymentRecord">
        insert into yt_finance_payment_record
        (
         record_id,
         user_id,
         payment_id,
         item_id,
         should_amount
        )
        values
        (
         #{recordId},
         #{userId},
         #{paymentId},
         #{itemId},
         #{shouldAmount}
        )
    </insert>
    <insert id="addPayment">
        insert into yt_finance_payment
        (
         payment_id,
         payment_no,
         payment_type,
         total_amount,
         user_id,
         create_time,
         create_user_id,
         available
        )
        values
        (
         #{paymentId},
         #{paymentNo},
         #{paymentType},
         #{totalAmount},
         #{userId},
         #{createTime},
         #{createUserId},
         #{available}
        )
    </insert>
    <update id="updateItem">
        update yt_finance_payment_item
        <set>
            <if test="itemName != null">
                item_name = #{itemName},
            </if>
            <if test="itemAmount != null">
                item_amount = #{itemAmount},
            </if>
            <if test="unit != null">
                unit = #{unit},
            </if>
            <if test="count != null">
                `count` = #{count},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId},
            </if>
            <if test="available != null">
                available = #{available}
            </if>
        </set>
        where item_id = #{itemId}
    </update>
    <update id="updatePayment">
        update yt_finance_payment
        <set>
            <if test="paymentNo != null">
                payment_no = #{paymentNo},
            </if>
            <if test="paymentType != null">
                payment_type = #{paymentType},
            </if>
            <if test="paymentStatus != null">
                payment_status = #{paymentStatus},
            </if>
            <if test="totalAmount != null">
                total_amount = #{totalAmount},
            </if>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            <if test="finishTime != null">
                finish_time = #{finishTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId},
            </if>
            <if test="available != null">
                available = #{available}
            </if>
        </set>
        where payment_id = #{paymentId}
    </update>
    <update id="updatePaymentRecord">
        update yt_finance_payment_record
        set receive_amount = should_amount, receive_time = now(),charge_status=1
        where payment_id = #{paymentId}
    </update>

    <select id="queryList" resultType="com.ytpm.middle.view.PaymentItemVO">
        select
            item_id, item_name, item_amount, unit, count, create_time, create_user_id, update_time, update_user_id, available
        from yt_finance_payment_item
        where available = 1
        <if test="itemName != null and itemName != ''">
            and item_name like concat('%',#{itemName},'%')
        </if>
    </select>
    <select id="selectItemOne" resultType="com.ytpm.middle.model.YtFinancePaymentItem">
        select
            item_id, item_name, item_amount, unit, count, create_time, create_user_id, update_time, update_user_id, available
        from yt_finance_payment_item
        where item_id = #{itemId}
    </select>
    <select id="paymentList" resultType="com.ytpm.middle.view.FinancePaymentVO">
        SELECT
            fp.payment_id,
            fp.payment_no,
            fp.total_amount,
            fp.payment_status,
            me.enterprise_id,
            me.enterprise_name,
            me.credit_code,
            me.enterprise_type,
            me.sharing_ratio,
            me.has_fast
        FROM
            yt_finance_payment fp
            JOIN yt_middle_enterprise me ON fp.user_id = me.user_id
        WHERE
            fp.available = 1
        <if test="paymentNo != null and paymentNo != ''">
            and fp.payment_no like concat('%',#{paymentNo},'%')
        </if>
        <if test="paymentStatus != null">
            and fp.payment_status = #{paymentStatus}
        </if>
        <if test="enterpriseType != null">
            and me.enterprise_type = #{enterpriseType}
        </if>
        order by fp.payment_status
    </select>
    <resultMap id="FinancePaymentVOMap" type="com.ytpm.middle.view.FinancePaymentVO">
        <id column="payment_id" property="paymentId" />
        <result column="payment_no" property="paymentNo" />
        <result column="total_amount" property="totalAmount" />
        <result column="payment_status" property="paymentStatus" />
        <result column="enterprise_name" property="enterpriseName" />
        <result column="credit_code" property="creditCode" />
        <result column="enterprise_type" property="enterpriseType" />
        <result column="has_fast" property="hasFast" />
        <result column="sharing_ratio" property="sharingRatio" />
        <result column="payment_date" property="paymentDate" />
        <collection property="itemList" ofType="com.ytpm.middle.view.FinancePaymentItemVO">
            <result column="item_id" property="itemId" />
            <result column="item_name" property="itemName" />
            <result column="item_amount" property="itemAmount" />
        </collection>
        <collection property="attachList" ofType="com.ytpm.middle.view.AttachVO">
            <result column="attach_id" property="attachId" />
            <result column="url" property="url" />
            <result column="size" property="size" />
            <result column="attach_name" property="attachName" />
        </collection>
    </resultMap>
    <select id="getPaymentDetail" resultMap="FinancePaymentVOMap">
        SELECT
            fp.payment_id,
            fp.payment_no,
            fp.total_amount,
            fp.payment_status,
            me.enterprise_name,
            me.credit_code,
            me.enterprise_type,
            me.has_fast,
            me.sharing_ratio,
            me.payment_date,
            fpr.item_id,
            (select item_name from yt_finance_payment_item where item_id = fpr.item_id) item_name,
            (select item_amount from yt_finance_payment_item where item_id = fpr.item_id) item_amount,
            pa.attach_id,
            pa.attach_name,
            pa.url,
            pa.size
        FROM
            yt_finance_payment fp
            JOIN yt_middle_enterprise me ON fp.user_id = me.user_id
            LEFT JOIN yt_finance_payment_record fpr ON fp.payment_id = fpr.payment_id
            LEFT JOIN yt_platform_attach pa on target_type = 2 and target_id = fp.payment_id
        WHERE
            fp.available = 1
        and fp.payment_id = #{paymentId}
        group by fp.payment_id,fpr.record_id,pa.attach_id
    </select>
    <select id="getPaymentByType" resultType="com.ytpm.middle.view.FinancePaymentVO">
        select
            payment_id, payment_no, payment_type, total_amount, payment_status, user_id, finish_time, create_time, create_user_id, update_time, update_user_id, available
        from yt_finance_payment
        where payment_type = 1 and user_id = #{userId}
    </select>
    <select id="findByItemIds" resultType="com.ytpm.middle.view.FinancePaymentItemVO">
        select
            item_id, item_name, item_amount, unit, count, create_time, create_user_id, update_time, update_user_id, available
        from yt_finance_payment_item
        where item_id in
        <foreach collection="itemIdList" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
</mapper>