AgentOrder.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace app\job\event;
  3. use think\facade\Cache;
  4. use app\job\model\plus\agent\Order as AgentOrderModel;
  5. /**
  6. * 分销商订单事件管理
  7. */
  8. class AgentOrder
  9. {
  10. // 模型
  11. private $model;
  12. /**
  13. * 执行函数
  14. */
  15. public function handle()
  16. {
  17. try {
  18. $this->model = new AgentOrderModel();
  19. $cacheKey = "task_space_AgentOrder";
  20. if (!Cache::has($cacheKey)) {
  21. $this->model->startTrans();
  22. try {
  23. // 发放分销订单佣金
  24. $this->grantMoney();
  25. $this->model->commit();
  26. } catch (\Exception $e) {
  27. $this->model->rollback();
  28. }
  29. Cache::set($cacheKey, time(), 60);
  30. }
  31. } catch (\Throwable $e) {
  32. echo 'ERROR AgentOrder: ' . $e->getMessage() . PHP_EOL;
  33. log_write('AgentOrder TASK : ' . '__ ' . $e->getMessage(), 'task');
  34. }
  35. return true;
  36. }
  37. /**
  38. * 发放分销订单佣金
  39. */
  40. private function grantMoney()
  41. {
  42. // 获取未结算佣金的订单列表
  43. $list = $this->model->getUnSettledList();
  44. if ($list->isEmpty()) return false;
  45. // 整理id集
  46. $invalidIds = [];
  47. $grantIds = [];
  48. // 发放分销订单佣金
  49. foreach ($list->toArray() as $item) {
  50. // 已失效的订单
  51. if ($item['order_master']['order_status']['value'] == 20) {
  52. $invalidIds[] = $item['id'];
  53. }
  54. // 已完成的订单
  55. if ($item['order_master']['order_status']['value'] == 30) {
  56. $grantIds[] = $item['id'];
  57. AgentOrderModel::grantMoney($item['order_master'], $item['order_type']['value']);
  58. }
  59. }
  60. // 标记已失效的订单
  61. $this->model->setInvalid($invalidIds);
  62. // 记录日志
  63. $this->dologs('invalidIds', ['Ids' => $invalidIds]);
  64. $this->dologs('grantMoney', ['Ids' => $grantIds]);
  65. return true;
  66. }
  67. /**
  68. * 记录日志
  69. */
  70. private function dologs($method, $params = [])
  71. {
  72. $value = 'behavior AgentOrder --' . $method;
  73. foreach ($params as $key => $val) {
  74. $value .= ' --' . $key . ' ' . (is_array($val) ? json_encode($val) : $val);
  75. }
  76. return log_write($value, 'task');
  77. }
  78. }