<?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.tmerclub.cloud.order.mapper.PurchaseOrderMapper">
  <resultMap id="purchaseOrderMap" type="com.tmerclub.cloud.order.model.PurchaseOrder">
    <id column="purchase_order_id" property="purchaseOrderId" />
    <result column="shop_id" property="shopId"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="deliver_time" property="deliverTime"/>
    <result column="delivery_type" property="deliveryType"/>
    <result column="supplier_id" property="supplierId"/>
    <result column="total_amount" property="totalAmount"/>
    <result column="total_stock" property="totalStock"/>
    <result column="actual_total_stock" property="actualTotalStock"/>
    <result column="status" property="status"/>
    <result column="remark" property="remark"/>
    <result column="employee_id" property="employeeId"/>
    <result column="purchase_order_addr_id" property="purchaseOrderAddrId"/>
    <result column="voucher_img_urls" property="voucherImgUrls"/>
    <result column="warehouse_id" property="warehouseId"/>
    <result column="supplier_warehouse_id" property="supplierWarehouseId"/>
    <result column="stock_point_type" property="stockPointType"/>
  </resultMap>

  <resultMap id="purchaseOrderAndProdMap" type="com.tmerclub.cloud.order.vo.PurchaseOrderVO" extends="purchaseOrderMap">
    <result column="consignee" property="consignee"/>
    <result column="mobile" property="mobile"/>
    <collection property="purchaseOrderItemList" ofType="com.tmerclub.cloud.order.vo.PurchaseOrderItemVO">
      <id column="purchase_item_id" property="purchaseItemId"/>
      <result column="spu_id" property="spuId"/>
      <result column="sku_id" property="skuId"/>
      <result column="purchase_amount" property="purchaseAmount"/>
      <result column="pic" property="pic"/>
      <result column="purchase_price" property="purchasePrice"/>
      <result column="purchase_stock" property="purchaseStock"/>
      <result column="be_delivered_num" property="beDeliveredNum"/>
      <result column="actual_stock" property="actualStock"/>
      <result column="status" property="status"/>
      <result column="delivery_type" property="deliveryType"/>
      <result column="spu_name" property="spuName"/>
      <result column="sku_name" property="skuName"/>
      <result column="party_code" property="partyCode"/>
<!--      <collection property="purchaseOrderItemLangList" ofType="com.tmerclub.cloud.order.model.PurchaseOrderItemLang">-->
<!--        <id column="lang_item_id" property="purchaseItemId"/>-->
<!--        <id column="lang" property="lang"/>-->
<!--      </collection>-->
    </collection>
  </resultMap>
  <resultMap id="unDeliveryOrderExcelMap" type="com.tmerclub.cloud.order.vo.UnDeliveryPurchaseOrderExcelVO">
      <result column="delivery_type" property="deliveryType"/>
      <result column="consignee" property="consignee"/>
      <result column="mobile" property="mobile"/>
      <result column="receiving_addr" property="receivingAddr"/>
      <result column="spu_name" property="spuName"/>
      <result column="sku_name" property="skuName"/>
      <result column="be_delivered_num" property="count"/>
