123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace app\task\behavior\sharing;
- use think\Cache;
- use app\task\model\sharing\Order as OrderModel;
- use app\task\model\sharing\Active as ActiveModel;
- use app\task\model\sharing\Setting as SettingModel;
- use app\common\service\Message as MessageService;
- use app\common\enum\sharing\ActiveStatus as ActiveStatusEnum;
- /**
- * 拼团订单行为管理
- * Class Active
- * @package app\task\behavior
- */
- class Active
- {
- /* @var ActiveModel $model */
- private $model;
- /**
- * 执行函数
- * @param $model
- * @return bool
- */
- public function run($model)
- {
- if (!$model instanceof ActiveModel) {
- return new ActiveModel and false;
- }
- $this->model = $model;
- if (!$model::$wxapp_id) {
- return false;
- }
- if (!Cache::has('__task_space__sharing_active__' . $model::$wxapp_id)) {
- try {
- // 拼团设置
- $config = SettingModel::getItem('basic');
- // 已过期的拼单更新状态(拼单失败)
- $this->onUpdateActiveEnd();
- // 更新拼团失败的订单并退款
- if ($config['auto_refund'] == true) {
- $this->onOrderRefund();
- }
- } catch (\Exception $e) {
- }
- Cache::set('__task_space__sharing_active__' . $model::$wxapp_id, time(), 10);
- }
- return true;
- }
- /**
- * 已过期的拼单更新状态
- * @return false|int
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- private function onUpdateActiveEnd()
- {
- // 获取已过期的拼单列表
- $list = $this->model->getEndedList();
- // 拼单ID集
- $activeIds = [];
- foreach ($list as $item) {
- $activeIds[] = $item['active_id'];
- }
- // 记录日志
- $this->dologs('onSetActiveEnd', [
- 'activeIds' => json_encode($activeIds),
- ]);
- // 发送拼团失败订阅消息
- foreach ($list as $item) {
- MessageService::send('sharing.active_status', [
- 'active' => $item,
- 'status' => ActiveStatusEnum::ACTIVE_STATE_FAIL
- ]);
- }
- // 更新已过期状态
- return $this->model->updateEndedStatus($activeIds);
- }
- /**
- * 更新拼团失败的订单并退款
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- private function onOrderRefund()
- {
- // 实例化拼单订单模型
- $model = new OrderModel;
- // 每次最多处理的个数,防止运行太久
- // 及微信申请退款API请求频率限制:150qps
- $maxLimit = 100;
- // 获取拼团失败的订单集
- $orderList = $model->getFailedOrderList($maxLimit);
- // 整理拼团订单id
- $orderIds = [];
- foreach ($orderList as $order) {
- $orderIds[] = $order['order_id'];
- }
- // 记录日志
- $this->dologs('onOrderRefund', [
- 'orderIds' => json_encode($orderIds),
- ]);
- if (empty($orderIds)) {
- return false;
- }
- // 更新拼团失败的订单并退款
- if ($model->updateFailedStatus($orderList)) {
- return true;
- }
- // 存在退款出错的订单记录日志
- $this->dologs('onOrderRefund', [
- 'error: ' => $model->getError()
- ]);
- return false;
- }
- /**
- * 记录日志
- * @param $method
- * @param array $params
- * @return bool|int
- */
- private function dologs($method, $params = [])
- {
- $value = 'behavior sharing Active --' . $method;
- foreach ($params as $key => $val)
- $value .= ' --' . $key . ' ' . $val;
- return log_write($value);
- }
- }
|