<?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.zfire.mall.manager.commonmapper.RequisitionMapper">

    <select id="list" resultType="com.zfire.mall.manager.bean.requisition.RequisitionBean">
        SELECT
       ro.*,
        roi.order_id,
        roi.sale_type_id,
        roi.sale_type_code,
        roi.sale_type_name,
        roi.requisition_order_id,
        roi.product_price_id,
        roi.material_id,
        roi.material_old_number,
        roi.material_code,
        roi.material_name,
        roi.service_id,
        roi.service_name,
        roi.policy_id,
        roi.policy_condition_id,
        roi.policy_material_id,
        roi.pop_type,
        roi.specification,
        roi.unit,
        roi.is_use_rebate,
        roi.rebate_rate,
        roi.customer_wallet_id,
        roi.customer_wallet_name,
        roi.customer_wallet_number,
        roi.customer_wallet_id2,
        roi.customer_wallet_name2,
        roi.is_direct_transfer,
        roi.direct_transfer_status,
        roi.direct_transfer_qty,
        roi.has_send_qty,
        roi.price,
        roi.qty,
        roi.total_amount,
        roi.rebate_amount,
        roi.disc_amount,
        roi.total_disc_amount,
        roi.single_rebate_amount,
        roi.single_pay_price,
        roi.pay_amount,
        roi.pay_rebate_amount,
        roi.pay_cash_amount,
        roi.pay_credit_amount,
        roi.old_qty,
        roi.old_pay_amount,
        roi.tax,
        roi.remark,
        roi.refundable_qty,
        roi.retired_qty,
        roi.refund_product_qty,
        roi.refund_product_amount,
        roi.create_time,
        roi.create_by,
        roi.king_dee_seq,
        roi.king_dee_entry_id
        FROM
            requisition_order ro
                LEFT JOIN requisition_order_item roi ON ro.id = roi.requisition_order_id
        join customer_product_type_rela cptr on roi.customer_wallet_id = cptr.customer_wallet_id
        where 1=1
        <if test="id != null and id != ''">
            and ro.id = #{id}
        </if>
        <if test="examineStatus != null and examineStatus != ''">
            and ro.examine_status = #{examineStatus}
        </if>
        <if test="customerName != null and customerName != ''">
            and ro.customer_name like concat('%',#{customerName},'%')
        </if>
        <if test="correspondName != null and correspondName != ''">
            and ro.correspond_name like concat('%',#{correspondName},'%')
        </if>
        <if test="specification != null and specification != ''">
            and roi.specification like concat('%',#{specification},'%')
        </if>
        <if test="customerNumber != null and customerNumber != ''">
            and ro.customer_number like concat('%',#{customerNumber},'%')
        </if>
        <if test="materialCode != null and materialCode != ''">
            and roi.material_code like concat('%',#{materialCode},'%')
        </if>
        <if test="materialName != null and materialName != ''">
            and roi.material_name like concat('%',#{materialName},'%')
        </if>
        <if test="startTime != null and startTime != ''">
            and ro.order_time between #{startTime} and #{endTime}
        </if>
        <if test="customerId != null and customerId != ''">
            and ro.customer_id = #{customerId}
        </if>
        <if test="serviceIds != null and serviceIds.size > 0">
            and cptr.service_id in
            <foreach item="item" index="index" collection="serviceIds" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        <if test="k3CategoryIds != null and k3CategoryIds.size > 0">
            and roi.k3_category_id in
            <foreach item="item" index="index" collection="k3CategoryIds" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
        </if>
        order by ro.create_time desc
    </select>



    <resultMap id="BaseResultMap" type="com.zfire.mall.manager.bean.requisition.RequisitionDetail">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="order_time" jdbcType="TIMESTAMP" property="orderTime"/>
        <result column="order_type" jdbcType="TINYINT" property="orderType"/>
        <result column="examine_status" jdbcType="VARCHAR" property="examineStatus"/>
        <result column="customer_number" jdbcType="VARCHAR" property="customerNumber"/>
        <result column="customer_id" jdbcType="VARCHAR" property="customerId"/>
        <result column="customer_name" jdbcType="VARCHAR" property="customerName"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="requsition_in" jdbcType="VARCHAR" property="requsitionIn"/>
        <result column="requsition_out" jdbcType="VARCHAR" property="requsitionOut"/>
        <result column="direction" jdbcType="VARCHAR" property="direction"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="approval_name" jdbcType="VARCHAR" property="approvalName"/>
        <result column="approval_time" jdbcType="TIMESTAMP" property="approvalTime"/>
        <result column="approval_remark" jdbcType="VARCHAR" property="approvalRemark"/>
        <result column="correspond_id" jdbcType="VARCHAR" property="correspondId"/>
        <result column="correspond_name" jdbcType="VARCHAR" property="correspondName"/>
        <result column="in_correspond_id" jdbcType="VARCHAR" property="inCorrespondId"/>
        <result column="in_correspond_name" jdbcType="VARCHAR" property="inCorrespondName"/>
        <result column="stock_name" jdbcType="VARCHAR" property="stockName"/>
        <result column="stock_id" jdbcType="VARCHAR" property="stockId"/>
        <result column="in_stock_name" jdbcType="VARCHAR" property="inStockName"/>
        <result column="in_stock_id" jdbcType="VARCHAR" property="inStockId"/>
        <result column="stock_status_type" jdbcType="VARCHAR" property="stockStatusType"/>
        <result column="in_stock_status_type" jdbcType="VARCHAR" property="inStockStatusType"/>
        <collection property="results" ofType="com.zfire.mall.manager.plus.entity.RequisitionOrderItem"
                    select="requisitionOrder" column="id">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="order_id" jdbcType="VARCHAR" property="orderId"/>
            <result column="sale_type_id" jdbcType="VARCHAR" property="saleTypeId"/>
            <result column="sale_type_code" jdbcType="VARCHAR" property="saleTypeCode"/>
            <result column="sale_type_name" jdbcType="VARCHAR" property="saleTypeName"/>
            <result column="material_id" jdbcType="VARCHAR" property="materialId"/>
            <result column="material_code" jdbcType="VARCHAR" property="materialCode"/>
            <result column="material_name" jdbcType="VARCHAR" property="materialName"/>
            <result column="specification" jdbcType="VARCHAR" property="specification"/>
            <result column="unit" jdbcType="VARCHAR" property="unit"/>
            <result column="has_send_qty" jdbcType="INTEGER" property="hasSendQty"/>
            <result column="price" jdbcType="DECIMAL" property="price"/>
            <result column="qty" jdbcType="INTEGER" property="qty"/>
            <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
            <result column="total_amount" jdbcType="DECIMAL" property="totalAmount"/>
            <result column="requisition_order_id" jdbcType="VARCHAR" property="requisitionOrderId"/>
        </collection>
    </resultMap>

    <select id="detail" resultMap="BaseResultMap">
        SELECT
        ro.*,
        (SELECT def_stock_status_name FROM king_dee_stock kds where ro.stock_id = kds.id) as stock_status_type,
        (SELECT def_stock_status_name FROM king_dee_stock kds where ro.in_stock_id = kds.id) as in_stock_status_type
        FROM
        requisition_order ro
        where 1=1
        <if test="id != null and id != ''">
            and ro.id = #{id}
        </if>
    </select>

    <select id="requisitionOrder" resultType="com.zfire.mall.manager.plus.entity.RequisitionOrderItem">
        SELECT
            *
        FROM
        requisition_order_item roi
        where roi.requisition_order_id=#{id}
    </select>
</mapper>