StatKhCgController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\stat\classes\StatKdClass;
  5. use biz\stat\classes\StatRefundClass;
  6. use bizGhs\order\classes\OrderClass;
  7. use bizGhs\order\classes\OrderItemClass;
  8. use bizGhs\order\classes\RefundOrderClass;
  9. use bizGhs\order\classes\RefundOrderItemClass;
  10. use bizGhs\order\models\Order;
  11. use bizGhs\order\models\RefundOrder;
  12. use yii\console\Controller;
  13. use Yii;
  14. class StatKhCgController extends Controller
  15. {
  16. // ./yii stat-kh-cg/bc
  17. public function actionBc()
  18. {
  19. $query = new \yii\db\Query();
  20. $query->from(Order::tableName());
  21. foreach ($query->batch() as $batch) {
  22. foreach ($batch as $order) {
  23. $id = $order['id'] ?? 0;
  24. $orderSn = $order['orderSn'] ?? '';
  25. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  26. if (empty($itemList)) {
  27. continue;
  28. }
  29. $totalNum = 0;
  30. foreach ($itemList as $key => $item) {
  31. $num = $item['num'] ?? 0;
  32. $totalNum = bcadd($totalNum, $num, 2);
  33. }
  34. OrderClass::updateById($id, ['itemNum' => $totalNum]);
  35. }
  36. }
  37. $query = new \yii\db\Query();
  38. $query->from(RefundOrder::tableName());
  39. foreach ($query->batch() as $batch) {
  40. foreach ($batch as $refund) {
  41. $id = $refund['id'] ?? 0;
  42. $orderSn = $refund['orderSn'] ?? '';
  43. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  44. $current = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  45. $customId = $current->customId ?? 0;
  46. $itemList = RefundOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  47. if (empty($itemList)) {
  48. continue;
  49. }
  50. $totalNum = 0;
  51. foreach ($itemList as $item) {
  52. $num = $item['itemNum'] ?? 0;
  53. $totalNum = bcadd($totalNum, $num, 2);
  54. }
  55. RefundOrderClass::updateById($id, ['itemNum' => $totalNum, 'customId' => $customId]);
  56. }
  57. }
  58. }
  59. // ./yii stat-kd/update
  60. public function actionUpdate()
  61. {
  62. ini_set('memory_limit', '2045M');
  63. set_time_limit(0);
  64. $sql = 'TRUNCATE `xhStatKd`;TRUNCATE `xhStatKdMonth`;';
  65. Yii::$app->db->createCommand($sql)->execute();
  66. $query = new \yii\db\Query();
  67. $query->from(Order::tableName());
  68. foreach ($query->batch() as $batch) {
  69. foreach ($batch as $order) {
  70. $status = $order['status'] ?? 0;
  71. if ($status == 1 || $status == 5) {
  72. continue;
  73. }
  74. $payWay = $order['payWay'] ?? 0;
  75. $amount = $order['actPrice'] ?? 0;
  76. $refundPrice = $order['refundPrice'] ?? 0;
  77. if ($refundPrice > 0) {
  78. $amount = bcadd($amount, $refundPrice, 2);
  79. }
  80. $shopId = $order['shopId'] ?? 0;
  81. $shop = ShopClass::getLockById($shopId);
  82. if (empty($shop)) {
  83. continue;
  84. }
  85. $date = date('Ymd', strtotime($order['addTime']));
  86. StatKdClass::replace($shop, $amount, $payWay, $date);
  87. }
  88. }
  89. $sql = 'TRUNCATE `xhStatRefund`;TRUNCATE `xhStatRefundMonth`;';
  90. Yii::$app->db->createCommand($sql)->execute();
  91. $query = new \yii\db\Query();
  92. $query->from(RefundOrder::tableName());
  93. foreach ($query->batch() as $batch) {
  94. foreach ($batch as $refund) {
  95. $relateOrderSn = $refund['relateOrderSn'] ?? '';
  96. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  97. $payWay = $order->payWay ?? 0;
  98. $shopId = $refund['shopId'] ?? 0;
  99. $shop = ShopClass::getLockById($shopId);
  100. $amount = $refund['refundPrice'] ?? 0;
  101. $date = date('Ymd', strtotime($refund['addTime']));
  102. StatRefundClass::replace($shop, $payWay, $amount, $date);
  103. }
  104. }
  105. }
  106. }