| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Models\Mongo\PayLogsModel;
- use App\Models\Mysql\WithDrawalLogModel;
- use App\Repositories\AgentBankInfoRepository;
- use App\Repositories\AgentRepository;
- use App\Repositories\BalanceLogRepository;
- use App\Repositories\Criterias\FinanceRechargeCriteria;
- use App\Repositories\Criterias\FinanceWithdrawCriteria;
- use App\Repositories\Criterias\OrderLimitCriteria;
- use App\Repositories\PayLogRepository;
- use App\Repositories\WithDrawalLogRepository;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Validator;
- use Modules\ManageApi\Responses\CommonSuccessUidResponse;
- use Modules\ManageApi\Responses\ConfigSuccessResponse;
- use Modules\ManageApi\Responses\FinanceRechargePageResponse;
- use Modules\ManageApi\Responses\FinanceWithdrawResponse;
- class FinanceController extends BaseController
- {
- /**
- * 充值记录
- *
- * @return \Illuminate\Http\JsonResponse|FinanceRechargePageResponse
- */
- public function rechargeLists()
- {
- /**
- * @var Validator $validatorFactory
- */
- $validatorFactory = app('validator');
- /**
- * @var PayLogRepository $payRepo
- */
- $payRepo = app(PayLogRepository::class);
- $inputData = $this->getJsonData();
- // 针对layui table自动分页请求参数,优先GET值.
- $queryPage = request()->has('page') ? request()->input('page', 1) : null;
- $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
- if (!is_null($queryPage)) {
- $inputData['page'] = $queryPage;
- }
- if (!is_null($queryPageSize)) {
- $inputData['page_size'] = $queryPageSize;
- }
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'user_uid' => 'nullable|string',
- 'user_name' => 'nullable|string',
- 'pay_type' => 'nullable|string',
- 'order_status' => 'nullable|integer',
- 'mom_app_uid' => 'nullable|string',
- 'order_sn' => 'nullable|string',
- 'start_date' => 'nullable|string',
- 'end_date' => 'nullable|string',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $orders = [
- ['created_at', 'DESC'],
- ];
- if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- } else {
- $pageSize = $inputData['page_size'];
- }
- $page = format_page($inputData['page'] ?? 0);
- $field = ['order_sn', 'user_uid', 'mom_app_uid', 'nick', 'agent_uid', 'real_amount', 'order_status', 'pay_channel', 'order_sn', 'created_at', 'paid_at', 'pay_type'];
- $res = $payRepo->pushCriteria(new FinanceRechargeCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, $field, $page);
- $recTotal = $this->rechargeTotal($inputData, $res);
- return new FinanceRechargePageResponse($res, $recTotal);
- }
- /**
- * 提现列表
- *
- * @return \Illuminate\Http\JsonResponse|FinanceWithdrawResponse
- */
- public function withdrawList()
- {
- /**
- * @var Validator $validatorFactory
- */
- $validatorFactory = app('validator');
- /**
- * @var WithDrawalLogRepository $witRepo
- */
- $witRepo = app(WithDrawalLogRepository::class);
- $inputData = $this->getJsonData();
- // 针对layui table自动分页请求参数,优先GET值.
- $queryPage = request()->has('page') ? request()->input('page', 1) : null;
- $queryPageSize = request()->has('page_size') ? request()->input('page_size', 10) : null;
- if (!is_null($queryPage)) {
- $inputData['page'] = $queryPage;
- }
- if (!is_null($queryPageSize)) {
- $inputData['page_size'] = $queryPageSize;
- }
- $rules = [
- 'page' => 'nullable|integer',
- 'page_size' => 'nullable|integer',
- 'agent_username' => 'nullable|string',
- 'status' => 'nullable|integer',
- 'type' => 'nullable|integer',
- 'start_date' => 'nullable|string',
- 'end_date' => 'nullable|string',
- 'order' => 'nullable|string',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- if (empty($inputData['order'])) {
- $orders = [
- ['id', 'DESC']
- ];
- } else {
- $orderMap = explode('-', $inputData['order']);
- $orders = [[$orderMap[0], $orderMap[1]]];
- }
- if (!isset($inputData['page_size']) || $inputData['page_size'] > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- } else {
- $pageSize = $inputData['page_size'];
- }
- $page = format_page($inputData['page'] ?? 0);
- $res = $witRepo->pushCriteria(new FinanceWithdrawCriteria($inputData))->pushCriteria(new OrderLimitCriteria($orders))->paginate($pageSize, ['*'], $page);
- $withdrawTotal = $this->withdrawTotal($inputData, $res);
- return new FinanceWithdrawResponse($res, $withdrawTotal);
- }
- /**
- * 提现操作
- *
- * @return \Illuminate\Http\JsonResponse|ConfigSuccessResponse
- * @throws \Exception
- */
- public function applyWithdraw()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'order_uid' => 'required|string',
- 'status' => 'required|integer|in:1,3'
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- /**
- * @var WithDrawalLogRepository $witRepo
- */
- $witRepo = app(WithDrawalLogRepository::class);
- /**
- * @var AgentRepository $witRepo
- */
- $agentRepo = app(AgentRepository::class);
- /**
- * @var BalanceLogRepository $bLogRepo
- */
- $bLogRepo = app(BalanceLogRepository::class);
- $witOrder = $witRepo->getOrderFirst($inputData['order_uid']);
- if (empty($witOrder)) {
- return json_fail('订单状态异常');
- }
- $agent = $agentRepo->findBy('uid', $witOrder->agent_uid);
- if (empty($agent)) {
- return json_fail('代理账户不存在');
- }
- $oldAgent = clone $agent;
- //开启事物
- DB::beginTransaction();
- if ($inputData['status'] == 1) { //拒绝
- $agent->balance += $witOrder->amount;
- }
- $agent->frozen -= $witOrder->amount;
- $res = $agent->save();
- //修改订单状态
- $res1 = $witRepo->update(['status' => $inputData['status'], 'paid_at' => date('Y-m-d H:i:s')], $inputData['order_uid'], 'uid');
- if (!$res || !$res1) {
- DB::rollBack();
- return json_fail('操作失败');
- }
- $res2 = $bLogRepo->record($agent, $oldAgent);
- if (!$res2) {
- DB::rollBack();
- return json_fail('帐变写入失败');
- }
- DB::commit();
- return new ConfigSuccessResponse();
- }
- /**
- * 删除提现银行卡
- *
- * @param AgentBankInfoRepository $bankRepo
- * @return \Illuminate\Http\JsonResponse|CommonSuccessUidResponse
- */
- public function withdrawBankDel(AgentBankInfoRepository $bankRepo)
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'uid' => 'required|string',
- ];
- $validator = Validator::make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $bankInfo = $bankRepo->findBy('uid', $inputData['uid']);
- if (empty($bankInfo)) {
- return json_fail(trans('common.err_resource_not_found'));
- }
- $oldBankInfo = clone $bankInfo;
- $result = $bankInfo->delete();
- if (!$result) {
- return json_fail('删除失败');
- } else {
- $logSubject = '删除一个提现银行卡,uid:' . $bankInfo->uid;
- $logSvalue = $bankInfo->uid;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $oldBankInfo->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return new CommonSuccessUidResponse($bankInfo->uid);
- }
- }
- /**
- * 提现统计计算
- *
- * @param $inputData
- * @param $res
- * @return array
- */
- private function withdrawTotal($inputData, $res)
- {
- unset($inputData['page']);
- unset($inputData['page_size']);
- if (empty($inputData)) {
- $witTotal['unpaid_amount'] = WithDrawalLogModel::where('status', '<>', 3)->sum('amount');
- } else {
- $data = $res->toArray()['data'];
- $unpaidAmount = 0;
- foreach ($data as $val) {
- $unpaidAmount += $val['amount'];
- }
- $witTotal['today_amount'] = $unpaidAmount;
- }
- $witTotal['today_amount'] = WithDrawalLogModel::query()->where('status', 3)->whereBetween('created_at', [Carbon::parse(date('Y-m-d') . ' 00:00:00'), Carbon::parse(date('Y-m-d') . ' 23:59:59')])->sum('amount');
- return $witTotal;
- }
- /**
- * 充值统计计算(如果没有查询条件就统计当日的数据)
- *
- * @param $inputData
- * @param $res
- */
- private function rechargeTotal($inputData, $res)
- {
- unset($inputData['page']);
- unset($inputData['page_size']);
- $data = $res->toArray()['data'];
- if (empty($inputData)) {
- $field = ['order_status', 'real_amount'];
- $data = PayLogsModel::query()->whereBetween('created_at', [Carbon::parse(date('Y-m-d') . ' 00:00:00'), Carbon::parse(date('Y-m-d') . ' 23:59:59')])->get($field)->toArray();
- }
- $succInt = 0;
- $orderInt = 0;
- $recTotal['total_money'] = 0;
- $recTotal['succ_money'] = 0;
- foreach ($data as $v) {
- if ($v['order_status'] == 1) {
- $succInt++;
- $recTotal['succ_money'] += $v['real_amount'];
- }
- $recTotal['total_money'] += $v['real_amount'];
- $orderInt ++;
- }
- $recTotal['succ_int'] = $succInt;
- $recTotal['order_int'] = $orderInt;
- // 成功率 = 成功的订单 / 总订单
- if ($succInt == 0) {
- $recTotal['succ_rate'] = 0;
- } else {
- //精度问题 2 / 14 * 100
- $recTotal['succ_rate'] = floor(round($succInt / $orderInt, 2) * 100);
- }
- return $recTotal;
- }
- }
|