| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- namespace App\Services\Common;
- use App\Models\PayMealsModel;
- use App\Services\BaseService;
- /**
- * 缴费充值套餐服务
- */
- class PayMealsService extends BaseService
- {
- /**
- * 获取列表
- */
- public function getList()
- {
- $params = request()->all();
-
- $page = $params['page'] ?? 1;
- $limit = $params['limit'] ?? 20;
- $type = $params['type'] ?? '';
- $status = $params['status'] ?? '';
- $keyword = $params['keyword'] ?? '';
- $query = PayMealsModel::where('mark', 1);
- // 类型筛选
- if ($type !== '') {
- $query->where('type', $type);
- }
- // 状态筛选
- if ($status !== '') {
- $query->where('status', $status);
- }
- // 关键词搜索
- if (!empty($keyword)) {
- $query->where(function($q) use ($keyword) {
- $q->where('money', 'like', "%{$keyword}%")
- ->orWhere('remark', 'like', "%{$keyword}%")
- ->orWhere('product_id', 'like', "%{$keyword}%");
- });
- }
- $total = $query->count();
- $list = $query->orderBy('sort', 'desc')
- ->orderBy('id', 'desc')
- ->offset(($page - 1) * $limit)
- ->limit($limit)
- ->get();
- // 格式化数据
- $result = [];
- foreach ($list as $item) {
- $data = $item->toArray();
- $data['money'] = number_format($data['money'], 2, '.', '');
- $data['discount'] = number_format($data['discount'], 2, '.', '');
- // 计算实付金额
- if ($data['discount'] > 0) {
- $data['pay_money'] = number_format($data['money'] * $data['discount'] / 100, 2, '.', '');
- } else {
- $data['pay_money'] = $data['money'];
- }
- $data['create_time_text'] = date('Y-m-d H:i:s', (int)$data['create_time']);
- $data['update_time_text'] = date('Y-m-d H:i:s', (int)$data['update_time']);
- $result[] = $data;
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $result,
- 'count' => $total
- ];
- }
- /**
- * 获取详情
- */
- public function getInfo($id = null)
- {
- if ($id === null) {
- $id = request()->input('id');
- }
-
- $info = PayMealsModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$info) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $info = $info->toArray();
- $info['money'] = number_format($info['money'], 2, '.', '');
- $info['discount'] = number_format($info['discount'], 2, '.', '');
- // 计算实付金额
- if ($info['discount'] > 0) {
- $info['pay_money'] = number_format($info['money'] * $info['discount'] / 100, 2, '.', '');
- } else {
- $info['pay_money'] = $info['money'];
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $info
- ];
- }
- /**
- * 添加
- */
- public function add()
- {
- $params = request()->all();
- $data = [
- 'product_id' => (int)($params['product_id'] ?? 0),
- 'money' => (float)($params['money'] ?? 0),
- 'discount' => (float)($params['discount'] ?? 0),
- 'remark' => (string)($params['remark'] ?? ''),
- 'type' => (int)($params['type'] ?? 1),
- 'sort' => (int)($params['sort'] ?? 0),
- 'status' => (int)($params['status'] ?? 1),
- 'create_time' => time(),
- 'update_time' => time(),
- 'mark' => 1
- ];
- $result = PayMealsModel::insert($data);
- if ($result) {
- return ['code' => 0, 'msg' => '添加成功'];
- }
- return ['code' => 1, 'msg' => '添加失败'];
- }
- /**
- * 编辑
- */
- public function edit()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $meal = PayMealsModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$meal) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $meal->product_id = $params['product_id'] ?? $meal->product_id;
- $meal->money = $params['money'] ?? $meal->money;
- $meal->discount = $params['discount'] ?? $meal->discount;
- $meal->remark = $params['remark'] ?? $meal->remark;
- $meal->sort = $params['sort'] ?? $meal->sort;
- $meal->status = $params['status'] ?? $meal->status;
- $meal->update_time = time();
- $meal->save();
- return ['code' => 0, 'msg' => '修改成功'];
- }
- /**
- * 删除
- */
- public function delete()
- {
- $id = request()->input('id');
-
- $meal = PayMealsModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$meal) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $meal->mark = 0;
- $meal->save();
- return ['code' => 0, 'msg' => '删除成功'];
- }
- /**
- * 批量删除
- */
- public function deleteAll($ids = null)
- {
- if ($ids === null) {
- $ids = request()->input('ids', []);
- }
-
- $count = PayMealsModel::whereIn('id', $ids)
- ->where('mark', 1)
- ->update(['mark' => 0]);
- return [
- 'code' => 0,
- 'msg' => "成功删除{$count}条记录"
- ];
- }
- /**
- * 设置状态
- */
- public function status()
- {
- $params = request()->all();
- $id = $params['id'] ?? 0;
- $status = $params['status'] ?? 1;
- $meal = PayMealsModel::where('id', $id)
- ->where('mark', 1)
- ->first();
- if (!$meal) {
- return ['code' => 1, 'msg' => '记录不存在'];
- }
- $meal->status = $status;
- $meal->update_time = time();
- $meal->save();
- return ['code' => 0, 'msg' => '设置成功'];
- }
- }
|