255], ]; } public function getUser(): \yii\db\ActiveQuery { return $this->hasOne(User::class, ['id' => 'user_id']); } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'user_id' => 'User ID', 'num' => 'Num', 'withdraw_status' => 'Withdraw Status', 'remark' => 'Remark', 'payment_evidence' => 'Payment Evidence', 'status' => 'Status', 'created_at' => 'Created At', 'updated_at' => 'Updated At', ]; } /** * 保存数据 * @param array $params * @return bool|UserWallet */ public static function setData(array $params){ try{ if(!empty($params['user_id'])){ $exist = self::find()->where(['user_id'=>$params['user_id'],'withdraw_status'=>BalanceRechargeLog::STATUS_APPLY])->select('id')->one(); if($exist){ throw new \Exception('尚有未审核的申请!'); } } if(!empty($params['id'])){ $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]); if(empty($model)) throw new \Exception('数据不存在或已删除'); }else{ $model = new self(); $model->mall_id = $params['mall_id']; $model->loadDefaultValues(); } if(!$model->load($params,'') || !$model->save()){ throw new Exception($model->getErrorMessage()); } return $model->toArray(); }catch(\Exception $e){ self::$static_error = $e->getMessage(); return false; } } /** * @throws Exception */ public static function updateLog($params) { $lock_key = RedisKeyEnum::suffix('BalanceRecharge', $params['id']); $identification = uniqid(); if (!LockHelper::lock($lock_key, $identification, 100, 100)) { \Yii::error('充值审核 获取锁(' . $lock_key . ')失败阻塞,充值审核'); return false; } $where = [['id' => $params['id']]]; $log = self::getOne($where); if (!$log) { throw new \Exception('记录不存在'); } if ($log->withdraw_status == 1) { throw new \Exception('审核已通过'); } if ($log->withdraw_status == 2) { throw new \Exception('审核已驳回'); } if ($params['status'] <= $log->withdraw_status) { throw new \Exception('状态错误, 请刷新重试'); } $t = \Yii::$app->db->beginTransaction(); try { switch ($params['status']) {// 1=提现申请通过,2=拒绝提现 case 1: self::remit($log,$params); break; case 2: self::reject($log,$params); break; default: throw new \Exception('错误的类型'); } $t->commit(); LockHelper::uLock($lock_key, $identification); } catch (\Exception $exception) { $t->rollBack(); LockHelper::uLock($lock_key, $identification); \Yii::error("BalanceRecharge error=" . $exception->getFile() . ";line:" . $exception->getLine() . ";message:" . $exception->getMessage()); throw new \Exception($exception->getMessage()); } return true; } /** * @throws Exception */ private static function remit($log,$params) { $userInfo = User::getOne([['id' => $log->user_id, 'status' => StatusEnum::ENABLED]]); if (!$userInfo) { throw new \Exception('该用户不存在!'); } $desc = "用户线下充值到余额:" . $log->num . "元"; $data = [ 'message_id' => 0, 'mall_id' => $log->mall_id, 'user_id' => $log->user_id, 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE, 'is_frozen' => false, 'unfrozen' => false, 'money' => abs($log->num), 'desc' => $desc, 'source_table' => self::tableName(), 'source_table_id' => $log->id, 'from_type' => UserWalletEnum::RECHARGE, 'capital_type' => UserWalletEnum::RECHARGE_OFFLINE, ]; $res = UserWalletService::handle(0, $data); if ($res) { $log->withdraw_status = BalanceRechargeLog::STATUS_AGREE; if(!empty($params['remark'])){ $log->remark = $params['remark']; } if(!$log->save()){ throw new \Exception('保存失败'); } }else{ throw new \Exception($msg); } } /** * @Note:拒绝 * @param $log * @return void * @throws \Exception */ private static function reject($log,$params) { if (empty($params['remark'])) { throw new \Exception('请填写备注'); } $log->withdraw_status = BalanceRechargeLog::STATUS_REJECT; $log->remark = $params['remark']; if (!$log->save()) { throw new \Exception('保存失败'); } return true; } }