package com.izouma.nineth.web; import com.izouma.nineth.domain.MetaStore; import com.izouma.nineth.domain.MetaUserProp; import com.izouma.nineth.dto.MetaRestResult; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.enums.MetaPropUsedType; import com.izouma.nineth.enums.MetaStoreCommodityType; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.MetaStoreRepo; import com.izouma.nineth.repo.MetaUserPropRepo; import com.izouma.nineth.service.MetaStoreService; import com.izouma.nineth.utils.ObjUtils; import com.izouma.nineth.utils.SecurityUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; import org.apache.commons.collections.CollectionUtils; 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("/metaStore") @AllArgsConstructor public class MetaStoreController extends BaseController { private MetaStoreService metaStoreService; private MetaStoreRepo metaStoreRepo; private MetaUserPropRepo metaUserPropRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public MetaStore save(@RequestBody MetaStore record) { if (Objects.isNull(record.getId())) { if (MetaStoreCommodityType.META_PROP.equals(record.getCommodityType()) && !checkMetaProp(record.getMetaPropId())) { throw new BusinessException("商店中已经存在该道具"); } if (MetaStoreCommodityType.NFT.equals(record.getCommodityType())) { MetaStore metaStore = metaStoreRepo.findByCommodityTypeAndNameAndDel(MetaStoreCommodityType.NFT, record.getName(), false); if (Objects.nonNull(metaStore)) { throw new BusinessException("商店中已经存在该NFT"); } } return metaStoreRepo.save(record); } MetaStore orig = metaStoreRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); return metaStoreRepo.save(orig); } @PostMapping("/{id}/onShelf") public void onShelf(@PathVariable Long id) { MetaStore metaStore = metaStoreRepo.findByIdAndDel(id, false); if (Objects.isNull(metaStore)) { throw new BusinessException("无记录"); } if (metaStore.isOnShelf()) { throw new BusinessException("当前商品已经处于上架状态!"); } metaStore.setOnShelf(true); metaStoreRepo.save(metaStore); } @PostMapping("/{id}/cancelOnShelf") public void cancelOnShelf(@PathVariable Long id) { MetaStore metaStore = metaStoreRepo.findByIdAndDel(id, false); if (Objects.isNull(metaStore)) { throw new BusinessException("无记录"); } if (!metaStore.isOnShelf()) { throw new BusinessException("当前商品已经处于下架状态!"); } metaStore.setOnShelf(false); metaStoreRepo.save(metaStore); } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { return metaStoreService.all(pageQuery); } @GetMapping("/get/{id}") public MetaStore get(@PathVariable Long id) { return metaStoreRepo.findById(id).orElseThrow(new BusinessException("无记录")); } @PostMapping("/del/{id}") public void del(@PathVariable Long id) { metaStoreRepo.softDelete(id); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } @GetMapping("/{metaPropId}/checkMetaProp") public boolean checkMetaProp(@PathVariable Long metaPropId) { MetaStore metaStore = metaStoreRepo.findByCommodityTypeAndMetaPropIdAndDel(MetaStoreCommodityType.META_PROP, metaPropId, false); return Objects.isNull(metaStore); } @PostMapping("/{id}/purchase") public MetaRestResult purchase(@PathVariable Long id) { return metaStoreService.purchase(id); } @GetMapping("findByType") public MetaRestResult> findByType(MetaStoreCommodityType commodityType) { List metaStores = metaStoreRepo.findAllByCommodityTypeAndOnShelfAndDel(commodityType, true, false); if (CollectionUtils.isEmpty(metaStores)) { return MetaRestResult.returnSuccess(metaStores); } if (MetaStoreCommodityType.META_PROP.equals(commodityType)) { Long userId = SecurityUtils.getAuthenticatedUser().getId(); metaStores.forEach(metaStore -> { MetaUserProp metaUserProp = metaUserPropRepo.findByUserIdAndMetaPropIdAndDel(userId, metaStore.getMetaPropId(), false); if (Objects.nonNull(metaUserProp) && MetaPropUsedType.PERMANENT.equals(metaUserProp.getUsedType()) && metaUserProp.getNum() >= 1) { metaStore.setCanPurchase(false); } }); } return MetaRestResult.returnSuccess(metaStores); } }