| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.izouma.nineth.web;
- import com.izouma.nineth.domain.MetaSpatialInfo;
- import com.izouma.nineth.dto.MetaRestResult;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.MetaSpatialInfoRepo;
- import com.izouma.nineth.service.MetaSpatialInfoService;
- import com.izouma.nineth.utils.ObjUtils;
- import com.izouma.nineth.utils.excel.ExcelUtils;
- import jodd.util.StringUtil;
- import lombok.AllArgsConstructor;
- import org.springframework.data.domain.Page;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.List;
- import java.util.Objects;
- @RestController
- @RequestMapping("/metaSpatialInfo")
- @AllArgsConstructor
- public class MetaSpatialInfoController extends BaseController {
- private MetaSpatialInfoService metaSpatialInfoService;
- private MetaSpatialInfoRepo metaSpatialInfoRepo;
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/save")
- public MetaSpatialInfo save(@RequestBody MetaSpatialInfo record) {
- if (Objects.nonNull(record.getAssetId())) {
- MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findByAssetIdAndDel(record.getAssetId(), false);
- if (Objects.nonNull(metaSpatialInfo) && !Objects.equals(metaSpatialInfo.getId(), record.getId())) {
- throw new BusinessException("当前资产id已经存在");
- }
- }
- if (StringUtil.isNotBlank(record.getHcTxHash())) {
- MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findByHcTxHashAndDel(record.getHcTxHash(), false);
- if (Objects.nonNull(metaSpatialInfo) && !Objects.equals(metaSpatialInfo.getId(), record.getId())) {
- throw new BusinessException("当前hash已经存在");
- }
- }
- MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findByRegionAndCoordinateAndDel(record.getRegion(), record.getCoordinate(), false);
- if (Objects.nonNull(metaSpatialInfo) && !Objects.equals(metaSpatialInfo.getId(), record.getId())) {
- throw new BusinessException("当前区域内已经存在该坐标的空间");
- }
- if (record.getId() != null) {
- MetaSpatialInfo orig = metaSpatialInfoRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
- ObjUtils.merge(orig, record);
- if (Objects.isNull(record.getAssetId())) {
- orig.setAssetId(null);
- }
- if (Objects.isNull(record.getUserId())) {
- orig.setUserId(null);
- }
- return metaSpatialInfoRepo.save(orig);
- }
- return metaSpatialInfoRepo.save(record);
- }
- //@PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/all")
- public Page<MetaSpatialInfo> all(@RequestBody PageQuery pageQuery) {
- return metaSpatialInfoService.all(pageQuery);
- }
- @GetMapping("/get/{id}")
- public MetaSpatialInfo get(@PathVariable Long id) {
- return metaSpatialInfoRepo.findById(id).orElseThrow(new BusinessException("无记录"));
- }
- @PostMapping("/del/{id}")
- public void del(@PathVariable Long id) {
- metaSpatialInfoRepo.softDelete(id);
- }
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
- List<MetaSpatialInfo> data = all(pageQuery).getContent();
- ExcelUtils.export(response, data);
- }
- @GetMapping("/{userId}/find")
- public MetaRestResult<List<MetaSpatialInfo>> findByUserId(@PathVariable Long userId) {
- List<MetaSpatialInfo> metaSpatialInfos = metaSpatialInfoRepo.findAllByUserIdAndDel(userId, false);
- return MetaRestResult.returnSuccess(metaSpatialInfos);
- }
- @GetMapping("/onSale")
- public MetaRestResult<Integer> findOnSale() {
- int onSale = metaSpatialInfoRepo.countBySaleAndDel(true, false);
- return MetaRestResult.returnSuccess(onSale);
- }
- @GetMapping("/{id}/detail")
- public MetaRestResult<MetaSpatialInfo> detail(@PathVariable Long id) {
- MetaSpatialInfo metaSpatialInfo = metaSpatialInfoRepo.findById(id).orElse(null);
- if (Objects.isNull(metaSpatialInfo)) {
- return MetaRestResult.returnError(String.format("没有id为:%S的空间信息", id));
- }
- return MetaRestResult.returnSuccess(metaSpatialInfo);
- }
- }
|