OrderBonusUpdateJob.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\common\events\order\CreatedOrderPluginBonusEvent;
  9. use app\common\models\order\OrderPluginBonus;
  10. use Illuminate\Bus\Queueable;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Schema;
  16. class OrderBonusUpdateJob implements ShouldQueue
  17. {
  18. use InteractsWithQueue, Queueable, SerializesModels;
  19. protected $tableName;
  20. protected $code;
  21. protected $foreignKey;
  22. // protected $localKey;
  23. protected $amountColumn;
  24. protected $orderId;
  25. protected $condition;
  26. public function __construct($tableName, $code, $foreignKey, $amountColumn, $orderId, $condition = null)
  27. {
  28. $this->tableName = $tableName;
  29. $this->code = $code;
  30. $this->foreignKey = $foreignKey;
  31. // $this->localKey = $localKey;
  32. $this->amountColumn = $amountColumn;
  33. $this->orderId = $orderId;
  34. $this->condition = $condition;
  35. }
  36. public function handle()
  37. {
  38. // 验证表是否存在
  39. $exists_table = Schema::hasTable($this->tableName);
  40. if (!$exists_table) {
  41. return;
  42. }
  43. $build = DB::table($this->tableName)
  44. ->select()
  45. ->where($this->foreignKey, $this->orderId);
  46. if ($this->condition) {
  47. $build = $build->where($this->condition);
  48. }
  49. // 分红记录IDs
  50. $ids = $build->pluck('id');
  51. // 分红总和
  52. $sum = $build->sum($this->amountColumn);
  53. if ($sum == 0) {
  54. return;
  55. }
  56. // 存入订单插件分红记录表
  57. $model = OrderPluginBonus::updateRow([
  58. 'order_id' => $this->orderId,
  59. 'ids' => $ids,
  60. 'code' => $this->code,
  61. 'amount' => $sum
  62. ]);
  63. // 暂时不用, 门店利润 在 门店订单结算时重新计算, 各个插件产生分红的事件监听不同.
  64. // 如果后期插件统一事件产生分红,再启用此事件
  65. //event(new CreatedOrderPluginBonusEvent($model));
  66. }
  67. }