Java开发笔记7(区域树查询)

1、controller:

//根据区域ID查询详细信息

@GetMapping("/list")
public Result list(String name, String stationId) {
Set<String> stationIds;
if (org.apache.commons.lang3.StringUtils.isBlank(stationId)) {
stationIds = this.getStationIds();
} else {
stationIds = Sets.newHashSet();
stationIds.add(stationId);
}
return Result.ok(stationRegionService.list(name, stationIds));
}

2、service:

List<StationRegionVO> list(String name, Set<String> stationIds);

3、serviceImpl:

@Override
// @Cacheable(value = "stationRegionList")
public List<StationRegionVO> list(String name, Set<String> stationIds) {
if (stationIds == null || stationIds.size() == 0) {
if (StringUtils.isEmpty(name)) {
return stationRegionDao.findStationRegionVOWithRoot();
}
Map<String, Object> params = new HashMap<>(2);
StringBuffer jql = new StringBuffer(60);
jql.append("select new com.cars.ict.rbpsems.vo.base.StationRegionVO (d) from StationRegion d where d.name like :name order by d.index asc, d.code desc");
params.put("name", "%" + name + "%");
return stationRegionDao.findListByJQL(jql.toString(), params);

} else { //===================只查询到二级区域==================在这里修改成下面的以查询全部======================
if (StringUtils.isEmpty(name)) {
List<StationRegionVO> ret = stationRegionDao.findRoot(stationIds);
for (StationRegionVO stationRegionVO : ret) {
for (StationRegionVO child : stationRegionVO.getChildren()) {
if (child.getChildren()!= null && !child.getChildren().isEmpty()) {
child.getChildren().clear();
}
}
}
return ret;
}

Map<String, Object> params = new HashMap<>(4);

StringBuffer jql = new StringBuffer(60);
jql.append("select new com.cars.ict.rbpsems.vo.base.StationRegionVO (d) from StationRegion d where d.name like :name and d.station.id in (:stationIds ) order by d.index asc, d.code desc");
params.put("name", "%" + name + "%");
params.put("stationIds", stationIds);
return stationRegionDao.findListByJQL(jql.toString(), params);
}

}

注: 上面是只查询到二级区域,查询全部则修改成下面的代码:

} else {
if (StringUtils.isEmpty(name)) {
return stationRegionDao.findRoot(stationIds);
}
 

 

4、dao:

public interface StationRegionDao extends BaseDao<StationRegion, String> {

/**
* 查询根节点区域数据
* @return
*/
@Query("select new com.cars.ict.rbpsems.vo.base.StationRegionVO (d) from StationRegion d where d.parent is null order by d.index asc, d.code desc")
List<StationRegionVO> findStationRegionVOWithRoot();


/**
* 根据车站插叙根节点数据
* @param stationIds
* @return
*/
@Query("select new com.cars.ict.rbpsems.vo.base.StationRegionVO (d) from StationRegion d where d.parent is null and d.station.id in (:stationIds ) order by d.index asc, d.code desc")
List<StationRegionVO> findRoot(@Param("stationIds") Set<String> stationIds);


}

5、entity:

package com.cars.ict.rbpsems.entity.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

/**
* 车站区域
*
* @author mike
*/
@Entity
@Data
@Table(name = "b_station_region_dict")
public class StationRegion implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GenericGenerator(name = "uuid", strategy = "uuid")
@GeneratedValue(generator = "uuid")
@Column(length = 32, name = "s_id")
private String id;
/**
* 区域名称
*/
@Column(length = 100, name = "s_name")
private String name;

/**
* 区域编码
*/
@Column(length = 50, name = "s_code")
private String code;

/**
* 区域编码
*/
@Column(name = "s_area_code")
private String areaCode;

/**
* 区域类型简称
*/
@Column(length = 50, name = "s_abbreviation")
private String abbreviation;

/**
* 站点
*/
@ManyToOne
@JoinColumn(name = "s_station_id")
private Station station;

/**
* 类型: 0 区域 1 车站
*/
@Column(length = 50, name = "s_type")
private String type = "0";
/**
* 车站区域照片
*/
@Column(length = 50, name = "s_station_region_pic")
private String stationRegionPic = "";

/**
* 排序
*/
@Column(name = "i_index")
private Integer index;

/**
* 上级区域
*/
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "s_pid")
private StationRegion parent;

