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 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 data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } @GetMapping("/{userId}/find") public MetaRestResult> findByUserId(@PathVariable Long userId) { List metaSpatialInfos = metaSpatialInfoRepo.findAllByUserIdAndDel(userId, false); return MetaRestResult.returnSuccess(metaSpatialInfos); } @GetMapping("/onSale") public MetaRestResult findOnSale() { int onSale = metaSpatialInfoRepo.countBySaleAndDel(true, false); return MetaRestResult.returnSuccess(onSale); } @GetMapping("/{id}/detail") public MetaRestResult 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); } }