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

    <resultMap id="RetailOutSheet" type="com.lframework.xingyun.sc.entity.RetailOutSheet">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="sc_id" property="scId"/>
        <result column="member_id" property="memberId"/>
        <result column="saler_id" property="salerId"/>
        <result column="payment_date" property="paymentDate"/>
        <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="RetailOutSheetFullDto" type="com.lframework.xingyun.sc.dto.retail.out.RetailOutSheetFullDto">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="sc_id" property="scId"/>
        <result column="member_id" property="memberId"/>
        <result column="saler_id" property="salerId"/>
        <result column="payment_date" property="paymentDate"/>
        <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.retail.out.RetailOutSheetFullDto$SheetDetailDto">
            <id column="detail_id" property="id"/>
            <result column="detail_main_product_id" property="mainProductId"/>
            <result column="detail_product_id" property="productId"/>
            <result column="detail_order_num" property="orderNum"/>
            <result column="detail_ori_price" property="oriPrice"/>
            <result column="detail_tax_price" property="taxPrice"/>
            <result column="detail_discount_rate" property="discountRate"/>
            <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_settle_status" property="settleStatus"/>
        </collection>
    </resultMap>

    <resultMap id="RetailOutSheetWithReturnDto" type="com.lframework.xingyun.sc.dto.retail.out.RetailOutSheetWithReturnDto">
        <id column="id" property="id"/>
        <result column="sc_id" property="scId"/>
        <result column="member_id" property="memberId"/>
        <result column="saler_id" property="salerId"/>
        <collection property="details" javaType="java.util.ArrayList" ofType="com.lframework.xingyun.sc.dto.retail.out.RetailOutSheetWithReturnDto$SheetDetailDto">
            <id column="detail_id" property="id"/>
            <result column="detail_product_id" property="productId"/>
            <result column="detail_order_num" property="orderNum"/>
            <result column="detail_ori_price" property="oriPrice"/>
            <result column="detail_tax_price" property="taxPrice"/>
            <result column="detail_discount_rate" property="discountRate"/>
            <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>

    <resultMap id="RetailProductDto" type="com.lframework.xingyun.sc.dto.retail.RetailProductDto">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="name" property="name"/>
        <result column="category_id" property="categoryId"/>
        <result column="category_name" property="categoryName"/>
        <result column="brand_id" property="brandId"/>
        <result column="brand_name" property="brandName"/>
        <result column="sku_code" property="skuCode"/>
        <result column="external_code" property="externalCode"/>
        <result column="spec" property="spec"/>
        <result column="unit" property="unit"/>
        <result column="retail_price" property="retailPrice"/>
        <result column="sale_tax_rate" property="taxRate"/>
        <result column="available" property="available"/>
    </resultMap>

    <sql id="RetailOutSheetDto_sql">
        SELECT
            s.id,
            s.code,
            s.sc_id,
            s.member_id,
            s.saler_id,
            s.payment_date,
            s.total_num,
            s.total_gift_num,
            s.total_amount,
            s.description,
            s.create_by,
            s.create_time,
            s.approve_by,
            s.approve_time,
            s.status,
            s.settle_status
        FROM tbl_retail_out_sheet AS s
    </sql>

    <sql id="RetailOutSheetFullDto_sql">
        SELECT
            s.id,
            s.code,
            s.sc_id,
            s.member_id,
            s.saler_id,
            s.payment_date,
            s.total_num,
            s.total_gift_num,
            s.total_amount,
            s.description,
            s.create_by,
            s.create_time,
            s.update_by,
            s.update_time,
            s.approve_by,
            s.approve_time,
            s.status,
            s.refuse_reason,
            s.settle_status,
            d.id AS detail_id,
            b.main_product_id AS detail_main_product_id,
            d.product_id AS detail_product_id,
            d.order_num AS detail_order_num,
            d.ori_price AS detail_ori_price,
            d.tax_price AS detail_tax_price,
            d.discount_rate AS detail_discount_rate,
            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.settle_status AS detail_settle_status
        FROM tbl_retail_out_sheet AS s
        LEFT JOIN tbl_retail_out_sheet_detail AS d ON d.sheet_id = s.id
        LEFT JOIN tbl_retail_out_sheet_detail_bundle AS b ON b.sheet_id = s.id AND b.product_detail_id = d.id
    </sql>

    <sql id="RetailProductDto_sql">
        SELECT
            g.id,
            g.code,
            g.name,
            c.id AS category_id,
            c.name AS category_name,
            b.id AS brand_id,
            b.name AS brand_name,
            g.sku_code,
            g.external_code,
            g.spec,
            g.unit,
            retail.price AS retail_price,
            g.sale_tax_rate,
            g.available
        FROM base_data_product AS g
        INNER JOIN base_data_product_retail AS retail ON retail.id = g.id
        LEFT JOIN base_data_product_category AS c ON c.id = g.category_id
        LEFT JOIN base_data_product_brand AS b ON b.id = g.brand_id
        LEFT JOIN recursion_mapping AS rm ON rm.node_id = c.id and rm.node_type = 2
    </sql>

    <select id="query" resultMap="RetailOutSheet">
        <include refid="RetailOutSheetDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND s.code = #{vo.code}
                </if>
                <if test="vo.memberId != null and vo.memberId != ''">
                    AND s.member_id = #{vo.memberId}
                </if>
                <if test="vo.scId != null and vo.scId != ''">
                    AND s.sc_id = #{vo.scId}
                </if>
                <if test="vo.salerId != null and vo.salerId != ''">
                    AND s.saler_id = #{vo.salerId}
                </if>
                <if test="vo.status != null">
                    AND s.status = #{vo.status}
                </if>
                <if test="vo.createBy != null and vo.createBy != ''">
                    AND s.create_by_id = #{vo.createBy}
                </if>
                <if test="vo.approveBy != null and vo.approveBy != ''">
                    AND s.approve_by = #{vo.approveBy}
                </if>
                <if test="vo.createStartTime != null">
                    AND s.create_time >= #{vo.createStartTime}
                </if>
                <if test="vo.createEndTime != null">
                    <![CDATA[
                    AND s.create_time <= #{vo.createEndTime}
                    ]]>
                </if>
                <if test="vo.approveStartTime != null">
                    AND s.approve_time >= #{vo.approveStartTime}
                </if>
                <if test="vo.approveEndTime != null">
                    <![CDATA[
                    AND s.approve_time <= #{vo.approveEndTime}
                    ]]>
                </if>
                <if test="vo.settleStatus != null">
                    AND s.settle_status = #{vo.settleStatus}
                </if>
            </if>
        </where>
        ORDER BY s.create_time DESC
    </select>
    <select id="selector" resultMap="RetailOutSheet">
        <include refid="RetailOutSheetDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND s.code = #{vo.code}
                </if>
                <if test="vo.memberId != null and vo.memberId != ''">
                    AND s.member_id = #{vo.memberId}
                </if>
                <if test="vo.scId != null and vo.scId != ''">
                    AND s.sc_id = #{vo.scId}
                </if>
                <if test="vo.status != null">
                    AND s.status = #{vo.status}
                </if>
                <if test="vo.createBy != null and vo.createBy != ''">
                    AND s.create_by_id = #{vo.createBy}
                </if>
                <if test="vo.createStartTime != null">
                    AND s.create_time >= #{vo.createStartTime}
                </if>
                <if test="vo.createEndTime != null">
                    <![CDATA[
                    AND s.create_time <= #{vo.createEndTime}
                    ]]>
                </if>
            </if>
        </where>
        ORDER BY s.create_time DESC
    </select>
    <select id="getDetail" resultMap="RetailOutSheetFullDto">
        <include refid="RetailOutSheetFullDto_sql"/>
        WHERE s.id = #{id}
    </select>
    <select id="getWithReturn" resultMap="RetailOutSheetWithReturnDto">
        SELECT
        s.id, s.code, s.sc_id, s.member_id, s.saler_id, d.id AS detail_id, d.sheet_id AS detail_sheet_id,
        d.product_id AS detail_product_id, d.order_num AS detail_order_num, d.ori_price AS detail_ori_price,
        d.tax_price AS detail_tax_price, d.discount_rate AS detail_discount_rate, 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_retail_out_sheet AS s
        LEFT JOIN (
        SELECT
        l.id, d.sheet_id, d.product_id, l.order_num, d.ori_price, d.tax_price, d.discount_rate, d.is_gift,
        d.tax_rate, d.description, d.order_no, l.return_num, l.order_no AS lot_order_no
        FROM tbl_retail_out_sheet_detail AS d
        INNER JOIN tbl_retail_out_sheet_detail_lot AS l ON l.detail_id = d.id
        <if test="requireOut">AND l.order_num > l.return_num</if>
        WHERE d.sheet_id = #{id}
        ) AS d ON d.sheet_id = s.id
        WHERE s.id = #{id}
        AND s.status = 3
        order by d.order_no, d.lot_order_no
    </select>
    <select id="queryWithReturn" resultMap="RetailOutSheet">
        <include refid="RetailOutSheetDto_sql"/>
        <if test="!multipleRelate">LEFT JOIN tbl_retail_return AS p ON p.out_sheet_id = s.id</if>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND s.code = #{vo.code}
                </if>
                <if test="vo.memberId != null and vo.memberId != ''">
                    AND s.member_id = #{vo.memberId}
                </if>
                <if test="vo.scId != null and vo.scId != ''">
                    AND s.sc_id = #{vo.scId}
                </if>
                AND s.status = 3
                <if test="vo.createBy != null and vo.createBy != ''">
                    AND s.create_by_id = #{vo.createBy}
                </if>
                <if test="vo.createStartTime != null">
                    AND s.create_time >= #{vo.createStartTime}
                </if>
                <if test="vo.createEndTime != null">
                    <![CDATA[
                    AND s.create_time <= #{vo.createEndTime}
                    ]]>
                </if>
            </if>
            <if test="!multipleRelate">AND p.id IS NULL</if>
        </where>
        <if test="!multipleRelate">GROUP BY s.id</if>
        ORDER BY s.create_time DESC
    </select>

    <select id="queryRetailByCondition" resultMap="RetailProductDto">
        <include refid="RetailProductDto_sql"/>
        <where>
            AND (
            g.code LIKE CONCAT('%', #{condition}, '%')
            OR g.name LIKE CONCAT('%', #{condition}, '%')
            OR g.sku_code LIKE CONCAT('%', #{condition}, '%')
            OR g.external_code LIKE CONCAT('%', #{condition}, '%')
            )
            <if test="isReturn != null and isReturn">
                AND g.product_type = 1
            </if>
            AND g.available = TRUE
        </where>
        ORDER BY g.code
    </select>
    <select id="queryRetailList" resultMap="RetailProductDto">
        <include refid="RetailProductDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.condition != null and vo.condition != ''">
                    AND (
                    g.code LIKE CONCAT('%', #{vo.condition}, '%')
                    OR g.name LIKE CONCAT('%', #{vo.condition}, '%')
                    OR g.sku_code LIKE CONCAT('%', #{vo.condition}, '%')
                    OR g.external_code LIKE CONCAT('%', #{vo.condition}, '%')
                    )
                </if>
                <if test="vo.brandId != null and vo.brandId != ''">
                    AND b.id = #{vo.brandId}
                </if>
                <if test="vo.categoryId != null and vo.categoryId != ''">
                    AND (c.id = #{vo.categoryId} OR FIND_IN_SET(#{vo.categoryId}, rm.path))
                </if>
                <if test="vo.isReturn != null and vo.isReturn">
                    AND g.product_type = 1
                </if>
            </if>
            AND g.available = TRUE
        </where>
        ORDER BY g.code
    </select>
    <select id="getRetailById" resultMap="RetailProductDto">
        <include refid="RetailProductDto_sql"/>
        WHERE g.id = #{id}
    </select>
</mapper>
