// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\store\controller; use app\common\service\Order as OrderService; use app\common\service\SybOil; use app\store\model\Merchant as MerchantModel; use app\store\model\Company as CompanyModel; use app\store\model\company\BalanceLog as CompanyBalanceLogModel; use app\store\model\User as UserModel; use app\store\model\user\BalanceLog as BalanceLogModel; use app\store\model\Order as OrderModel; use app\store\model\Goods as GoodsModel; use think\response\Json; use think\Exception; use app\common\enum\merchant\balanceLog\Scene as SceneEnum; use app\common\lock\RedisLock; use think\facade\Db; use app\console\service\Order as OrderServiceNew; /** * 用户管理 * Class User * @package app\store\controller */ class User extends Controller { /** * 用户列表 * @return array * @throws \think\db\exception\DbException */ public function list() { /* $service = new OrderServiceNew; $service->deliveryEvent($this->storeId); print_r('111');*/ // 用户列表 $model = new UserModel; $list = $model->getList($this->request->param(), $this->store['user']); return $this->renderSuccess(compact('list')); } /** * 添加用户 * @return array|mixed * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function add() { // 企油通改造:分油用户池已停用(司机信息改由下单平台随单传入),不再向上游注册用户。 return $this->renderError('分油用户池已停用'); } /** * 用户详情 * @param int $userId * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \cores\exception\BaseException */ public function detail(int $userId): Json { $userInfo = UserModel::detail($userId); return $this->renderSuccess(compact('userInfo')); } /** * 编辑用户 * @param int $userId * @return Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \cores\exception\BaseException */ public function edit(int $userId): Json { $model = UserModel::detail($userId); $data = $this->postForm(); // 更新记录 if ($model->edit($data)) { return $this->renderSuccess('更新成功'); } return $this->renderError($model->getError() ?: '更新失败'); } /** * 删除用户 * @param int $userId * @return array */ public function delete(int $userId) { // 用户详情 $model = UserModel::detail($userId); if ($model->setDelete()) { return $this->renderSuccess('删除成功'); } return $this->renderError($model->getError() ?: '删除失败'); } public function processUserOrders() { $redisLock = new RedisLock('user_order_lock'); $maxAttempts = 10; // 最大尝试次数 $attempt = 0; $sleepTime = 500000; // 每次尝试失败后的等待时间(微秒),500000微秒 = 0.5秒 while ($attempt < $maxAttempts) { $lockAcquired = $redisLock->acquireLock(); if ($lockAcquired) { break; } $attempt++; usleep($sleepTime); // 等待一段时间再尝试 } if (!$lockAcquired) { throw new Exception("网络繁忙请稍后"); } //$todayStart = strtotime(date('Y-m-d 00:00:00')); // 查找一个未被调用的用户 $account = Db::name('account') ->alias('a') ->join('company c', 'a.company_id = c.company_id') ->where('a.last_called_at', '=', 0) ->where('c.status', '=', 10) ->field('a.*') ->limit(1) ->find(); if (!$account) { throw new Exception("没有可用的用户"); } // 更新用户的 `last_called_at` Db::name('account') ->where('account_id', $account['account_id']) ->update(['last_called_at' => time()]); $redisLock->releaseLock(); return $account; } /** * 用户分油 * @param int $userId * @param string $target * @return array|mixed */ public function oil(int $userId, string $target) { // 企油通改造:后台给小程序用户手动分油依赖已下线的上游(智慧油客)+ 分油用户池,现已停用。 return $this->renderError('小程序油品链路已停用'); } /** * 用户充值 * @param int $userId * @param string $target * @return array */ public function recharge(int $userId, string $target) { // 用户详情 $model = UserModel::detail($userId); if ($model->recharge($target, $this->postForm())) { return $this->renderSuccess('操作成功'); } return $this->renderError($model->getError() ?: '操作失败'); } /** * 修改会员等级 * @param int $userId * @return array */ public function grade(int $userId) { // 用户详情 $model = UserModel::detail($userId); if ($model->updateGrade($this->postForm())) { return $this->renderSuccess('操作成功'); } return $this->renderError($model->getError() ?: '操作失败'); } /** * 获取所有商户 * @return mixed * @throws \think\exception\DbException */ public function merchant(): Json { $user = $this->store['user']; $list = MerchantModel::getAll($user); return $this->renderSuccess(compact('list','user')); } }