doum
2026-06-17 7ff92e90d2318f5c2597c4ba01e0cbfde983cec0
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
package com.doumee.service.business.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import com.doumee.dao.business.*;
import com.doumee.dao.business.dto.YwCustomerGsConfigDTO;
import com.doumee.dao.business.model.*;
import com.doumee.service.business.YwCustomerDeviceAutoBindService;
import com.doumee.service.business.YwCustomerRechargeBizService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
@Slf4j
public class YwCustomerDeviceAutoBindServiceImpl implements YwCustomerDeviceAutoBindService {
 
    private static final int BIND_SOURCE_CONTRACT = 1;
 
    @Autowired
    private YwContractMapper ywContractMapper;
    @Autowired
    private YwContractRoomMapper ywContractRoomMapper;
    @Autowired
    private YwElectricalRoomMapper ywElectricalRoomMapper;
    @Autowired
    private YwCustomerElectricalMapper ywCustomerElectricalMapper;
    @Autowired
    private YwCustomerConditionerMapper ywCustomerConditionerMapper;
    @Autowired
    private YwConditionerMapper ywConditionerMapper;
    @Autowired
    private YwCustomerRechargeBizService ywCustomerRechargeBizService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncByContractId(Integer contractId, LoginUserInfo user) {
        if (contractId == null) {
            return;
        }
        YwContract contract = ywContractMapper.selectById(contractId);
        if (contract == null || Objects.equals(contract.getIsdeleted(), Constants.ONE)) {
            return;
        }
        if (contract.getRenterId() == null) {
            return;
        }
        if (!isActiveContract(contract)) {
            return;
        }
        List<Integer> roomIds = listContractRoomIds(contractId);
        if (roomIds.isEmpty()) {
            return;
        }
        bindElectricals(contract, roomIds, user);
        bindConditioners(contract, roomIds, user);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncByCustomerId(Integer customerId, LoginUserInfo user) {
        if (customerId == null) {
            return;
        }
        List<YwContract> contracts = ywContractMapper.selectList(new QueryWrapper<YwContract>().lambda()
                .eq(YwContract::getRenterId, customerId)
                .eq(YwContract::getIsdeleted, Constants.ZERO)
                .in(YwContract::getStatus, Arrays.asList(Constants.ZERO, Constants.ONE, Constants.THREE)));
        for (YwContract c : contracts) {
            if (isActiveContract(c)) {
                syncByContractId(c.getId(), user);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unbindByContractId(Integer contractId, LoginUserInfo user) {
        if (contractId == null) {
            return;
        }
        Date now = new Date();
        Integer editor = user != null ? user.getId() : null;
        ywCustomerElectricalMapper.update(null, new UpdateWrapper<YwCustomerElectrical>().lambda()
                .set(YwCustomerElectrical::getIsdeleted, Constants.ONE)
                .set(YwCustomerElectrical::getEditDate, now)
                .set(YwCustomerElectrical::getEditor, editor)
                .eq(YwCustomerElectrical::getContractId, contractId)
                .eq(YwCustomerElectrical::getBindSource, BIND_SOURCE_CONTRACT)
                .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO));
    }
 
    private boolean isActiveContract(YwContract contract) {
        if (contract.getStartDate() == null || contract.getEndDate() == null) {
            return false;
        }
        long now = System.currentTimeMillis();
        return contract.getStartDate().getTime() <= now
                && contract.getEndDate().getTime() >= now
                && !Objects.equals(contract.getStatus(), Constants.FOUR);
    }
 
    private List<Integer> listContractRoomIds(Integer contractId) {
        return ywContractRoomMapper.selectList(new QueryWrapper<YwContractRoom>().lambda()
                        .eq(YwContractRoom::getContractId, contractId)
                        .eq(YwContractRoom::getType, Constants.ZERO)
                        .eq(YwContractRoom::getIsdeleted, Constants.ZERO))
                .stream().map(YwContractRoom::getRoomId).filter(Objects::nonNull).distinct()
                .collect(Collectors.toList());
    }
 
    private void bindElectricals(YwContract contract, List<Integer> roomIds, LoginUserInfo user) {
        List<YwElectricalRoom> relRooms = ywElectricalRoomMapper.selectList(new QueryWrapper<YwElectricalRoom>().lambda()
                .in(YwElectricalRoom::getRoomId, roomIds)
                .eq(YwElectricalRoom::getType, Constants.ZERO)
                .eq(YwElectricalRoom::getIsdeleted, Constants.ZERO));
        Set<Integer> electricalIds = relRooms.stream().map(YwElectricalRoom::getObjId)
                .filter(Objects::nonNull).collect(Collectors.toCollection(LinkedHashSet::new));
        if (electricalIds.isEmpty()) {
            return;
        }
        Set<Integer> boundOthers = listBoundElectricalIdsExcept(contract.getRenterId());
        Date now = new Date();
        for (Integer eid : electricalIds) {
            if (boundOthers.contains(eid)) {
                log.warn("skip electrical {} already bound to other customer", eid);
                continue;
            }
            YwCustomerElectrical exist = ywCustomerElectricalMapper.selectOne(new QueryWrapper<YwCustomerElectrical>().lambda()
                    .eq(YwCustomerElectrical::getCustomerId, contract.getRenterId())
                    .eq(YwCustomerElectrical::getElectricalId, eid)
                    .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO)
                    .last("limit 1"));
            if (exist != null) {
                if (exist.getContractId() == null) {
                    exist.setContractId(contract.getId());
                    exist.setBindSource(BIND_SOURCE_CONTRACT);
                    exist.setEditDate(now);
                    exist.setEditor(user != null ? user.getId() : null);
                    ywCustomerElectricalMapper.updateById(exist);
                }
                continue;
            }
            YwCustomerElectrical rel = new YwCustomerElectrical();
            rel.setCreator(user != null ? user.getId() : null);
            rel.setCreateDate(now);
            rel.setEditor(user != null ? user.getId() : null);
            rel.setEditDate(now);
            rel.setIsdeleted(Constants.ZERO);
            rel.setCustomerId(contract.getRenterId());
            rel.setElectricalId(eid);
            rel.setBindSource(BIND_SOURCE_CONTRACT);
            rel.setContractId(contract.getId());
            ywCustomerElectricalMapper.insert(rel);
        }
    }
 
    private void bindConditioners(YwContract contract, List<Integer> roomIds, LoginUserInfo user) {
        Set<Integer> conditionerIds = new LinkedHashSet<>();
        List<YwElectricalRoom> acRooms = ywElectricalRoomMapper.selectList(new QueryWrapper<YwElectricalRoom>().lambda()
                .in(YwElectricalRoom::getRoomId, roomIds)
                .eq(YwElectricalRoom::getType, Constants.ONE)
                .eq(YwElectricalRoom::getIsdeleted, Constants.ZERO));
        acRooms.stream().map(YwElectricalRoom::getObjId).filter(Objects::nonNull).forEach(conditionerIds::add);
        if (conditionerIds.isEmpty()) {
            List<YwConditioner> byRoom = ywConditionerMapper.selectList(new QueryWrapper<YwConditioner>().lambda()
                    .in(YwConditioner::getRoomId, roomIds)
                    .eq(YwConditioner::getIsdeleted, Constants.ZERO));
            byRoom.stream().map(YwConditioner::getId).forEach(conditionerIds::add);
        }
        if (conditionerIds.isEmpty()) {
            return;
        }
        YwCustomerGsConfigDTO dto = new YwCustomerGsConfigDTO();
        dto.setCustomerId(contract.getRenterId());
        dto.setIsPwr(Constants.ONE);
        dto.setIsRestStop(Constants.ZERO);
        dto.setGsBz("合同自动关联");
        dto.setStopMoney(BigDecimal.ZERO);
        List<YwCustomerGsConfigDTO.ConditionerItem> items = new ArrayList<>();
        for (Integer cid : conditionerIds) {
            YwCustomerGsConfigDTO.ConditionerItem item = new YwCustomerGsConfigDTO.ConditionerItem();
            item.setConditionerId(cid);
            item.setDevRatio(100);
            items.add(item);
        }
        dto.setConditioners(items);
        try {
            ywCustomerRechargeBizService.saveCustomerGsConfig(dto, user != null ? user : systemUser());
        } catch (BusinessException e) {
            log.warn("auto bind conditioner GS failed contractId={}: {}", contract.getId(), e.getMessage());
        }
    }
 
    private Set<Integer> listBoundElectricalIdsExcept(Integer customerId) {
        List<YwCustomerElectrical> list = ywCustomerElectricalMapper.selectList(new QueryWrapper<YwCustomerElectrical>().lambda()
                .eq(YwCustomerElectrical::getIsdeleted, Constants.ZERO)
                .ne(customerId != null, YwCustomerElectrical::getCustomerId, customerId));
        return list.stream().map(YwCustomerElectrical::getElectricalId).collect(Collectors.toSet());
    }
 
    private LoginUserInfo systemUser() {
        LoginUserInfo u = new LoginUserInfo();
        u.setId(1);
        u.setRealname("system");
        return u;
    }
}