/**
* 车站区域是否被监控管理
*/
@Column(name = "s_ismonitored")
private String ismonitored = "0";

@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
private List<StationRegion> children;

public StationRegion() {
super();
}

public StationRegion(String name, String code, String abbreviation, Station station, String type,
StationRegion parent, List<StationRegion> children) {
super();
this.name = name;
this.code = code;
this.abbreviation = abbreviation;
this.station = station;
this.type = type;
this.parent = parent;
this.children = children;
}

public String getStationRegionPic() {
return stationRegionPic;
}

public void setStationRegionPic(String stationRegionPic) {
this.stationRegionPic = stationRegionPic;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public Station getStation() {
return station;
}

public void setStation(Station station) {
this.station = station;
}

public StationRegion getParent() {
return parent;
}

public void setParent(StationRegion parent) {
this.parent = parent;
}

public String getAbbreviation() {
return abbreviation;
}

public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}

public List<StationRegion> getChildren() {
return children;
}

public void setChildren(List<StationRegion> children) {
this.children = children;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getIsmonitored() {
return ismonitored;
}

public void setIsmonitored(String ismonitored) {
this.ismonitored = ismonitored;
}

public Integer getIndex() {
return index;
}

public void setIndex(Integer index) {
this.index = index;
}
}

 

6、vo:

 

 

package com.cars.ict.rbpsems.vo.base;


import com.cars.ict.rbpsems.entity.base.StationRegion;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* 车站区域VO
*
* @author mike
*/
public class StationRegionVO implements Serializable {
private String id;
/**
* 区域名称
*/
private String name;

/**
* 区域编码
*/
private String code;

/**
* 区域类型简称
*/
private String abbreviation;


/**
* 类型: 区域类型--> 标准区域模版1 -->自定义
*/
private String type = "自定义";
/**
* 车站区域照片
*/
private String stationRegionPic = "";

/**
* 排序
*/
private Integer index;

private List<StationRegionVO> children;

public StationRegionVO() {
super();
}

public StationRegionVO(StationRegion stationRegion) {
super();
if (stationRegion != null) {
this.id = stationRegion.getId();
this.name = stationRegion.getName();
this.code = stationRegion.getCode();
this.abbreviation = stationRegion.getAbbreviation();
this.type = stationRegion.getType();
this.stationRegionPic = stationRegion.getStationRegionPic();
this.index = stationRegion.getIndex();
this.children = StationRegionVO.stationRegionToVO(stationRegion.getChildren());
}
}

public static StationRegionVO stationRegionToVO(StationRegion stationRegion) {
if (stationRegion != null) {
StationRegionVO stationRegionVO = new StationRegionVO();
stationRegionVO.setId(stationRegion.getId());
stationRegionVO.setName(stationRegion.getName());
stationRegionVO.setCode(stationRegion.getCode());
stationRegionVO.setAbbreviation(stationRegion.getAbbreviation());
stationRegionVO.setType(stationRegion.getType());
stationRegionVO.setStationRegionPic(stationRegion.getStationRegionPic());
stationRegionVO.setIndex(stationRegion.getIndex());
if (stationRegion.getChildren() != null && stationRegion.getChildren().size() != 0) {
stationRegionVO.setChildren(StationRegionVO.stationRegionToVO(stationRegion.getChildren()));
}
return stationRegionVO;
}
return null;
}

public static List<StationRegionVO> stationRegionToVO(List<StationRegion> stationRegionList) {
if (stationRegionList != null && stationRegionList.size() != 0) {
return stationRegionList.stream().map(StationRegionVO::stationRegionToVO)
.sorted((o1, o2) -> {
if (o1 != null && o2 != null) {
Integer x = Optional.ofNullable(o1.getIndex()).orElse(999);
Integer y = Optional.ofNullable(o2.getIndex()).orElse(999);
return Integer.compare(x, y);
} else {
return 0;
}
})
.collect(Collectors.toList());
}
return null;
}


public String getStationRegionPic() {
return stationRegionPic;
}

public void setStationRegionPic(String stationRegionPic) {
this.stationRegionPic = stationRegionPic;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}


public String getAbbreviation() {
return abbreviation;
}

public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}

