1.执行sql
DROP TABLE IF EXISTS `aflc_city_tree`;
CREATE TABLE `aflc_city_tree` (
`id` varchar(64) NOT NULL COMMENT '编号',
`pid` varchar(64) DEFAULT NULL COMMENT '上级主键',
`level` int(4) DEFAULT NULL COMMENT '地区级别(1:省份province,2:市city,3:区县district)',
`name` varchar(80) NOT NULL COMMENT '名称',
`code` varchar(50) DEFAULT NULL COMMENT '区域编码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`del_flag` varchar(1) DEFAULT NULL COMMENT '删除状态删除状态(1:删除,0:未删除)',
PRIMARY KEY (`id`),
KEY `code` (`code`),
KEY `pid` (`pid`),
KEY `level` (`level`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='业务城市树';
2.实体类
@Data
public class AflcCityTreeListDto implements java.io.Serializable{
private static final long serialVersionUID = 1L;
@ApiModelProperty(value="编号",name="id")
private String id;
@ApiModelProperty(value="上级主键",name="pid")
private String pid;
@ApiModelProperty(value="名称",name="name")
private String name;
@ApiModelProperty(value="区域编码",name="code")
private String code;
@ApiModelProperty(value="当前层级",name="level")
private String level;
@ApiModelProperty(value="父类名称",name="pname")
private String pname;
private List children;
}
3.dao层
parameterType="com.anfachina.aflc.query.common.AflcCityTreeQuery">
select
from aflc_city_tree where
del_flag = 0
order by create_time desc
parameterType="com.anfachina.aflc.query.common.AflcCityTreeQuery">
select
from aflc_city_tree where
del_flag = 0 and level!=3
order by create_time desc
//三级城市树
List findCityPageListBySQL(@Param("currentPage") int currentPage, @Param("pageSize") int pageSize, @Param("vo") AflcCityTreeQuery vo);
//二级城市树
List findProvincePageListBySQL(@Param("currentPage") int currentPage, @Param("pageSize") int pageSize, @Param("vo") AflcCityTreeQuery vo);
4.service实现类
public List findTopNodes(List aflcCityTreeDto) {
List nodeList = new ArrayList<>();
for (AflcCityTreeListDto node1 : aflcCityTreeDto) {
boolean mark = false;
for (AflcCityTreeListDto node2 : aflcCityTreeDto) {
if (node1.getPid() != null && node1.getPid().equals(node2.getId())) {
mark = true;
node1.setPname(node2.getName());
if (node2.getChildren() == null) {
node2.setChildren(new ArrayList<>());
}
node2.getChildren().add(node1);
break;
}
}
if (!mark) {
nodeList.add(node1);
}
}
return nodeList;
}
@Override
public List findCityPageListBySQL(int currentPage, int pageSize, AflcCityTreeQuery vo) {
List sysMateDicts = this.aflcCityTreedao.findCityPageListBySQL(currentPage, pageSize, vo);
return findTopNodes(sysMateDicts);
}
@Override
public List findProvincePageListBySQL(int currentPage, int pageSize, AflcCityTreeQuery vo) {
List sysMateDicts = this.aflcCityTreedao.findProvincePageListBySQL(currentPage, pageSize, vo);
return findTopNodes(sysMateDicts);
}
5.controller
/**
* 获取三级业务城市树表
*
* @param model
* @return vo
* @throws Exception
*/
@GetMapping(value = "/treaCode")
@ApiOperation(value = "获取三级业务城市树表", notes = "获取三级业务城市树表")
@ApiImplicitParams({@ApiImplicitParam(paramType = "query", dataType = "String", name = "access_token", value = "授权token", required = true)})
public HttpResultVo findAflcCityTreeCode() throws Exception {
HttpResultVo vo = new HttpResultVo();
AflcCityTreeQuery cityTreeQuery = new AflcCityTreeQuery();
List list = this.aflcCityTreeService.findCityPageListBySQL(1, Integer.MAX_VALUE, cityTreeQuery);
vo.setData(list);
vo.setStatus(AnfaCommonServiceStatus.STATUS_SUCCESS);
vo.setErrorInfo(null);
return vo;
}
二级同理