<?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.displace.order.DisplaceOrderReserveMapper">

    <resultMap id="listResultMap" type="com.zfire.mall.manager.bean.displace.order.DisplaceOrderBean">
        <result column="displace_order_id" jdbcType="VARCHAR" property="displaceOrderId" />
        <result column="admin_company_id" jdbcType="VARCHAR" property="adminCompanyId" />
        <result column="customer_id" jdbcType="VARCHAR" property="customerId" />
        <result column="customer_number" jdbcType="VARCHAR" property="customerNumber" />
        <result column="customer_name" jdbcType="VARCHAR" property="customerName" />
        <result column="order_type" jdbcType="VARCHAR" property="orderType" />
        <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
        <result column="order_date" jdbcType="VARCHAR" property="orderDate" />
        <result column="main_id" jdbcType="VARCHAR" property="mainId" />
        <result column="main_name" jdbcType="VARCHAR" property="mainName" />
        <result column="engin_record_no" jdbcType="VARCHAR" property="enginRecordNo" />
        <result column="project_no" jdbcType="VARCHAR" property="projectNo" />
        <result column="project_name" jdbcType="VARCHAR" property="projectName" />
        <result column="use_unit" jdbcType="VARCHAR" property="useUnit" />
        <result column="install_address" jdbcType="VARCHAR" property="installAddress" />
        <result column="policy_id" jdbcType="VARCHAR" property="policyId" />
        <result column="policy_code" jdbcType="VARCHAR" property="policyCode" />
        <result column="policy_title" jdbcType="VARCHAR" property="policyTitle" />
        <result column="policy_remark" jdbcType="VARCHAR" property="policyRemark" />
        <result column="service_id" jdbcType="VARCHAR" property="serviceId" />
        <result column="service_name" jdbcType="VARCHAR" property="serviceName" />
        <result column="take_deposit" jdbcType="BOOLEAN" property="takeDeposit" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="examine_note" jdbcType="VARCHAR" property="examineNote" />
        <result column="examine_status" jdbcType="VARCHAR" property="examineStatus" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="create_name" jdbcType="VARCHAR" property="createName" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
        <result column="update_name" jdbcType="VARCHAR" property="updateName" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="submit_by" jdbcType="VARCHAR" property="submitBy" />
        <result column="submit_name" jdbcType="VARCHAR" property="submitName" />
        <result column="submit_time" jdbcType="TIMESTAMP" property="submitTime" />
        <result column="confirm_by" jdbcType="VARCHAR" property="confirmBy" />
        <result column="confirm_name" jdbcType="VARCHAR" property="confirmName" />
        <result column="confirm_time" jdbcType="TIMESTAMP" property="confirmTime" />
        <collection property="items" resultMap="itemResultList" />
    </resultMap>

    <resultMap id="itemResultList" type="com.zfire.mall.manager.plus.entity.DisplaceOrderItem">
        <result column="itemId" jdbcType="VARCHAR" property="id" />
        <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
        <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_number" jdbcType="VARCHAR" property="materialNumber" />
        <result column="material_name" jdbcType="VARCHAR" property="materialName" />
        <result column="material_old_number" jdbcType="VARCHAR" property="materialOldNumber" />
        <result column="specification" jdbcType="VARCHAR" property="specification" />
        <result column="unit" jdbcType="VARCHAR" property="unit" />
        <result column="price" jdbcType="DECIMAL" property="price" />
        <result column="qty" jdbcType="INTEGER" property="qty" />
        <result column="disc_amount" jdbcType="DECIMAL" property="discAmount" />
        <result column="total_disc_amount" jdbcType="DECIMAL" property="totalDiscAmount" />
        <result column="total_amount" jdbcType="DECIMAL" property="totalAmount" />
        <result column="rebate_rate" jdbcType="DECIMAL" property="rebateRate" />
        <result column="rebate_amount" jdbcType="DECIMAL" property="rebateAmount" />
        <result column="deposit_rate" jdbcType="DECIMAL" property="depositRate" />
        <result column="customer_wallet_id" jdbcType="VARCHAR" property="customerWalletId" />
        <result column="customer_wallet_name" jdbcType="VARCHAR" property="customerWalletName" />
        <result column="customer_wallet_id2" jdbcType="VARCHAR" property="customerWalletId2" />
        <result column="customer_wallet_name2" jdbcType="VARCHAR" property="customerWalletName2" />
        <result column="single_rebate_amount" jdbcType="DECIMAL" property="singleRebateAmount" />
        <result column="single_pay_price" jdbcType="DECIMAL" property="singlePayPrice" />
        <result column="single_deposit_amount" jdbcType="DECIMAL" property="singleDepositAmount" />
        <result column="pay_amount" jdbcType="DECIMAL" property="payAmount" />
        <result column="pay_rebate_amount" jdbcType="DECIMAL" property="payRebateAmount" />
        <result column="total_deposit_amount" jdbcType="DECIMAL" property="totalDepositAmount" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="tax" jdbcType="DECIMAL" property="tax" />
        <result column="has_send_qty" jdbcType="INTEGER" property="hasSendQty" />
        <result column="refundable_qty" jdbcType="INTEGER" property="refundableQty" />
        <result column="retired_qty" jdbcType="INTEGER" property="retiredQty" />
    </resultMap>

    <select id="listPage" resultType="com.zfire.mall.manager.bean.displace.order.DisplaceOrderListBean">
        SELECT
            a.*,
            b.sale_type_id,
            b.sale_type_code,
            b.sale_type_name,
            b.material_id,
            b.material_name,
            b.material_number,
            b.material_old_number,
            b.specification,
            b.unit,
            b.price,
            b.qty,
            b.disc_amount,
            b.total_disc_amount,
            b.total_amount,
            b.rebate_rate,
            b.rebate_amount,
            b.deposit_rate,
            b.customer_wallet_id,
            b.customer_wallet_name,
            b.customer_wallet_id2,
            b.customer_wallet_name2,
            b.single_rebate_amount,
            b.single_pay_price,
            b.single_deposit_amount,
            b.pay_amount,
            b.pay_rebate_amount,
            b.total_deposit_amount,
            b.tax,
            b.remark,
            b.has_send_qty,
            b.refundable_qty,
            b.retired_qty
        FROM
            displace_order a JOIN displace_order_item b ON a.displace_order_id = b.parent_id
        <where>
            <if test="customerId != null and customerId != ''">
                AND a.customer_id = #{customerId}
            </if>
            <if test="displaceOrderId != null and displaceOrderId != ''">
                AND a.displace_order_id = #{displaceOrderId}
            </if>
            <if test="originOrderNo != null and originOrderNo != ''">
                AND a.order_no = #{originOrderNo}
            </if>
            <if test="customerKeyword != null and customerKeyword != ''">
                AND (a.customer_name LIKE CONCAT('%', #{customerKeyword}, '%')
                OR a.customer_number LIKE CONCAT('%', #{customerKeyword}, '%')
                )
            </if>
            <if test="productName != null and productName != ''">
                AND b.material_name = #{productName}
            </if>
            <if test="orderType != null and orderType != ''">
                AND a.order_type = #{orderType}
            </if>
            <if test="startOrderTime != null and startOrderTime!=''">
                AND a.order_date <![CDATA[ >= ]]> #{startOrderTime}
            </if>
            <if test="endOrderTime != null and endOrderTime!=''">
                AND a.order_date <![CDATA[ <= ]]> #{endOrderTime}
            </if>
            <if test="createName != null and createName != ''">
                AND a.create_name LIKE CONCAT('%', #{createName},'%')
            </if>
            <if test="confirmName != null and confirmName != ''">
                AND a.confirm_name LIKE CONCAT('%', #{confirmName},'%')
            </if>
            <if test="confirmName != null and confirmName != ''">
                AND a.confirm_name LIKE CONCAT('%', #{confirmName},'%')
            </if>
            <if test="examineStatus != null and examineStatus != ''">
                AND a.examine_status = #{examineStatus}
            </if>
            <if test="serviceId != null and serviceId != ''">
                AND a.service_id = #{serviceId}
            </if>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                AND a.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="serviceIds != null and serviceIds.size > 0">
                and b.service_id in
                <foreach item="item" index="index" collection="serviceIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            AND a.del = 0
        </where>
        ORDER BY a.create_time DESC, a.displace_order_id DESC
    </select>
    <select id="detail" resultMap="listResultMap">
        SELECT
        a.*,
        b.sale_type_id,
        b.sale_type_code,
        b.sale_type_name,
        b.material_id,
        b.material_name,
        b.material_number,
        b.material_old_number,
        b.specification,
        b.unit,
        b.price,
        b.qty,
        b.disc_amount,
        b.total_disc_amount,
        b.total_amount,
        b.rebate_rate,
        b.rebate_amount,
        b.deposit_rate,
        b.customer_wallet_id,
        b.customer_wallet_name,
        b.customer_wallet_id2,
        b.customer_wallet_name2,
        b.single_rebate_amount,
        b.single_pay_price,
        b.single_deposit_amount,
        b.pay_amount,
        b.pay_rebate_amount,
        b.total_deposit_amount,
        b.tax,
        b.remark,
        b.has_send_qty,
        b.refundable_qty,
        b.retired_qty
        FROM
        displace_order a JOIN displace_order_item b ON a.displace_order_id = b.parent_id
        <where>
            a.displace_order_id = #{id}
            <if test="customerId != null and customerId != ''">
                AND a.customer_id = #{customerId}
            </if>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                AND a.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="serviceIds != null and serviceIds.size > 0">
                and b.service_id in
                <foreach item="item" index="index" collection="serviceIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            AND a.del = 0
        </where>
    </select>

</mapper>