// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\AccountLogModel; use App\Models\ActionLogModel; use App\Services\BaseService; use Illuminate\Support\Facades\DB; /** * 账户明细管理-服务类 * @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'] ?? ''; $storeId = $params['store_id'] ?? ''; $date = $params['date'] ?? []; $query = AccountLogModel::with(['member'])->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 ($storeId) { $query->where('a.store_id', $storeId); } // 订单号搜索 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(DB::raw('abs(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'])) { foreach ($list['data'] as &$item) { $item['create_time'] = datetime($item['create_time'],'Y-m-d H:i:s'); } } return [ 'msg' => '操作成功', 'code' => 0, 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'counts' => $counts, 'data' => isset($list['data']) ? $list['data'] : [], 'count' => isset($list['total']) ? $list['total'] : 0 ]; } /** * 查询 * @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); } $storeId = isset($params['store_id']) ? $params['store_id'] : 0; if ($storeId) { $query->where('a.store_id', $storeId); } 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); } $storeId = isset($params['store_id']) ? $params['store_id'] : 0; if ($storeId) { $query->where('a.store_id', $storeId); } // 订单号搜索 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(); // 格式化数据 foreach ($list as &$item) { $item['create_time_text'] = date('Y-m-d H:i:s', (int)$item['create_time']); } 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['update_time_text'] = date('Y-m-d H:i:s', (int)$info['update_time']); 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), 'after_money' => (float)($params['after_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['after_money'])) { $log->before_money = (float)$params['after_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' => '删除成功']; } } }