| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <?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\Services\BaseService;
- /**
- * 账户明细管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class AccountService
- * @package App\Services\Common
- */
- 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)
- {
- $status = $params['status'] ?? '';
- $type = $params['type'] ?? '';
- $keyword = $params['keyword'] ?? '';
- $orderNo = $params['order_no'] ?? '';
- $userId = $params['user_id'] ?? '';
- $date = $params['date'] ?? [];
- $query = AccountLogModel::from('account_logs as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where('a.mark', 1);
- // 状态筛选
- if ($status !== '' && $status != 0) {
- $query->where('a.status', $status);
- }
- // 类型筛选
- if ($type !== '' && $type != 0) {
- $query->where('a.type', $type);
- }
- // 用户ID筛选
- if ($userId) {
- $query->where('a.user_id', $userId);
- }
- // 订单号搜索
- if (!empty($orderNo)) {
- $query->where('a.source_order_no', 'like', "%{$orderNo}%");
- }
- // 关键词搜索(用户昵称/手机号)
- if (!empty($keyword)) {
- $query->where(function ($q) use ($keyword) {
- $q->where('b.nickname', 'like', "%{$keyword}%")
- ->orWhere('b.mobile', 'like', "%{$keyword}%");
- });
- }
- // 日期筛选
- if (!empty($date) && is_array($date)) {
- $start = $date[0] ?? '';
- $end = $date[1] ?? '';
- if ($start) {
- $query->where('a.create_time', '>=', strtotime($start));
- }
- if ($end && $start < $end) {
- $query->where('a.create_time', '<=', strtotime($end));
- }
- }
- // 统计数据
- $model = clone $query;
- $counts = [
- 'count' => $model->count('a.id'),
- 'total' => $model->sum('a.money')
- ];
- // 分页查询
- $list = $query->select(['a.*', 'b.mobile', 'b.nickname'])
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
-
- $list = $list ? $list->toArray() : [];
- // 格式化数据
- if ($list && isset($list['data'])) {
- $accountTypes = config('payment.accountTypes', [
- 1 => '商城消费',
- 2 => '充值缴费',
- 3 => '商城退款',
- 4 => '佣金提现',
- 5 => '提现驳回',
- 6 => '平台入款'
- ]);
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
- $item['create_time_text'] = date('Y-m-d H:i:s', (int)$item['create_time']);
- $type = isset($item['type']) ? intval($item['type']) : 0;
- $item['type_text'] = $item['remark'] ?: ($accountTypes[$type] ?? '收支明细');
- $item['money'] = number_format($item['money'], 2, '.', '');
- $item['before_money'] = number_format($item['before_money'], 2, '.', '');
- $item['status_text'] = ['', '已完成', '待处理', '失败/取消'][$item['status']] ?? '未知';
- }
- }
- 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];
- $status = isset($params['status']) ? $params['status'] : 0;
- $type = isset($params['type']) ? $params['type'] : 0;
- if ($status > 0) {
- $where['a.status'] = $status;
- }
- 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);
- }
- 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.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));
- }
- });
- }
- /**
- * 获取列表
- */
- public function getList()
- {
- $params = request()->all();
- $page = $params['page'] ?? 1;
- $limit = $params['limit'] ?? 20;
- $status = $params['status'] ?? '';
- $type = $params['type'] ?? '';
- $keyword = $params['keyword'] ?? '';
- $orderNo = $params['order_no'] ?? '';
- $userId = $params['user_id'] ?? '';
- $date = $params['date'] ?? [];
- $query = AccountLogModel::from('account_logs as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where('a.mark', 1);
- // 状态筛选
- if ($status !== '' && $status != 0) {
- $query->where('a.status', $status);
- }
- // 类型筛选
- if ($type !== '' && $type != 0) {
- $query->where('a.type', $type);
- }
- // 用户ID筛选
- if ($userId) {
- $query->where('a.user_id', $userId);
- }
- // 订单号搜索
- if (!empty($orderNo)) {
- $query->where('a.source_order_no', 'like', "%{$orderNo}%");
- }
- // 关键词搜索(用户昵称/手机号)
- if (!empty($keyword)) {
- $query->where(function ($q) use ($keyword) {
- $q->where('b.nickname', 'like', "%{$keyword}%")
- ->orWhere('b.mobile', 'like', "%{$keyword}%");
- });
- }
- // 日期筛选
- if (!empty($date) && is_array($date)) {
- $start = $date[0] ?? '';
- $end = $date[1] ?? '';
- if ($start) {
- $query->where('a.create_time', '>=', strtotime($start));
- }
- if ($end && $start < $end) {
- $query->where('a.create_time', '<=', strtotime($end));
- }
- }
- // 统计数据
- $counts = [
- 'count' => $query->count('a.id'),
- 'total' => $query->sum('a.money')
- ];
- $total = $counts['count'];
- $list = $query->select(['a.*', 'b.mobile', 'b.nickname'])
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc')
- ->offset(($page - 1) * $limit)
- ->limit($limit)
- ->get()
- ->toArray();
- // 格式化数据
- $accountTypes = config('payment.accountTypes', [
- 1 => '商城消费',
- 2 => '充值缴费',
- 3 => '商城退款',
- 4 => '佣金提现',
- 5 => '提现驳回',
- 6 => '平台入款'
- ]);
- foreach ($list as &$item) {
- $item['create_time_text'] = date('Y-m-d H:i:s', (int)$item['create_time']);
- $item['type_text'] = $item['remark'] ?: ($accountTypes[$item['type']] ?? '收支明细');
- $item['money'] = number_format($item['money'], 2, '.', '');
- $item['before_money'] = number_format($item['before_money'], 2, '.', '');
- $item['status_text'] = ['', '已完成', '待处理', '失败/取消'][$item['status']] ?? '未知';
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list,
- 'count' => $total,
- 'counts' => $counts
- ];
- }
- /**
- * 获取详情
- */
- public function getInfo($id = null)
- {
- if ($id === null) {
- $id = request()->input('id');
- }
- $info = AccountLogModel::from('account_logs as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where('a.id', $id)
- ->where('a.mark', 1)
- ->select(['a.*', 'b.mobile', 'b.nickname'])
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $info = $info->toArray();
- $info['create_time_text'] = date('Y-m-d H:i:s', (int)$info['create_time']);
- $info['money'] = number_format($info['money'], 2, '.', '');
- $info['before_money'] = number_format($info['before_money'], 2, '.', '');
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $info
- ];
- }
- /**
- * 添加账户明细
- */
- public function add()
- {
- $params = request()->all();
- $data = [
- 'user_id' => (int)($params['user_id'] ?? 0),
- 'source_order_no' => $params['source_order_no'] ?? '',
- 'type' => (int)($params['type'] ?? 1),
- 'money' => (float)($params['money'] ?? 0),
- 'before_money' => (float)($params['before_money'] ?? 0),
- 'remark' => $params['remark'] ?? '',
- 'status' => (int)($params['status'] ?? 1),
- 'create_time' => time(),
- 'update_time' => time(),
- 'mark' => 1
- ];
- $result = AccountLogModel::insert($data);
- if ($result) {
- return ['code' => 0, 'msg' => '添加成功'];
- }
- return ['code' => 1, 'msg' => '添加失败'];
- }
- /**
- * 编辑账户明细
- */
- public function edit()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $log = AccountLogModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$log) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- if (isset($params['user_id'])) {
- $log->user_id = (int)$params['user_id'];
- }
- if (isset($params['source_order_no'])) {
- $log->source_order_no = $params['source_order_no'];
- }
- if (isset($params['type'])) {
- $log->type = (int)$params['type'];
- }
- if (isset($params['money'])) {
- $log->money = (float)$params['money'];
- }
- if (isset($params['before_money'])) {
- $log->before_money = (float)$params['before_money'];
- }
- if (isset($params['remark'])) {
- $log->remark = $params['remark'];
- }
- if (isset($params['status'])) {
- $log->status = (int)$params['status'];
- }
- $log->update_time = time();
- $log->save();
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => '修改账户明细', 'content' => json_encode($params, 256), 'module' => 'admin']);
- ActionLogModel::record();
- return ['code' => 0, 'msg' => '修改成功'];
- }
- /**
- * 设置状态
- */
- public function status()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $status = $params['status'] ?? 1;
- $log = AccountLogModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$log) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $log->status = $status;
- $log->update_time = time();
- $log->save();
- return ['code' => 0, 'msg' => '设置成功'];
- }
- /**
- * 删除
- * @return array
- */
- public function delete()
- {
- $id = request()->input('id');
- if (is_array($id)) {
- // 批量删除
- $count = AccountLogModel::whereIn('id', $id)
- ->where('mark', 1)
- ->update(['mark' => 0, 'update_time' => time()]);
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "批量删除账户明细", 'content' => json_encode($id, 256), 'module' => 'admin']);
- ActionLogModel::record();
- return ['code' => 0, 'msg' => "成功删除{$count}条记录"];
- } else {
- // 单个删除
- $log = AccountLogModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$log) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $log->mark = 0;
- $log->update_time = time();
- $log->save();
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除账户明细", 'content' => json_encode(['id' => $id], 256), 'module' => 'admin']);
- ActionLogModel::record();
- return ['code' => 0, 'msg' => '删除成功'];
- }
- }
- }
|