2021与蓝度共同重构项目,服务端
chenjiantian
2021-12-14 7f9a1ccdad55ce31edd68cfb2c2bd8a4068481c7
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
package com.sandu.ximon.admin.service;
 
import com.sandu.common.execption.BusinessException;
import com.sandu.common.redis.RedisService;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.param.PoleBindingParam;
import com.sandu.ximon.admin.param.PoleParam;
import com.sandu.ximon.admin.redis.LightKey;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.enums.PoleBindingEnums;
import com.sandu.ximon.dao.mapper.PoleMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 灯杆相关
 *
 * @author chenjiantian
 */
@Service
@Slf4j
@AllArgsConstructor
public class PoleService extends BaseServiceImpl<PoleMapper, Pole> {
 
    private final RedisService redisService;
    private final PoleBindingService poleBindingService;
 
    public boolean addPole(PoleParam param) {
        Pole pole = new Pole();
        BeanUtils.copyProperties(param, pole);
        pole.setPoleCode(generatePoleCode());
        return save(pole);
    }
 
    public boolean updatePole(Long poleId, PoleParam param) {
        Pole pole = getById(poleId);
        if (pole == null) {
            throw new BusinessException("未找到该灯杆");
        }
        Pole update = new Pole();
        BeanUtils.copyProperties(param, update);
        update.setId(poleId);
        return updateById(update);
    }
 
    /**
     * 删除灯杆
     */
    public boolean deletePole(Long poleId) {
        Pole pole = getById(poleId);
        if (pole == null) {
            throw new BusinessException("未找到该灯杆");
        }
        return removeById(poleId);
    }
 
 
    /**
     * 生成灯杆编号
     */
    private Long generatePoleCode() {
        StringBuilder sb = new StringBuilder();
        String date = new SimpleDateFormat("yyMMdd").format(new Date());
        sb.append(date);
        String key = LightKey.POLE_SN.key(null);
        Long increment = redisService.incr(key, 1);
        String incrementStr = increment.toString();
        if (incrementStr.length() <= 4) {
            sb.append(String.format("%04d", increment));
        } else {
            sb.append(incrementStr);
        }
        return Long.parseLong(sb.toString());
    }
 
    /**
     * 灯杆绑定设备
     *
     * @param poleId 绑定灯杆id
     * @param param  被绑定设备信息
     * @return 是否成功
     */
    public boolean bindPole(Long poleId, PoleBindingParam param) {
        Pole pole = getById(poleId);
        if (pole == null) {
            throw new BusinessException("未找到该灯杆");
        }
        return poleBindingService.bindPole( poleId,  param);
    }
}