public List<StationRegionVO> getChildren() {
return children;
}

public void setChildren(List<StationRegionVO> children) {
this.children = children;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Integer getIndex() {
return index;
}

public void setIndex(Integer index) {
this.index = index;
}
}

 

 

 

============================对应区域树的增删改================================

7、增: 
1.controller:
/**
* 添加区域
*
* @return
*/
@PostMapping("/save")
public Result save(@RequestBody StationRegionDTO dto) {
return Result.ok(stationRegionService.save(dto));
}
2、service:
String save(StationRegionDTO dto);
3、Impl:
@Transactional
@Override
public String save(StationRegionDTO dto) {
StationRegion region = getStationRegion(dto);
StationRegion save = stationRegionDao.save(region);
return save.getId();
}


4、其中的getStationRegion()方法:(因为使用save方法,所以要对dto进行构建)
/**
* 构建车站区域信息
* @param dto
* @return com.cars.ict.rbpsems.entity.base.StationRegion
* @author xueyj
* @date 2019/7/23-9:51
*/
private StationRegion getStationRegion(StationRegionDTO dto) {
StationRegion region = new StationRegion();
region.setName(dto.getName());
region.setCode(generateCode(dto.getPid()));
region.setAbbreviation(dto.getAbbreviation());
String pid = dto.getPid();
if (!StringUtils.isEmpty(pid)) {
StationRegion parent = stationRegionDao.findById(pid).orElse(null);
if (parent != null) {
region.setParent(parent);
region.setStation(parent.getStation());
}
}
/**
* @Description:添加自定义区域照片
*/
if (!StringUtils.isEmpty(dto.getStationRegionPic())) {
String path = filePath + dto.getStationRegionPic();
File file = new File(path);
if (file.exists()) {
region.setStationRegionPic(dto.getStationRegionPic());
}
}
return region;
}
 
5、dto:
 
package com.cars.ict.rbpsems.dto.base;
/**
* StationRegionDTO class
*
* @author duke.ma
* @date 2019.0408
*/
public class StationRegionDTO implements java.io.Serializable{

private String id;

/**
* 区域名称
*/
private String name;

/**
* 区域简称
*/
private String abbreviation;

/**
* 区域编码
*/
private String code;

/**
* 站点id
*
*/
private String stationId;

/**
* 上级区域ID
*/
private String pid;


/**
* 车站区域照片
*/
private String stationRegionPic;


/**
* 类型
*/
private String type;


private Integer index;

public String getStationRegionPic() {
return stationRegionPic;
}

public void setStationRegionPic(String stationRegionPic) {
this.stationRegionPic = stationRegionPic;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getStationId() {
return stationId;
}

public void setStationId(String stationId) {
this.stationId = stationId;
}

public String getPid() {
return pid;
}

public void setPid(String pid) {
this.pid = pid;
}

public String getAbbreviation() {
return abbreviation;
}

public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Integer getIndex() {
return index;
}

public void setIndex(Integer index) {
this.index = index;
}

@Override
public String toString() {
return "StationRegionDTO{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", abbreviation='" + abbreviation + '\'' +
", code='" + code + '\'' +
", stationId='" + stationId + '\'' +
", pid='" + pid + '\'' +
", stationRegionPic='" + stationRegionPic + '\'' +
", type='" + type + '\'' +
'}';
}
}



8、删: 
1、controller:
/**
* 删除区域
*
* @param id
* @return
*/
@PostMapping("/delete/{id}")
public Result delete(@PathVariable("id") String id) {
try {
stationRegionService.delete(id);
return Result.ok(id);
} catch (Exception e) {
return Result.error(id, Constant.EXISTS_RELATION_NOT_DELETE_MSG);
}
}
2、service:
void delete(String id);

3、serviceImpl:
@Transactional
@Override
public void delete(String id) {
// 根据设备区域简称,查找区域信息
StationRegion stationRegion = stationRegionDao.findByAbbreviation(STATION_REGION);
// 根据区域id,更新设备区域信息
deviceService.updateDeviceStationRegion(id, stationRegion);
// 删除区域信息
stationRegionDao.deleteById(id);
}
4.dao:

public interface DeviceService {
 
