|
|
@@ -1,464 +0,0 @@
|
|
|
-<?php
|
|
|
-// +----------------------------------------------------------------------
|
|
|
-// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
|
|
|
-// +----------------------------------------------------------------------
|
|
|
-// | 版权所有 2017~2021 LARAVEL研发中心
|
|
|
-// +----------------------------------------------------------------------
|
|
|
-// | 官方网站: http://www.laravel.cn
|
|
|
-// +----------------------------------------------------------------------
|
|
|
-// | Author: laravel开发员 <laravel.qq.com>
|
|
|
-// +----------------------------------------------------------------------
|
|
|
-
|
|
|
-namespace App\Services\Api;
|
|
|
-
|
|
|
-use App\Models\AccountLogModel;
|
|
|
-use App\Models\ActionLogModel;
|
|
|
-use App\Models\DepositModel;
|
|
|
-use App\Models\MemberModel;
|
|
|
-use App\Models\OrderModel;
|
|
|
-use App\Services\BaseService;
|
|
|
-use App\Services\ConfigService;
|
|
|
-use App\Services\PaymentService;
|
|
|
-use App\Services\RedisService;
|
|
|
-use App\Services\SmsService;
|
|
|
-use Illuminate\Support\Facades\DB;
|
|
|
-
|
|
|
-/**
|
|
|
- * 保证金订单管理-服务类
|
|
|
- * @author laravel开发员
|
|
|
- * @since 2020/11/11
|
|
|
- * @package App\Services\Api
|
|
|
- */
|
|
|
-class DepositService extends BaseService
|
|
|
-{
|
|
|
- // 静态对象
|
|
|
- protected static $instance = null;
|
|
|
-
|
|
|
- /**
|
|
|
- * 构造函数
|
|
|
- * @author laravel开发员
|
|
|
- * @since 2020/11/11
|
|
|
- */
|
|
|
- public function __construct()
|
|
|
- {
|
|
|
- $this->model = new DepositModel();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 静态入口
|
|
|
- */
|
|
|
- public static function make()
|
|
|
- {
|
|
|
- if (!self::$instance) {
|
|
|
- self::$instance = new static();
|
|
|
- }
|
|
|
- return self::$instance;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 列表数据
|
|
|
- * @param $params
|
|
|
- * @param int $pageSize
|
|
|
- * @return array
|
|
|
- */
|
|
|
- public function getDataList($params, $pageSize = 15)
|
|
|
- {
|
|
|
- $cacheKey = "caches:deposit:{$pageSize}_" . ($params ? md5(json_encode($params)) : 0);
|
|
|
- $datas = RedisService::get($cacheKey);
|
|
|
- if (empty($datas)) {
|
|
|
- $query = $this->getQuery($params)
|
|
|
- ->orderBy('a.refund_status', 'asc')
|
|
|
- ->orderBy('a.id', 'desc');
|
|
|
-
|
|
|
- $field = ["a.*"];
|
|
|
- $list = $query->select($field)
|
|
|
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
|
|
|
- $list = $list ? $list->toArray() : [];
|
|
|
- if ($list) {
|
|
|
- foreach ($list['data'] as &$item){
|
|
|
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H:i:s') : '';
|
|
|
- $item['time_text'] = $item['create_time']? datetime($item['create_time'],'Y年m月d日') : '';
|
|
|
- }
|
|
|
- $datas = [
|
|
|
- 'pageSize' => $pageSize,
|
|
|
- 'total' => isset($list['total']) ? $list['total'] : 0,
|
|
|
- 'list' => isset($list['data']) ? $list['data'] : []
|
|
|
- ];
|
|
|
-
|
|
|
- RedisService::set($cacheKey, $datas, rand(3, 5));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $datas;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 查询条件
|
|
|
- * @param $params
|
|
|
- * @return mixed
|
|
|
- */
|
|
|
- public function getQuery($params)
|
|
|
- {
|
|
|
- $where = ['a.status' => 0, 'a.mark' => 1];
|
|
|
- $status = isset($params['status']) ? $params['status'] : 0;
|
|
|
- if ($status > 0) {
|
|
|
- $where['a.status'] = $status;
|
|
|
- } else {
|
|
|
- unset($where['a.status']);
|
|
|
- }
|
|
|
-
|
|
|
- $model = $this->model->with(['user'])->from('deposit_orders as a')
|
|
|
- ->leftJoin('member as b','b.id','=','a.user_id')
|
|
|
- ->where($where)
|
|
|
- ->where(function ($query) use ($params) {
|
|
|
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
|
|
|
- $refund = isset($params['refund']) ? intval($params['refund']) : 0;
|
|
|
- if($refund == 1){
|
|
|
- if ($keyword) {
|
|
|
- $query->where('a.refund_no', 'like', "%{$keyword}%");
|
|
|
- }
|
|
|
-
|
|
|
- $query->where(['a.status'=>3])->where('a.refund_status','>',0);
|
|
|
- }else{
|
|
|
- if ($keyword) {
|
|
|
- $query->where('a.order_no', 'like', "%{$keyword}%");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 用户
|
|
|
- $account = isset($params['account']) ? trim($params['account']) : '';
|
|
|
- if ($account) {
|
|
|
- $query->where(function ($query) use ($account) {
|
|
|
- $query->where('b.mobile', 'like', "%{$account}%")
|
|
|
- ->orWhere('b.nickname', 'like', "%{$account}%")
|
|
|
- ->orWhere('b.id', intval($account));
|
|
|
- });
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
|
|
|
- if ($userId>0) {
|
|
|
- $query->where('a.user_id', $userId);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return $model;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 详情信息
|
|
|
- * @param $id
|
|
|
- * @return mixed
|
|
|
- */
|
|
|
- public function getInfo($id)
|
|
|
- {
|
|
|
- $cacheKey = "caches:deposit:info_{$id}";
|
|
|
- $info = RedisService::get($cacheKey);
|
|
|
- if($info){
|
|
|
- return $info;
|
|
|
- }
|
|
|
-
|
|
|
- $info = $this->model->with(['user'])->where(['id' => $id])->first();
|
|
|
- $info = $info? $info->toArray() :[];
|
|
|
- if($info){
|
|
|
- RedisService::set($cacheKey, $info, rand(5,10));
|
|
|
- }
|
|
|
- return $info;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 充值保证金
|
|
|
- * @param $userId
|
|
|
- * @param $params
|
|
|
- * @return array|false
|
|
|
- */
|
|
|
- public function pay($userId, $params)
|
|
|
- {
|
|
|
- // 参数验证
|
|
|
- $deposit = isset($params['money'])? floatval($params['money']) : 0;
|
|
|
- $payType = isset($params['pay_type'])? intval($params['pay_type']) : 0;
|
|
|
- $depositMoney = ConfigService::make()->getConfigByCode('deposit_money',0);
|
|
|
- if($deposit<=0 || $deposit != $depositMoney){
|
|
|
- RedisService::clear("caches:config:app_ios");
|
|
|
- RedisService::clear("caches:config:app_android");
|
|
|
- $this->error = 2054;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if(!in_array($payType,[10,20])){
|
|
|
- $this->error = 2055;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $cacheLockKey = "caches:members:deposit_pay:{$userId}";
|
|
|
- if(RedisService::get($cacheLockKey)){
|
|
|
- $this->error = 1034;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 判断用户账号状态
|
|
|
- RedisService::set($cacheLockKey, ['user_id'=>$userId,'params'=>$params], rand(10,20));
|
|
|
- $userInfo = MemberService::make()->getInfo($userId,[], true);
|
|
|
- $confirmStatus = isset($userInfo['confirm_status'])? $userInfo['confirm_status'] : 0;
|
|
|
- $status = isset($userInfo['status'])? $userInfo['status'] : 0;
|
|
|
- if(empty($userInfo) || $status != 1){
|
|
|
- $this->error = 2016;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 账号审核情况
|
|
|
- if($confirmStatus != 1){
|
|
|
- $this->error = 2042;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 判断用户保证金缴纳情况
|
|
|
- $userDeposit = isset($userInfo['deposit'])? floatval($userInfo['deposit']) : 0;
|
|
|
- $depositTotal = $this->getPayDeposit($userId);
|
|
|
- if($depositTotal>0 && $userDeposit && ($userDeposit >= $depositMoney && $depositTotal >= $depositMoney)){
|
|
|
- $this->error = 2053;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 是否有退保申请
|
|
|
- if($this->model->where(['user_id'=>$userId,'refund_status'=>1,'pay_status'=>2,'status'=>3,'mark'=>1])->value('id')){
|
|
|
- $this->error = 2644;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 创建订单
|
|
|
- $order = [
|
|
|
- 'order_no'=> get_order_num('DE'),
|
|
|
- 'user_id'=> $userId,
|
|
|
- 'money'=> $deposit,
|
|
|
- 'type'=> 1,
|
|
|
- 'pay_type'=> $payType,
|
|
|
- 'pay_status'=> 1,
|
|
|
- 'create_time'=> time(),
|
|
|
- 'status'=> 1,
|
|
|
- 'mark'=>1
|
|
|
- ];
|
|
|
-
|
|
|
- DB::beginTransaction();
|
|
|
- if(!$orderId = $this->model->insertGetId($order)){
|
|
|
- $this->error = 2056;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /* TODO 支付处理 */
|
|
|
- $payOrder = [
|
|
|
- 'type'=> 1,
|
|
|
- 'order_no'=> $order['order_no'],
|
|
|
- 'pay_money'=> $deposit,
|
|
|
- 'body'=> '充值保证金',
|
|
|
- ];
|
|
|
-
|
|
|
- // 调起支付
|
|
|
- switch($payType){
|
|
|
- case 20: // 支付宝
|
|
|
- $payment = PaymentService::make()->aliPay($userInfo, $payOrder,'deposit');
|
|
|
- if(empty($payment)){
|
|
|
- DB::rollBack();
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = PaymentService::make()->getError();
|
|
|
- return false;
|
|
|
- }
|
|
|
- break;
|
|
|
- case 10: // 微信支付
|
|
|
- $payment = PaymentService::make()->wechatPay($userInfo, $payOrder,'deposit');
|
|
|
- if(empty($payment)){
|
|
|
- DB::rollBack();
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = PaymentService::make()->getError();
|
|
|
- return false;
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- $this->error = 2035;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 用户操作记录
|
|
|
- DB::commit();
|
|
|
- $this->error = 2122;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return [
|
|
|
- 'order_id'=> $orderId,
|
|
|
- 'payment'=> $payment,
|
|
|
- 'total'=> $payOrder['pay_money'],
|
|
|
- 'pay_type'=> $payType,
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 实际有效保证金金额
|
|
|
- * @param $userId 用户ID
|
|
|
- * @return array|mixed
|
|
|
- */
|
|
|
- public function getPayDeposit($userId)
|
|
|
- {
|
|
|
- $cacheKey = "caches:members:deposit_check:{$userId}";
|
|
|
- $data = RedisService::get($cacheKey);
|
|
|
- if($data){
|
|
|
- return $data;
|
|
|
- }
|
|
|
-
|
|
|
- $depositTotal = $this->model->where(['type'=>1,'user_id'=> $userId, 'pay_status'=>2,'status'=>3,'mark'=>1])
|
|
|
- ->whereIn('refund_status',[0,3])
|
|
|
- ->sum('money');
|
|
|
- RedisService::set($cacheKey, $depositTotal, rand(5,10));
|
|
|
- return $depositTotal;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 退保申请
|
|
|
- * @param $userId
|
|
|
- * @param $params
|
|
|
- * @return false|int[]
|
|
|
- */
|
|
|
- public function refund($userId, $params)
|
|
|
- {
|
|
|
- // 参数验证
|
|
|
- $depositId = isset($params['id'])? intval($params['id']) : 0;
|
|
|
- $deposit = isset($params['money'])? floatval($params['money']) : 0;
|
|
|
- $remark = isset($params['remark'])? trim($params['remark']) : '';
|
|
|
- $cacheLockKey = "caches:members:deposit_refund:{$userId}";
|
|
|
- if(RedisService::get($cacheLockKey)){
|
|
|
- $this->error = 1034;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 判断用户账号状态
|
|
|
- RedisService::set($cacheLockKey, ['user_id'=>$userId,'params'=>$params], rand(10,20));
|
|
|
- $userInfo = MemberService::make()->getInfo($userId,[], true);
|
|
|
- $confirmStatus = isset($userInfo['confirm_status'])? $userInfo['confirm_status'] : 0;
|
|
|
- $status = isset($userInfo['status'])? $userInfo['status'] : 0;
|
|
|
- if(empty($userInfo) || $status != 1){
|
|
|
- $this->error = 2016;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 账号审核情况
|
|
|
- if($confirmStatus != 1){
|
|
|
- $this->error = 2042;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 验证码验证(不传不验证)
|
|
|
- $mobile = isset($params['mobile'])? trim($params['mobile']) : '';
|
|
|
- $smsCode = isset($params['sms_code'])? trim($params['sms_code']) : '';
|
|
|
- if (!$depositId && isset($params['sms_code']) && !SmsService::make()->check($mobile, $smsCode, 'refund')) {
|
|
|
- $this->error = SmsService::make()->getError();
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 判断用户保证金缴纳情况
|
|
|
- $userDeposit = isset($userInfo['deposit'])? floatval($userInfo['deposit']) : 0;
|
|
|
- if($depositId){
|
|
|
- $depositInfo = $this->model->where(['id'=> $depositId,'type'=>1,'user_id'=> $userId, 'pay_status'=>2,'status'=>3,'mark'=>1])
|
|
|
- ->whereIn('refund_status',[0,3])->orderBy('create_time','desc')->first();
|
|
|
- }else{
|
|
|
- $depositInfo = $this->model->where(['type'=>1,'user_id'=> $userId, 'pay_status'=>2,'status'=>3,'mark'=>1])
|
|
|
- ->whereIn('refund_status',[0,3])->orderBy('create_time','desc')->first();
|
|
|
- $depositId = isset($depositInfo['id'])? $depositInfo['id'] : 0;
|
|
|
- }
|
|
|
-
|
|
|
- $depositInfo = $depositInfo? $depositInfo->toArray() : [];
|
|
|
- $depositTotal = isset($depositInfo['money'])? floatval($depositInfo['money']) : 0;
|
|
|
- if(empty($depositInfo) || $depositId<=0 || $depositTotal<=0 || $userDeposit<=0){
|
|
|
- $this->error = 2057;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if($userDeposit > $depositTotal || $deposit != $userDeposit){
|
|
|
- $this->error = 2058;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 是否有进行中订单
|
|
|
- if(OrderModel::whereIn('status',[1,2])->where(['user_id'=>$userId,'mark'=>1])->value('id')){
|
|
|
- $this->error = 2062;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 退保处理
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
- // 扣除
|
|
|
- $updateData = [
|
|
|
- 'deposit'=> 0,
|
|
|
- 'update_time'=>time()
|
|
|
- ];
|
|
|
- if(!MemberModel::where(['id'=> $userId])->update($updateData)){
|
|
|
- $this->error = 2059;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- DB::rollBack();
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 退款单数据更新
|
|
|
- $depositOrderNo = $depositInfo['pay_type'] == 10? $depositInfo['order_no']: get_order_num('DR');
|
|
|
- $order = [
|
|
|
- 'refund_at'=> date('Y-m-d H:i:s'),
|
|
|
- 'remark'=> $remark,
|
|
|
- 'refund_confirm_remark'=>'',
|
|
|
- 'refund_no'=> $depositOrderNo,
|
|
|
- 'refund_money'=> $deposit,
|
|
|
- 'refund_mobile'=> $mobile,
|
|
|
- 'refund_status'=> 1,
|
|
|
- 'update_time'=>time(),
|
|
|
- ];
|
|
|
-
|
|
|
- if(!$this->model->where(['id'=>$depositId])->update($order)){
|
|
|
- $this->error = 2061;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- DB::rollBack();
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $log = [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'source_order_no' => $depositOrderNo,
|
|
|
- 'type' => 3,
|
|
|
- 'money' => $deposit,
|
|
|
- 'before_money' => $userDeposit,
|
|
|
- 'date'=> date('Y-m-d'),
|
|
|
- 'create_time' => time(),
|
|
|
- 'remark' => '退保申请',
|
|
|
- 'status' => 1,
|
|
|
- 'mark' => 1,
|
|
|
- ];
|
|
|
- if(!AccountLogModel::insertGetId($log)){
|
|
|
- $this->error = 2061;
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- DB::rollBack();
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- $this->error = 2060;
|
|
|
- DB::commit();
|
|
|
-
|
|
|
- // 操作日志
|
|
|
- ActionLogModel::setRecord($userId,['type'=>2,'title'=>'申请退保','content'=>"手机号:{$mobile},退保金额:{$deposit}元,单号:{$depositOrderNo}",'module'=>'deposit']);
|
|
|
- ActionLogModel::record();
|
|
|
-
|
|
|
- RedisService::clear($cacheLockKey);
|
|
|
- RedisService::clear("caches:deposit:info_{$depositId}");
|
|
|
- return ['id'=> $depositId,'order_no'=>$depositOrderNo,'money'=>$userDeposit];
|
|
|
- }
|
|
|
-}
|