DealerOrder.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\task\behavior;
  3. use think\Cache;
  4. use app\task\model\Order as OrderModel;
  5. use app\task\model\dealer\Order as DealerOrderModel;
  6. /**
  7. * 分销商订单行为管理
  8. * Class DealerOrder
  9. * @package app\task\behavior
  10. */
  11. class DealerOrder
  12. {
  13. /* @var DealerOrderModel $model */
  14. private $model;
  15. /**
  16. * 执行函数
  17. * @param $model
  18. * @return bool
  19. * @throws \think\exception\PDOException
  20. */
  21. public function run($model)
  22. {
  23. if (!$model instanceof DealerOrderModel) {
  24. return new DealerOrderModel and false;
  25. }
  26. $this->model = $model;
  27. if (!Cache::has('__task_space__DealerOrder')) {
  28. $this->model->startTrans();
  29. try {
  30. // 发放分销订单佣金
  31. $this->grantMoney();
  32. $this->model->commit();
  33. } catch (\Exception $e) {
  34. $this->model->rollback();
  35. }
  36. Cache::set('__task_space__DealerOrder', time(), 3600);
  37. }
  38. return true;
  39. }
  40. /**
  41. * 发放分销订单佣金
  42. * @return bool
  43. * @throws \think\Exception
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @throws \think\exception\DbException
  47. */
  48. private function grantMoney()
  49. {
  50. // 获取未结算佣金的订单列表
  51. $list = $this->model->getUnSettledList();
  52. if ($list->isEmpty()) return false;
  53. // 整理id集
  54. $invalidIds = [];
  55. $grantIds = [];
  56. // 发放分销订单佣金
  57. foreach ($list->toArray() as $item) {
  58. // 已失效的订单
  59. if ($item['order_master']['order_status']['value'] == 20) {
  60. $invalidIds[] = $item['id'];
  61. }
  62. // 已完成的订单
  63. if ($item['order_master']['order_status']['value'] == 30) {
  64. $grantIds[] = $item['id'];
  65. DealerOrderModel::grantMoney($item['order_master'], $item['order_type']['value']);
  66. }
  67. }
  68. // 标记已失效的订单
  69. $this->model->setInvalid($invalidIds);
  70. // 记录日志
  71. $this->dologs('invalidIds', ['Ids' => $invalidIds]);
  72. $this->dologs('grantMoney', ['Ids' => $grantIds]);
  73. return true;
  74. }
  75. /**
  76. * 记录日志
  77. * @param $method
  78. * @param array $params
  79. * @return bool|int
  80. */
  81. private function dologs($method, $params = [])
  82. {
  83. $value = 'behavior DealerOrder --' . $method;
  84. foreach ($params as $key => $val) {
  85. $value .= ' --' . $key . ' ' . (is_array($val) ? json_encode($val) : $val);
  86. }
  87. return log_write($value);
  88. }
  89. }