Skip to content

Commit

Permalink
compute order sales count by script
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyecommerce committed Jan 8, 2019
1 parent 1b2374b commit 67a7b1f
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
116 changes: 116 additions & 0 deletions app/console/modules/Order/controllers/ItemController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/

namespace fecshop\app\console\modules\Order\controllers;

use Yii;
use yii\console\Controller;

/**
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class ItemController extends Controller
{
public $pageNum = 10;

public function actionCount()
{
$data = $this->getOrderData();
if (isset($data['count']) && $data['count']) {
echo $data['count'];
}

return;
}

public function actionPagenum()
{
$data = $this->getOrderData();
if (isset($data['count']) && $data['count']) {
echo ceil($data['count'] / $this->pageNum);
}

return;
}

public function actionComputesellercount($pageNum)
{
Yii::$service->product->updateAllScoreToZero();
$orderData = $this->getOrderData($pageNum);
$order_ids = [];
if (isset($orderData['coll']) && is_array($orderData['coll'])) {
foreach ($orderData['coll'] as $one) {
$order_ids[] = $one['order_id'];
}
}
if (empty($order_ids)) {
return false;
}
$productSaleArr = [];
$itemData = $this->getOrderItemsData($order_ids);
if (isset($itemData['coll']) && is_array($itemData['coll']) ) {
foreach ($itemData['coll'] as $item) {
$sku = $item['sku'];
$qty = $item['qty'];

if (isset($productSaleArr[$sku])) {
$productSaleArr[$sku] += (int)$qty;
} else {
$productSaleArr[$sku] = (int)$qty;
}
}
}
echo "Print Sku And Sales Qty List: \n";
var_dump($productSaleArr);
foreach ($productSaleArr as $sku => $qty) {
$product = Yii::$service->product->getBySku((string)$sku, false);
if ($product['sku']) {
$product['score'] += (int)$qty;
$product->save();
//echo $sku."=>".$qty."\n";
}
}

return ture;
}


public function getOrderItemsData($order_ids){
if (!is_array($order_ids) || empty($order_ids)) {
return;
}
$filter = [
'where' => [
['in', 'order_id', $order_ids]
],
];

return Yii::$service->order->item->coll($filter);
}

public function getOrderData($pageNum = 0){
$paymentOrderStatus = Yii::$service->order->getOrderPaymentedStatusArr();
$orderProductSaleInMonths = Yii::$service->order->orderProductSaleInMonths;
$beginDate = strtotime('-'.$orderProductSaleInMonths.' months');
$filter = [
'where' => [
['order_status' => $paymentOrderStatus],
['>', 'created_at', $beginDate]
],
];
if ($pageNum) {
$filter['numPerPage'] = $this->pageNum;
$filter['pageNum'] = $pageNum;
}

return Yii::$service->order->coll($filter);
}

}
5 changes: 5 additions & 0 deletions services/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Order extends Service
// 订单号格式。
public $increment_id = 1000000000;

// 计算销量的订单时间范围(将最近几个月内的订单中的产品销售个数累加,作为产品的销量值,譬如3代表计算最近3个月的订单产品)
// 0:代表计算订单表中所有的订单。
// 这个值用于console入口(脚本端),通过shell脚本执行,计算产品的销量,将订单中产品个数累加作为产品的销量,然后将这个值更新到产品表字段中,用于产品按照销量排序或者过滤
public $orderProductSaleInMonths = 3;

// 将xx分钟内未支付的pending订单取消掉,并释放产品库存的设置
public $minuteBeforeThatReturnPendingStock = 60;

Expand Down
5 changes: 5 additions & 0 deletions services/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,9 @@ protected function actionUpdateProductReviewInfo($spu, $avag_rate, $count, $lang
{
return $this->_product->updateProductReviewInfo($spu, $avag_rate, $count, $lang_code, $avag_lang_rate, $lang_count, $rate_total_arr, $rate_lang_total_arr);
}

public function updateAllScoreToZero()
{
return $this->_product->updateAllScoreToZero();
}
}
4 changes: 4 additions & 0 deletions services/product/ProductMongodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,8 @@ public function updateProductReviewInfo($spu, $avag_rate, $count, $lang_code, $a
}
}
}

public function updateAllScoreToZero(){
return $this->_productModel->getCollection()->update([], ['score' => 0]);
}
}
16 changes: 16 additions & 0 deletions shell/order/productSellerCount.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
Cur_Dir=$(cd `dirname $0`; pwd)
# get product all count.
count=`$Cur_Dir/../../../../../yii order/item/count`
pagenum=`$Cur_Dir/../../../../../yii order/item/pagenum`

echo "There are $count order products to process"
echo "There are $pagenum pages to process"
echo "##############ALL BEGINING###############";
for (( i=1; i<=$pagenum; i++ ))
do
$Cur_Dir/../../../../../yii order/item/computesellercount $i
echo "Page $i done"
done

echo "##############ALL COMPLETE###############";

0 comments on commit 67a7b1f

Please sign in to comment.