1. 需求分析
根据需求:层级只有2层,第一层为工作组分类,第二层为工作组,所以并不是树形结构
① 工作组分类的增删改查
② 工作组的增删改查
③ 查询所有的工作组分类和工作组分类下的工作组
2. 数据库表结构设计
设计两张表,一张表保存工作组分类,第二张表保存工作组,其中工作组表中需要保存工作组分类的id
create table t_group_category (
`id` int(20) NOT NULL AUTO_INCREMENT COMMENT '工作组分类id',
`name` varchar(50) NOT NULL COMMENT '工作组分类名称',
`createTime` datetime DEFAULT NULL COMMENT '创建时间',
`updateTime` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
create table t_group (
`int` varchar(20) NOT NULL AUTO_INCREMENT COMMENT '工作组id',
`name` varchar(50) NOT NULL COMMENT '工作组名称',
`categoryId` int(20) NOT NULL COMMENT '工作组分类id',
`createTime` datetime DEFAULT NULL COMMENT '创建时间',
`updateTime` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
3. 工作组分类实体类
@Data
@NoArgsConstructor
public class GroupCategory {
public GroupCategory(String name){
this.name = name;
this.createTime = new Date();
this.updateTime = new Date();
}
private Integer id;
private String name;
private Date createTime;
private Date updateTime;
@Transient
private List<Group> groupList;
}
4. 工作组实体类
@Data
@NoArgsConstructor
public class Group {
public Group(Integer categoryId,String name){
this.categoryId = categoryId;
this.name = name;
this.createTime = new Date();
this.updateTime = new Date();
}
private Integer id;
private String name;
private Integer categoryId;
private Date createTime;
private Date updateTime;
}
5. 查询列表
① Controller层
@Api(tags = "工作组分类管理")
@RestController
@RequestMapping("/api/v1")
@Slf4j
@ResponseResult
public class GroupCategoryController {
@Autowired
private GroupCategoryService groupCategoryService;
@ApiOperation(value = "查询工作组分类列表")
@OperateLog(target = "auth.group.category", action = "auth.group.category.list.get")
@GetMapping("/groupCategory")
public List<GroupCategory> groupCategoryList(){
return groupCategoryService.getList();
}
}
② Service层:
@Service
public class GroupCategoryServiceImpl implements GroupCategoryService {
@Autowired
private GroupCategoryDao groupCategoryDao;
@Autowired
private GroupDao groupDao;
@Override
public List<GroupCategory> getList() {
List<GroupCategory> groupCategoryList = groupCategoryDao.findGroupCategoryList();
if(CollectionUtils.isEmpty(groupCategoryList)){
return groupCategoryList;
}
for(GroupCategory groupCategory:groupCategoryList){
List<Group> groupList = groupDao.findGroupByCategoryId(groupCategory.getId());
groupCategory.setGroupList(groupList);
}
return groupCategoryList;
}
}
③ 响应结果:
{
"code": 0,
"message": "成功",
"data": [
{
"id": 1,
"name": "工作组分类1",
"createTime": "2022-03-25 17:10:49",
"updateTime": "2022-03-25 17:10:49",
"groupList": [
{
"id": 2,
"name": "研发组",
"categoryId": 1,
"createTime": "2022-03-28 15:15:06",
"updateTime": "2022-03-28 15:15:06"
},
{
"id": 3,
"name": "测试组",
"categoryId": 1,
"createTime": null,
"updateTime": null
}
]
},
{
"id": 2,
"name": "工作组分类2",
"createTime": "2022-03-28 16:18:56",
"updateTime": "2022-03-28 16:18:59",
"groupList": []
}
]
}
|