|
|
|
@ -0,0 +1,157 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
*
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
*
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.yami.shop.admin.controller; // 定义类所在的包
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; // 引入MyBatis-Plus的条件查询包装器
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; // 引入MyBatis-Plus的分页接口
|
|
|
|
|
import com.yami.shop.bean.enums.AreaLevelEnum; // 引入区域级别枚举
|
|
|
|
|
import com.yami.shop.bean.model.Area; // 引入区域模型
|
|
|
|
|
import com.yami.shop.common.exception.YamiShopBindException; // 引入自定义异常类
|
|
|
|
|
import com.yami.shop.common.util.PageParam; // 引入分页参数工具类
|
|
|
|
|
import com.yami.shop.service.AreaService; // 引入区域服务类
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; // 引入Spring的@Autowired注解
|
|
|
|
|
import com.yami.shop.common.response.ServerResponseEntity; // 引入服务器响应实体类
|
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize; // 引入Spring Security的PreAuthorize注解
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import jakarta.validation.Valid; // 引入Jakarta Validation的Valid注解
|
|
|
|
|
import java.util.List; // 引入Java的List接口
|
|
|
|
|
import java.util.Objects; // 引入Java的Objects工具类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AreaController类,用于管理区域信息。
|
|
|
|
|
* 该类包含了分页获取区域、获取省市、通过父级ID获取区域列表、获取区域信息、保存区域、修改区域和删除区域的方法。
|
|
|
|
|
* @作者 lgh on 2018/10/26.
|
|
|
|
|
*/
|
|
|
|
|
@RestController // 标注这是一个控制器类,并且其返回结果直接写入HTTP响应体中,而不是视图名称
|
|
|
|
|
@RequestMapping("/admin/area") // 定义请求路径的根地址为/admin/area
|
|
|
|
|
public class AreaController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private AreaService areaService; // 自动注入区域服务类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分页获取区域信息
|
|
|
|
|
* @param area 查询条件
|
|
|
|
|
* @param page 分页参数
|
|
|
|
|
* @return 服务器响应实体,包含分页后的区域信息
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/page")
|
|
|
|
|
@PreAuthorize("@pms.hasPermission('admin:area:page')") // 权限检查
|
|
|
|
|
public ServerResponseEntity<IPage<Area>> page(Area area, PageParam<Area> page) {
|
|
|
|
|
IPage<Area> areaPage = areaService.page(page, new LambdaQueryWrapper<Area>());
|
|
|
|
|
return ServerResponseEntity.success(areaPage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取省市信息
|
|
|
|
|
* @param area 查询条件
|
|
|
|
|
* @return 服务器响应实体,包含查询到的省市信息
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/list")
|
|
|
|
|
@PreAuthorize("@pms.hasPermission('admin:area:list')") // 权限检查
|
|
|
|
|
public ServerResponseEntity<List<Area>> list(Area area) {
|
|
|
|
|
List<Area> areas = areaService.list(new LambdaQueryWrapper<Area>()
|
|
|
|
|
.like(area.getAreaName() != null, Area::getAreaName, area.getAreaName()));
|
|
|
|
|
return ServerResponseEntity.success(areas);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过父级ID获取区域列表
|
|
|
|
|
* @param pid 父级ID
|
|
|
|
|
* @return 服务器响应实体,包含查询到的区域列表
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/listByPid")
|
|
|
|
|
public ServerResponseEntity<List<Area>> listByPid(Long pid) {
|
|
|
|
|
List<Area> list = areaService.listByPid(pid);
|
|
|
|
|
return ServerResponseEntity.success(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取区域信息
|
|
|
|
|
* @param id 区域ID
|
|
|
|
|
* @return 服务器响应实体,包含查询到的区域信息
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/info/{id}")
|
|
|
|
|
@PreAuthorize("@pms.hasPermission('admin:area:info')") // 权限检查
|
|
|
|
|
public ServerResponseEntity<Area> info(@PathVariable("id") Long id) {
|
|
|
|
|
Area area = areaService.getById(id);
|
|
|
|
|
return ServerResponseEntity.success(area);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存区域信息
|
|
|
|
|
* @param area 区域信息
|
|
|
|
|
* @return 服务器响应实体
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping
|
|
|
|
|
@PreAuthorize("@pms.hasPermission('admin:area:save')") // 权限检查
|
|
|
|
|
public ServerResponseEntity<Void> save(@Valid @RequestBody Area area) {
|
|
|
|
|
if (area.getParentId() != null) {
|
|
|
|
|
Area parentArea = areaService.getById(area.getParentId());
|
|
|
|
|
area.setLevel(parentArea.getLevel() + 1);
|
|
|
|
|
areaService.removeAreaCacheByParentId(area.getParentId());
|
|
|
|
|
}
|
|
|
|
|
areaService.save(area);
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改区域信息
|
|
|
|
|
* @param area 区域信息
|
|
|
|
|
* @return 服务器响应实体
|
|
|
|
|
*/
|
|
|
|
|
@PutMapping
|
|
|
|
|
@PreAuthorize("@pms.hasPermission('admin:area:update')") // 权限检查
|
|
|
|
|
public ServerResponseEntity<Void> update(@Valid @RequestBody Area area) {
|
|
|
|
|
Area areaDb = areaService.getById(area.getAreaId());
|
|
|
|
|
// 判断当前省市区级别,如果是1级、2级则不能修改级别,不能修改成别人的下级
|
|
|
|
|
if(Objects.equals(areaDb.getLevel(), AreaLevelEnum.FIRST_LEVEL.value()) && !Objects.equals(area.getLevel(), AreaLevelEnum.FIRST_LEVEL.value())){
|
|
|
|
|
throw new YamiShopBindException("不能改变一级行政地区的级别");
|
|
|
|
|
}
|
|
|
|
|
if(Objects.equals(areaDb.getLevel(), AreaLevelEnum.SECOND_LEVEL.value()) && !Objects.equals(area.getLevel(), AreaLevelEnum.SECOND_LEVEL.value())){
|
|
|
|
|
throw new YamiShopBindException("不能改变二级行政地区的级别");
|
|
|
|
|
}
|
|
|
|
|
hasSameName(area);
|
|
|
|
|
areaService.updateById(area);
|
|
|
|
|
areaService.removeAreaCacheByParentId(area.getParentId());
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除区域信息
|
|
|
|
|
* @param id 区域ID
|
|
|
|
|
* @return 服务器响应实体
|
|
|
|
|
*/
|
|
|
|
|
@DeleteMapping("/{id}")
|
|
|
|
|
@PreAuthorize("@pms.hasPermission('admin:area:delete')") // 权限检查
|
|
|
|
|
public ServerResponseEntity<Void> delete(@PathVariable Long id) {
|
|
|
|
|
Area area = areaService.getById(id);
|
|
|
|
|
areaService.removeById(id);
|
|
|
|
|
areaService.removeAreaCacheByParentId(area.getParentId());
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否有相同名称的区域
|
|
|
|
|
* @param area 区域信息
|
|
|
|
|
*/
|
|
|
|
|
private void hasSameName(Area area) {
|
|
|
|
|
long count = areaService.count(new LambdaQueryWrapper<Area>()
|
|
|
|
|
.eq(Area::getParentId, area.getParentId())
|
|
|
|
|
.eq(Area::getAreaName, area.getAreaName())
|
|
|
|
|
.ne(Objects.nonNull(area.getAreaId()) && !Objects.equals(area.getAreaId(), 0L), Area::getAreaId, area.getAreaId())
|
|
|
|
|
);
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
throw new YamiShopBindException("该地区已存在");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|