Quellcode durchsuchen

开发完成服务类目相关的接口

jinshihui vor 2 Wochen
Ursprung
Commit
e4d912914e

+ 132 - 0
nightFragrance-massage/src/main/java/com/ylx/servicecategory/controller/ServiceCategoryController.java

@@ -0,0 +1,132 @@
+package com.ylx.servicecategory.controller;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ylx.common.annotation.Log;
+import com.ylx.common.core.domain.R;
+import com.ylx.common.enums.BusinessType;
+import com.ylx.common.exception.ServiceException;
+import com.ylx.servicecategory.domain.ServiceCategory;
+import com.ylx.servicecategory.service.ServiceCategoryService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+/**
+ * 服务类目控制层
+ */
+@Slf4j
+@RestController
+@Api(tags = {"服务类目管理"})
+@RequestMapping("serviceCategory")
+public class ServiceCategoryController {
+
+    @Resource
+    private ServiceCategoryService serviceCategoryService;
+
+    /**
+     * 根据主键ID查询服务类目详情
+     *
+     * @param id 服务类目ID
+     * @return 服务类目详情
+     */
+    @GetMapping("detail")
+    @ApiOperation("根据主键ID查询服务类目详情")
+    public R<ServiceCategory> detail(@RequestParam("id") Long id) {
+        try {
+            return R.ok(serviceCategoryService.getServiceCategoryDetail(id));
+        } catch (ServiceException e) {
+            return R.fail(e.getMessage());
+        } catch (Exception e) {
+            log.error("查询服务类目详情异常", e);
+            return R.fail("查询服务类目详情失败");
+        }
+    }
+
+    /**
+     * 分页查询服务类目
+     *
+     * @param page            分页参数
+     * @param serviceCategory 查询条件
+     * @return 服务类目分页数据
+     */
+    @GetMapping("page")
+    @ApiOperation("分页查询服务类目")
+    public R<Page<ServiceCategory>> page(Page<ServiceCategory> page, ServiceCategory serviceCategory) {
+        try {
+            return R.ok(serviceCategoryService.pageServiceCategory(page, serviceCategory));
+        } catch (ServiceException e) {
+            return R.fail(e.getMessage());
+        } catch (Exception e) {
+            log.error("分页查询服务类目异常", e);
+            return R.fail("分页查询服务类目失败");
+        }
+    }
+
+    /**
+     * 新增服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 新增后的服务类目ID
+     */
+    @PostMapping("add")
+    @ApiOperation("新增服务类目")
+    @Log(title = "新增服务类目", businessType = BusinessType.INSERT)
+    public R<Long> add(@RequestBody ServiceCategory serviceCategory) {
+        try {
+            return R.ok(serviceCategoryService.addServiceCategory(serviceCategory));
+        } catch (ServiceException e) {
+            return R.fail(e.getMessage());
+        } catch (Exception e) {
+            log.error("新增服务类目异常", e);
+            return R.fail("新增服务类目失败");
+        }
+    }
+
+    /**
+     * 修改服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 修改结果
+     */
+    @PostMapping("update")
+    @ApiOperation("修改服务类目")
+    @Log(title = "修改服务类目", businessType = BusinessType.UPDATE)
+    public R<Boolean> update(@RequestBody ServiceCategory serviceCategory) {
+        try {
+            return R.ok(serviceCategoryService.updateServiceCategory(serviceCategory));
+        } catch (ServiceException e) {
+            return R.fail(e.getMessage());
+        } catch (Exception e) {
+            log.error("修改服务类目异常", e);
+            return R.fail("修改服务类目失败");
+        }
+    }
+
+    /**
+     * 删除服务类目
+     *
+     * @param id 服务类目ID
+     * @return 删除结果
+     */
+    @PostMapping("delete")
+    @ApiOperation("删除服务类目")
+    @Log(title = "删除服务类目", businessType = BusinessType.DELETE)
+    public R<Boolean> delete(@RequestParam("id") Long id) {
+        try {
+            return R.ok(serviceCategoryService.deleteServiceCategory(id));
+        } catch (ServiceException e) {
+            return R.fail(e.getMessage());
+        } catch (Exception e) {
+            log.error("删除服务类目异常", e);
+            return R.fail("删除服务类目失败");
+        }
+    }
+}

+ 72 - 0
nightFragrance-massage/src/main/java/com/ylx/servicecategory/domain/ServiceCategory.java

@@ -0,0 +1,72 @@
+package com.ylx.servicecategory.domain;
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+import java.time.LocalDateTime;
+
+/**
+ * 服务类目实体类
+ */
+@Data
+@TableName("service_category")
+public class ServiceCategory {
+
+    /**
+     * 主键ID
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 服务类目名称
+     */
+    @TableField("name")
+    private String name;
+
+    /**
+     * 服务类目图标
+     */
+    @TableField("icon")
+    private String icon;
+
+    /**
+     * 排序值
+     */
+    @TableField("sort")
+    private Integer sort;
+
+    /**
+     * 是否首页展示
+     */
+    @TableField("is_home_display")
+    private Integer isHomeDisplay;
+
+    /**
+     * 是否上线
+     */
+    @TableField("is_online")
+    private Integer isOnline;
+
+    /**
+     * 创建时间
+     */
+    @TableField(value = "create_time", fill = FieldFill.INSERT)
+    private LocalDateTime createTime;
+
+    /**
+     * 更新时间
+     */
+    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
+    private LocalDateTime updateTime;
+
+    /**
+     * 是否删除
+     */
+    @TableField("is_deleted")
+    @TableLogic
+    private Integer isDeleted;
+}

