Active.php 3.9 KB

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