MetaSpatialInfoController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.MetaSpatialInfo;
  3. import com.izouma.nineth.dto.MetaRestResult;
  4. import com.izouma.nineth.dto.PageQuery;
  5. import com.izouma.nineth.exception.BusinessException;
  6. import com.izouma.nineth.repo.MetaSpatialInfoRepo;
  7. import com.izouma.nineth.service.MetaSpatialInfoService;
  8. import com.izouma.nineth.utils.ObjUtils;
  9. import com.izouma.nineth.utils.excel.ExcelUtils;
  10. import jodd.util.StringUtil;
  11. import lombok.AllArgsConstructor;
  12. import org.springframework.data.domain.Page;
  13. import org.springframework.web.bind.annotation.*;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.util.List;
  17. import java.util.Objects;
  18. @RestController
  19. @RequestMapping("/metaSpatialInfo")
  20. @AllArgsConstructor
  21. public class MetaSpatialInfoController extends BaseController {
  22. private MetaSpatialInfoService metaSpatialInfoService;
  23. private MetaSpatialInfoRepo metaSpatialInfoRepo;
  24. //@PreAuthorize("hasRole('ADMIN')")
  25. @PostMapping("/save")
  26. public MetaSpatialInfo save(@RequestBody MetaSpatialInfo record) {
  27. if (Objects.nonNull(record.getAssetId())) {
  28. MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findByAssetIdAndDel(record.getAssetId(), false);
  29. if (Objects.nonNull(metaSpatialInfo) && !Objects.equals(metaSpatialInfo.getId(), record.getId())) {
  30. throw new BusinessException("当前资产id已经存在");
  31. }
  32. }
  33. if (StringUtil.isNotBlank(record.getHcTxHash())) {
  34. MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findByHcTxHashAndDel(record.getHcTxHash(), false);
  35. if (Objects.nonNull(metaSpatialInfo) && !Objects.equals(metaSpatialInfo.getId(), record.getId())) {
  36. throw new BusinessException("当前hash已经存在");
  37. }
  38. }
  39. MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findByRegionAndCoordinateAndDel(record.getRegion(), record.getCoordinate(), false);
  40. if (Objects.nonNull(metaSpatialInfo) && !Objects.equals(metaSpatialInfo.getId(), record.getId())) {
  41. throw new BusinessException("当前区域内已经存在该坐标的空间");
  42. }
  43. if (record.getId() != null) {
  44. MetaSpatialInfo orig = metaSpatialInfoRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  45. ObjUtils.merge(orig, record);
  46. if (Objects.isNull(record.getAssetId())) {
  47. orig.setAssetId(null);
  48. }
  49. if (Objects.isNull(record.getUserId())) {
  50. orig.setUserId(null);
  51. }
  52. return metaSpatialInfoRepo.save(orig);
  53. }
  54. return metaSpatialInfoRepo.save(record);
  55. }
  56. //@PreAuthorize("hasRole('ADMIN')")
  57. @PostMapping("/all")
  58. public Page<MetaSpatialInfo> all(@RequestBody PageQuery pageQuery) {
  59. return metaSpatialInfoService.all(pageQuery);
  60. }
  61. @GetMapping("/get/{id}")
  62. public MetaSpatialInfo get(@PathVariable Long id) {
  63. return metaSpatialInfoRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  64. }
  65. @PostMapping("/del/{id}")
  66. public void del(@PathVariable Long id) {
  67. metaSpatialInfoRepo.softDelete(id);
  68. }
  69. @GetMapping("/excel")
  70. @ResponseBody
  71. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  72. List<MetaSpatialInfo> data = all(pageQuery).getContent();
  73. ExcelUtils.export(response, data);
  74. }
  75. @GetMapping("/{userId}/find")
  76. public MetaRestResult<List<MetaSpatialInfo>> findByUserId(@PathVariable Long userId) {
  77. List<MetaSpatialInfo> metaSpatialInfos = metaSpatialInfoRepo.findAllByUserIdAndDel(userId, false);
  78. return MetaRestResult.returnSuccess(metaSpatialInfos);
  79. }
  80. @GetMapping("/onSale")
  81. public MetaRestResult<Integer> findOnSale() {
  82. int onSale = metaSpatialInfoRepo.countBySaleAndDel(true, false);
  83. return MetaRestResult.returnSuccess(onSale);
  84. }
  85. @GetMapping("/{id}/detail")
  86. public MetaRestResult<MetaSpatialInfo> detail(@PathVariable Long id) {
  87. MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findById(id).orElse(null);
  88. if (Objects.isNull(metaSpatialInfo)) {
  89. return MetaRestResult.returnError(String.format("没有id为:%S的空间信息", id));
  90. }
  91. return MetaRestResult.returnSuccess(metaSpatialInfo);
  92. }
  93. }