Active.php 4.0 KB

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