Ver código fonte

#ADD 商户设备授权

谭斌 2 anos atrás
pai
commit
db0647793d

+ 70 - 0
device-api-service/src/main/java/com/xy/service/DeviceInfoServiceImpl.java

@@ -1,7 +1,9 @@
 package com.xy.service;
 
+import cn.hutool.core.collection.CollUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.xy.collections.list.JArrayList;
 import com.xy.collections.list.JList;
@@ -22,6 +24,7 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -174,6 +177,73 @@ public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceI
         return queryPage(page);
     }
 
+    @ApiOperation("商户设备授权")
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public R<Boolean> mercDeviceAuth(DeviceInfoDto.MercDeviceAuthDto auth) {
+        Long mercId = auth.getMercId();
+        //商户最终设备列表
+        List<Long> deviceIds = auth.getDeviceIds();
+        List<DeviceInfo> devices = getDevicesByMercId(mercId);
+        //取消商户设备授权
+        if (CollUtil.isEmpty(deviceIds)) {
+            if (CollUtil.isEmpty(devices)) {
+                return R.ok(Boolean.TRUE);
+            }
+            return R.ok(removeMerRefDevices(devices));
+        }
+        //更新商户设备授权
+        List<DeviceInfo> deviceInfos = this.listByIds(deviceIds);
+        deviceInfos.forEach(deviceInfo -> {
+            //绑定关系
+            deviceInfo.setMercId(mercId);
+        });
+        updateBatchById(deviceInfos);
+        //原来存在的设备关系,不在最终设备列表中的移除
+        if (CollUtil.isNotEmpty(devices)) {
+            List<Long> oldIds = new ArrayList<>();
+            List<Long> removeIds = new ArrayList<>();
+            devices.forEach(device -> oldIds.add(device.getDeviceId()));
+            oldIds.forEach(deviceId -> {
+                //不在最终设备列表中的待移除
+                if (!deviceIds.contains(deviceId)) {
+                    removeIds.add(deviceId);
+                }
+            });
+            if (CollUtil.isNotEmpty(removeIds)) {
+                List<DeviceInfo> removeList = this.listByIds(removeIds);
+                removeMerRefDevices(removeList);
+            }
+        }
+        return R.ok(Boolean.TRUE);
+    }
+
+    /**
+     * 批量移除商户设备绑定关系
+     *
+     * @param deviceInfos
+     * @return
+     */
+    private Boolean removeMerRefDevices(List<DeviceInfo> deviceInfos) {
+        deviceInfos.forEach(deviceInfo -> {
+            //-1 释放关系
+            deviceInfo.setMercId(-1L);
+        });
+        //批量更新
+        updateBatchById(deviceInfos);
+        return updateBatchById(deviceInfos);
+    }
+
+    /**
+     * 获取商户设备列表
+     *
+     * @param mercId
+     * @return
+     */
+    private List<DeviceInfo> getDevicesByMercId(Long mercId) {
+        return list(Wrappers.<DeviceInfo>lambdaQuery().eq(DeviceInfo::getMercId, mercId));
+    }
+
     private R<PageBean<DeviceInfoDto.Vo2>> queryPage(DeviceInfoDto.Page page) {
         IPage<DeviceInfoQueryPage> iPage = baseMapper.queryPage(toIPage(page.getPage()), page);
         return R.ok(toPageBean(DeviceInfoDto.Vo2.class, iPage));

+ 21 - 0
device-api/src/main/java/com/xy/dto/DeviceInfoDto.java

@@ -10,6 +10,7 @@ import lombok.experimental.Accessors;
 import javax.validation.constraints.NotNull;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.util.List;
 
 @Data
 @Accessors(chain = true)
@@ -194,4 +195,24 @@ public class DeviceInfoDto {
         @ApiModelProperty("区域名称")
         private String districtName;
     }
+
+
+    /**
+     * 商户设备授权
+     *
+     * @author 谭斌
+     * @date 2023/02/09
+     */
+    @Data
+    @Accessors(chain = true)
+    public static class MercDeviceAuthDto {
+        @ApiModelProperty(value = "对应商户的全量设备ID(传空则取消商户所有机器)", required = false)
+        private List<Long> deviceIds;
+
+        @NotNull(message = "商户ID不可为空")
+        @ApiModelProperty(value = "商户ID", required = true)
+        private Long mercId;
+
+
+    }
 }

+ 11 - 0
device-api/src/main/java/com/xy/service/DeviceInfoService.java

@@ -3,6 +3,7 @@ package com.xy.service;
 import com.xy.annotate.RestMappingController;
 import com.xy.dto.DeviceInfoDto;
 import com.xy.utils.R;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 
@@ -43,4 +44,14 @@ public interface DeviceInfoService {
      */
     @PostMapping("checkOpenDoor")
     R checkOpenDoor(@RequestBody DeviceInfoDto.Obj obj);
+
+
+    /**
+     * 商户设备授权
+     *
+     * @param auth
+     * @return {@link R}<{@link Boolean}>
+     */
+    @PostMapping("mercDeviceAuth")
+    R<Boolean> mercDeviceAuth(@RequestBody @Validated DeviceInfoDto.MercDeviceAuthDto auth);
 }