|
|
@@ -342,4 +342,184 @@ class BalanceLogService extends BaseService
|
|
|
$this->error = '提现申请成功,请耐心等候审核~';
|
|
|
return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 积分转账
|
|
|
+ * @param $userId
|
|
|
+ * @param $params
|
|
|
+ * @return array|false
|
|
|
+ */
|
|
|
+ public function transfer($userId, $params)
|
|
|
+ {
|
|
|
+ // 参数验证
|
|
|
+ $money = isset($params['money']) ? floatval($params['money']) : 0;
|
|
|
+ $accountType = isset($params['type']) && $params['type']? intval($params['type']) : 3;
|
|
|
+ $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
|
|
|
+ if ($money <= 0) {
|
|
|
+ $this->error = '请输入转账数量';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($mobile)) {
|
|
|
+ $this->error = '请输入对方手机账号';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $openTransfer = ConfigService::make()->getConfigByCode("transfer_{$accountType}_open", 1);
|
|
|
+ if (!$openTransfer) {
|
|
|
+ $this->error = '转账功能未开放';
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 锁
|
|
|
+ $cacheLockKey = "caches:members:transfer:{$userId}_{$accountType}";
|
|
|
+ if (RedisService::get($cacheLockKey)) {
|
|
|
+ $this->error = 1034;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断用户账号状态
|
|
|
+ $fields = [2=>'property',3=>'bd_score',4=>'ls_score'];
|
|
|
+ $field = isset($fields[$accountType])? $fields[$accountType]:'bd_score';
|
|
|
+ RedisService::set($cacheLockKey, ['user_id' => $userId, 'params' => $params], rand(10, 20));
|
|
|
+ $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
|
|
|
+ ->select(['id', 'balance','mobile','nickname','property','bd_score','ls_score', 'status'])
|
|
|
+ ->first();
|
|
|
+ $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
|
|
|
+ $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
|
|
|
+ $userMobile = isset($userInfo['mobile']) &&$userInfo['mobile']? $userInfo['mobile'] : '';
|
|
|
+ $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
|
|
|
+ $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
|
|
|
+ if (empty($userInfo) || $status != 1) {
|
|
|
+ $this->error = 2016;
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($money > $balance) {
|
|
|
+ $this->error = '该账户积分余额不足';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $total = $money;
|
|
|
+ $actualMoney = $money;
|
|
|
+ $accountTypeName = ['账户','收益余额','数字资产','报单积分','绿色积分'][$accountType];
|
|
|
+ $accountTypeName = $accountTypeName?$accountTypeName:'账户';
|
|
|
+
|
|
|
+ // 对方账户验证
|
|
|
+ $transferAccount = MemberModel::where(['mobile' => $mobile, 'mark' => 1])
|
|
|
+ ->select(['id', 'balance','property','bd_score','ls_score', 'status'])
|
|
|
+ ->first();
|
|
|
+ $status = isset($transferAccount['status']) ? $transferAccount['status'] : 0;
|
|
|
+ $transferBalance = isset($transferAccount[$field]) ? $transferAccount[$field] : 0;
|
|
|
+ if (empty($transferAccount) || $status != 1) {
|
|
|
+ $this->error = '对方账户不存在或已冻结';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转账处理
|
|
|
+ DB::beginTransaction();
|
|
|
+ $updateData = ["{$field}" => DB::raw("{$field} - {$money}"), 'update_time' => time()];
|
|
|
+
|
|
|
+ // 扣除会员账户
|
|
|
+ if (!MemberModel::where(['id' => $userId])->update($updateData)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '转账处理失败';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $updateData = ["{$field}" => DB::raw("{$field} + {$money}"), 'update_time' => time()];
|
|
|
+ if (!MemberModel::where(['mobile' => $mobile])->update($updateData)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '转账处理失败';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $orderNo = get_order_num('TS');
|
|
|
+ $order = [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'order_no' => $orderNo,
|
|
|
+ 'money' => $money,
|
|
|
+ 'total' => $total,
|
|
|
+ 'after_money' => moneyFormat(max(0, $balance - $money), 2),
|
|
|
+ 'actual_money' => $actualMoney,
|
|
|
+ 'user_type' => 1,
|
|
|
+ 'type' => 3,
|
|
|
+ 'account_type' => $accountType,
|
|
|
+ 'pay_type' => 30,
|
|
|
+ 'realname' => '',
|
|
|
+ 'account_name' => $accountTypeName,
|
|
|
+ 'account' => $mobile,
|
|
|
+ 'account_remark' => '',
|
|
|
+ 'date' => date('Y-m-d'),
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'status' => 4,
|
|
|
+ 'mark' => 1
|
|
|
+ ];
|
|
|
+ if (!$orderId = $this->model::insertGetId($order)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '提现处理失败';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $log = [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'source_order_no' => $orderNo,
|
|
|
+ 'user_type' => 1,
|
|
|
+ 'account_type' => $accountType,
|
|
|
+ 'type' => 9,
|
|
|
+ 'money' => -$money,
|
|
|
+ 'after_money' => moneyFormat(max(0, $balance - $money), 2),
|
|
|
+ 'date' => date('Y-m-d'),
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'remark' => "转账到{$mobile}",
|
|
|
+ 'status' => 1,
|
|
|
+ 'mark' => 1,
|
|
|
+ ];
|
|
|
+ if (!$accountId = AccountLogModel::insertGetId($log)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '转账处理失败';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $userMobileText = $userMobile?format_mobile($userMobile):$nickname;
|
|
|
+ $log = [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'source_order_no' => $orderNo,
|
|
|
+ 'user_type' => 1,
|
|
|
+ 'account_type' => $accountType,
|
|
|
+ 'type' => 9,
|
|
|
+ 'money' => $money,
|
|
|
+ 'after_money' => moneyFormat(max(0, $transferBalance + $money), 2),
|
|
|
+ 'date' => date('Y-m-d'),
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'remark' => "收到{$userMobileText}用户的{$accountTypeName}转账",
|
|
|
+ 'status' => 1,
|
|
|
+ 'mark' => 1,
|
|
|
+ ];
|
|
|
+ if (!$accountId = AccountLogModel::insertGetId($log)) {
|
|
|
+ DB::rollBack();
|
|
|
+ $this->error = '转账处理失败';
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+
|
|
|
+ // 操作日志
|
|
|
+ ActionLogModel::setRecord($userId, ['type' => 2, 'title' => "{$accountTypeName}积分转账", 'content' => "账户{$userMobileText}转至{$mobile},数量{$money},单号:{$orderNo}", 'module' => 'balanceLog']);
|
|
|
+ ActionLogModel::record();
|
|
|
+ RedisService::clear($cacheLockKey);
|
|
|
+ $this->error = '转账成功~';
|
|
|
+ return ['id' => $orderId, 'aid' => $accountId, 'money' => $money];
|
|
|
+ }
|
|
|
}
|