2021与蓝度共同重构项目,服务端
liuhaonan
2022-10-24 7a00187d197548e23a38dcb87187ef6b2b7f8bfb
ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleBindingService.java
@@ -34,8 +34,6 @@
     */
    public boolean bindPole(Long poleId, PoleBindingParam param) {
        Integer deviceType = param.getDeviceType();
//        if (PoleBindingEnums.LIGHT.getCode().equals(deviceType)) {
//        }
        int size = 0;
        switch (deviceType.toString()) {
@@ -49,6 +47,18 @@
                size = SpringContextHolder.getBean(C3ChargingService.class).list(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, param.getDeviceCode())).size();
                break;
            case PoleBindingEnums.ATMOSPHERIC:
                Pole byId = SpringContextHolder.getBean(PoleService.class).getById(poleId);
                if (byId == null) {
                    throw new BusinessException("未找到灯杆");
                }
                if (byId.getDeviceCode() != null || !byId.getDeviceCode().isEmpty()) {
                    if (!param.getDeviceCode().equals(byId.getDeviceCode())) {
                        throw new BusinessException("灯杆mac和大气mac不一致, 无法绑定");
                    }
                } else {
                    throw new BusinessException("灯杆mac为空,无法绑定大气设备");
                }
                size = SpringContextHolder.getBean(AirEquipmentService.class).list(Wrappers.lambdaQuery(AirEquipment.class).eq(AirEquipment::getMac, param.getDeviceCode())).size();
                break;
            case PoleBindingEnums.WATER:
@@ -101,12 +111,24 @@
        PoleBinding one = getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, param.getDeviceCode()));
        if (one == null) {
            //充电桩保存绑定关系需要先判断是不是正确的灯杆
            if (deviceType == 2) {
                C3mCharging mCharging = SpringContextHolder.getBean(C3ChargingService.class).
                        getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, param.getDeviceCode()));
                Pole poleInfo = SpringContextHolder.getBean(PoleService.class).getById(poleId);
                if (!mCharging.getPoleDevicesCode().equals(poleInfo.getDeviceCode())) {
                    throw new BusinessException("绑定出错!该充电桩硬件绑定的灯杆mac为:" + mCharging.getPoleDevicesCode());
                }
            }
            PoleBinding poleBinding = new PoleBinding();
            poleBinding.setPoleId(poleId);
            poleBinding.setDeviceType(deviceType);
            poleBinding.setDeviceName(param.getDeviceName());
            poleBinding.setDeviceCode(param.getDeviceCode());
            return save(poleBinding);
        } else {
            //修改单灯的绑定关系的时候要额外处理的事务
            // 删除灯杆的devicescode
@@ -117,6 +139,18 @@
                    pole.setDeviceCode(null);
                    poleService.updateById(pole);
                }
            }
            //充电桩保存绑定关系需要先判断是不是正确的灯杆
            if (deviceType == 2) {
                C3mCharging mCharging = SpringContextHolder.getBean(C3ChargingService.class).
                        getOne(Wrappers.lambdaQuery(C3mCharging.class).eq(C3mCharging::getMcuUdid, param.getDeviceCode()));
                Pole poleInfo = SpringContextHolder.getBean(PoleService.class).getById(poleId);
                if (!mCharging.getPoleDevicesCode().equals(poleInfo.getDeviceCode())) {
                    throw new BusinessException("绑定出错!该充电桩硬件绑定的灯杆mac为:" + mCharging.getPoleDevicesCode());
                }
            }
            one.setPoleId(poleId);
@@ -159,6 +193,13 @@
        if (deviceCode == null) {
            throw new BusinessException("设备编号不能为空");
        }
        if (SecurityUtils.getClientId() != null) {
            //查灯杆归属关系
            boolean belong = isBelong(deviceCode, null);
            if (!belong) {
                throw new BusinessException("该灯杆不属于您,无法解绑!");
            }
        }
        //直接删除设备不需要灯杆ID
        PoleBinding one = getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode));
@@ -179,12 +220,9 @@
     *
     * @param deviceCode 设备编号
     * @param type       设备类型
     * @return 已绑定返回true,未绑定返回false
     * @return 属于该用户返回true,不是返回false
     */
    public boolean isBelong(String deviceCode, String type) {
        if (type.isEmpty() || deviceCode.isEmpty()) {
            throw new BusinessException("设备编号或类型不能为空");
        }
        Long userId = SecurityUtils.getClientId();
        Pole binding = baseMapper.getPoleByBinding(type, deviceCode, userId);
        if (binding != null) {