// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\store\controller; use app\common\enum\merchant\balanceLog\Scene as SceneEnum; use app\common\lock\RedisLock; use app\common\model\store\Setting as SettingModel; use app\common\service\IdingjuOil; use app\common\service\SybOil; use app\store\model\Code as CodeModel; use app\store\model\Merchant as MerchantModel; use app\store\model\merchant\BalanceLog as BalanceLogModel; use app\store\model\Notify as NotifyModel; use app\store\model\Order as OrderModel; use app\store\model\OrderGoods as OrderGoodsModel; use app\store\model\Shop as ShopModel; use app\store\model\store\User; use app\store\model\store\User as UserModel; use app\store\model\Company as CompanyModel; use app\store\model\company\BalanceLog as CompanyBalanceLogModel; use app\store\model\order\QrCode as QrCodeModel; use GatewayWorker\Lib\Gateway; use think\facade\Cache; use think\facade\Db; use think\response\Json; use app\common\enum\order\OrderStatus as OrderStatusEnum; /** * 订单管理 * Class Order * @package app\store\controller */ class Order extends Controller { /** * 订单列表 * @param string $dataType * @return array */ public function list(string $dataType): Json { // 订单列表 $model = new OrderModel; $user = $this->store['user']; // $merchant = MerchantModel::getAll($user); $list = $model->getList($dataType, $this->request->param(), $user); $list['dataType'] = $dataType; return $this->renderSuccess($list); } /** * 订单详情 * @param int $orderId * @return array */ public function detail(int $orderId): Json { // 订单详情 $model = new OrderModel; if (!$detail = $model->getDetail($orderId)) { return $this->renderError('未找到该订单记录'); } return $this->renderSuccess(compact('detail')); } /** * 修改订单砖状态 * @return array * @throws \think\exception\DbException */ public function updatestatus(): Json { $postData = $this->postForm(); $postData['order_id'] = $this->request->param('orderId'); if(empty($postData['status'])){ return $this->renderError('请选择订单状态'); } $model = OrderModel::detail($postData['order_id']); $NotifyModel = new NotifyModel(); if ($model->updateStatus($postData)) { //已支付订单才进行回调通知 if ($postData['status'] == 20) { $codeModel = CodeModel::detail(['code_id'=>$model['old_code_id'],'status'=>1]); if($codeModel['code_id']){ $codeModel->modifyUsed($model['order_id']); //写入队列 $create_time = is_numeric($codeModel['create_time'])?$codeModel['create_time']:strtotime($codeModel['create_time']); $code_queue = ['code_id' => $codeModel['code_id'], 'expire_time' => $create_time + (float)$codeModel['expiration_time']]; // 过期时间 $code_json = json_encode($code_queue,320); Cache::store('redis')->lRem($codeModel['passageway'].'_'.round($codeModel['money'],2), $code_json, 1); } if ($NotifyModel->add($model)) { return $this->renderSuccess('修改成功'); } else { return $this->renderError($NotifyModel->getError() ?: '修改失败'); } }else{ return $this->renderSuccess('修改成功'); } } return $this->renderError($model->getError() ?: '修改失败'); } /** * 手动回调 * @param $order_id * @return array * @throws \think\exception\DbException */ public function callback(): Json { $postData = $this->postForm(); $postData['order_id'] = $this->request->param('orderId'); $order_id = $postData['order_id']; $password = $postData['password']; //验证密码 $user = UserModel::detail($this->store['user']['store_user_id']); if(!password_verify($password, $user['password'])){ return $this->renderError('登录密码错误'); } $model = OrderModel::detail($order_id); $NotifyModel = new NotifyModel(); if ($NotifyModel->add($model)) { return $this->renderSuccess('操作成功'); } return $this->renderError($model->getError() ?: '操作失败'); } /** 测试发送数据 * @return Json */ public function send(): Json { //Gateway::sendToClient('c0a8008a07d000000002',json_encode(['type' => 'test_send'])); Gateway::sendToUid($this->store['user']['store_user_id'], json_encode(['type' => 'test_send'])); } /** * 撤销分油 * @param $order_id * @return array * @throws \think\exception\DbException */ public function cancelShare(): Json { // 企油通改造:本功能依赖已下线的上游(智慧油客/定聚),现已停用。 return $this->renderError('小程序油品链路已停用'); } /** * 获取支付二维码 * @return Json * @throws \think\db\exception\DbException */ public function qrCode(): Json { // 企油通改造:本功能依赖已下线的上游(智慧油客/定聚),现已停用。 return $this->renderError('小程序油品链路已停用'); } /** * @param $goodsNo * @return string */ public function getCompany($goodsNo): string { switch ($goodsNo) { case 'djgdface100': case 'djgdface200': case 'djgdface300': return 'umQZhIHKofyA'; case 'jmgxyyface100': case 'jmgxyyface200': case 'jmgxyyface300': case 'jmgzface100': case 'jmgzface200': case 'jmgzface300': case 'jmfjface100': case 'jmfjface200': case 'jmfjface300': return 'HpWqXIgYvTBg'; case 'face003': return 'NKJBgaaRNdiL'; // 测试参数 default: return ''; } } public function processUserOrders($companyId) { $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) { throwError('网络繁忙请稍后'); } $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', '<', $todayStart) ->where('a.is_delete', '=', 0) ->where('a.state', '=', 0) ->where('a.company_id', '=', $companyId) ->where('c.status', '=', 10) ->field('a.*') ->limit(1) ->find(); if (!$account) { throwError("没有可用的用户"); } // 更新用户的 `last_called_at` Db::name('account') ->where('account_id', $account['account_id']) ->update(['last_called_at' => time(), 'state' => 1]); $redisLock->releaseLock(); return $account; } /** * 手动分油 * @return Json * @throws \think\db\exception\DbException */ public function share(): Json { // 企油通改造:本功能依赖已下线的上游(智慧油客/定聚),现已停用。 return $this->renderError('小程序油品链路已停用'); } }