You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Service/UserService.php

273 lines
10 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
use App\Exception\BusinessException;
use App\Helper\Baofu\Baofu;
use App\Model\App;
use App\Model\BankCard;
use App\Model\User;
class UserService extends AbstractService
{
public function companyRegister(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if ($user) {
throw new BusinessException('用户已存在');
}
$emial = $data['email'];
$memberId = $this->generateMemberId($app->app_id, $data['userId']);
$baofu = new Baofu();
$url = $baofu->apply($memberId, $emial, $token);
return $url;
}
public function register(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if ($user) {
throw new BusinessException('用户已存在');
}
$memberId = $this->generateMemberId($app->app_id, $data['userId']);
$baofu = new Baofu();
$url = $baofu->register($memberId, $token);
return $url;
}
public function rsyncUser($memberId, $appId, $userId, $customerType = 'PERSON') {
$baofu = new Baofu();
$userInfo = $baofu->queryCustomerInfo($memberId, $customerType);
return $this->saveUser($userInfo, $appId, $userId);
}
public function saveUser($userInfo, $appId, $userId) {
$memberId = $userInfo['loginNo'] ?? $userInfo['loginMobile'];
$user = User::where('app_id', $appId)->where('member_id', $memberId)->first();
if ($user) {
$user->contract_no = $userInfo['contractNo'];
$user->realname_flag = $userInfo['realNameFlag'] ?? 'Y';
$user->bind_card_flag = $userInfo['bindCardFlag'];
$user->set_pwd_flag = $userInfo['setPwdFlag'];
$user->status = $userInfo['operatorStatus'] ?? $userInfo['status'];
$user->email = $userInfo['email'] ?? '';
$user->mobile = $userInfo['mobile'] ?? '';
$user->real_name = $userInfo['customerName'] ?? '';
$user->certificate_no = $userInfo['certificateNo'] ?? '';
$user->user_type = $userInfo['customerType'];
$user->apply_no = $userInfo['applyNo'] ?? '';
} else {
$user = new User();
$user->user_id = $userId;
$user->app_id = $appId;
$user->member_id = $memberId;
$user->contract_no = $userInfo['contractNo'];
$user->realname_flag = $userInfo['realNameFlag'] ?? 'Y';
$user->bind_card_flag = $userInfo['bindCardFlag'];
$user->set_pwd_flag = $userInfo['setPwdFlag'];
$user->status = $userInfo['status'];
$user->email = $userInfo['email'] ?? '';
$user->mobile = $userInfo['mobile'] ?? '';
$user->real_name = $userInfo['customerName'] ?? '';
$user->certificate_no = $userInfo['certificateNo'] ?? '';
$user->user_type = $userInfo['customerType'];
$user->apply_no = $userInfo['applyNo'] ?? '';
}
$user->save();
return $user;
}
public function rsyncBankCards($memberId) {
$user = User::where('member_id', $memberId)->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$bankCards = $baofu->findBindBankCards([
'loginNo' => $memberId,
'contractNo' => $user->contract_no,
]);
$oldAgreementNos = BankCard::where('member_id', $memberId)->get()->pluck('agreement_no')->toArray();
$agreementNos = array_column($bankCards, 'agreementNo');
$deletedAgreementNos = array_diff($oldAgreementNos, $agreementNos);
BankCard::where('member_id', $memberId)->whereIn('agreement_no', $deletedAgreementNos)->delete();
foreach ($bankCards as $bankCard) {
$this->saveBankCard($bankCard, $user);
}
}
public function getUserBankCards($userId, App $app)
{
$bankCards = BankCard::where('app_id', $app->app_id)->where('user_id', $userId)->get();
$records = [];
foreach ($bankCards as $bankCard) {
$records[] = [
'agreementNo' => $bankCard->agreement_no,
'cardUserName' => $bankCard->card_user_name,
'lastCardNo' => $bankCard->last_card_no,
'bankMobile' => $bankCard->bank_mobile,
'bankCode' => $bankCard->bank_code,
'bankName' => $bankCard->bank_name,
'cardType' => $bankCard->card_type,
'cnapsCode' => $bankCard->cnaps_code,
'publicFlag' => $bankCard->public_flag,
'repaymentDate' => $bankCard->repayment_date,
'mainFlag' => $bankCard->main_flag,
'status' => $bankCard->status,
'avaFlag' => $bankCard->ava_flag,
'remark' => $bankCard->remark,
];
}
return $records;
}
public function saveBankCard(array $data, User $user)
{
$appId = $user->app_id;
$userId = $user->user_id;
$bankCard = BankCard::where('app_id', $appId)
->where('member_id', $user->member_id)
->where('agreement_no', $data['agreementNo'])
->first();
if ($bankCard) {
$bankCard->agreement_no = $data['agreementNo'];
$bankCard->card_user_name = $data['cardUserName'];
$bankCard->last_card_no = $data['lastCardNo'];
$bankCard->bank_mobile = $data['bankMobile'] ?: '';
$bankCard->bank_code = $data['bankCode'];
$bankCard->bank_name = $data['bankName'];
$bankCard->card_type = $data['cardType'];
$bankCard->cnaps_code = $data['cnapsCode'] ?: '';
$bankCard->public_flag = $data['publicFlag'];
$bankCard->repayment_date = $data['repaymentDate'] ?: '';
$bankCard->main_flag = $data['mainFlag'];
$bankCard->status = $data['status'];
$bankCard->ava_flag = $data['avaFlag'] ?: '';
$bankCard->remark = $data['remark'] ?: '';
} else {
$bankCard = new BankCard();
$bankCard->app_id = $appId;
$bankCard->user_id = $userId;
$bankCard->member_id = $user->member_id;
$bankCard->agreement_no = $data['agreementNo'];
$bankCard->card_user_name = $data['cardUserName'];
$bankCard->last_card_no = $data['lastCardNo'];
$bankCard->bank_mobile = $data['bankMobile'] ?: '';
$bankCard->bank_code = $data['bankCode'];
$bankCard->bank_name = $data['bankName'];
$bankCard->card_type = $data['cardType'];
$bankCard->cnaps_code = $data['cnapsCode'] ?: '';
$bankCard->public_flag = $data['publicFlag'];
$bankCard->repayment_date = $data['repaymentDate'] ?: '';
$bankCard->main_flag = $data['mainFlag'];
$bankCard->status = $data['status'];
$bankCard->ava_flag = $data['avaFlag'] ?: '';
$bankCard->remark = $data['remark'] ?: '';
}
$bankCard->save();
return $bankCard;
}
public function generateMemberId($appId, $userId) {
return md5($appId . '-' . $userId);
}
public function bindCard(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$url = $baofu->cardBind($user->member_id, $token);
return $url;
}
public function unbindCard(array $data, App $app) {
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$result = $baofu->cardUnbind($user->member_id, $data['agreementNo']);
if ($result) {
$this->rsyncBankCards($user->member_id) ;
return true;
}
return false;
}
public function pwdForget(array $data, App $app, $token) {
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
return $baofu->pwdForget($user->member_id, $token);
}
public function pwdModify(array $data, App $app, $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
return $baofu->pwdModify($user->member_id, $token);
}
public function getBalance($memberId)
{
$baofu = new Baofu();
return $baofu->getBalance($memberId);
}
public function signWithdrawEntrust(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$url = $baofu->signWithdrawEntrust($user->member_id, $token);
return $url;
}
public function unbindWithdrawEntrust(array $data, App $app, string $token)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$url = $baofu->unbindWithdrawEntrust($user->member_id, $token);
return $url;
}
public function querySignEntrust(array $data, App $app)
{
$user = User::where('app_id', $app->app_id)->where('user_id', $data['userId'])->first();
if (empty($user)) {
throw new BusinessException('用户不存在');
}
$baofu = new Baofu();
$result = $baofu->findSignEntrustResult($user->member_id);
return $result;
}
}