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='" + pid + '\'' + ", stationRegionPic='" + stationRegionPic + '\'' + ", type='" + type + '\'' + '}'; } } 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; } }