package com.ruoyi.system.controller; import java.util.List; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletRequest; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.domain.ServiceCate; import com.ruoyi.system.service.IServiceCateService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; /** * 服务分类Controller(二级分类管理) * * @author ruoyi * @date 2025-05-13 */ @RestController @RequestMapping("/system/ServiceCate") public class ServiceCateController extends BaseController { @Autowired private IServiceCateService serviceCateService; /** * 查询服务分类列表 */ @PreAuthorize("@ss.hasPermi('system:ServiceCate:list')") @GetMapping("/list") public TableDataInfo list(ServiceCate serviceCate, HttpServletRequest request) { // 如果pageSize大于1000,说明是要获取所有数据,不分页 String pageSizeStr = request.getParameter("pageSize"); if (pageSizeStr != null && Integer.parseInt(pageSizeStr) > 1000) { // 不分页查询 List list = serviceCateService.selectServiceCateList(serviceCate); TableDataInfo dataTable = new TableDataInfo(); dataTable.setRows(list); dataTable.setTotal(list.size()); return dataTable; } else { // 正常分页查询 startPage(); List list = serviceCateService.selectServiceCateList(serviceCate); return getDataTable(list); } } /** * 导出服务分类列表 */ @PreAuthorize("@ss.hasPermi('system:ServiceCate:export')") @Log(title = "服务分类", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, ServiceCate serviceCate) { List list = serviceCateService.selectServiceCateList(serviceCate); ExcelUtil util = new ExcelUtil(ServiceCate.class); util.exportExcel(response, list, "服务分类数据"); } /** * 获取服务分类详细信息 */ @PreAuthorize("@ss.hasPermi('system:ServiceCate:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { return success(serviceCateService.selectServiceCateById(id)); } /** * 新增服务分类 */ @PreAuthorize("@ss.hasPermi('system:ServiceCate:add')") @Log(title = "服务分类", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody ServiceCate serviceCate) { // 验证分类级别(只允许二级分类) if (serviceCate.getParentId() == null) { serviceCate.setParentId(0L); // 一级分类 } else { // 检查父级分类是否为一级分类 ServiceCate parent = serviceCateService.selectServiceCateById(serviceCate.getParentId()); if (parent == null) { return error("父级分类不存在"); } if (parent.getParentId() != 0L) { return error("只支持二级分类,不能在二级分类下创建子分类"); } } // 设置默认值 if (serviceCate.getSort() == null) { serviceCate.setSort(0L); } if (serviceCate.getBrowse() == null) { serviceCate.setBrowse(0L); } return toAjax(serviceCateService.insertServiceCate(serviceCate)); } /** * 修改服务分类 */ @PreAuthorize("@ss.hasPermi('system:ServiceCate:edit')") @Log(title = "服务分类", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody ServiceCate serviceCate) { // 不能将自己设为父级 if (serviceCate.getId() != null && serviceCate.getId().equals(serviceCate.getParentId())) { return error("不能将自己设为父级分类"); } // 如果修改了父级分类,需要验证 if (serviceCate.getParentId() != null && serviceCate.getParentId() != 0L) { ServiceCate parent = serviceCateService.selectServiceCateById(serviceCate.getParentId()); if (parent == null) { return error("父级分类不存在"); } if (parent.getParentId() != 0L) { return error("只支持二级分类,不能设置三级分类"); } } return toAjax(serviceCateService.updateServiceCate(serviceCate)); } /** * 分类状态修改 */ @PreAuthorize("@ss.hasPermi('system:ServiceCate:changeStatus')") @Log(title = "修改状态", businessType = BusinessType.UPDATE) @PutMapping("/changeStatus") public AjaxResult changeStatus(@RequestBody ServiceCate serviceCate) { ServiceCate newServiceCate = serviceCateService.selectServiceCateById(serviceCate.getId()); newServiceCate.setStatus(serviceCate.getStatus()); return toAjax(serviceCateService.updateServiceCate(newServiceCate)); } /** * 删除服务分类 */ @PreAuthorize("@ss.hasPermi('system:ServiceCate:remove')") @Log(title = "服务分类", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { // 检查是否有子分类 for (Long id : ids) { ServiceCate childQuery = new ServiceCate(); childQuery.setParentId(id); List children = serviceCateService.selectServiceCateList(childQuery); if (children != null && children.size() > 0) { ServiceCate parent = serviceCateService.selectServiceCateById(id); return error("分类【" + parent.getTitle() + "】存在子分类,不能删除"); } } return toAjax(serviceCateService.deleteServiceCateByIds(ids)); } }