OrderBonusJob.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Author: 芸众商城 www.yunzshop.com
  4. * Date: 2018/9/19
  5. * Time: 下午3:37
  6. */
  7. namespace app\Jobs;
  8. use app\backend\modules\charts\models\OrderIncomeCount;
  9. use app\common\events\order\CreatedOrderPluginBonusEvent;
  10. use app\common\models\Order;
  11. use app\common\models\order\OrderPluginBonus;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Queue\InteractsWithQueue;
  15. use Illuminate\Contracts\Queue\ShouldQueue;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Schema;
  18. class OrderBonusJob implements ShouldQueue
  19. {
  20. use InteractsWithQueue, Queueable, SerializesModels;
  21. protected $tableName;
  22. protected $code;
  23. protected $foreignKey;
  24. protected $localKey;
  25. protected $amountColumn;
  26. protected $orderModel;
  27. protected $totalDividend;
  28. protected $condition;
  29. public function __construct($tableName, $code, $foreignKey, $localKey, $amountColumn, $orderModel, $totalDividend = 0, $condition = null)
  30. {
  31. //跟订单使用同个队列运行,防止产生相同的orderIncome
  32. $queueCount = Order::queueCount();
  33. if ($queueCount) {
  34. $this->queue = 'order:' . ($orderModel->id % Order::queueCount());
  35. }
  36. $this->tableName = $tableName;
  37. $this->code = $code;
  38. $this->foreignKey = $foreignKey;
  39. $this->localKey = $localKey;
  40. $this->amountColumn = $amountColumn;
  41. $this->orderModel = Order::find($orderModel->id);
  42. $this->totalDividend = $totalDividend;
  43. $this->condition = $condition;
  44. }
  45. public function handle()
  46. {
  47. // 验证表是否存在
  48. $exists_table = Schema::hasTable($this->tableName);
  49. if (!$exists_table) {
  50. return;
  51. }
  52. $build = DB::table($this->tableName)
  53. ->select()
  54. ->where($this->foreignKey, (string)$this->orderModel[$this->localKey]);
  55. //分红条件
  56. if ($this->condition) {
  57. $build = $build->where($this->condition);
  58. }
  59. // 分红记录IDs
  60. $ids = $build->pluck('id');
  61. // 分红总和
  62. $sum = $build->sum($this->amountColumn);
  63. if ($sum == 0) {
  64. return;
  65. }
  66. $undividend = 0;
  67. if ($this->totalDividend) {
  68. $undividend = $this->totalDividend - $sum;
  69. }
  70. \Log::info($this->code . '分红插入表');
  71. // 存入订单插件分红记录表
  72. $model = OrderPluginBonus::addRow([
  73. 'order_id' => $this->orderModel->id,
  74. 'uniacid' => $this->orderModel->uniacid,
  75. 'table_name' => $this->tableName,
  76. 'ids' => $ids,
  77. 'code' => $this->code,
  78. 'amount' => $sum,
  79. 'undividend' => $undividend,
  80. 'status' => 0,
  81. 'price' => $this->orderModel->price,
  82. 'member_id' => $this->orderModel->uid,
  83. 'order_sn' => $this->orderModel->order_sn,
  84. ]);
  85. if ($model) {
  86. $this->addCount($sum, $undividend);
  87. }
  88. // 暂时不用, 门店利润 在 门店订单结算时重新计算, 各个插件产生分红的事件监听不同.
  89. // 如果后期插件统一事件产生分红,再启用此事件
  90. //event(new CreatedOrderPluginBonusEvent($model));
  91. }
  92. public function addCount($sum, $undividend)
  93. {
  94. // $count = 1;
  95. $field = str_replace('-', '_', $this->code);
  96. $order_income = OrderIncomeCount::where('order_id', $this->orderModel->id)->first();
  97. if (!$order_income) {
  98. \Log::debug('订单分红统计,缺少订单ID' . $this->orderModel->id . '的数据');
  99. $order_income = (new OrderCountContentJob($this->orderModel))->handle();
  100. }
  101. $order_income->$field = $sum;
  102. $order_income->undividend += $undividend;
  103. $order_income->save();
  104. }
  105. }