<!--      <collection property="purchaseOrderItemLangList" ofType="com.tmerclub.cloud.order.model.PurchaseOrderItemLang">-->
<!--        <id column="lang_item_id" property="purchaseItemId"/>-->
<!--        <id column="lang" property="lang"/>-->
<!--      </collection>-->
  </resultMap>
    <resultMap id="purchaseOrderDetailExcelMap" type="com.tmerclub.cloud.order.vo.PurchaseOrderExcelVO">
        <result column="purchase_order_id" property="purchaseOrderId" />
        <result column="create_time" property="createTime"/>
        <result column="delivery_type" property="deliveryType"/>
        <result column="consignee" property="consignee"/>
        <result column="mobile" property="mobile"/>
        <result column="receiving_addr" property="receivingAddr"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="pay_type" property="payType"/>
        <result column="status" property="status"/>
        <result column="spu_name" property="spuName"/>
        <result column="sku_name" property="skuName"/>
        <result column="purchase_price" property="purchasePrice"/>
        <result column="purchase_stock" property="purchaseStock"/>
        <result column="purchase_Amount" property="purchaseAmount"/>
        <collection property="orderItemLangList" ofType="com.tmerclub.cloud.order.vo.PurchaseOrderItemLangVO">
            <id column="lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
            <result column="sku_name" property="skuName"/>
        </collection>
    </resultMap>
    <resultMap id="mongoPurchaseOrderMap" type="com.tmerclub.cloud.order.bo.mongo.MongoPurchaseOrderBO" extends="purchaseOrderMap">
        <result column="consignee" property="consignee"/>
        <result column="mobile" property="mobile"/>
        <collection property="purchaseOrderItems" columnPrefix="poi_" ofType="com.tmerclub.cloud.order.bo.mongo.MongoPurchaseOrderItemBO">
            <id column="purchase_item_id" property="purchaseItemId"/>
            <result column="spu_id" property="spuId"/>
            <result column="sku_id" property="skuId"/>
            <result column="purchase_amount" property="purchaseAmount"/>
            <result column="purchase_price" property="purchasePrice"/>
            <result column="purchase_stock" property="purchaseStock"/>
            <result column="actual_stock" property="actualStock"/>
            <result column="delivery_type" property="deliveryType"/>
            <result column="status" property="status"/>
            <result column="spu_code" property="spuCode"/>
            <result column="party_code" property="partyCode"/>
            <result column="pic" property="pic"/>
            <result column="be_delivered_num" property="beDeliveredNum"/>
            <collection property="purchaseOrderItemLangs" ofType="com.tmerclub.cloud.order.model.PurchaseOrderItemLang">
                <id column="lang" property="lang"/>
                <result column="spu_name" property="spuName"/>
                <result column="sku_name" property="skuName"/>
            </collection>
        </collection>
        <collection property="purchaseOrderLangs" ofType="com.tmerclub.cloud.order.model.PurchaseOrderLang">
            <result column="lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
        </collection>
    </resultMap>

  <sql id="Vo_Column_List">
    purchase_order_id,
    `shop_id`,
    `create_time`,
    `update_time`,
    `deliver_time`,
    `delivery_time`,
    `delivery_type`,
    `supplier_id`,
    `total_amount`,
    `total_stock`,
    `actual_total_stock`,
    `status`,
    `remark`,
    `employee_id`,
    `purchase_order_addr_id`,
    `voucher_img_urls`,
    `warehouse_id`,
    `supplier_warehouse_id`,
    `stock_point_type`
  </sql>

  <sql id="Join_Column_List">
    po.`purchase_order_id`,
    po.`shop_id`,
    po.`create_time`,
    po.`update_time`,
    po.`deliver_time`,
    po.`delivery_time`,
    po.`delivery_type`,
    po.`supplier_id`,
    po.`total_amount`,
    po.`total_stock`,
    po.`actual_total_stock`,
    po.`status`,
    po.`remark`,
    po.`employee_id`,
    po.`purchase_order_addr_id`,
    po.`voucher_img_urls`,
    po.`warehouse_id`,
    po.`supplier_warehouse_id`,
    po.`stock_point_type`
  </sql>
    <sql id="conditions">
        <where>
            <if test="purchaseOrder.shopId != null ">
                AND po.shop_id = #{purchaseOrder.shopId}
            </if>
            <if test="purchaseOrder.consignee != null and purchaseOrder.consignee != ''">
                AND oa.consignee LIKE CONCAT('%', #{purchaseOrder.consignee}, '%')
            </if>
            <if test="purchaseOrder.spuName != null and purchaseOrder.spuName != ''">
                AND ol.spu_name LIKE CONCAT('%', #{purchaseOrder.spuName}, '%')
            </if>
            <if test="purchaseOrder.supplierId != null ">
                AND po.supplier_id = #{purchaseOrder.supplierId}
            </if>
            <if test="purchaseOrder.status != null ">
                AND po.status = #{purchaseOrder.status}
            </if>
            <if test="supplierIds != null and supplierIds.size() > 0">
                AND po.supplier_id IN
                <foreach collection="supplierIds" item="supplierId" open="(" separator="," close=")">
                    #{supplierId}
                </foreach>
            </if>
            <if test="shopIds != null and shopIds.size() > 0">
                AND po.shop_id IN
                <foreach collection="shopIds" item="shopId" open="(" separator="," close=")">
                    #{shopId}
                </foreach>
            </if>
            <if test="purchaseOrder.startTime != null and purchaseOrder.endTime != null">
                AND po.create_time >= #{purchaseOrder.startTime} and po.create_time <![CDATA[ <= ]]> #{purchaseOrder.endTime}
            </if>
        </where>
    </sql>

    <select id="listByPurchaseOrderIds" resultType="com.tmerclub.cloud.order.vo.PurchaseOrderVO">
        select <include refid="Vo_Column_List"/>
        from purchase_order
        where
        <foreach collection="purchaseOrderIds" item="purchaseOrderId" open="(" close=")" separator="or">
            purchase_order_id = #{purchaseOrderId}
        </foreach>
    </select>

  <select id="listUnDeliveryPurchaseOrder" resultMap="unDeliveryOrderExcelMap">
    SELECT  po.purchase_order_id,po.delivery_type,oa.mobile,oa.consignee,
            CONCAT(oa.province, oa.city, oa.area, oa.addr) AS receiving_addr,
            oi.be_delivered_num,oil.spu_name,oil.sku_name
    FROM purchase_order po
    left join purchase_order_lang ol on ol.purchase_order_id = po.purchase_order_id and lang = 1
    left join purchase_order_addr oa on oa.purchase_order_addr_id = po.purchase_order_addr_id
    left join purchase_order_item oi on oi.purchase_order_id = po.purchase_order_id
    left join purchase_order_item_lang oil on oi.purchase_item_id = oil.purchase_item_id and oil.lang = 1
    <include refid="conditions"/>
    ORDER BY po.purchase_order_id DESC
  </select>

  <select id="listPurchaseOrderDetail" resultMap="purchaseOrderDetailExcelMap">
    SELECT  po.purchase_order_id,po.create_time,po.delivery_type,oa.mobile,oa.consignee,
            CONCAT(oa.province, oa.city, oa.area, oa.addr) AS receiving_addr,po.total_amount,
            po.status,oi.purchase_price,oi.purchase_stock,oi.purchase_amount,
            oil.lang,oil.spu_name,oil.sku_name
    FROM purchase_order po
    left join purchase_order_lang ol on ol.purchase_order_id = po.purchase_order_id and lang = 1
    left join purchase_order_addr oa on oa.purchase_order_addr_id = po.purchase_order_addr_id
    left join purchase_order_item oi on oi.purchase_order_id = po.purchase_order_id
    left join purchase_order_item_lang oil on oi.purchase_item_id = oil.purchase_item_id and oil.lang = 1
    <include refid="conditions"/>
    ORDER BY po.purchase_order_id DESC
  </select>
  <select id="getByPurchaseOrderId" resultMap="purchaseOrderMap">
    select <include refid="Vo_Column_List"/> from purchase_order where purchase_order_id = #{purchaseOrderId}
  </select>

  <insert id="save" useGeneratedKeys="true" keyProperty="purchaseOrderId">
    insert into purchase_order (
      `purchase_order_id`,`shop_id`,`deliver_time`,`delivery_time`,`delivery_type`,`supplier_id`,`total_amount`,`total_stock`,
      `actual_total_stock`,`status`,`remark`,`employee_id`,`purchase_order_addr_id`,`voucher_img_urls`, `warehouse_id`,
      `supplier_warehouse_id`, `stock_point_type`)
    values (
      #{purchaseOrder.purchaseOrderId},#{purchaseOrder.shopId},#{purchaseOrder.deliverTime},#{purchaseOrder.deliveryTime},
      #{purchaseOrder.deliveryType},#{purchaseOrder.supplierId},#{purchaseOrder.totalAmount},#{purchaseOrder.totalStock},
      #{purchaseOrder.actualTotalStock},#{purchaseOrder.status},#{purchaseOrder.remark},#{purchaseOrder.employeeId},
      #{purchaseOrder.purchaseOrderAddrId},#{purchaseOrder.voucherImgUrls}, #{purchaseOrder.warehouseId},
      #{purchaseOrder.supplierWarehouseId}, #{purchaseOrder.stockPointType});
  </insert>

  <update id="updateByPurchaseId">
    update purchase_order
    <set>
      <if test="purchaseOrder.shopId != null">
        `shop_id` = #{purchaseOrder.shopId},
      </if>
      <if test="purchaseOrder.deliverTime != null">
        `deliver_time` = #{purchaseOrder.deliverTime},
      </if>
        <if test="purchaseOrder.deliveryTime != null">
        `delivery_time` = #{purchaseOrder.deliveryTime},
      </if>
      <if test="purchaseOrder.deliveryType != null">
        `delivery_type` = #{purchaseOrder.deliveryType},
      </if>
      <if test="purchaseOrder.supplierId != null">
        `supplier_id` = #{purchaseOrder.supplierId},
      </if>
      <if test="purchaseOrder.totalAmount != null">
        `total_amount` = #{purchaseOrder.totalAmount},
      </if>
      <if test="purchaseOrder.totalStock != null">
        `total_stock` = #{purchaseOrder.totalStock},
      </if>
      <if test="purchaseOrder.actualTotalStock != null">
        `actual_total_stock` = #{purchaseOrder.actualTotalStock},
      </if>
      <if test="purchaseOrder.status != null">
        `status` = #{purchaseOrder.status},
      </if>
      <if test="purchaseOrder.remark != null">
        `remark` = #{purchaseOrder.remark},
      </if>
        <if test="purchaseOrder.purchaseOrderAddrId != null">
        `purchase_order_addr_id` = #{purchaseOrder.purchaseOrderAddrId},
      </if>
      <if test="purchaseOrder.employeeId != null">
        `employee_id` = #{purchaseOrder.employeeId},
      </if>
      <if test="purchaseOrder.voucherImgUrls != null">
        `voucher_img_urls` = #{purchaseOrder.voucherImgUrls},
      </if>
    </set>
    where purchase_order_id = #{purchaseOrder.purchaseOrderId}
  </update>

  <select id="info" resultMap="purchaseOrderAndProdMap">
    SELECT <include refid="Join_Column_List" />,
      poi.`purchase_item_id`,
      poi.`create_time` ,
      poi.`update_time`,
      poi.`spu_id`,
      poi.`sku_id`,
      poi.`spu_code`,
      poi.`party_code`,
      poi.`pic`,
      poi.`purchase_amount`,
      poi.`purchase_price`,
      poi.`purchase_stock`,
      poi.`actual_stock`,
      poi.`delivery_type`,
      poi.`status`,
      poi.`be_delivered_num`,
      poil.spu_name,
      poil.sku_name
    FROM purchase_order po
    LEFT JOIN purchase_order_item poi ON poi.purchase_order_id = po.purchase_order_id
    LEFT JOIN purchase_order_item_lang poil ON poil.purchase_item_id = poi.purchase_item_id
    <where>
        po.purchase_order_id = #{purchaseOrderId}
        AND poil.lang = #{lang}
        <if test="supplierId != null">
          AND po.supplier_id = #{supplierId}
        </if>
    </where>
  </select>

  <select id="getMongoPruchaseOrderInfo" resultMap="mongoPurchaseOrderMap">
    SELECT <include refid="Join_Column_List" />,
      poa.mobile,
      poa.consignee,
      poil.lang,
      poil.spu_name,
      poi.`purchase_item_id`         poi_purchase_item_id,
      poi.`spu_id`                   poi_spu_id,
      poi.`sku_id`                   poi_sku_id,
      poi.`spu_code`                 poi_spu_code,
      poi.`party_code`               poi_party_code,
      poi.`pic`                      poi_pic,
      poi.`purchase_amount`          poi_purchase_amount,
      poi.`purchase_price`           poi_purchase_price,
      poi.`purchase_stock`           poi_purchase_stock,
      poi.`actual_stock`             poi_actual_stock,
      poi.`delivery_type`            poi_delivery_type,
      poi.`status`                   poi_status,
      poi.`be_delivered_num`         poi_be_delivered_num,
      poil.lang                      poi_lang,
      poil.spu_name                  poi_spu_name,
      poil.sku_name                  poi_sku_name
    FROM purchase_order po
    LEFT JOIN `purchase_order_item` poi ON po.`purchase_order_id` = poi.`purchase_order_id`
    LEFT JOIN purchase_order_addr poa ON poa.purchase_order_addr_id = po.purchase_order_addr_id
    LEFT JOIN purchase_order_lang pol on pol.purchase_order_id = po.purchase_order_id
    LEFT JOIN purchase_order_item_lang poil on poil.purchase_item_id = poi.purchase_item_id
    WHERE po.purchase_order_id = #{purchaseOrderId}
  </select>
</mapper>
