Java开发笔记6(添加区域)(小小)
1.Controller:
/**
* 添加区域
*
* @return
*/
@PostMapping("/save")
public Result save(@RequestBody StationRegionDTO dto) {
return Result.ok(stationRegionService.save(dto));
}
2.Service:
/**
* 根据dto保存区域数据
*
* @param dto
* @return
*/
String save(StationRegionDTO dto);
3.ServiceImpl:
@Transactional
@Override
public String save(StationRegionDTO dto) {
StationRegion region = getStationRegion(dto);
StationRegion save = stationRegionDao.save(region);
return save.getId();
}
/**
* 构建车站区域信息方法
* @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());
//加入排序 2022/2/18 gs
region.setIndex(dto.getIndex());
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;
}
4.DTO:
package com.cars.ict.rbpsems.dto.base;
/**
* StationRegionDTO class
*
* @author duke.ma
* @date 2019.04。08
*/
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='" +