123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- package com.zy.bms.controller;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.zy.bms.common.ServerResponse;
- import com.zy.bms.common.enums.ResponseCode;
- import com.zy.bms.entity.DeviceBase;
- import com.zy.bms.entity.User;
- import com.zy.bms.entity.UserPrivilege;
- import com.zy.bms.entity.lp.LpInfoState;
- import com.zy.bms.pojo.io.LpInfoRecordIO;
- import com.zy.bms.service.*;
- import com.zy.bms.service.lp.ILpInfoRecordService;
- import com.zy.bms.service.lp.ILpInfoStateService;
- import com.zy.bms.utils.AesUtil;
- import com.zy.bms.utils.RabbitMQApi;
- import com.zy.bms.utils.RandomCode;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- import java.util.*;
- @RestController
- @RequestMapping("/bms/api/wx_app")
- public class UbiLpWxController extends BaseController {
- @Resource
- private IUserService userService;
- @Resource
- private IUserPrivilegeService userPrivilegeService;
- @Resource
- private IGroupService groupService;
- @Resource
- private IDeviceBaseService deviceBaseService;
- @Resource
- private IPostMqttMsgService postMqttMsgService;
- @Resource
- private ILpInfoStateService lpInfoStateService;
- @Resource
- private ILpInfoRecordService lpInfoRecordService;
-
- @PostMapping("login.do")
- public ServerResponse login(String code) {
- String openId = userService.getWxAppId(code);
- if (openId == null) return ServerResponse.error();
- User user = userService.getOne(new QueryWrapper<User>().eq("open_id", openId));
-
- if (user == null) {
- user = new User();
- user.setOpenId(openId);
- user.setRandomCode(RandomCode.UUID_8());
- userService.save(user);
- }
- Map<String, String> res = new HashMap<>();
- res.put("userId", AesUtil.encrypt(user.getId().toString()));
- res.put("uuid", user.getRandomCode());
- res.put("admin", user.getAdmin().toString());
- return ServerResponse.success(res);
- }
-
- @GetMapping("connections.do")
- public ServerResponse connections() {
- User user = userService.getOne(new QueryWrapper<User>().eq("user_id", userId()));
- if (user == null || user.getAdmin() != 1) {
- return ServerResponse.custom(ResponseCode.AUTHOR);
- }
- return ServerResponse.success(RabbitMQApi.connections());
- }
-
- @GetMapping("users.do")
- public ServerResponse users() {
- User user = userService.getOne(new QueryWrapper<User>().eq("user_id", userId()));
- if (user == null || user.getAdmin() != 1) {
- return ServerResponse.custom(ResponseCode.AUTHOR);
- }
- return ServerResponse.success(RabbitMQApi.users());
- }
-
- @GetMapping("getGroups.do")
- public ServerResponse getGroupsByUserId() {
- return ServerResponse.success(groupService.getByUserId(userId()));
- }
-
- @GetMapping("checkOpenNum.do")
- public ServerResponse checkOpenNum(String openNum) {
- return ServerResponse.success(deviceBaseService.checkOpenNum(openNum));
- }
-
- @GetMapping("getDevicesByGroupId.do")
- public ServerResponse getDevicesByGroupId(String groupId) {
- Set<String> groupIds = new HashSet<>(userPrivilegeService.getGroupIdsByUserId(userId()));
- if (!groupIds.contains(groupId)) return ServerResponse.warning("无权限");
- return ServerResponse.success(deviceBaseService.listByGroupIdWx(groupId));
- }
-
- @GetMapping("getDevicesByUserId.do")
- public ServerResponse getDevicesByUserId() {
- List<String> groupIds = userPrivilegeService.getGroupIdsByUserId(userId());
- if (groupIds.isEmpty()) return ServerResponse.success(null);
- return ServerResponse.success(deviceBaseService.listByGroupIdsWx(groupIds));
- }
-
- @GetMapping("scan.do")
- public ServerResponse scan(String openNum) {
- DeviceBase device = deviceBaseService.getOne(new QueryWrapper<DeviceBase>().eq("open_num", openNum));
- if (device == null) return ServerResponse.warning("设备码错误!");
-
- UserPrivilege userPrivilege = userPrivilegeService.getOne(new QueryWrapper<UserPrivilege>()
- .eq("group_id", device.getGroupId()).eq("user_id", userId()));
- if (userPrivilege == null) {
- UserPrivilege entity = new UserPrivilege();
- entity.setUserId(userId());
- entity.setGroupId(device.getGroupId());
- userPrivilegeService.save(entity);
- }
- return ServerResponse.success(device.getType());
- }
-
- @GetMapping("getDeviceDetail.do")
- public ServerResponse getDeviceDetail(String openNum) {
- return ServerResponse.success(deviceBaseService.getDetailWx(openNum));
- }
-
- @PostMapping("updateLp.do")
- public ServerResponse updateLp(LpInfoState entity) {
- return ServerResponse.success(lpInfoStateService.updateWakeAndThresh(entity));
- }
-
- @GetMapping("buzzing.do")
- public ServerResponse buzzing(String openNum) {
- postMqttMsgService.requestBuzzingUbi(openNum);
- return ServerResponse.success();
- }
-
- @GetMapping("heartbeat.do")
- public ServerResponse heartbeat(String openNum) {
- postMqttMsgService.requestHeartbeatUbi(openNum);
- return ServerResponse.success();
- }
-
- @GetMapping("getLpHistoryData.do")
- public ServerResponse getLpHistoryData(LpInfoRecordIO io) {
- return ServerResponse.success(lpInfoRecordService.getHistoryList(io));
- }
- }
|