doum
3 天以前 7ec3683c8e41460f4bb0bd3a6677198742313e2b
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package com.doumee.service.business.impl;
 
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.dto.YwConditionerLockDTO;
import com.doumee.dao.business.dto.YwConditionerOperateDTO;
import com.doumee.dao.business.YwConditionerGatewayMapper;
import com.doumee.dao.business.YwConditionerMapper;
import com.doumee.dao.business.model.YwConditioner;
import com.doumee.dao.business.model.YwConditionerActions;
import com.doumee.dao.business.model.YwConditionerGateway;
import com.doumee.service.business.ConditionerBizService;
import com.doumee.service.business.YwConditionerActionsService;
import com.doumee.service.business.YwConditionerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 空调设备信息Service实现
 * @author renkang
 * @date 2026/04/03
 */
@Service
public class YwConditionerServiceImpl implements YwConditionerService {
 
    @Autowired
    private YwConditionerMapper ywConditionerMapper;
    @Autowired
    private YwConditionerGatewayMapper gatewayMapper;
    @Autowired
    private ConditionerBizService conditionerBizService;
    @Autowired
    private YwConditionerActionsService ywConditionerActionsService;
 
    @Override
    public Integer create(YwConditioner ywConditioner) {
        if (Objects.isNull(ywConditioner)
                || StringUtils.isBlank(ywConditioner.getName())
                || StringUtils.isBlank(ywConditioner.getCode())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = ywConditioner.getLoginUserInfo();
        // 校验设备编号唯一
        if (ywConditionerMapper.selectCount(new QueryWrapper<YwConditioner>().lambda()
                .eq(YwConditioner::getIsdeleted, Constants.ZERO)
                .eq(YwConditioner::getCode, ywConditioner.getCode())) > Constants.ZERO) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "设备编号重复!");
        }
        ywConditioner.setCreateDate(new Date());
        ywConditioner.setCreator(loginUserInfo.getId());
        ywConditioner.setEditDate(new Date());
        ywConditioner.setEditor(loginUserInfo.getId());
        ywConditioner.setIsdeleted(Constants.ZERO);
        ywConditionerMapper.insert(ywConditioner);
        return ywConditioner.getId();
    }
 
    @Override
    public void deleteById(Integer id, LoginUserInfo user) {
        ywConditionerMapper.update(new UpdateWrapper<YwConditioner>()
                .lambda()
                .set(YwConditioner::getIsdeleted, Constants.ONE)
                .set(YwConditioner::getEditDate, DateUtil.getCurrDateTime())
                .set(YwConditioner::getEditor, user)
                .eq(YwConditioner::getId, id)
        );
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        for (Integer id : ids) {
            this.deleteById(id, user);
        }
    }
 
    @Override
    public void updateById(YwConditioner ywConditioner) {
        if (Objects.isNull(ywConditioner)
                || Objects.isNull(ywConditioner.getId())
                || StringUtils.isBlank(ywConditioner.getName())
                || StringUtils.isBlank(ywConditioner.getCode())) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo loginUserInfo = ywConditioner.getLoginUserInfo();
        // 校验设备编号唯一(排除自身)
        if (ywConditionerMapper.selectCount(new QueryWrapper<YwConditioner>().lambda()
                .eq(YwConditioner::getIsdeleted, Constants.ZERO)
                .eq(YwConditioner::getCode, ywConditioner.getCode())
                .ne(YwConditioner::getId, ywConditioner.getId())) > Constants.ZERO) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "设备编号重复!");
        }
        ywConditioner.setEditDate(new Date());
        ywConditioner.setEditor(loginUserInfo.getId());
        ywConditionerMapper.updateById(ywConditioner);
    }
 
    @Override
    public YwConditioner findById(Integer id) {
        return ywConditionerMapper.selectOne(new QueryWrapper<YwConditioner>().lambda()
                .eq(YwConditioner::getId, id)
                .last(" limit 1 "));
    }
 
    @Override
    public PageData<YwConditioner> findPage(PageWrap<YwConditioner> pageWrap) {
        IPage<YwConditioner> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<YwConditioner> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        YwConditioner model = pageWrap.getModel();
        queryWrapper.selectAll(YwConditioner.class)
                .and(Objects.nonNull(model) && StringUtils.isNotBlank(model.getName()),
                        i -> i.like(YwConditioner::getName, model.getName())
                             .or()
                             .like(YwConditioner::getCode, model.getName()))
                .eq(Objects.nonNull(model) && Objects.nonNull(model.getStatus()), YwConditioner::getStatus, model.getStatus())
                .eq(YwConditioner::getIsdeleted, Constants.ZERO)
                .orderByDesc(YwConditioner::getCreateDate);
        IPage<YwConditioner> iPage = ywConditionerMapper.selectJoinPage(page, YwConditioner.class, queryWrapper);
        return PageData.from(iPage);
    }
 
    @Override
    public PageData<YwConditioner> findCardPage(PageWrap<YwConditioner> pageWrap) {
        IPage<YwConditioner> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        Utils.MP.blankToNull(pageWrap.getModel());
        YwConditioner model = pageWrap.getModel() == null ? new YwConditioner() : pageWrap.getModel();
        MPJLambdaWrapper<YwConditioner> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(YwConditioner.class)
                .eq(YwConditioner::getIsdeleted, Constants.ZERO)
                .isNotNull(YwConditioner::getPlatformDevId)
                .and(StringUtils.isNotBlank(model.getDevKeyword()), w -> w
                        .like(YwConditioner::getName, model.getDevKeyword())
                        .or().like(YwConditioner::getCode, model.getDevKeyword())
                        .or().like(YwConditioner::getRoomName, model.getDevKeyword()))
                .eq(StringUtils.isNotBlank(model.getOnlineFilter()), YwConditioner::getOnline, model.getOnlineFilter())
                .eq(model.getPwrFilter() != null, YwConditioner::getPwr, model.getPwrFilter())
                .eq(StringUtils.isNotBlank(model.getWgMacFilter()), YwConditioner::getWgMac, model.getWgMacFilter())
                .orderByAsc(YwConditioner::getWgQid)
                .orderByAsc(YwConditioner::getId);
        PageData<YwConditioner> pageData = PageData.from(
                ywConditionerMapper.selectJoinPage(page, YwConditioner.class, queryWrapper));
        fillGatewayBzFromGateway(pageData.getRecords());
        return pageData;
    }
 
    /**
     * 从本地网关镜像表 yw_conditioner_gateway 关联填充备注(优先平台网关ID,其次 MAC)
     */
    private void fillGatewayBzFromGateway(List<YwConditioner> records) {
        if (CollectionUtils.isEmpty(records)) {
            return;
        }
        List<String> macs = records.stream()
                .map(YwConditioner::getWgMac)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());
        List<Integer> platformWgIds = records.stream()
                .map(YwConditioner::getWgId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        if (macs.isEmpty() && platformWgIds.isEmpty()) {
            return;
        }
        QueryWrapper<YwConditionerGateway> gwQuery = new QueryWrapper<>();
        gwQuery.lambda().eq(YwConditionerGateway::getIsdeleted, Constants.ZERO);
        if (!macs.isEmpty() && !platformWgIds.isEmpty()) {
            gwQuery.lambda().and(w -> w.in(YwConditionerGateway::getWgMac, macs)
                    .or().in(YwConditionerGateway::getPlatformWgId, platformWgIds));
        } else if (!macs.isEmpty()) {
            gwQuery.lambda().in(YwConditionerGateway::getWgMac, macs);
        } else {
            gwQuery.lambda().in(YwConditionerGateway::getPlatformWgId, platformWgIds);
        }
        List<YwConditionerGateway> gateways = gatewayMapper.selectList(gwQuery);
        Map<String, String> bzByMac = new HashMap<>();
        Map<Integer, String> bzByPlatformWgId = new HashMap<>();
        for (YwConditionerGateway gw : gateways) {
            if (StringUtils.isNotBlank(gw.getWgBz())) {
                if (StringUtils.isNotBlank(gw.getWgMac())) {
                    bzByMac.putIfAbsent(gw.getWgMac().trim(), gw.getWgBz());
                }
                if (gw.getPlatformWgId() != null) {
                    bzByPlatformWgId.putIfAbsent(gw.getPlatformWgId(), gw.getWgBz());
                }
            }
        }
        for (YwConditioner row : records) {
            String bz = null;
            if (StringUtils.isNotBlank(row.getWgMac())) {
                bz = bzByMac.get(row.getWgMac().trim());
            }
            if (StringUtils.isBlank(bz) && row.getWgId() != null) {
                bz = bzByPlatformWgId.get(row.getWgId());
            }
            if (StringUtils.isNotBlank(bz)) {
                row.setWgBz(bz);
            }
        }
    }
 
    @Override
    public String syncAll() {
        return conditionerBizService.syncAll();
    }
 
    @Override
    public String syncDevicesAndStatus() {
        return conditionerBizService.syncIndoorUnits();
    }
 
    @Override
    public String operate(YwConditionerOperateDTO dto, LoginUserInfo user) {
        return conditionerBizService.operate(dto, user);
    }
 
    @Override
    public String lock(YwConditionerLockDTO dto, LoginUserInfo user) {
        return conditionerBizService.lock(dto, user);
    }
 
    @Override
    public PageData<YwConditionerActions> historyPage(PageWrap<YwConditionerActions> pageWrap) {
        return ywConditionerActionsService.findPage(pageWrap);
    }
 
    @Override
    public List<Map<String, Object>> gatewayOptions() {
        return conditionerBizService.gatewayOptions();
    }
}