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;
|
}
|
}
|