| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AccountLogModel;
- use App\Models\ActionLogModel;
- use App\Models\BalanceLogModel;
- use App\Models\MemberModel;
- use App\Models\MessageModel;
- use App\Models\PtAccountModel;
- use App\Services\BaseService;
- use App\Services\PaymentService;
- use App\Services\RedisService;
- use Illuminate\Support\Facades\DB;
- /**
- * 余额管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class BalanceLogService extends BaseService
- {
- public static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * AccountService constructor.
- */
- public function __construct()
- {
- $this->model = new BalanceLogModel();
- }
- /**
- * 静态入口
- * @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);
- $model = clone $query;
- $counts = [
- 'count' => $model->count('a.id'),
- 'total' => $model->sum('a.money'),
- ];
- $list = $query->select(['a.*'])
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc')
- ->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['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
- $item['member'] = isset($item['member']) ? $item['member'] : [];
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'counts' => $counts,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- $type = isset($params['type']) ? $params['type'] : 0;
- if ($type > 0) {
- $where['a.type'] = $type;
- }
- $accountType = isset($params['account_type']) ? $params['account_type'] : 0;
- if ($accountType > 0) {
- $where['a.account_type'] = $accountType;
- }
- return $this->model->with(['member'])->from("balance_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);
- }
- if ($keyword) {
- $query->where(function ($query) use ($keyword) {
- $query->where('b.nickname', 'like', "%{$keyword}%")
- ->orWhere('b.mobile', 'like', "%{$keyword}%");
- });
- }
- $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
- if ($orderNo) {
- $query->where(function ($query) use ($orderNo) {
- $query->where('a.order_no', 'like', "%{$orderNo}%");
- });
- }
- $status = isset($params['status']) ? $params['status'] : 0;
- if ($status == 0) {
- $query->whereIn('a.status', [2, 4]);
- } else if ($status < 0) {
- $query->whereIn('a.status', [1, 3]);
- } else if ($status > 0) {
- $query->where('a.status', $status);
- }
- })
- ->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));
- }
- });
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 审核
- * @param $adminId
- * @param $params
- * @return array|false
- */
- public function confirm($adminId, $params)
- {
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $status = isset($params['status']) ? intval($params['status']) : 0;
- $payStatus = isset($params['pay_status']) ? intval($params['pay_status']) : 0;
- $confirmRemark = isset($params['confirm_remark']) ? trim($params['confirm_remark']) : '';
- $payImg = isset($params['pay_img']) ? trim($params['pay_img']) : '';
- $payImg = $payImg ? get_image_path($payImg) : '';
- $info = $this->model->with(['member'])->where(['id' => $id, 'mark' => 1])->first();
- $userInfo = isset($info['member']) ? $info['member'] : [];
- $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
- $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
- $orderStatus = isset($info['status']) ? $info['status'] : 0;
- $accountType = isset($info['account_type']) ? $info['account_type'] : 0;
- $payType = isset($info['pay_type']) ? $info['pay_type'] : 10;
- $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
- $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
- $money = isset($info['money']) ? $info['money'] : 0;
- $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
- $fields = [1 => 'balance', 2 => 'property', 4 => 'ls_score'];
- $field = isset($fields[$accountType]) ? $fields[$accountType] : 'balance';
- $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
- if (empty($info) || empty($userInfo) || $orderUserId <= 0 || $money <= 0) {
- $this->error = '提现信息不存在或参数错误';
- return false;
- }
- if ($orderStatus != 1) {
- $this->error = '提现订单状态不可操作';
- return false;
- }
- if (!in_array($status, [2, 3])) {
- $this->error = '审核状态错误';
- return false;
- }
- if ($status == 3 && empty($confirmRemark)) {
- $this->error = '请填写审核驳回备注';
- return false;
- }
- DB::beginTransaction();
- $status = $payStatus == 20 && $status == 2 && $payType!=10 ? 4 : $status;
- $updateData = ['status' => $status, 'pay_status' => $payStatus, 'pay_img' => $payImg, 'confirm_admin_id' => $adminId, 'update_time' => time(), 'confirm_remark' => $confirmRemark];
- if (!$this->model->where(['id' => $id])->update($updateData)) {
- DB::rollBack();
- $this->error = '提现审核失败';
- return false;
- }
- // 如果驳回
- if ($status == 3) {
- $updateData = ["{$field}" => DB::raw("{$field} + {$money}"), 'update_time' => time()];
- if (!MemberModel::where(['id' => $orderUserId])->update($updateData)) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- $log = [
- 'user_id' => $orderUserId,
- 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
- 'account_type' => $accountType,
- 'user_type' => isset($info['user_type']) ? $info['user_type'] : 1,
- 'type' => 5,
- 'money' => $money,
- 'after_money' => moneyFormat($balance + $money, 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => '提现驳回',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- } else if ($status == 2) {
- // 微信打款
- $result = [];
- if ($payStatus == 20) {
- $order = [
- 'order_no' => $orderNo,
- 'pay_money' => $actualMoney,
- 'account' => $openid,
- 'real_name' => $realname,
- 'body' => ['账户提现', '余额提现', '数字资产提现', '报单积分提现', '绿色积分提现'][$accountType],
- ];
- if (!$result = PaymentService::make()->transfer($order)) {
- DB::rollBack();
- $this->error = PaymentService::make()->getError();
- return false;
- }
- $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
- $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
- $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
- $updateData = ['pay_status' => 20, 'pay_at' => date('Y-m-d H:i:s'), 'package_info' => $packageInfo, 'mch_id' => $mchId, 'batch_id' => $batchId, 'receive_status' => 2, 'update_time' => time()];
- if (!$this->model->where(['id' => $id])->update($updateData)) {
- DB::rollBack();
- $this->error = '提现打款处理失败';
- return false;
- }
- // 数字资产底池和平台运营账户入账
- $poolMoney = isset($info['pool_money']) ? $info['pool_money'] : 0;
- $ptMoney = isset($info['pt_money']) ? $info['pt_money'] : 0;
- if ($accountType == 2) {
- $poolInfo = PtAccountModel::where(['mark' => 1])->first();
- $poolTotal = isset($poolInfo['pool_total']) ? $poolInfo['pool_total'] : 0;
- $poolBalance = isset($poolInfo['balance']) ? $poolInfo['balance'] : 0;
- $poolInfo->today_pool += $poolMoney;
- $poolInfo->pool_total += $poolMoney;
- $poolInfo->balance += $ptMoney;
- $poolInfo->update_time = time();
- if (!$poolInfo->save()) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- $log = [
- 'user_id' => 0,
- 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
- 'account_type' => 5,
- 'user_type' => 0,
- 'type' => 10,
- 'money' => $poolMoney,
- 'after_money' => moneyFormat($poolTotal + $poolMoney, 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => '用户资产提现回流平台底池',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- $log = [
- 'user_id' => 0,
- 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
- 'account_type' => 5,
- 'user_type' => 0,
- 'type' => 11,
- 'money' => $ptMoney,
- 'after_money' => moneyFormat($poolBalance + $ptMoney, 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => '用户资产提现回流运营账户',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- }
- }
- }
- DB::commit();
- $this->error = '提现审核成功';
- return ['id' => $id, 'money' => $money, 'status' => $status];
- }
- /**
- * 打款
- * @param $adminId
- * @param $params
- * @return array|false
- */
- public function payment($adminId, $params)
- {
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $payStatus = isset($params['pay_status']) ? intval($params['pay_status']) : 0;
- $payImg = isset($params['pay_img']) ? trim($params['pay_img']) : '';
- $payImg = $payImg ? get_image_path($payImg) : '';
- $info = $this->model->with(['member'])->where(['id' => $id, 'mark' => 1])->first();
- $userInfo = isset($info['member']) ? $info['member'] : [];
- $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
- $realname = isset($userInfo['realname']) ? $userInfo['realname'] : '';
- $orderStatus = isset($info['status']) ? $info['status'] : 0;
- $accountType = isset($info['account_type']) ? $info['account_type'] : 0;
- $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
- $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
- $money = isset($info['money']) ? $info['money'] : 0;
- $actualMoney = isset($info['actual_money']) ? $info['actual_money'] : 0;
- $fields = [1 => 'balance', 2 => 'property', 4 => 'ls_score'];
- $field = isset($fields[$accountType]) ? $fields[$accountType] : 'balance';
- $balance = isset($userInfo[$field]) ? $userInfo[$field] : 0;
- if (empty($info) || empty($userInfo) || $orderUserId <= 0 || $money <= 0) {
- $this->error = '提现信息不存在或参数错误';
- return false;
- }
- if ($orderStatus != 2) {
- $this->error = '提现订单状态不可操作';
- return false;
- }
- if ($payStatus != 20) {
- $this->error = '请选择已打款';
- return false;
- }
- DB::beginTransaction();
- $updateData = ['pay_status' => $payStatus, 'pay_img' => $payImg, 'update_time' => time()];
- if ($payStatus == 20) {
- $updateData['status'] = 4;
- }
- if (!$this->model->where(['id' => $id])->update($updateData)) {
- DB::rollBack();
- $this->error = '提现打款处理失败';
- return false;
- }
- // 线上打款逻辑
- $order = [
- 'order_no' => $orderNo,
- 'pay_money' => $actualMoney,
- 'account' => $openid,
- 'real_name' => $realname,
- 'body' => ['账户提现', '余额提现', '数字资产提现', '报单积分提现', '绿色积分提现'][$accountType],
- ];
- if (!$result = PaymentService::make()->transfer($order)) {
- DB::rollBack();
- $this->error = PaymentService::make()->getError();
- return false;
- }
- $batchId = isset($result['batch_id']) ? $result['batch_id'] : '';
- $mchId = isset($result['mch_id']) ? $result['mch_id'] : '';
- $packageInfo = isset($result['package_info']) ? $result['package_info'] : '';
- $updateData = ['pay_status' => 20, 'pay_at' => date('Y-m-d H:i:s'), 'package_info' => $packageInfo, 'mch_id' => $mchId, 'batch_id' => $batchId, 'receive_status' => 2, 'update_time' => time()];
- if (!$this->model->where(['id' => $id])->update($updateData)) {
- DB::rollBack();
- $this->error = '提现打款处理失败';
- return false;
- }
- // 数字资产底池和平台运营账户入账
- $poolMoney = isset($info['pool_money']) ? $info['pool_money'] : 0;
- $ptMoney = isset($info['pt_money']) ? $info['pt_money'] : 0;
- if ($accountType == 2) {
- $poolInfo = PtAccountModel::where(['mark' => 1])->first();
- $poolTotal = isset($poolInfo['pool_total']) ? $poolInfo['pool_total'] : 0;
- $poolBalance = isset($poolInfo['balance']) ? $poolInfo['balance'] : 0;
- $poolInfo->today_pool += $poolMoney;
- $poolInfo->pool_total += $poolMoney;
- $poolInfo->balance += $ptMoney;
- $poolInfo->update_time = time();
- if (!$poolInfo->save()) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- $log = [
- 'user_id' => 0,
- 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
- 'account_type' => 5,
- 'user_type' => 0,
- 'type' => 10,
- 'money' => $poolMoney,
- 'after_money' => moneyFormat($poolTotal + $poolMoney, 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => '用户资产提现回流平台底池',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- $log = [
- 'user_id' => 0,
- 'source_order_no' => isset($info['order_no']) ? $info['order_no'] : '',
- 'account_type' => 5,
- 'user_type' => 0,
- 'type' => 11,
- 'money' => $ptMoney,
- 'after_money' => moneyFormat($poolBalance + $ptMoney, 2),
- 'date' => date('Y-m-d'),
- 'create_time' => time(),
- 'remark' => '用户资产提现回流运营账户',
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!AccountLogModel::insertGetId($log)) {
- DB::rollBack();
- $this->error = '提现审核处理失败';
- return false;
- }
- }
- DB::commit();
- $this->error = '在线打款处理成功';
- return ['id' => $id, 'money' => $money, 'status' => $payStatus];
- }
- /**
- * 删除
- * @return array
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除余额明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete(); // TODO: Change the autogenerated stub
- }
- /*\提现统计
- * @param int $type
- * @return array|mixed
- */
- public function getTotal($type = 1)
- {
- $cacheKey = "caches:balances:total_{$type}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $data = $this->model->where(['mark' => 1])
- ->where(function ($query) use ($type) {
- if ($type == 1) {
- $query->where(['type' => 2])->whereIn('status', [2, 4]);
- } else {
- $query->where(['type' => 2]);
- }
- })->sum('money');
- if ($data) {
- RedisService::set($cacheKey, $data, rand(300, 600));
- }
- return $data;
- }
- }
|