2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-04-22 8a5b6b092754685b4da940d2a35a855832f92465
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.sandu.ximon.admin.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.param.PoleBindingParam;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.mapper.PoleBindingMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import javax.swing.*;
import java.util.List;
 
/**
 * @author chenjiantian
 * @date 2021/12/14 11:48
 */
@Service
@AllArgsConstructor
public class PoleBindingService extends BaseServiceImpl<PoleBindingMapper, PoleBinding> {
 
    private final LightReportDataService lightReportDataService;
 
    /**
     * 灯杆绑定设备
     *
     * @param poleId
     * @param param
     * @return
     */
    public boolean bindPole(Long poleId, PoleBindingParam param) {
        Integer deviceType = param.getDeviceType();
//        if (PoleBindingEnums.LIGHT.getCode().equals(deviceType)) {
//        }
        LambdaQueryWrapper<PoleBinding> eq = Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getPoleId, poleId);
        List<PoleBinding> list = list(eq);
        if (list.size() != 0) {
            for (PoleBinding poleBinding : list) {
                //充电桩的绑定是网络请求是带过来的灯杆信息,要允许直接修改
                if (poleBinding.getDeviceType().equals(deviceType) && deviceType != 2) {
                    throw new BusinessException("该灯杆已绑定过相同类型设备");
                }
            }
        }
 
        PoleBinding one = getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, param.getDeviceCode()));
        if (one == null) {
            PoleBinding poleBinding = new PoleBinding();
            poleBinding.setPoleId(poleId);
            poleBinding.setDeviceType(deviceType);
            poleBinding.setDeviceName(param.getDeviceName());
            poleBinding.setDeviceCode(param.getDeviceCode());
            return save(poleBinding);
        } else {
            //删除灯杆的devicescode
            PoleService poleService = SpringContextHolder.getBean(PoleService.class);
            Pole pole = poleService.getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getId, one.getPoleId()));
            if (pole != null) {
                pole.setDeviceCode(null);
                poleService.updateById(pole);
            }
 
            one.setPoleId(poleId);
            one.setDeviceType(param.getDeviceType());
            one.setDeviceName(param.getDeviceName());
            one.setDeviceCode(param.getDeviceCode());
            return updateById(one);
        }
    }
 
    /**
     * 灯杆解绑设备,删除设备前需要解绑
     *
     * @param deviceCode
     */
    public boolean unBindPole(Long poleId, String deviceCode) {
        if (deviceCode == null) {
            throw new BusinessException("设备编号不能为空");
        }
        PoleBinding one;
        //直接删除设备不需要灯杆ID
        if (poleId == null) {
            one = getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode));
        } else {
            one = getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode).eq(PoleBinding::getPoleId, poleId));
 
            //删除灯杆的devicescode
            PoleService poleService = SpringContextHolder.getBean(PoleService.class);
            Pole pole = poleService.getOne(Wrappers.lambdaQuery(Pole.class).eq(Pole::getId, poleId));
            if (pole != null) {
                pole.setDeviceCode(null);
                poleService.updateById(pole);
            }
 
 
        }
        if (one != null) {
            return removeById(one.getId());
        } else {
            throw new BusinessException("设备不存在绑定情况或灯杆ID不正确");
        }
    }
 
    /**
     * 设备删除,直接解绑设备相关绑定数据
     *
     * @param deviceCode
     */
    public boolean unBindPole(String deviceCode) {
        if (deviceCode == null) {
            throw new BusinessException("设备编号不能为空");
        }
        //直接删除设备不需要灯杆ID
        PoleBinding one = getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode));
 
        if (one != null) {
            return removeById(one.getId());
        } else {
            return false;
        }
    }
 
    public PoleBinding getPoleIdByMac(String deviceCode) {
        return getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, deviceCode));
    }
}