model = new VipOrderModel(); $cacheKey = "task_space_VipOrder"; if (!Cache::has($cacheKey)) { $this->model->startTrans(); try { // 发放VIP订单佣金 $this->grantMoney(); $this->model->commit(); } catch (\Exception $e) { log_write('VipOrder TASK : ' . '__ ' . $e->getMessage()); $this->model->rollback(); } Cache::set($cacheKey, time(), 60); } } catch (\Throwable $e) { echo 'ERROR VipOrder: ' . $e->getMessage() . PHP_EOL; log_write('VipOrder TASK : ' . '__ ' . $e->getMessage(), 'task'); } return true; } /** * 发放VIP订单佣金 */ private function grantMoney() { // 获取未结算佣金的订单列表 $list = $this->model->getUnSettledList(); if ($list->isEmpty()) return false; // 整理id集 $invalidIds = []; $grantIds = []; // 发放VIP订单佣金 foreach ($list as $item) { // 已失效的订单 if ($item['orderMaster']['order_status']['value'] == 20) { $invalidIds[] = $item['id']; } // 已完成的订单 if ($item['orderMaster']['order_status']['value'] == 30) { $grantIds[] = $item['id']; VipOrderModel::grantMoney($item['orderMaster'], OrderTypeEnum::MASTER); } } // 标记已失效的订单 $this->model->setInvalid($invalidIds); // 记录日志 $this->dologs('invalidIds', ['Ids' => $invalidIds]); $this->dologs('grantMoney', ['Ids' => $grantIds]); return true; } /** * 记录日志 */ private function dologs($method, $params = []) { $value = 'behavior VipOrder --' . $method; foreach ($params as $key => $val) { $value .= ' --' . $key . ' ' . (is_array($val) ? json_encode($val) : $val); } return log_write($value, 'task'); } }