   /**
* 根据区域简称,查找区域信息
* @param Abbreviation
* @return com.cars.ict.rbpsems.entity.base.StationRegion
* @author xueyj
* @date 2019/7/19-11:24
*/
StationRegion findByAbbreviation(String Abbreviation);
 
/**
* 更新设备区域id
*
* @param stationRegionId 区域id
* @param stationRegion 区域信息
* @return void
* @author xueyj
* @date 2019/7/19-11:02
*/
void updateDeviceStationRegion(String stationRegionId, StationRegion stationRegion);


9、改: 
1.controller:

@PostMapping("/update")
public Result update(@RequestBody StationRegionDTO dto) {
try {
stationRegionService.update(dto);
return Result.ok(dto.getId());
} catch (Exception e) {
return Result.error(dto.getId(), e.getMessage());
}

}
2、service:
void update(StationRegionDTO dto) throws Exception;
3、serviceImpl:
    @Transactional
@Override
public void update(StationRegionDTO dto) throws Exception {
StationRegion region = stationRegionDao.findById(dto.getId()).orElse(null);
// 拖拽修改pid
StationRegion pr = null;

if (!StringUtils.isEmpty(dto.getPid())) {
pr = stationRegionDao.findById(dto.getPid()).orElse(null);
}
if (region != null) {
if (pr != null) {
region.setParent(pr);
}
if (dto.getIndex() != null) {
// 修改index
int num = Optional.ofNullable(region.getIndex()).orElse(0) - Optional.ofNullable(dto.getIndex()).orElse(0);
if (num > 0) {
stationRegionDao.updateIndexUp(dto.getPid(), dto.getIndex(), region.getIndex());
} else if (num < 0) {
stationRegionDao.updateIndexOff(dto.getPid(), dto.getIndex(), region.getIndex());
}
region.setIndex(dto.getIndex());
}
if (!StringUtils.isEmpty(dto.getName())) {
region.setName(dto.getName());
}
if (!StringUtils.isEmpty(dto.getAbbreviation())) {
region.setAbbreviation(dto.getAbbreviation());
}
stationRegionDao.save(region);

/**
* @Description:更新自定义区域照片
*/
if (!StringUtils.isEmpty(dto.getStationRegionPic())) {
String path = filePath + dto.getStationRegionPic();
File file = new File(path);
if (file.exists()) {
region.setStationRegionPic(dto.getStationRegionPic());
stationRegionDao.save(region);
}
}
} else {
throw new Exception(Constant.NOT_EXISTS_MSG); //数据不存在
}
}
4、dao:


public interface StationRegionDao extends BaseDao<StationRegion, String> {

@Value("${upload.image}")
private String filePath;


@Modifying
@Transactional
@Query("update StationRegion o set o.index =o.index+1 where o.parent.id =:pid and o.index >=:idx and o.index <:num")
int updateIndexUp(@Param("pid") String pid,@Param("idx") Integer index,@Param("num") Integer num);
 
@Modifying
@Transactional
@Query("update StationRegion o set o.index =o.index-1 where o.parent.id =:pid and o.index <=:idx and o.index >:num")
int updateIndexOff(@Param("pid") String pid,@Param("idx") Integer index,@Param("num") Integer num);


}





posted @   sensen~||^_^|||&  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示