package com.ruoyi.system.controller; import java.math.BigDecimal; import java.util.List; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.ControllerUtil.AppletControllerUtil; import com.ruoyi.system.domain.ServiceCate; import com.ruoyi.system.service.IServiceCateService; 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.ServiceGoods; import com.ruoyi.system.service.IServiceGoodsService; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; /** * 服务内容Controller * * @author ruoyi * @date 2025-05-13 */ @RestController @RequestMapping("/system/ServiceGoods") public class ServiceGoodsController extends BaseController { @Autowired private IServiceGoodsService serviceGoodsService; @Autowired private IServiceCateService serviceCateService; /** * 查询服务内容列表 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:list')") @GetMapping("/list") public TableDataInfo list(ServiceGoods serviceGoods) { startPage(); List list = serviceGoodsService.selectServiceGoodsList(serviceGoods); for (ServiceGoods serviceGoodsdata : list) { ServiceCate serviceCate = serviceCateService.selectServiceCateById(serviceGoodsdata.getCateId()); if (serviceCate != null) { serviceGoodsdata.setCateName(serviceCate.getTitle()); } serviceGoodsdata.setIcon(com.ruoyi.system.ControllerUtil.AppletControllerUtil.buildImageUrl(serviceGoodsdata.getIcon())); } return getDataTable(list); } /** * 导出服务内容列表 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:export')") @Log(title = "服务内容", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, ServiceGoods serviceGoods) { List list = serviceGoodsService.selectServiceGoodsList(serviceGoods); // 处理数据,设置中文名称 for (ServiceGoods item : list) { // 设置状态名称 if ("1".equals(item.getStatus())) { item.setStatusName("启用"); } else { item.setStatusName("停用"); } // 设置下单类型名称 if (item.getServicetype() != null) { switch (item.getServicetype()) { case 1: item.setServiceTypeName("预约下单"); break; case 2: item.setServiceTypeName("报价下单"); break; default: item.setServiceTypeName("未知类型"); break; } } // 设置规格类型名称 if (item.getSkuType() != null) { switch (item.getSkuType()) { case 1: item.setSkuTypeName("单规格"); break; case 2: item.setSkuTypeName("多规格"); break; default: item.setSkuTypeName("未知类型"); break; } } // 设置类型名称 if (item.getType() != null) { switch (item.getType()) { case 1: item.setTypeName("服务"); break; case 2: item.setTypeName("商品"); break; default: item.setTypeName("未知类型"); break; } } // 设置分类名称 if (item.getCateId() != null) { ServiceCate serviceCate = serviceCateService.selectServiceCateById(item.getCateId()); if (serviceCate != null) { item.setCateName(serviceCate.getTitle()); } } // 设置是否跳转服务名称 if (item.getIsforservice() != null) { switch (item.getIsforservice()) { case 1: item.setIsForServiceName("是"); break; case 2: item.setIsForServiceName("否"); break; default: item.setIsForServiceName("未知"); break; } } // 设置是否可拼团名称 if (item.getIsgroup() != null) { switch (item.getIsgroup()) { case 1: item.setIsGroupName("是"); break; case 2: item.setIsGroupName("否"); break; default: item.setIsGroupName("未知"); break; } } // 设置是否可设置次卡名称 if (item.getIsonce() != null) { switch (item.getIsonce()) { case 1: item.setIsOnceName("是"); break; case 2: item.setIsOnceName("否"); break; default: item.setIsOnceName("未知"); break; } } // 设置分佣模式名称 if (item.getCommissiontype() != null) { switch (item.getCommissiontype()) { case 1: item.setCommissionTypeName("系统分佣"); break; case 2: item.setCommissionTypeName("独立分佣"); break; case 3: item.setCommissionTypeName("不分佣"); break; default: item.setCommissionTypeName("未知模式"); break; } } // 设置派单模式名称 if (item.getDispatchtype() != null) { switch (item.getDispatchtype()) { case 1: item.setDispatchTypeName("系统派单"); break; case 2: item.setDispatchTypeName("后台手动派单"); break; case 3: item.setDispatchTypeName("指定工人"); break; default: item.setDispatchTypeName("未知模式"); break; } } // 设置是否为一口价名称 if (item.getIsfixed() != null) { switch (item.getIsfixed()) { case 1: item.setIsFixedName("是"); break; case 2: item.setIsFixedName("否"); break; default: item.setIsFixedName("未知"); break; } } // 处理问答数量 if (item.getQuestions() != null && !item.getQuestions().isEmpty()) { try { // 尝试解析JSON格式的问答数据,计算数量 if (item.getQuestions().startsWith("[") && item.getQuestions().endsWith("]")) { // 简单的JSON数组计数 String[] questions = item.getQuestions().split("\\{"); item.setQuestionCount(String.valueOf(questions.length - 1)); } else { item.setQuestionCount("1"); } } catch (Exception e) { item.setQuestionCount("0"); } } else { item.setQuestionCount("0"); } // 处理技能名称转换 if (item.getSkillIds() != null && !item.getSkillIds().trim().isEmpty()) { try { // 这里可以根据需要查询技能表获取技能名称 // 暂时设置为技能ID的显示 item.setSkillNames(item.getSkillIds()); } catch (Exception e) { item.setSkillNames("技能解析失败"); } } else { item.setSkillNames("无"); } } ExcelUtil util = new ExcelUtil(ServiceGoods.class); util.exportExcel(response, list, "服务内容数据"); } /** * 获取服务内容详细信息 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:query')") @GetMapping(value = "/selectServiceCateList/{type}") public AjaxResult selectServiceCateList(@PathVariable("type") Integer type) { ServiceCate serviceGoods = new ServiceCate(); if (type != null){ serviceGoods.setType(Long.valueOf(type)); } return success(serviceCateService.selectServiceCateList(serviceGoods)); // return success(serviceCateService.selectServiceCateList(new ServiceCate())); } /** * 获取服务内容详细信息 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:query')") @GetMapping(value = "/selectList") public AjaxResult selectList() { ServiceGoods serviceGoods = new ServiceGoods(); serviceGoods.setIsfixed(1); return success(serviceGoodsService.selectServiceGoodsList(serviceGoods)); } /** * 获取服务内容详细信息 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:query')") @GetMapping(value = "/selectTypeList/{type}") public AjaxResult selectTypeList(@PathVariable("type") Integer type) { ServiceGoods serviceGoods = new ServiceGoods(); if (type != null){ serviceGoods.setType(type); } return success(serviceGoodsService.selectServiceGoodsList(serviceGoods)); } /** * 获取服务内容详细信息 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { ServiceGoods serviceGoods = serviceGoodsService.selectServiceGoodsById(id); serviceGoods.setIcon(AppletControllerUtil.buildImageUrl(serviceGoods.getIcon())); if (serviceGoods != null){ if (serviceGoods.getSkuType()==null){ serviceGoods.setSkuType(1); JSONObject jsonObject =new JSONObject(); jsonObject.put("type","single"); serviceGoods.setSku(jsonObject.toJSONString()); serviceGoodsService.updateServiceGoods(serviceGoods); } if (serviceGoods.getSku()!=null){ if (serviceGoods.getSku().equals("{}")){ JSONObject jsonObject =new JSONObject(); jsonObject.put("type","single"); serviceGoods.setSku(jsonObject.toJSONString()); serviceGoodsService.updateServiceGoods(serviceGoods); } } if(StringUtils.isNotBlank(serviceGoods.getSkillIds())){ serviceGoods.setSkillIds(AppletControllerUtil.convertToJSONArray(serviceGoods.getSkillIds()).toJSONString()); } if(StringUtils.isNotBlank(serviceGoods.getImgs())){ serviceGoods.setImgs(AppletControllerUtil.convertToJSONArray(serviceGoods.getImgs()).toJSONString()); } } return success(serviceGoods); } /** * 新增服务内容 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:add')") @Log(title = "服务内容", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody ServiceGoods serviceGoods) { // 验证和处理基检现象数据格式 validateAndProcessBasicField(serviceGoods); // 处理一级和二级分类ID processCategoryIds(serviceGoods); if (serviceGoods.getPostage()==null){ serviceGoods.setPostage(BigDecimal.ZERO); } if(StringUtils.isNotBlank(serviceGoods.getSkillIds())){ serviceGoods.setSkillIds(AppletControllerUtil.convertToJSONArray(serviceGoods.getSkillIds()).toJSONString()); } if(StringUtils.isNotBlank(serviceGoods.getImgs())){ serviceGoods.setImgs(AppletControllerUtil.convertToJSONArray(serviceGoods.getImgs()).toJSONString()); } return toAjax(serviceGoodsService.insertServiceGoods(serviceGoods)); } /** * 修改服务内容 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:edit')") @Log(title = "服务内容", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody ServiceGoods serviceGoods) { // 验证和处理基检现象数据格式 validateAndProcessBasicField(serviceGoods); if(StringUtils.isNotBlank(serviceGoods.getSkillIds())){ serviceGoods.setSkillIds(AppletControllerUtil.convertToJSONArray(serviceGoods.getSkillIds()).toJSONString()); } if(StringUtils.isNotBlank(serviceGoods.getImgs())){ serviceGoods.setImgs(AppletControllerUtil.convertToJSONArray(serviceGoods.getImgs()).toJSONString()); } // 处理一级和二级分类ID processCategoryIds(serviceGoods); return toAjax(serviceGoodsService.updateServiceGoods(serviceGoods)); } /** * 定时任务状态修改 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:changeStatus')") @Log(title = "修改状态", businessType = BusinessType.UPDATE) @PutMapping("/changeStatus") public AjaxResult changeStatus(@RequestBody ServiceGoods serviceGoods) { ServiceGoods newServiceGoods = serviceGoodsService.selectServiceGoodsById(serviceGoods.getId()); newServiceGoods.setStatus(serviceGoods.getStatus()); return toAjax(serviceGoodsService.updateServiceGoods(newServiceGoods)); } /** * 删除服务内容 */ @PreAuthorize("@ss.hasPermi('system:ServiceGoods:remove')") @Log(title = "服务内容", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(serviceGoodsService.deleteServiceGoodsByIds(ids)); } /** * 验证和处理基检现象数据格式 * 确保基检现象字段为有效的JSON数组字符串格式 */ private void validateAndProcessBasicField(ServiceGoods serviceGoods) { String basic = serviceGoods.getBasic(); if (basic != null && !basic.trim().isEmpty()) { try { // 尝试解析为JSON数组,验证格式是否正确 com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); Object parsed = mapper.readValue(basic, Object.class); if (parsed instanceof java.util.List) { // 如果是有效的JSON数组,保持不变 // 可以在这里进行额外的数据清理,比如去除空字符串 @SuppressWarnings("unchecked") java.util.List list = (java.util.List) parsed; list.removeIf(item -> item == null || item.trim().isEmpty()); // 重新序列化,确保格式统一 String cleanedBasic = mapper.writeValueAsString(list); serviceGoods.setBasic(cleanedBasic); } else { // 如果不是数组格式,设置为null serviceGoods.setBasic(null); } } catch (Exception e) { // 如果JSON解析失败,尝试按逗号分隔的字符串处理(兼容旧数据) String[] items = basic.split(","); java.util.List list = new java.util.ArrayList<>(); for (String item : items) { String trimmed = item.trim(); if (!trimmed.isEmpty()) { list.add(trimmed); } } if (!list.isEmpty()) { try { com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); String jsonArray = mapper.writeValueAsString(list); serviceGoods.setBasic(jsonArray); } catch (Exception ex) { // 转换失败,设置为null serviceGoods.setBasic(null); } } else { serviceGoods.setBasic(null); } } } } /** * 处理一级和二级分类ID * 根据选择的分类自动设置一级和二级分类ID * 如果选择的是一级分类,则一级分类ID记录,二级分类ID设为0 * 如果选择的是二级分类,则记录一级分类ID和二级分类ID */ private void processCategoryIds(ServiceGoods serviceGoods) { Long cateId = serviceGoods.getCateId(); if (cateId != null && cateId > 0) { try { // 查询当前选择的分类信息 ServiceCate currentCate = serviceCateService.selectServiceCateById(cateId); if (currentCate != null) { // 判断是否为一级分类(parentId为null或0) if (currentCate.getParentId() == null || currentCate.getParentId() == 0) { // 选择的是一级分类 serviceGoods.setFirstCateId(cateId); serviceGoods.setSecondCateId(0L); } else { // 选择的是二级分类 serviceGoods.setFirstCateId(currentCate.getParentId()); serviceGoods.setSecondCateId(cateId); } } else { // 分类不存在,设置为null serviceGoods.setFirstCateId(null); serviceGoods.setSecondCateId(null); } } catch (Exception e) { // 异常情况下设置为null serviceGoods.setFirstCateId(null); serviceGoods.setSecondCateId(null); } } else { // 没有选择分类 serviceGoods.setFirstCateId(null); serviceGoods.setSecondCateId(null); } } }