Browse Source

增加订单导入功能

abiao 4 years ago
parent
commit
563a7a05e4
1 changed files with 23 additions and 0 deletions
  1. 23 0
      app/models/order_detail.rb

+ 23 - 0
app/models/order_detail.rb

@@ -7,6 +7,29 @@ class OrderDetail < ActiveRecord::Base
   # has_one :order, :foreign_key=>:order_id, :primary_key=>:order_id
   validates :order_no,:product_id,:nums, presence: true
   validate :order_validation
+  after_create :import_orders
+
+  def import_orders
+    # 获取商品信息
+    pd = Product.where("id = ?", self.product_id).first
+    detail = OrderDetail.where("id = ?", self.id).first
+    od = Order.where("order_id = ?", self.order_no).first
+
+    if !pd.blank? && !od.blank?
+      detail.order_id=od.id
+      detail.product_name=pd.name
+      detail.relate_product_id=pd.relate_product_id
+      detail.pv=pd.pv
+      detail.price=pd.price
+      detail.order_status=order.status
+      detail.depart=order.depart
+      od.pv=od.pv+detail.nums*detail.pv
+    end
+
+    # 更新order信息
+    od.save
+    detail.save
+  end
   def order_validation
     if self.product_id > 0
       prd = Product.where("id = ?", self.product_id).first