|
@@ -1,22 +1,18 @@
|
|
package cc.iotkit.manager.controller;
|
|
package cc.iotkit.manager.controller;
|
|
|
|
|
|
-import cc.iotkit.dao.*;
|
|
|
|
-import cc.iotkit.manager.model.vo.SpaceDeviceVo;
|
|
|
|
-import cc.iotkit.manager.model.vo.SpaceInfo;
|
|
|
|
-import cc.iotkit.model.*;
|
|
|
|
-import cc.iotkit.model.device.DeviceInfo;
|
|
|
|
-import cc.iotkit.model.product.Product;
|
|
|
|
-import cc.iotkit.model.space.SpaceDevice;
|
|
|
|
|
|
+import cc.iotkit.common.exception.BizException;
|
|
|
|
+import cc.iotkit.dao.HomeRepository;
|
|
|
|
+import cc.iotkit.dao.SpaceRepository;
|
|
|
|
+import cc.iotkit.manager.service.DataOwnerService;
|
|
|
|
+import cc.iotkit.manager.utils.AuthUtil;
|
|
|
|
+import cc.iotkit.model.space.Home;
|
|
|
|
+import cc.iotkit.model.space.Space;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.data.domain.Example;
|
|
|
|
-import org.springframework.data.domain.Sort;
|
|
|
|
-import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
@@ -24,51 +20,76 @@ import java.util.stream.Collectors;
|
|
public class SpaceController {
|
|
public class SpaceController {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
- private SpaceDeviceRepository spaceDeviceRepository;
|
|
|
|
|
|
+ private SpaceRepository spaceRepository;
|
|
@Autowired
|
|
@Autowired
|
|
- private DeviceRepository deviceRepository;
|
|
|
|
|
|
+ private HomeRepository homeRepository;
|
|
@Autowired
|
|
@Autowired
|
|
- private DeviceCache deviceCache;
|
|
|
|
- @Autowired
|
|
|
|
- private ProductCache productCache;
|
|
|
|
|
|
+ private DataOwnerService dataOwnerService;
|
|
|
|
+
|
|
|
|
+ @PostMapping("/saveHome/{id}")
|
|
|
|
+ public void saveHome(@PathVariable("id") String id, Home home) {
|
|
|
|
+ Optional<Home> optHome = homeRepository.findById(id);
|
|
|
|
+ if (!optHome.isPresent()) {
|
|
|
|
+ throw new BizException("home does not exist");
|
|
|
|
+ }
|
|
|
|
+ Home oldHome = optHome.get();
|
|
|
|
+ dataOwnerService.checkOwner(oldHome);
|
|
|
|
+ if (StringUtils.isNotBlank(home.getName())) {
|
|
|
|
+ oldHome.setName(home.getName());
|
|
|
|
+ }
|
|
|
|
+ if (StringUtils.isNotBlank(home.getAddress())) {
|
|
|
|
+ oldHome.setName(home.getAddress());
|
|
|
|
+ }
|
|
|
|
+ homeRepository.save(oldHome);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 我的空间设备列表
|
|
|
|
+ */
|
|
|
|
+ @GetMapping("/spaces")
|
|
|
|
+ public List<Space> getSpaces() {
|
|
|
|
+ return spaceRepository.findByUidOrderByCreateAtDesc(AuthUtil.getUserId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 在当前家庭中添加空间
|
|
|
|
+ */
|
|
|
|
+ @PostMapping("/add")
|
|
|
|
+ public void addSpace(String name) {
|
|
|
|
+ String uid = AuthUtil.getUserId();
|
|
|
|
+ Home currHome = homeRepository.findByUidAndCurrent(uid, true);
|
|
|
|
+ if (currHome == null) {
|
|
|
|
+ throw new BizException("current home does not exist");
|
|
|
|
+ }
|
|
|
|
+ spaceRepository.save(Space.builder()
|
|
|
|
+ .homeId(currHome.getId())
|
|
|
|
+ .name(name)
|
|
|
|
+ .uid(uid)
|
|
|
|
+ .createAt(System.currentTimeMillis())
|
|
|
|
+ .build());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @DeleteMapping("/delSpace/{id}")
|
|
|
|
+ public void delSpace(@PathVariable("id") String id) {
|
|
|
|
+ checkExistAndOwner(id);
|
|
|
|
+ spaceRepository.deleteById(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @PostMapping("/saveSpace/{id}")
|
|
|
|
+ public void saveSpace(@PathVariable("id") String id, String name) {
|
|
|
|
+ Space oldSpace = checkExistAndOwner(id);
|
|
|
|
+ oldSpace.setName(name);
|
|
|
|
+ spaceRepository.save(oldSpace);
|
|
|
|
+ }
|
|
|
|
|
|
-// @PostMapping("/list")
|
|
|
|
-// public Paging<SpaceInfo> getDevices(int page,
|
|
|
|
-// int limit,
|
|
|
|
-// String address) {
|
|
|
|
-// Criteria condition = new Criteria();
|
|
|
|
-// if (StringUtils.isNotBlank(address)) {
|
|
|
|
-// condition.and("address").regex(".*" + address + ".*");
|
|
|
|
-// }
|
|
|
|
-// List<UserInfo> userInfoList = userInfoDao.find(condition, (page - 1) * limit,
|
|
|
|
-// limit, Sort.Order.desc("createAt"));
|
|
|
|
-//
|
|
|
|
-// List<SpaceInfo> spaces = userInfoList.stream().map((u ->
|
|
|
|
-// new SpaceInfo(u.getAddress(), u.getUid())))
|
|
|
|
-// .collect(Collectors.toList());
|
|
|
|
-//
|
|
|
|
-// return new Paging<>(userInfoDao.count(condition),
|
|
|
|
-// spaces);
|
|
|
|
-// }
|
|
|
|
|
|
+ private Space checkExistAndOwner(String id) {
|
|
|
|
+ Optional<Space> optSpace = spaceRepository.findById(id);
|
|
|
|
+ if (!optSpace.isPresent()) {
|
|
|
|
+ throw new BizException("space does not exist");
|
|
|
|
+ }
|
|
|
|
|
|
- @GetMapping("/{userId}/devices")
|
|
|
|
- public List<SpaceDeviceVo> getDevices(@PathVariable("userId") String userId) {
|
|
|
|
- List<SpaceDeviceVo> deviceVos = new ArrayList<>();
|
|
|
|
- List<SpaceDevice> devices = spaceDeviceRepository.findAll(Example.of(SpaceDevice.builder().uid(userId).build()));
|
|
|
|
- devices.forEach(sd -> {
|
|
|
|
- DeviceInfo deviceInfo = deviceCache.findByDeviceId(sd.getDeviceId());
|
|
|
|
- Product product = productCache.findById(deviceInfo.getProductKey());
|
|
|
|
- deviceVos.add(SpaceDeviceVo.builder()
|
|
|
|
- .deviceId(sd.getDeviceId())
|
|
|
|
- .name(sd.getName())
|
|
|
|
- .picUrl(product.getImg())
|
|
|
|
- .spaceName(sd.getSpaceName())
|
|
|
|
- .online(deviceInfo.getState().isOnline())
|
|
|
|
- .property(deviceInfo.getProperty())
|
|
|
|
- .productKey(deviceInfo.getProductKey())
|
|
|
|
- .build());
|
|
|
|
- });
|
|
|
|
- return deviceVos;
|
|
|
|
|
|
+ dataOwnerService.checkOwner(optSpace.get());
|
|
|
|
+ return optSpace.get();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|