1、目标
{
"success": true,
"status": 200,
"message": "获取成功",
"data": [
{
"id": "100",
"className": "法外狂徒",
"children": [
{
"id": "101",
"className": "A站点",
"children": [
{
"id": "103",
"className": "A下站点a",
"children": [
{
"id": "106",
"className": "Aa3站点",
"children": [],
"ancestors": "0,100,101,103",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
}
],
"ancestors": "0,100,101",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
},
{
"id": "104",
"className": "A下站点b",
"children": [
{
"id": "113",
"className": "王五",
"children": [],
"ancestors": "0,100,101,104",
"companyId": "40",
"creatorId": "111",
"creatorTime": null,
"updaterId": null,
"updateTime": null
}
],
"ancestors": "0,100,101",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
}
],
"ancestors": "0,100",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
},
{
"id": "102",
"className": "B站点",
"children": [
{
"id": "105",
"className": "B站点a",
"children": [],
"ancestors": "0,100.102",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
}
],
"ancestors": "0,100",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
}
],
"ancestors": "0",
"companyId": "40",
"creatorId": null,
"creatorTime": "2021-08-20 16:16:06",
"updaterId": null,
"updateTime": null
},
{
"id": "112",
"className": "王五",
"children": [],
"ancestors": "0",
"companyId": "40",
"creatorId": "111",
"creatorTime": null,
"updaterId": null,
"updateTime": null
},
{
"id": "114",
"className": "13分点",
"children": [],
"ancestors": "0",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
},
{
"id": "115",
"className": "14分点",
"children": [],
"ancestors": "0",
"companyId": "40",
"creatorId": null,
"creatorTime": null,
"updaterId": null,
"updateTime": null
}
]
}
2、数据库表
表内容
3.实体类
@Data
public class EquipmentClass {
private Long id; //分类ID
private String className; //分类名称
private String parentId; //父级ID
private String ancestors; //祖级列表
private String companyId; //公司ID
private String creatorId; //创建人ID
private String creatorTime; //创建时间
private String updaterId; //更新人
private String updateTime; //更新时间
}
tree实体类
//分类节点类
@Data
public class TreeNode implements Serializable {
private String id; //分类ID
private String className; //分类名称
private List<TreeNode> children = new ArrayList<TreeNode>();
private String ancestors; //祖级列表
private String companyId; //公司ID
private String creatorId; //创建人ID
private String creatorTime; //创建时间
private String updaterId; //更新人
private String updateTime; //更新时间
}
4、Mapper
<!--查询列表 getProjectList-->
<select id="getProjectList" resultMap="ProjectMap">
<bind name="notIdlist" value="(pageNum-1)*rowCount"/>
select
*
from
om_fnd_project_mgmt
where
company_id = #{companyId}
<if test="searchContent !=null">
and project_name like '%${searchContent}%'
</if>
order by
project_id
desc
<if test="pageNum!=0">
LIMIT
#{notIdlist},#{rowCount}
</if>
</select>
5、DAO层
@Mapper
public interface EquipmentClassDao {
//获取分类
public List<EquipmentClass> getEquipmentClassList(String companyId) throws Exception;
}
6、service层
public interface EquipmentClassService {
public Object getEquipmentClassList(String companyId) throws Exception;
}
7、serviceImpl层
@Service("EquipmentClassService")
@Transactional
public class EquipmentClassServiceImpl implements EquipmentClassService {
@Autowired
private EquipmentClassDao equipmentClassDao;
@Override
public Object getEquipmentClassList(String companyId) throws Exception {
List<EquipmentClass> equipmentClassList = equipmentClassDao.getEquipmentClassList(companyId);
System.out.println(equipmentClassList);
List<EquipmentClass> dList = equipmentClassList;
Set<String> idSet=new HashSet<>();
Map<String, List<TreeNode>> map=new HashMap<String, List<TreeNode>>();
TreeNode node=null;
for (EquipmentClass equipmentClass : dList) {
if(!idSet.contains(equipmentClass.getId())) {
node = new TreeNode();
node.setId(equipmentClass.getId()+""); //ID
node.setClassName(equipmentClass.getClassName()); //名字
node.setAncestors(equipmentClass.getAncestors()); //祖级列表
node.setCompanyId(equipmentClass.getCompanyId()); //公司ID
node.setCreatorId(equipmentClass.getCreatorId()); //创建人ID
node.setCreatorTime(equipmentClass.getCreatorTime());//创建时间
node.setUpdaterId(equipmentClass.getUpdaterId());//更新人
node.setCreatorTime(equipmentClass.getUpdateTime());//更新时间
if(map.get(equipmentClass.getParentId())==null) { //生成父ID节点
map.put(equipmentClass.getParentId()+"", new ArrayList<>());
}
map.get(equipmentClass.getParentId()).add(node);
idSet.add(equipmentClass.getId()+"");
}
}
Iterator<String> it=map.keySet().iterator();
String id=null;
while(it.hasNext()) {
id = it.next();
List<TreeNode> childeNodes=map.get(id);
for (TreeNode treeNode : childeNodes) {
if(map.get(treeNode.getId())!=null) {
treeNode.getChildren().addAll(map.get(treeNode.getId()));
}
}
}
// System.out.println(JSONObject.toJSON(map.get("0")));
// Object object = JSONObject.toJSON(map.get(100));
// System.out.println(map.get("0"));
return new Result(200,"获取成功",true,map.get("0"));
}
}
8、Controller层
@Api(tags = "设备分类")
@ApiSort(3)
@RequestMapping("/equipmentClass")
@RestController
@CrossOrigin
public class EquipmentClassController {
@Autowired
private EquipmentClassService equipmentClassService;
@ApiOperation(value = "设备分类tree结构")
@RequestMapping(value="/getEquipmentClassList",method= RequestMethod.GET)
@CrossOrigin
public Object getEquipmentClassList(@RequestParam String companyId) throws Exception {
return equipmentClassService.getEquipmentClassList(companyId);
}