+ 63 - 0
nightFragrance-massage/src/main/java/com/ylx/servicecategory/mapper/ServiceCategoryMapper.java

@@ -0,0 +1,63 @@
+package com.ylx.servicecategory.mapper;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ylx.servicecategory.domain.ServiceCategory;
+
+/**
+ * 服务类目数据访问层
+ */
+public interface ServiceCategoryMapper extends BaseMapper<ServiceCategory> {
+
+    /**
+     * 根据主键ID查询服务类目详情
+     *
+     * @param id 服务类目ID
+     * @return 服务类目详情
+     */
+    default ServiceCategory selectServiceCategoryById(Long id) {
+        return selectById(id);
+    }
+
+    /**
+     * 分页查询服务类目
+     *
+     * @param page         分页参数
+     * @param queryWrapper 查询条件
+     * @return 服务类目分页数据
+     */
+    default Page<ServiceCategory> selectServiceCategoryPage(Page<ServiceCategory> page, LambdaQueryWrapper<ServiceCategory> queryWrapper) {
+        return selectPage(page, queryWrapper);
+    }
+
+    /**
+     * 新增服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 影响行数
+     */
+    default int insertServiceCategory(ServiceCategory serviceCategory) {
+        return insert(serviceCategory);
+    }
+
+    /**
+     * 根据主键修改服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 影响行数
+     */
+    default int updateServiceCategoryById(ServiceCategory serviceCategory) {
+        return updateById(serviceCategory);
+    }
+
+    /**
+     * 根据主键删除服务类目
+     *
+     * @param id 服务类目ID
+     * @return 影响行数
+     */
+    default int deleteServiceCategoryById(Long id) {
+        return deleteById(id);
+    }
+}

+ 52 - 0
nightFragrance-massage/src/main/java/com/ylx/servicecategory/service/ServiceCategoryService.java

@@ -0,0 +1,52 @@
+package com.ylx.servicecategory.service;
+
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.ylx.servicecategory.domain.ServiceCategory;
+
+/**
+ * 服务类目服务接口
+ */
+public interface ServiceCategoryService extends IService<ServiceCategory> {
+
+    /**
+     * 根据主键ID查询服务类目详情
+     *
+     * @param id 服务类目ID
+     * @return 服务类目详情
+     */
+    ServiceCategory getServiceCategoryDetail(Long id);
+
+    /**
+     * 分页查询服务类目
+     *
+     * @param page            分页参数
+     * @param serviceCategory 查询条件
+     * @return 服务类目分页数据
+     */
+    Page<ServiceCategory> pageServiceCategory(Page<ServiceCategory> page, ServiceCategory serviceCategory);
+
+    /**
+     * 新增服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 新增后的服务类目ID
+     */
+    Long addServiceCategory(ServiceCategory serviceCategory);
+
+    /**
+     * 修改服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 是否修改成功
+     */
+    Boolean updateServiceCategory(ServiceCategory serviceCategory);
+
+    /**
+     * 删除服务类目
+     *
+     * @param id 服务类目ID
+     * @return 是否删除成功
+     */
+    Boolean deleteServiceCategory(Long id);
+}

+ 173 - 0
nightFragrance-massage/src/main/java/com/ylx/servicecategory/service/impl/ServiceCategoryServiceImpl.java

