Skip to content

Instantly share code, notes, and snippets.

@TsaiKoga
Created October 17, 2017 04:45
Show Gist options
  • Save TsaiKoga/3e5f63d9ac61f2be4392cb81d02be4c3 to your computer and use it in GitHub Desktop.
Save TsaiKoga/3e5f63d9ac61f2be4392cb81d02be4c3 to your computer and use it in GitHub Desktop.
Ruby on Rails paginate search products datas
per_page = 100
page = 0
total_page = (Product.count.to_f / per_page).ceil
total_page.times do
products = Product.limit(per_page).offset(page * per_page)
bill_items = bill_inventory_valuation_details products # 符合条件的采购入库单数据,此方法的查找也必须对 products 进行批量查找
sell_items = sell_inventory_valuation_details products # 符合条件的销售出库数据
package_items = package_inventory_valuation_details products # 异常出库数据
adjust = adjust_inventory_valuation_details products # 调整仓库数据
products.each do |product|
datas = []
datas += bill_items[product.id].map{|item| {id: item.id, quantity: item.quantity, price: item.price}}
# 对查出的数据进行处理,构造最终结果
... ...
end
def bill_inventory_valuation_details products
bill_items = PurchaseOrderBillItem.joins(:purchase_order_bill => :purchase_order).
includes(:purchase_order_bill => :purchase_order).
where("purchase_order_bills.status = ?", PurchaseOrderBill::STATUS[:finished]).
where("purchase_order_bills.date BETWEEN ? AND ?", @start_date, @end_date).
where("purchase_order_bill_items.product_id IN (?)", products.pluck(:id)).
group_by(&:product_id)
end
def sell_inventory_valuation_details products
sell_items = OutStorageOrderItem.joins(:out_storage_order).
includes(:out_storage_order).
where("out_storage_orders.out_storage_order_type = ?", 3).
where("out_storage_orders.created_at BETWEEN ? AND ?", @start_date.beginning_of_day, @end_date.end_of_day).
where("out_storage_orders.package_id IS NOT NULL OR out_storage_orders.package_group_id IS NOT NULL").
where("out_storage_order_items.product_id IN (?)", products.pluck(:id)).
group_by(&:product_id)
end
def adjust_inventory_valuation_details products
adjust = AdjustInventoryRecord.where("the_date BETWEEN ? AND ?", @start_date, @end_date).where(product_id: products.pluck(:id)).group_by(&:product_id)
end
def package_inventory_valuation_details products
package_items = PackageItem.eager_load(:package).where(product_id: products.pluck(:id)).
where("packages.state = ?", Package::STATE_VALUES[:processed]).
where("packages.dropship = ?", false).
where("packages.shipped_at >= ? AND packages.shipped_at <= ?", @start_date, @end_date).group_by(&:product_id)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment