package com.doumee.service.business.impl;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.device.ElectronicToolUtil;
|
import com.doumee.core.device.WaterElectricityUtil;
|
import com.doumee.core.device.model.response.CollectorStatusResponse;
|
import com.doumee.core.device.model.response.ElectronicDataResponse;
|
import com.doumee.core.device.model.response.MeterInfoResponse;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.core.haikang.model.param.respose.AcsDeviceInfoResponse;
|
import com.doumee.core.haikang.model.param.respose.AscDeviceStatusInfoResponse;
|
import com.doumee.core.haikang.model.param.respose.DoorsInfoResponse;
|
import com.doumee.core.haikang.model.param.respose.LedRecordsInfoResponse;
|
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.dao.business.model.Device;
|
import com.doumee.dao.business.model.DeviceData;
|
import com.doumee.dao.business.model.YwDevice;
|
import com.doumee.dao.business.model.YwElectrical;
|
import com.doumee.dao.business.model.YwElectricalParam;
|
import com.doumee.core.utils.Utils;
|
import com.doumee.dao.business.YwElectricalMapper;
|
import com.doumee.service.business.YwElectricalBizService;
|
import com.doumee.service.business.YwElectricalService;
|
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 org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
import com.doumee.core.model.LoginUserInfo;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* 电表设备信息Service实现
|
* @author doumee
|
* @date 2026-05-20 14:59:06
|
*/
|
@Service
|
public class YwElectricalServiceImpl implements YwElectricalService {
|
|
@Autowired
|
private YwElectricalMapper ywElectricalMapper;
|
@Autowired
|
private YwElectricalBizService ywElectricalBizService;
|
|
@Override
|
public Integer create(YwElectrical ywElectrical) {
|
ywElectricalMapper.insert(ywElectrical);
|
return ywElectrical.getId();
|
}
|
|
@Override
|
public void deleteById(Integer id) {
|
ywElectricalMapper.deleteById(id);
|
}
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids ) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
for(Integer id :ids){
|
deleteById(id);
|
}
|
}
|
@Override
|
public void deleteById(Integer id, LoginUserInfo user) {
|
ywElectricalMapper.deleteById(id);
|
}
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids, LoginUserInfo user) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
for(Integer id :ids){
|
deleteById(id,user);
|
}
|
}
|
@Override
|
public void delete(YwElectrical ywElectrical) {
|
UpdateWrapper<YwElectrical> deleteWrapper = new UpdateWrapper<>(ywElectrical);
|
ywElectricalMapper.delete(deleteWrapper);
|
}
|
|
|
@Override
|
public void updateById(YwElectrical ywElectrical) {
|
ywElectricalMapper.updateById(ywElectrical);
|
}
|
|
@Override
|
public void updateByIdInBatch(List<YwElectrical> ywElectricals) {
|
if (CollectionUtils.isEmpty(ywElectricals)) {
|
return;
|
}
|
for (YwElectrical ywElectrical: ywElectricals) {
|
this.updateById(ywElectrical);
|
}
|
}
|
|
@Override
|
public YwElectrical findById(Integer id) {
|
return ywElectricalMapper.selectById(id);
|
}
|
|
@Override
|
public YwElectrical findOne(YwElectrical ywElectrical) {
|
QueryWrapper<YwElectrical> wrapper = new QueryWrapper<>(ywElectrical).last("limit 1");
|
return ywElectricalMapper.selectOne(wrapper);
|
}
|
|
@Override
|
public List<YwElectrical> findList(YwElectrical ywElectrical) {
|
QueryWrapper<YwElectrical> wrapper = new QueryWrapper<>(ywElectrical);
|
return ywElectricalMapper.selectList(wrapper);
|
}
|
|
@Override
|
public PageData<YwElectrical> findPage(PageWrap<YwElectrical> pageWrap) {
|
IPage<YwElectrical> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
MPJLambdaWrapper<YwElectrical> queryWrapper = new MPJLambdaWrapper<>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
queryWrapper.selectAll(YwElectrical.class)
|
.selectAs(YwElectricalParam::getName, YwElectrical::getParamName)
|
.leftJoin(YwElectricalParam.class, on -> on
|
.eq(YwElectricalParam::getId, YwElectrical::getElectricalParamId)
|
.eq(YwElectricalParam::getIsdeleted, Constants.ZERO))
|
.eq(YwElectrical::getIsdeleted, Constants.ZERO);
|
if (StringUtils.isNotBlank(pageWrap.getModel().getMeterKeyword())) {
|
String kw = pageWrap.getModel().getMeterKeyword().trim();
|
queryWrapper.and(w -> w.like(YwElectrical::getName, kw).or().like(YwElectrical::getAddress, kw));
|
}
|
queryWrapper.eq(pageWrap.getModel().getId() != null,YwElectrical::getId, pageWrap.getModel().getId());
|
queryWrapper.eq(pageWrap.getModel().getCreator() != null,YwElectrical::getCreator, pageWrap.getModel().getCreator());
|
if (pageWrap.getModel().getCreateDate() != null) {
|
queryWrapper.ge(YwElectrical::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate()));
|
queryWrapper.le(YwElectrical::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate()));
|
}
|
queryWrapper.eq(pageWrap.getModel().getEdirot() != null,YwElectrical::getEdirot, pageWrap.getModel().getEdirot());
|
if (pageWrap.getModel().getEditDate() != null) {
|
queryWrapper.ge(YwElectrical::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate()));
|
queryWrapper.le(YwElectrical::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate()));
|
}
|
queryWrapper.eq(pageWrap.getModel().getIsdeleted() != null,YwElectrical::getIsdeleted, pageWrap.getModel().getIsdeleted());
|
queryWrapper.eq(pageWrap.getModel().getRemark() != null,YwElectrical::getRemark, pageWrap.getModel().getRemark());
|
queryWrapper.eq(pageWrap.getModel().getCollectorId() != null,YwElectrical::getCollectorId, pageWrap.getModel().getCollectorId());
|
queryWrapper.eq(pageWrap.getModel().getCode() != null,YwElectrical::getCode, pageWrap.getModel().getCode());
|
queryWrapper.eq(pageWrap.getModel().getAddress() != null,YwElectrical::getAddress, pageWrap.getModel().getAddress());
|
queryWrapper.eq(pageWrap.getModel().getDeviceType() != null,YwElectrical::getDeviceType, pageWrap.getModel().getDeviceType());
|
queryWrapper.eq(pageWrap.getModel().getRate() != null,YwElectrical::getRate, pageWrap.getModel().getRate());
|
queryWrapper.eq(pageWrap.getModel().getPayType() != null,YwElectrical::getPayType, pageWrap.getModel().getPayType());
|
queryWrapper.eq(pageWrap.getModel().getName() != null,YwElectrical::getName, pageWrap.getModel().getName());
|
queryWrapper.eq(pageWrap.getModel().getGatewayId() != null,YwElectrical::getGatewayId, pageWrap.getModel().getGatewayId());
|
queryWrapper.eq(pageWrap.getModel().getGatewayPort() != null,YwElectrical::getGatewayPort, pageWrap.getModel().getGatewayPort());
|
if (pageWrap.getModel().getLastHeartbeatDate() != null) {
|
queryWrapper.ge(YwElectrical::getLastHeartbeatDate, Utils.Date.getStart(pageWrap.getModel().getLastHeartbeatDate()));
|
queryWrapper.le(YwElectrical::getLastHeartbeatDate, Utils.Date.getEnd(pageWrap.getModel().getLastHeartbeatDate()));
|
}
|
queryWrapper.eq(pageWrap.getModel().getBalance() != null,YwElectrical::getBalance, pageWrap.getModel().getBalance());
|
queryWrapper.eq(pageWrap.getModel().getRelayStatus() != null,YwElectrical::getRelayStatus, pageWrap.getModel().getRelayStatus());
|
if (pageWrap.getModel().getLastOpenDate() != null) {
|
queryWrapper.ge(YwElectrical::getLastOpenDate, Utils.Date.getStart(pageWrap.getModel().getLastOpenDate()));
|
queryWrapper.le(YwElectrical::getLastOpenDate, Utils.Date.getEnd(pageWrap.getModel().getLastOpenDate()));
|
}
|
if (pageWrap.getModel().getLastCleanDate() != null) {
|
queryWrapper.ge(YwElectrical::getLastCleanDate, Utils.Date.getStart(pageWrap.getModel().getLastCleanDate()));
|
queryWrapper.le(YwElectrical::getLastCleanDate, Utils.Date.getEnd(pageWrap.getModel().getLastCleanDate()));
|
}
|
queryWrapper.eq(pageWrap.getModel().getOnline() != null,YwElectrical::getOnline, pageWrap.getModel().getOnline());
|
queryWrapper.eq(pageWrap.getModel().getAccountStatus() != null,YwElectrical::getAccountStatus, pageWrap.getModel().getAccountStatus());
|
queryWrapper.eq(pageWrap.getModel().getStatus() != null,YwElectrical::getStatus, pageWrap.getModel().getStatus());
|
queryWrapper.eq(pageWrap.getModel().getWarnType() != null,YwElectrical::getWarnType, pageWrap.getModel().getWarnType());
|
queryWrapper.eq(pageWrap.getModel().getType() != null,YwElectrical::getType, pageWrap.getModel().getType());
|
queryWrapper.eq(pageWrap.getModel().getDId() != null,YwElectrical::getDId, pageWrap.getModel().getDId());
|
queryWrapper.eq(pageWrap.getModel().getCId() != null,YwElectrical::getCId, pageWrap.getModel().getCId());
|
queryWrapper.eq(pageWrap.getModel().getDescription() != null,YwElectrical::getDescription, pageWrap.getModel().getDescription());
|
queryWrapper.eq(pageWrap.getModel().getCsq() != null,YwElectrical::getCsq, pageWrap.getModel().getCsq());
|
if (pageWrap.getModel().getDisconnectTime() != null) {
|
queryWrapper.ge(YwElectrical::getDisconnectTime, Utils.Date.getStart(pageWrap.getModel().getDisconnectTime()));
|
queryWrapper.le(YwElectrical::getDisconnectTime, Utils.Date.getEnd(pageWrap.getModel().getDisconnectTime()));
|
}
|
if (pageWrap.getModel().getConnectTime() != null) {
|
queryWrapper.ge(YwElectrical::getConnectTime, Utils.Date.getStart(pageWrap.getModel().getConnectTime()));
|
queryWrapper.le(YwElectrical::getConnectTime, Utils.Date.getEnd(pageWrap.getModel().getConnectTime()));
|
}
|
queryWrapper.eq(pageWrap.getModel().getBalanceBattery() != null,YwElectrical::getBalanceBattery, pageWrap.getModel().getBalanceBattery());
|
if (pageWrap.getModel().getBalanceTime() != null) {
|
queryWrapper.ge(YwElectrical::getBalanceTime, Utils.Date.getStart(pageWrap.getModel().getBalanceTime()));
|
queryWrapper.le(YwElectrical::getBalanceTime, Utils.Date.getEnd(pageWrap.getModel().getBalanceTime()));
|
}
|
queryWrapper.orderByDesc(YwElectrical::getId);
|
IPage<YwElectrical> result = ywElectricalMapper.selectJoinPage(page, YwElectrical.class,queryWrapper);
|
PageData<YwElectrical> pageData = PageData.from(result);
|
ywElectricalBizService.enrichList(pageData.getRecords());
|
return pageData;
|
}
|
@Override
|
public long count(YwElectrical ywElectrical) {
|
QueryWrapper<YwElectrical> wrapper = new QueryWrapper<>(ywElectrical);
|
return ywElectricalMapper.selectCount(wrapper);
|
}
|
@Override
|
@Transactional
|
public void getElectricalStatus(){
|
if(Constants.DEALING_ELECTRICAL_STATUS){
|
return ;
|
}
|
Constants.DEALING_ELECTRICAL_STATUS =true;
|
try {
|
List<Device> editList = new ArrayList<>();
|
Date date = new Date();
|
//查询全部门禁设备数据
|
ElectronicDataResponse<List<CollectorStatusResponse>> response = ElectronicToolUtil.collectorStatus();
|
if (!ElectronicToolUtil.isDataApiSuccess(response)) {
|
throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),
|
ElectronicToolUtil.dataApiErrorMessage(response, "同步采集器状态异常"));
|
}
|
if(response.getData()!=null && response.getData().size()>0){
|
//逻辑删除
|
for(CollectorStatusResponse d : response.getData()){
|
ywElectricalMapper.update(null,new UpdateWrapper<YwElectrical>().lambda()
|
.set(YwElectrical::getOnline, d.getOnline()?1:0)
|
.set(YwElectrical::getDescription,d.getDescription())
|
.set(YwElectrical::getCsq,d.getCsq())
|
.set(YwElectrical::getDisconnectTime,d.getDisconnect_time())
|
.set(YwElectrical::getConnectTime,d.getConnect_time())
|
.set(YwElectrical::getEditDate,date)
|
.eq(YwElectrical::getCollectorId,d.getCollectorid()));
|
}
|
}
|
}catch (Exception e){
|
throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "同步设备状态失败!");
|
}finally {
|
Constants.DEALING_ELECTRICAL_STATUS =false;
|
}
|
}
|
@Override
|
@Transactional
|
public String syncAll(YwElectrical ywElectrical){
|
if(Constants.DEALING_ELECTRICAL_SYNCDEVICE){
|
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "同步任务正在执行哦,请稍后查看结果!") ;
|
}
|
Constants.DEALING_ELECTRICAL_SYNCDEVICE =true;
|
try {
|
List<YwElectrical> deleteList = new ArrayList<>();
|
List<YwElectrical> addList = new ArrayList<>();
|
List<YwElectrical> editList = new ArrayList<>();
|
Date date = new Date();
|
//查询全部门禁设备数据
|
List<YwElectrical> allList = ywElectricalMapper.selectList(new QueryWrapper<YwElectrical>().lambda()
|
.eq(YwElectrical::getIsdeleted,Constants.ZERO));
|
ElectronicDataResponse<List<MeterInfoResponse>> response = ElectronicToolUtil.meterList();
|
if (!ElectronicToolUtil.isDataApiSuccess(response)) {
|
throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),
|
ElectronicToolUtil.dataApiErrorMessage(response, "同步电表数据异常"));
|
}
|
List<MeterInfoResponse> meterData = response.getData();
|
if (meterData == null) {
|
meterData = Collections.emptyList();
|
}
|
if (meterData.isEmpty()) {
|
return "同步完成:第三方平台暂无电表数据";
|
}
|
getDataChangeList(allList, meterData, addList, editList, deleteList, date);
|
if(deleteList.size()>0){
|
for(YwElectrical d : deleteList){
|
ywElectricalMapper.updateById(d);
|
}
|
}
|
if(addList.size()>0){
|
for (YwElectrical d : addList) {
|
ywElectricalMapper.insert(d);
|
}
|
}
|
if(editList.size()>0){
|
for(YwElectrical d : editList){
|
ywElectricalMapper.updateById(d);
|
}
|
}
|
return "同步数据:新增【"+addList.size()+"】条,更新【"+editList.size()+"】条,删除【"+deleteList.size()+"】条";
|
} catch (BusinessException e) {
|
throw e;
|
} catch (Exception e) {
|
e.printStackTrace();
|
String detail = StringUtils.isNotBlank(e.getMessage()) ? e.getMessage() : e.getClass().getSimpleName();
|
throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "同步失败:" + detail);
|
} finally {
|
Constants.DEALING_ELECTRICAL_SYNCDEVICE =false;
|
}
|
}
|
private void getDataChangeList(List<YwElectrical> allList,
|
List<MeterInfoResponse> dataList ,
|
List<YwElectrical> addList,
|
List<YwElectrical> editList,
|
List<YwElectrical> deleteList,
|
Date date ) {
|
if(dataList!=null && dataList.size()>0){
|
//获取海康全部门禁组数据
|
for(MeterInfoResponse device : dataList){
|
YwElectrical model = getExistedDevice(device,allList);
|
if(model !=null){
|
//如果已存在,则更新数据
|
model = initDeviceDataByData(model,device,date,dataList);
|
editList.add(model);
|
}else{
|
//如果不存在,则新增数据
|
model = new YwElectrical();
|
model = initDeviceDataByData(model,device,date,dataList);
|
addList.add(model);
|
}
|
}
|
}
|
if (dataList != null && !dataList.isEmpty()) {
|
getDeleteDeviceList(allList, dataList, deleteList, date);
|
}
|
}
|
private boolean isDeletedDevice(YwElectrical device, List<MeterInfoResponse> allHkList) {
|
if (allHkList != null && allHkList.size() > 0) {
|
for(MeterInfoResponse r : allHkList){
|
if(StringUtils.equals(device.getDId(), r.getId())){
|
//表示未删除
|
return false;
|
}
|
}
|
}
|
return true;
|
|
}
|
private void getDeleteDeviceList(List<YwElectrical> allList, List<MeterInfoResponse> allHkList,List<YwElectrical> deleteList ,Date date) {
|
if(allList!=null && allList.size()>0){
|
for(YwElectrical device : allList){
|
if(isDeletedDevice(device,allHkList)){
|
device.setIsdeleted(Constants.ONE);
|
device.setEditDate(date);
|
deleteList.add(device);
|
}
|
}
|
}
|
}
|
private YwElectrical initDeviceDataByData(YwElectrical model, MeterInfoResponse door, Date date, List<MeterInfoResponse> allHkList) {
|
MeterInfoResponse device = getDeviceByid(door.getCollectorid(),allHkList);
|
Date originCreateDate = model.getCreateDate();
|
model.setIsdeleted(Constants.ZERO);
|
model.setEditDate(date);
|
if (originCreateDate == null) {
|
model.setCreateDate(date);
|
}
|
model.setDescription(door.getDescription());
|
model.setName(door.getDescription());
|
model.setCId(door.getCid());
|
model.setDId(door.getId());
|
model.setPriceid(door.getPriceid());
|
model.setParamId(door.getParam_id());
|
model.setDeviceType(door.getDevice_type());
|
model.setAccountId(door.getAccount_id());
|
model.setAddress(door.getAddress());
|
model.setCollectorId(door.getCollectorid());
|
model.setType(door.getType());
|
model.setRelayStatus(door.getRelay_state());
|
model.setCsq(door.getCsq());
|
model.setAccountStatus(resolveAccountStatusByPurchaseCount(door.getCount()));
|
model.setRemark(JSONObject.toJSONString(device));
|
return model;
|
}
|
|
/** 购买次数大于 0 视为已开户 */
|
private Integer resolveAccountStatusByPurchaseCount(String purchaseCount) {
|
if (StringUtils.isBlank(purchaseCount)) {
|
return Constants.ZERO;
|
}
|
try {
|
return new BigDecimal(purchaseCount.trim()).compareTo(BigDecimal.ZERO) > 0
|
? Constants.ONE : Constants.ZERO;
|
} catch (NumberFormatException e) {
|
return Constants.ZERO;
|
}
|
}
|
private MeterInfoResponse getDeviceByid(String indexCode, List<MeterInfoResponse> allHkList ) {
|
if(allHkList!=null && allHkList.size()>0){
|
for(MeterInfoResponse info : allHkList){
|
if(StringUtils.equals(indexCode,info.getCollectorid())){
|
return info;
|
}
|
}
|
}
|
|
return new MeterInfoResponse();
|
}
|
private YwElectrical getExistedDevice(MeterInfoResponse device, List<YwElectrical> allList) {
|
if(allList.size()>0){
|
for(YwElectrical r : allList){
|
if(StringUtils.equals(r.getCollectorId(), device.getCollectorid())){
|
//表示未删除
|
return r;
|
}
|
}
|
}
|
return null;
|
}
|
|
@Override
|
public void controlDo(Device param){
|
YwElectrical model = ywElectricalMapper.selectById(param.getId());
|
if(model ==null && Constants.equalsInteger(param.getType(),Constants.DEVICE_TYPE.dianbiao)){
|
throw new BusinessException(ResponseStatus.DATA_EMPTY);
|
}
|
if (param.getStatus() == null || param.getCmdDate() == null
|
||param.getCmdDate().getTime() <= System.currentTimeMillis()) {
|
//如果是开闸
|
throw new BusinessException(ResponseStatus.BAD_REQUEST);
|
}
|
|
boolean r ;
|
String date = DateUtil.formatDate(new Date(),"yyyyMMddHHmmss");
|
if(Constants.equalsInteger(param.getStatus(),Constants.ONE)) {
|
//如果是开闸
|
r = WaterElectricityUtil.electricityAct(param.getIp(),Integer.parseInt(param.getPort()),param.getNo(),0,date);
|
}else {
|
//如果是合闸
|
r = WaterElectricityUtil.electricityAct(param.getIp(),Integer.parseInt(param.getPort()),param.getNo(),1,date);
|
}
|
if(!r){
|
throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"远程控制电表失败,请稍后重试!");
|
}
|
/*String curremak = "【"+param.getLoginUserInfo().getRealname()
|
+"】于"+ DateUtil.getPlusTime2(new Date()) +"进行了"+(Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【合闸】":"【分闸】")+"操作,开关【"+param.getChannelNo()+"】;";
|
deviceMapper.update(null,new UpdateWrapper<Device>().lambda()
|
.set(Device::getRemark,curremak)
|
.set(Device::getEditDate,new Date())
|
.set(Device::getEdirot,param.getLoginUserInfo().getId())
|
.eq(Device::getId,param.getId()));
|
DeviceData data = new DeviceData();
|
data.setCreateDate(new Date());
|
data.setEditDate(new Date());
|
data.setCreator(param.getLoginUserInfo().getId());
|
data.setEditor(param.getLoginUserInfo().getId());
|
data.setDeviceId(param.getId()+"");
|
data.setDataType(Constants.ONE);//
|
data.setVal1("远程控制");
|
data.setVal2(curremak);
|
data.setHappenTime(DateUtil.getPlusTime2(data.getCreateDate()));
|
data.setVal3((Constants.equalsInteger(param.getStatus(),Constants.ONE)?"【合闸】":"【分闸】"));
|
data.setVal4(param.getLoginUserInfo().getRealname());
|
data.setVal5(param.getChannelNo());
|
deviceDataMapper.insert(data);
|
*/
|
}
|
}
|