<?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.lframework.xingyun.sc.mappers.ReceiveSheetMapper">

    <resultMap id="ReceiveSheet" type="com.lframework.xingyun.sc.entity.ReceiveSheet">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="sc_id" property="scId"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="purchaser_id" property="purchaserId"/>
        <result column="payment_date" property="paymentDate"/>
        <result column="receive_date" property="receiveDate"/>
        <result column="purchase_order_id" property="purchaseOrderId"/>
        <result column="total_num" property="totalNum"/>
        <result column="total_gift_num" property="totalGiftNum"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="approve_by" property="approveBy"/>
        <result column="approve_time" property="approveTime"/>
        <result column="status" property="status"/>
        <result column="settle_status" property="settleStatus"/>
    </resultMap>

    <resultMap id="ReceiveSheetFullDto" type="com.lframework.xingyun.sc.dto.purchase.receive.ReceiveSheetFullDto">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="sc_id" property="scId"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="purchaser_id" property="purchaserId"/>
        <result column="payment_date" property="paymentDate"/>
        <result column="receive_date" property="receiveDate"/>
        <result column="purchase_order_id" property="purchaseOrderId"/>
        <result column="total_num" property="totalNum"/>
        <result column="total_gift_num" property="totalGiftNum"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="approve_by" property="approveBy"/>
        <result column="approve_time" property="approveTime"/>
        <result column="status" property="status"/>
        <result column="refuse_reason" property="refuseReason"/>
        <result column="settle_status" property="settleStatus"/>
        <collection property="details" javaType="java.util.ArrayList" ofType="com.lframework.xingyun.sc.dto.purchase.receive.ReceiveSheetFullDto$OrderDetailDto">
            <id column="detail_id" property="id"/>
            <result column="detail_product_id" property="productId"/>
            <result column="detail_order_num" property="orderNum"/>
            <result column="detail_tax_price" property="taxPrice"/>
            <result column="detail_is_gift" property="isGift"/>
            <result column="detail_tax_rate" property="taxRate"/>
            <result column="detail_description" property="description"/>
            <result column="detail_order_no" property="orderNo"/>
            <result column="detail_purchase_order_detail_id" property="purchaseOrderDetailId"/>
        </collection>
    </resultMap>

    <resultMap id="ReceiveSheetWithReturnDto"
               type="com.lframework.xingyun.sc.dto.purchase.receive.ReceiveSheetWithReturnDto">
        <id column="id" property="id"/>
        <result column="sc_id" property="scId"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="purchaser_id" property="purchaserId"/>
        <collection property="details"
                    ofType="com.lframework.xingyun.sc.dto.purchase.receive.ReceiveSheetWithReturnDto$DetailDto"
                    javaType="java.util.ArrayList">
            <id column="detail_id" property="id"/>
            <result column="detail_product_id" property="productId"/>
            <result column="detail_order_num" property="orderNum"/>
            <result column="detail_tax_price" property="taxPrice"/>
            <result column="detail_is_gift" property="isGift"/>
            <result column="detail_tax_rate" property="taxRate"/>
            <result column="detail_description" property="description"/>
            <result column="detail_order_no" property="orderNo"/>
            <result column="detail_return_num" property="returnNum"/>
        </collection>
    </resultMap>

    <sql id="ReceiveSheetDto_sql">
        SELECT
            r.id,
            r.code,
            r.sc_id,
            r.supplier_id,
            r.purchaser_id,
            r.payment_date,
            r.receive_date,
            r.purchase_order_id,
            r.total_num,
            r.total_gift_num,
            r.total_amount,
            r.description,
            r.create_by,
            r.create_time,
            r.approve_by,
            r.approve_time,
            r.status,
            r.settle_status
        FROM tbl_receive_sheet AS r
        LEFT JOIN tbl_purchase_order AS o ON o.id = r.purchase_order_id
    </sql>

    <sql id="ReceiveSheetFullDto_sql">
        SELECT
            r.id,
            r.code,
            r.sc_id,
            r.supplier_id,
            r.purchaser_id,
            r.payment_date,
            r.receive_date,
            r.purchase_order_id,
            r.total_num,
            r.total_gift_num,
            r.total_amount,
            r.description,
            r.create_by,
            r.create_time,
            r.update_by,
            r.update_time,
            r.approve_by,
            r.approve_time,
            r.status,
            r.refuse_reason,
            r.settle_status,
            d.id AS detail_id,
            d.product_id AS detail_product_id,
            d.order_num AS detail_order_num,
            d.tax_price AS detail_tax_price,
            d.is_gift AS detail_is_gift,
            d.tax_rate AS detail_tax_rate,
            d.description AS detail_description,
            d.order_no AS detail_order_no,
            d.purchase_order_detail_id AS detail_purchase_order_detail_id
        FROM tbl_receive_sheet AS r
        LEFT JOIN tbl_receive_sheet_detail AS d ON d.sheet_id = r.id
    </sql>

    <sql id="ReceiveSheetWithReturnDto_sql">
        SELECT
            s.id,
            s.sc_id,
            s.supplier_id,
            s.purchaser_id,
            d.id AS detail_id,
            d.product_id AS detail_product_id,
            d.order_num AS detail_order_num,
            d.tax_price AS detail_tax_price,
            d.is_gift AS detail_is_gift,
            d.tax_rate AS detail_tax_rate,
            d.description AS detail_description,
            d.order_no AS detail_order_no,
            d.return_num AS detail_return_num
        FROM tbl_receive_sheet AS s
        LEFT JOIN tbl_receive_sheet_detail AS d ON d.sheet_id = s.id
    </sql>

    <select id="query" resultMap="ReceiveSheet">
        <include refid="ReceiveSheetDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND r.code = #{vo.code}
                </if>
                <if test="vo.supplierId != null and vo.supplierId != ''">
                    AND r.supplier_id = #{vo.supplierId}
                </if>
                <if test="vo.scId != null and vo.scId != ''">
                    AND r.sc_id = #{vo.scId}
                </if>
                <if test="vo.purchaserId != null and vo.purchaserId != ''">
                    AND r.purchaser_id = #{vo.purchaserId}
                </if>
                <if test="vo.status != null">
                    AND r.status = #{vo.status}
                </if>
                <if test="vo.createBy != null and vo.createBy != ''">
                    AND r.create_by_id = #{vo.createBy}
                </if>
                <if test="vo.approveBy != null and vo.approveBy != ''">
                    AND r.approve_by = #{vo.approveBy}
                </if>
                <if test="vo.createStartTime != null">
                    AND r.create_time >= #{vo.createStartTime}
                </if>
                <if test="vo.createEndTime != null">
                    <![CDATA[
                    AND r.create_time <= #{vo.createEndTime}
                    ]]>
                </if>
                <if test="vo.approveStartTime != null">
                    AND r.approve_time >= #{vo.approveStartTime}
                </if>
                <if test="vo.approveEndTime != null">
                    <![CDATA[
                    AND r.approve_time <= #{vo.approveEndTime}
                    ]]>
                </if>
                <if test="vo.purchaseOrderCode != null and vo.purchaseOrderCode != ''">
                    AND o.code = #{vo.purchaseOrderCode}
                </if>
                <if test="vo.settleStatus != null">
                    AND r.settle_status = #{vo.settleStatus}
                </if>
            </if>
        </where>
        ORDER BY r.create_time DESC
    </select>
    <select id="getDetail" resultMap="ReceiveSheetFullDto">
        <include refid="ReceiveSheetFullDto_sql"/>
        WHERE r.id = #{id}
        ORDER BY d.order_no
    </select>
    <select id="selector" resultMap="ReceiveSheet">
        <include refid="ReceiveSheetDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND r.code = #{vo.code}
                </if>
                <if test="vo.supplierId != null and vo.supplierId != ''">
                    AND r.supplier_id = #{vo.supplierId}
                </if>
                <if test="vo.scId != null and vo.scId != ''">
                    AND r.sc_id = #{vo.scId}
                </if>
                <if test="vo.status != null">
                    AND r.status = #{vo.status}
                </if>
                <if test="vo.createBy != null and vo.createBy != ''">
                    AND r.create_by_id = #{vo.createBy}
                </if>
                <if test="vo.createStartTime != null">
                    AND r.create_time >= #{vo.createStartTime}
                </if>
                <if test="vo.createEndTime != null">
                    <![CDATA[
                    AND r.create_time <= #{vo.createEndTime}
                    ]]>
                </if>
            </if>
        </where>
        ORDER BY r.create_time DESC
    </select>
    <select id="getWithReturn" resultMap="ReceiveSheetWithReturnDto">
        SELECT
        s.id, s.sc_id, s.supplier_id, s.purchaser_id, d.id AS detail_id, d.product_id AS detail_product_id,
        d.order_num AS detail_order_num, d.tax_price AS detail_tax_price, d.is_gift AS detail_is_gift,
        d.tax_rate AS detail_tax_rate, d.description AS detail_description, d.order_no AS detail_order_no,
        d.return_num AS detail_return_num
        FROM tbl_receive_sheet AS s
        LEFT JOIN tbl_receive_sheet_detail AS d ON d.sheet_id = s.id
        <if test="requireReceive">AND d.order_num > d.return_num</if>
        WHERE s.id = #{id}
        AND s.status = 3
        ORDER BY d.order_no
    </select>
    <select id="queryWithReturn" resultMap="ReceiveSheet">
        <include refid="ReceiveSheetDto_sql"/>
        <if test="!multipleRelate">LEFT JOIN tbl_purchase_return AS p ON p.receive_sheet_id = r.id</if>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND r.code = #{vo.code}
                </if>
                <if test="vo.supplierId != null and vo.supplierId != ''">
                    AND r.supplier_id = #{vo.supplierId}
                </if>
                <if test="vo.scId != null and vo.scId != ''">
                    AND r.sc_id = #{vo.scId}
                </if>
                AND r.status = 3
                <if test="vo.createBy != null and vo.createBy != ''">
                    AND r.create_by_id = #{vo.createBy}
                </if>
                <if test="vo.createStartTime != null">
                    AND r.create_time >= #{vo.createStartTime}
                </if>
                <if test="vo.createEndTime != null">
                    <![CDATA[
                    AND r.create_time <= #{vo.createEndTime}
                    ]]>
                </if>
            </if>
            <if test="!multipleRelate">AND p.id IS NULL</if>
        </where>
        <if test="!multipleRelate">GROUP BY r.id</if>
        ORDER BY r.create_time DESC
    </select>
    <select id="getApprovedList" resultMap="ReceiveSheet">
        <include refid="ReceiveSheetDto_sql"/>
        WHERE r.supplier_id = #{supplierId}
        <if test="startTime != null">
            AND r.approve_time >= #{startTime}
        </if>
        <if test="endTime != null">
            <![CDATA[
            AND r.approve_time <= #{endTime}
            ]]>
        </if>
        AND r.status = 3
        AND r.settle_status = #{settleStatus}
        AND r.tx_id IS NULL
        ORDER BY r.approve_time DESC
    </select>
</mapper>
