// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AccountLogModel; use App\Models\MemberModel; use App\Models\PayMealsModel; use App\Models\PayOrdersModel; use App\Services\BaseService; use App\Services\PaymentService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; use PhpOffice\PhpSpreadsheet\Calculation\Statistical\Distributions\F; /** * 交易管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class AccountService */ class AccountService extends BaseService { public static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * AccountService constructor. */ public function __construct() { $this->model = new AccountLogModel(); } /** * 静态入口 * @return static|null */ 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) { $query = $this->getQuery($params); $list = $query->select(['a.*']) ->orderBy('a.create_time','desc') ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ $accountTypes = config('payment.accountTypes'); 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']? dateFormat($item['create_time'],'Y年m月d日') : ''; $type = isset($item['type'])? intval($item['type']) : 0; $item['type_text'] = isset($item['remark'])? trim($item['remark']) : ''; if(empty($item['type_text'])){ $item['type_text'] = isset($accountTypes[$type])? $accountTypes[$type] : '收支明细'; } $item['change_type'] = 1; if(in_array($type,[1,2,4])){ $item['change_type'] = 2; } } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 充值记录 * @param $params * @param int $pageSize * @return array */ public function getPayLog($params, $pageSize = 15) { $userId = isset($params['user_id'])?$params['user_id'] : 0; $list = PayOrdersModel::from('pay_orders as a') ->where(function($query){ $query->where('a.status','>',1)->orWhere(function($query){ $query->where('a.status',1)->orWhere('a.create_time','>=', time() - 300); }); }) ->where(['user_id'=> $userId,'mark'=>1]) ->select(['a.*']) ->orderBy('a.create_time','desc') ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list? $list->toArray() :[]; if($list){ $types = [1=>'话费充值',2=>'电费充值',3=>'燃气充值']; $statusArr = [1=>'取消支付',2=>'已支付',3=>'充值中',4=>'充值成功',5=>'充值失败资金原路退回',6=>'充值成功部分其他原路退回']; foreach($list['data'] as &$item){ $item['time_text'] = $item['create_time']? datetime($item['create_time'],'Y/m/d H:i:s') : ''; $type = isset($item['type'])? intval($item['type']) : 0; $status = isset($item['status'])? intval($item['status']) : 0; $item['type_text'] = isset($types[$type])? $types[$type] : '充值'; $item['status_text'] = isset($statusArr[$status])? $statusArr[$status] : '充值中'; if($status != 4 && $item['failed_remark']){ $item['status_text'] = $item['failed_remark']; } } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } public function getQuery($params) { $where = ['a.mark' => 1]; $type = isset($params['type'])? $params['type'] : 0; if($type>0){ $where['a.type'] = $type; } return $this->model->with(['member'])->from("account_logs as a") ->leftJoin('member as b','b.id','=','a.user_id') ->where($where) ->where(function ($query) use($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; $userId = isset($params['user_id'])? $params['user_id'] : 0; if($userId){ $query->where('a.user_id',$userId); } $status = isset($params['status'])? $params['status'] : 0; if($status){ $query->where('a.status',$status); }else{ $query->whereNotIn('a.status',[2]); } if ($keyword) { $query->where(function($query) use($keyword){ $query->where('b.nickname','like',"%{$keyword}%") ->orWhere('b.mobile','like',"%{$keyword}%") ->orWhere('b.realname','like',"%{$keyword}%"); }); } $orderNo = isset($params['order_no'])? trim($params['order_no']) : ''; if($orderNo){ $query->where(function($query) use($orderNo){ $query->where('a.source_order_no','like',"%{$orderNo}%"); }); } }) ->where(function ($query) use($params){ // 日期 $date = isset($params['date']) ? $params['date'] : []; $start = isset($date[0])? $date[0] : ''; $end = isset($date[1])? $date[1] : ''; $end = $start>=$end? '' : $end; if ($start) { $query->where('a.create_time','>=', strtotime($start)); } if($end){ $query->where('a.create_time','<=', strtotime($end)); } }); } /** * 今日金额统计数据 * @param $userId * @param int $type * @return array|mixed */ public function getTotalByDay($userId, $type=1) { $cacheKey = "caches:accounts:total_{$userId}_{$type}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model->where(['user_id'=> $userId,'type'=>$type,'status'=>1,'mark'=>1]) ->where('create_time','>=', strtotime(date('Y-m-d'))) ->sum('money'); if($data){ RedisService::set($cacheKey, $data, rand(5,10)); } return $data; } /** * 充值套餐 * @param $type * @return array|mixed */ public function getPayMealList($type) { $cacheKey = "caches:accounts:mealList_{$type}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = PayMealsModel::where(['type'=>$type,'status'=>1,'mark'=>1]) ->orderBy('sort','desc') ->orderBy('id','asc') ->get(); $datas = $datas? $datas->toArray() :[]; if($datas){ RedisService::set($cacheKey, $datas, rand(300,600)); } return $datas; } /** * 生活充值 * @param $userId 用户ID * @param $params * @return array|false */ public function recharge($userId, $params) { $mealId = isset($params['id']) ? $params['id'] : 0; // 套餐ID $account = isset($params['account']) ? $params['account'] : ''; // 充值账号/手机号 $cacheKey = "caches:members:pay:{$userId}_{$mealId}"; if (RedisService::get($cacheKey . '_lock')) { $this->error = '请不要频繁提交~'; return false; } if(empty($account)){ $this->error = '请输入充值账号'; return false; } RedisService::set($cacheKey, ['date' => date('Y-m-d H:i:s')], rand(2, 3)); $mealInfo = PayMealsModel::where(['id' => $mealId, 'status' => 1, 'mark' => 1]) ->select(['id','product_id', 'money', 'type', 'discount', 'remark', 'status']) ->first(); $money = isset($mealInfo['money']) ? floatval($mealInfo['money']) : 0; $discount = isset($mealInfo['discount']) ? intval($mealInfo['discount']) : 0; $mealType = isset($mealInfo['type']) && $mealInfo['type'] ? intval($mealInfo['type']) : 1; $productId= isset($mealInfo['product_id']) ? $mealInfo['product_id'] : 0; $remark= isset($mealInfo['remark']) ? $mealInfo['remark'] : ''; $price = $discount?moneyFormat($money*$discount/100,2): $money; if (empty($mealInfo)) { $this->error = '该套餐不存在'; RedisService::clear($cacheKey . '_lock'); return false; } if ($price <= 0 || $money <= 0 || $productId<=0) { $this->error = '该套餐参数错误'; RedisService::clear($cacheKey . '_lock'); return false; } $info = MemberModel::where(['id' => $userId, 'mark' => 1]) ->select(['id', 'openid', 'mobile', 'status']) ->first(); $openid = isset($info['openid']) ? $info['openid'] : ''; if (!$info || $info['status'] != 1) { $this->error = 1045; RedisService::clear($cacheKey . '_lock'); return false; } if (empty($openid)) { $this->error = '用户参数错误,请重新授权登录后尝试~'; RedisService::clear($cacheKey . '_lock'); return false; } // 创建订单 $orderNo = get_order_num('PR'); $types = ['','话费充值','电费充值','燃气充值']; $remark = isset($types[$mealType])? $types[$mealType] : '生活充值'; $order = [ 'order_no' => $orderNo, 'user_id' => $userId, 'meal_id' => $mealId, 'product_id' => $productId, 'total' => $price, 'type' => $mealType, 'account' => $account, 'discount' => $discount, 'create_time' => time(), 'remark' => $remark, 'status' => 1, 'mark' => 1 ]; DB::beginTransaction(); if (!$orderId = PayOrdersModel::insertGetId($order)) { $this->error = '创建充值订单失败'; return false; } /* TODO 支付处理 */ $payOrder = [ 'type' => 1, 'order_no' => $orderNo, 'pay_money' => $price, 'body' => $remark, 'openid' => $openid ]; // 调起支付 $payment = PaymentService::make()->minPay($info, $payOrder, 'pay'); if (empty($payment)) { DB::rollBack(); RedisService::clear($cacheKey . '_lock'); $this->error = PaymentService::make()->getError(); return false; } // 用户操作记录 DB::commit(); $this->error = '创建充值订单成功,请前往支付~'; // 删除超时订单 PayOrdersModel::where(['status'=>1])->where('create_time','<=', time() - 6 * 3600)->delete(); RedisService::clear($cacheKey . '_lock'); return [ 'order_id' => $orderId, 'payment' => $payment, 'total' => $payOrder['pay_money'], 'pay_type' => 10, ]; } }