Active.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\task\behavior\sharing;
  3. use think\Cache;
  4. use app\task\model\sharing\Order as OrderModel;
  5. use app\task\model\sharing\Active as ActiveModel;
  6. use app\task\model\sharing\Setting as SettingModel;
  7. use app\common\service\Message as MessageService;
  8. use app\common\enum\sharing\ActiveStatus as ActiveStatusEnum;
  9. /**
  10. * 拼团订单行为管理
  11. * Class Active
  12. * @package app\task\behavior
  13. */
  14. class Active
  15. {
  16. /* @var ActiveModel $model */
  17. private $model;
  18. /**
  19. * 执行函数
  20. * @param $model
  21. * @return bool
  22. */
  23. public function run($model)
  24. {
  25. if (!$model instanceof ActiveModel) {
  26. return new ActiveModel and false;
  27. }
  28. $this->model = $model;
  29. if (!$model::$wxapp_id) {
  30. return false;
  31. }
  32. if (!Cache::has('__task_space__sharing_active__' . $model::$wxapp_id)) {
  33. try {
  34. // 拼团设置
  35. $config = SettingModel::getItem('basic');
  36. // 已过期的拼单更新状态(拼单失败)
  37. $this->onUpdateActiveEnd();
  38. // 更新拼团失败的订单并退款
  39. if ($config['auto_refund'] == true) {
  40. $this->onOrderRefund();
  41. }
  42. } catch (\Exception $e) {
  43. }
  44. Cache::set('__task_space__sharing_active__' . $model::$wxapp_id, time(), 10);
  45. }
  46. return true;
  47. }
  48. /**
  49. * 已过期的拼单更新状态
  50. * @return false|int
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @throws \think\exception\DbException
  54. */
  55. private function onUpdateActiveEnd()
  56. {
  57. // 获取已过期的拼单列表
  58. $list = $this->model->getEndedList();
  59. // 拼单ID集
  60. $activeIds = [];
  61. foreach ($list as $item) {
  62. $activeIds[] = $item['active_id'];
  63. }
  64. // 记录日志
  65. $this->dologs('onSetActiveEnd', [
  66. 'activeIds' => json_encode($activeIds),
  67. ]);
  68. // 发送拼团失败订阅消息
  69. foreach ($list as $item) {
  70. MessageService::send('sharing.active_status', [
  71. 'active' => $item,
  72. 'status' => ActiveStatusEnum::ACTIVE_STATE_FAIL
  73. ]);
  74. }
  75. // 更新已过期状态
  76. return $this->model->updateEndedStatus($activeIds);
  77. }
  78. /**
  79. * 更新拼团失败的订单并退款
  80. * @return bool
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. */
  85. private function onOrderRefund()
  86. {
  87. // 实例化拼单订单模型
  88. $model = new OrderModel;
  89. // 每次最多处理的个数,防止运行太久
  90. // 及微信申请退款API请求频率限制:150qps
  91. $maxLimit = 100;
  92. // 获取拼团失败的订单集
  93. $orderList = $model->getFailedOrderList($maxLimit);
  94. // 整理拼团订单id
  95. $orderIds = [];
  96. foreach ($orderList as $order) {
  97. $orderIds[] = $order['order_id'];
  98. }
  99. // 记录日志
  100. $this->dologs('onOrderRefund', [
  101. 'orderIds' => json_encode($orderIds),
  102. ]);
  103. if (empty($orderIds)) {
  104. return false;
  105. }
  106. // 更新拼团失败的订单并退款
  107. if ($model->updateFailedStatus($orderList)) {
  108. return true;
  109. }
  110. // 存在退款出错的订单记录日志
  111. $this->dologs('onOrderRefund', [
  112. 'error: ' => $model->getError()
  113. ]);
  114. return false;
  115. }
  116. /**
  117. * 记录日志
  118. * @param $method
  119. * @param array $params
  120. * @return bool|int
  121. */
  122. private function dologs($method, $params = [])
  123. {
  124. $value = 'behavior sharing Active --' . $method;
  125. foreach ($params as $key => $val)
  126. $value .= ' --' . $key . ' ' . $val;
  127. return log_write($value);
  128. }
  129. }