@@ -0,0 +1,173 @@
+package com.ylx.servicecategory.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ylx.common.exception.ServiceException;
+import com.ylx.common.utils.StringUtils;
+import com.ylx.servicecategory.domain.ServiceCategory;
+import com.ylx.servicecategory.mapper.ServiceCategoryMapper;
+import com.ylx.servicecategory.service.ServiceCategoryService;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+
+/**
+ * 服务类目服务实现类
+ */
+@Service
+public class ServiceCategoryServiceImpl extends ServiceImpl<ServiceCategoryMapper, ServiceCategory> implements ServiceCategoryService {
+
+    /**
+     * 根据主键ID查询服务类目详情
+     *
+     * @param id 服务类目ID
+     * @return 服务类目详情
+     */
+    @Override
+    public ServiceCategory getServiceCategoryDetail(Long id) {
+        checkIdParam(id);
+
+        ServiceCategory serviceCategory = this.baseMapper.selectServiceCategoryById(id);
+        if (serviceCategory == null) {
+            throw new ServiceException("服务类目不存在");
+        }
+        return serviceCategory;
+    }
+
+    /**
+     * 分页查询服务类目
+     *
+     * @param page            分页参数
+     * @param serviceCategory 查询条件
+     * @return 服务类目分页数据
+     */
+    @Override
+    public Page<ServiceCategory> pageServiceCategory(Page<ServiceCategory> page, ServiceCategory serviceCategory) {
+        Page<ServiceCategory> pageParam = page == null ? new Page<>(1, 10) : page;
+        LambdaQueryWrapper<ServiceCategory> queryWrapper = new LambdaQueryWrapper<>();
+
+        if (serviceCategory != null) {
+            queryWrapper.like(StringUtils.isNotBlank(serviceCategory.getName()), ServiceCategory::getName, serviceCategory.getName())
+                    .eq(serviceCategory.getIsHomeDisplay() != null, ServiceCategory::getIsHomeDisplay, serviceCategory.getIsHomeDisplay())
+                    .eq(serviceCategory.getIsOnline() != null, ServiceCategory::getIsOnline, serviceCategory.getIsOnline());
+        }
+        queryWrapper.orderByAsc(ServiceCategory::getSort).orderByAsc(ServiceCategory::getId);
+
+        return this.baseMapper.selectServiceCategoryPage(pageParam, queryWrapper);
+    }
+
+    /**
+     * 新增服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 新增后的服务类目ID
+     */
+    @Override
+    public Long addServiceCategory(ServiceCategory serviceCategory) {
+        checkAddParam(serviceCategory);
+
+        LocalDateTime now = LocalDateTime.now();
+        serviceCategory.setId(null);
+        serviceCategory.setCreateTime(now);
+        serviceCategory.setUpdateTime(now);
+        serviceCategory.setIsDeleted(0);
+
+        int insertResult = this.baseMapper.insertServiceCategory(serviceCategory);
+        if (insertResult <= 0) {
+            throw new ServiceException("新增服务类目失败");
+        }
+        return serviceCategory.getId();
+    }
+
+    /**
+     * 修改服务类目
+     *
+     * @param serviceCategory 服务类目
+     * @return 是否修改成功
+     */
+    @Override
+    public Boolean updateServiceCategory(ServiceCategory serviceCategory) {
+        checkUpdateParam(serviceCategory);
+
+        ServiceCategory dbServiceCategory = this.baseMapper.selectById(serviceCategory.getId());
+        if (dbServiceCategory == null) {
+            throw new ServiceException("服务类目不存在");
+        }
+
+        serviceCategory.setUpdateTime(LocalDateTime.now());
+        int updateResult = this.baseMapper.updateServiceCategoryById(serviceCategory);
+        if (updateResult <= 0) {
+            throw new ServiceException("修改服务类目失败");
+        }
+        return true;
+    }
+
+    /**
+     * 删除服务类目
+     *
+     * @param id 服务类目ID
+     * @return 是否删除成功
+     */
+    @Override
+    public Boolean deleteServiceCategory(Long id) {
+        checkIdParam(id);
+
+        ServiceCategory dbServiceCategory = this.baseMapper.selectById(id);
+        if (dbServiceCategory == null) {
+            throw new ServiceException("服务类目不存在");
+        }
+
+        int deleteResult = this.baseMapper.deleteServiceCategoryById(id);
+        if (deleteResult <= 0) {
+            throw new ServiceException("删除服务类目失败");
+        }
+        return true;
+    }
+
+    private void checkAddParam(ServiceCategory serviceCategory) {
+        checkRequiredParam(serviceCategory);
+    }
+
+    private void checkUpdateParam(ServiceCategory serviceCategory) {
+        checkRequiredParam(serviceCategory);
+        if (serviceCategory.getId() == null) {
+            throw new ServiceException("服务类目ID不能为空");
+        }
+    }
+
+    private void checkIdParam(Long id) {
+        if (id == null) {
+            throw new ServiceException("服务类目ID不能为空");
+        }
+    }
+
+    private void checkRequiredParam(ServiceCategory serviceCategory) {
+        if (serviceCategory == null) {
+            throw new ServiceException("服务类目不能为空");
+        }
+        if (StringUtils.isBlank(serviceCategory.getName())) {
+            throw new ServiceException("服务名称不能为空");
+        }
+        if (serviceCategory.getSort() == null) {
+            throw new ServiceException("序号不能为空");
+        }
+        if (serviceCategory.getSort() < 0) {
+            throw new ServiceException("序号不能小于0");
+        }
+
+        serviceCategory.setName(serviceCategory.getName().trim());
+        serviceCategory.setIsHomeDisplay(normalizeSwitchValue(serviceCategory.getIsHomeDisplay(), "首页展示"));
+        serviceCategory.setIsOnline(normalizeSwitchValue(serviceCategory.getIsOnline(), "上架"));
+    }
+
+    private Integer normalizeSwitchValue(Integer value, String fieldName) {
+        if (value == null) {
+            return 0;
+        }
+        if (value == 0 || value == 1) {
+            return value;
+        }
+        throw new ServiceException(fieldName + "状态值不正确");
+    }
+}