提交 5ae61b77 authored 作者: 黄承天's avatar 黄承天

[设置模块]增加统计图设置相关接口

上级 82c014ea
package com.tykj.setting.controller;
import com.tykj.base.result.ResultUtil;
import com.tykj.setting.entity.vo.StatisticsSetting;
import com.tykj.setting.service.StatisticsSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@Api(tags = "统计图设置相关接口")
@RequestMapping("/setting/statistics")
@RestController
public class StatisticsSettingController {
@Autowired
private StatisticsSettingService statisticsSettingService;
@PostMapping
@ApiOperation("新增统计图设置")
public ResponseEntity save(@RequestBody StatisticsSetting statisticsSetting){
statisticsSettingService.save(statisticsSetting);
return ResultUtil.success("", "保存成功");
}
@PutMapping
@ApiOperation("更新统计图设置")
public ResponseEntity update(@RequestBody StatisticsSetting statisticsSetting){
statisticsSettingService.update(statisticsSetting);
return ResultUtil.success("", "更新成功");
}
@GetMapping
@ApiOperation("查找所有统计图设置")
public ResponseEntity findAll(){
return ResultUtil.success(statisticsSettingService.findAll(),"查找成功");
}
@GetMapping("/limit/{limit}")
@ApiOperation("指定数量查找序号前几个统计图设置")
public ResponseEntity findLimit(@PathVariable Integer limit){
return ResultUtil.success(statisticsSettingService.findByLimit(limit),"查找成功");
}
@GetMapping("/byId/{id}")
@ApiOperation("按id查找单个统计图设置")
public ResponseEntity findById(@PathVariable Integer id){
return ResultUtil.success(statisticsSettingService.findById(id),"查找成功");
}
@DeleteMapping("/{id}")
@ApiOperation("按id删除单个统计图设置")
public ResponseEntity deleteById(@PathVariable Integer id){
statisticsSettingService.deleteById(id);
return ResultUtil.success("", "删除成功");
}
}
package com.tykj.setting.entity.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
public class StatisticsSetting {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty("主键")
private Integer id;
@ApiModelProperty("名称 ")
private String name;
@ApiModelProperty("类型")
private Integer type;
@ApiModelProperty("序号")
private Integer order;
@ApiModelProperty("参数内容")
private String params;
}
package com.tykj.setting.repository;
import com.tykj.setting.entity.vo.StatisticsSetting;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface StatisticsSettingRepository extends JpaRepository<StatisticsSetting,Integer> {
List<StatisticsSetting> findAllByOrderGreaterThanEqual(Integer index);
List<StatisticsSetting> findAllByOrderLessThanEqual(Integer index);
}
package com.tykj.setting.service;
import com.tykj.setting.entity.vo.StatisticsSetting;
import com.tykj.setting.repository.StatisticsSettingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@Service
public class StatisticsSettingService {
@Autowired
private StatisticsSettingRepository statisticsSettingRepository;
public void save(StatisticsSetting statisticsSetting) {
boolean newData = Objects.isNull(statisticsSetting.getId());
if (newData) {
handleAndSave(statisticsSetting);
} else {
throw new RuntimeException("新增数据不可附带id");
}
}
public void update(StatisticsSetting statisticsSetting) {
boolean exist = Objects.nonNull(statisticsSetting.getId()) && statisticsSettingRepository.existsById(statisticsSetting.getId());
if (exist) {
handleAndSave(statisticsSetting);
} else {
throw new RuntimeException("未找到该id的数据");
}
}
public List<StatisticsSetting> findByLimit(Integer limit) {
return statisticsSettingRepository.findAllByOrderLessThanEqual(limit);
}
public List<StatisticsSetting> findAll() {
return statisticsSettingRepository.findAll();
}
public StatisticsSetting findById(Integer id) {
return statisticsSettingRepository.findById(id).orElseThrow(() -> new RuntimeException("未找到该id的数据"));
}
public void deleteById(Integer id) {
StatisticsSetting statisticsSetting = statisticsSettingRepository.findById(id).orElseThrow(() -> new RuntimeException("未找到该id的数据"));
Integer order = statisticsSetting.getOrder();
statisticsSettingRepository.deleteById(id);
//矫正后排数据的序号
List<StatisticsSetting> otherStatisticsSettingList = statisticsSettingRepository.findAllByOrderGreaterThanEqual(order);
for (StatisticsSetting setting : otherStatisticsSettingList) {
setting.setOrder(setting.getOrder() - 1);
statisticsSettingRepository.save(setting);
}
}
private void handleAndSave(StatisticsSetting statisticsSetting) {
Integer order = statisticsSetting.getOrder();
long count = statisticsSettingRepository.count();
if (Objects.isNull(order) || order > count + 1 || order < 0) {
//如果检查到序号值非法 则强制将序号赋值为末尾序号
statisticsSetting.setOrder(Math.toIntExact(count + 1));
} else {
//矫正后排数据的序号
List<StatisticsSetting> otherStatisticsSettingList = statisticsSettingRepository.findAllByOrderGreaterThanEqual(statisticsSetting.getOrder());
for (StatisticsSetting setting : otherStatisticsSettingList) {
setting.setOrder(setting.getOrder() + 1);
statisticsSettingRepository.save(setting);
}
}
statisticsSettingRepository.save(statisticsSetting);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论