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', strtotime($data['create_time'])); $data['update_time_text'] = date('Y-m-d H:i:s', strtotime($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), 'phone_type' => (int)($params['phone_type'] ?? 1), 'phone_service' => (int)($params['phone_service'] ?? 1), 'electric_type' => (int)($params['electric_type'] ?? 1), 'gas_type' => (int)($params['gas_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 = isset($params['remark']) ? $params['remark'] : ''; $meal->phone_type = $params['phone_type'] ?? $meal->phone_type; $meal->phone_service = $params['phone_service'] ?? $meal->phone_service; $meal->electric_type = $params['electric_type'] ?? $meal->electric_type; $meal->gas_type = $params['gas_type'] ?? $meal->gas_type; $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' => '设置成功']; } }