package com.doumee.service.business.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.device.WaterElectricityUtil; import com.doumee.core.exception.BusinessException; import com.doumee.core.haikang.model.HKConstants; import com.doumee.core.haikang.model.param.BaseResponse; import com.doumee.core.haikang.model.param.request.CustomBroadcastRequest; import com.doumee.core.haikang.model.param.request.TransparentChannelSingleRequest; import com.doumee.core.haikang.service.HKService; 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.*; import com.doumee.dao.business.model.Device; import com.doumee.dao.business.model.DeviceData; import com.doumee.dao.business.model.PlatformBroadcastLog; import com.doumee.service.business.DeviceService; import com.doumee.service.business.impl.hksync.HkSyncPushServiceImpl; import lombok.extern.slf4j.Slf4j; 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 java.util.*; /** * 设备信息表Service实现 * @author 江蹄蹄 * @date 2023/11/30 15:33 */ @Service @Slf4j public class DeviceServiceImpl implements DeviceService { @Autowired private DeviceMapper deviceMapper; @Autowired private DeviceDataMapper deviceDataMapper; @Autowired private PlatformMapper platformMapper; @Autowired private PlatformDeviceMapper platformDeviceMapper; @Autowired private SystemDictDataBiz systemDictDataBiz; @Autowired private PlatformBroadcastLogMapper platformBroadcastLogMapper; @Autowired private InterfaceLogMapper interfaceLogMapper; @Override public Integer create(Device model) { model.setCreator(model.getLoginUserInfo().getId()+""); model.setEdirot(model.getCreator()); model.setIsdeleted(Constants.ZERO); model.setEditDate(new Date()); model.setCreateDate(model.getEditDate()); if(model.getDoorNameObj()!=null && (Constants.equalsInteger(model.getType(),Constants.DEVICE_TYPE.duanluqi) ||Constants.equalsInteger(model.getType(),Constants.DEVICE_TYPE.dianbiao))){ model.setDoorName(JSONObject.toJSONString(model.getDoorNameObj())); if(StringUtils.isNotBlank(model.getLevel())){ if(getNumberByStr(model.getLevel()) <300){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,控制时长必须大于等于300秒"); } } } deviceMapper.insert(model); return model.getId(); } @Override public void deleteById(Integer id, LoginUserInfo userInfo) { Device update = new Device(); update.setEdirot(userInfo.getId()+""); update.setEditDate(new Date()); update.setIsdeleted(Constants.ONE); update.setId(id); deviceMapper.updateById(update); } @Override public void delete(Device device) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(device); deviceMapper.delete(deleteWrapper); } @Override public void deleteByIdInBatch(List ids, LoginUserInfo userInfo) { if (CollectionUtils.isEmpty(ids)) { return; } for(Integer id :ids){ deleteById(id,userInfo); } } @Override public void updateById(Device device) { device.setEdirot(device.getLoginUserInfo().getId()+""); device.setEditDate(new Date()); if(device.getDoorNameObj()!=null && (Constants.equalsInteger(device.getType(),Constants.DEVICE_TYPE.duanluqi) ||Constants.equalsInteger(device.getType(),Constants.DEVICE_TYPE.dianbiao))){ device.setDoorName(JSONObject.toJSONString(device.getDoorNameObj())); } Device model = deviceMapper.selectById(device.getId()); if(model ==null){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } deviceMapper.updateById(device); } @Override public void updateByIdInBatch(List devices) { if (CollectionUtils.isEmpty(devices)) { return; } for (Device device: devices) { this.updateById(device); } } @Override public Device findById(Integer id) { Device d = deviceMapper.selectById(id); if(StringUtils.isNotBlank(d.getDoorName()) &&Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.duanluqi)){ try { //断路器设备参数 d.setDoorNameObj(JSONObject.parseObject(d.getDoorName())); }catch (Exception e){ } } return d; } @Override public Device findOne(Device device) { QueryWrapper wrapper = new QueryWrapper<>(device); return deviceMapper.selectOne(wrapper); } @Override public List findList(Device device) { device.setIsdeleted(Constants.ZERO); QueryWrapper wrapper = new QueryWrapper<>(device); return deviceMapper.selectList(wrapper); } @Override public List findIdAndNameList(Device param) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(param); wrapper.select(Device::getId, Device::getName, Device::getDoorName,Device::getRegionPathName, Device::getRegionName); wrapper.eq(null != param.getType(),Device::getType,param.getType()) .eq(null !=param.getIsdeleted(),Device::getIsdeleted,param.getIsdeleted()) .eq(Objects.isNull(param.getIsdeleted()),Device::getIsdeleted,Constants.ZERO) .eq(null != param.getHkStatus(),Device::getHkStatus,param.getHkStatus()); List list = deviceMapper.selectList(wrapper); if(list!=null){ for(Device d : list){ if(StringUtils.isNotBlank(d.getDoorName()) &&Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.duanluqi)){ try { //断路器设备参数 d.setDoorNameObj(JSONObject.parseObject(d.getDoorName())); }catch (Exception e){ } } } } return list; } @Override public PageData findPage(PageWrap pageWrap) { IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); QueryWrapper queryWrapper = new QueryWrapper<>(); pageWrap.getModel().setIsdeleted(Constants.ZERO); Utils.MP.blankToNull(pageWrap.getModel()); if (pageWrap.getModel().getId() != null) { queryWrapper.lambda().eq(Device::getId, pageWrap.getModel().getId()); } if (pageWrap.getModel().getCreator() != null) { queryWrapper.lambda().eq(Device::getCreator, pageWrap.getModel().getCreator()); } if (pageWrap.getModel().getCreateDate() != null) { queryWrapper.lambda().ge(Device::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate())); queryWrapper.lambda().le(Device::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate())); } if (pageWrap.getModel().getEdirot() != null) { queryWrapper.lambda().eq(Device::getEdirot, pageWrap.getModel().getEdirot()); } if (pageWrap.getModel().getEditDate() != null) { queryWrapper.lambda().ge(Device::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate())); queryWrapper.lambda().le(Device::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate())); } if (pageWrap.getModel().getIsdeleted() != null) { queryWrapper.lambda().eq(Device::getIsdeleted, pageWrap.getModel().getIsdeleted()); } if (pageWrap.getModel().getRemark() != null) { queryWrapper.lambda().eq(Device::getRemark, pageWrap.getModel().getRemark()); } if (pageWrap.getModel().getIsEntrance() != null) { queryWrapper.lambda().eq(Device::getIsEntrance, pageWrap.getModel().getIsEntrance()); } if (pageWrap.getModel().getName() != null) { queryWrapper.lambda().like(Device::getName, pageWrap.getModel().getName()); } if (pageWrap.getModel().getDoorNo() != null) { queryWrapper.lambda().like(Device::getDoorNo, pageWrap.getModel().getDoorNo()); } if (pageWrap.getModel().getNo() != null) { queryWrapper.lambda().like(Device::getNo, pageWrap.getModel().getNo()); } if (pageWrap.getModel().getDoorName() != null) { queryWrapper.lambda().like(Device::getDoorName, pageWrap.getModel().getDoorName()); } if (pageWrap.getModel().getRegionPathName() != null) { queryWrapper.lambda().like(Device::getRegionPathName, pageWrap.getModel().getRegionPathName()); } if (pageWrap.getModel().getRegionName() != null) { queryWrapper.lambda().like(Device::getRegionName, pageWrap.getModel().getRegionName()); } if (pageWrap.getModel().getHkId() != null) { queryWrapper.lambda().eq(Device::getHkId, pageWrap.getModel().getHkId()); } if (pageWrap.getModel().getHkStatus() != null) { queryWrapper.lambda().eq(Device::getHkStatus, pageWrap.getModel().getHkStatus()); } if (pageWrap.getModel().getHkDate() != null) { queryWrapper.lambda().ge(Device::getHkDate, Utils.Date.getStart(pageWrap.getModel().getHkDate())); queryWrapper.lambda().le(Device::getHkDate, Utils.Date.getEnd(pageWrap.getModel().getHkDate())); } if (pageWrap.getModel().getSortnum() != null) { queryWrapper.lambda().eq(Device::getSortnum, pageWrap.getModel().getSortnum()); } if (pageWrap.getModel().getType() != null) { queryWrapper.lambda().eq(Device::getType, pageWrap.getModel().getType()); } if (pageWrap.getModel().getResourceType() != null) { queryWrapper.lambda().eq(Device::getResourceType, pageWrap.getModel().getResourceType()); } if (pageWrap.getModel().getNo() != null) { queryWrapper.lambda().eq(Device::getNo, pageWrap.getModel().getNo()); } if (pageWrap.getModel().getChannelNo() != null) { queryWrapper.lambda().eq(Device::getChannelNo, pageWrap.getModel().getChannelNo()); } if (pageWrap.getModel().getDevTypeCode() != null) { queryWrapper.lambda().eq(Device::getDevTypeCode, pageWrap.getModel().getDevTypeCode()); } if (pageWrap.getModel().getManufature() != null) { queryWrapper.lambda().eq(Device::getManufature, pageWrap.getModel().getManufature()); } if (pageWrap.getModel().getIp() != null) { queryWrapper.lambda().eq(Device::getIp, pageWrap.getModel().getIp()); } if (pageWrap.getModel().getPort() != null) { queryWrapper.lambda().eq(Device::getPort, pageWrap.getModel().getPort()); } if (pageWrap.getModel().getInoutType() != null) { queryWrapper.lambda().eq(Device::getInoutType, pageWrap.getModel().getInoutType()); } if (pageWrap.getModel().getLevel() != null) { queryWrapper.lambda().eq(Device::getLevel, pageWrap.getModel().getLevel()); } if (pageWrap.getModel().getIsUsed() != null) { queryWrapper.lambda().eq(Device::getIsUsed, pageWrap.getModel().getIsUsed()); } if (pageWrap.getModel().getOnline() != null) { queryWrapper.lambda().eq(Device::getOnline, pageWrap.getModel().getOnline()); } if (pageWrap.getModel().getKqType() != null) { queryWrapper.lambda().eq(Device::getKqType, pageWrap.getModel().getKqType()); } if (pageWrap.getModel().getStatus() != null) { queryWrapper.lambda().eq(Device::getStatus, pageWrap.getModel().getStatus()); } for(PageWrap.SortData sortData: pageWrap.getSorts()) { if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { queryWrapper.orderByDesc(sortData.getProperty()); } else { queryWrapper.orderByAsc(sortData.getProperty()); } } IPage result = deviceMapper.selectPage(page, queryWrapper); if(result!=null){ for(Device d : result.getRecords()){ if(StringUtils.isNotBlank(d.getDoorName()) && (Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.duanluqi)|| Constants.equalsInteger(d.getType(),Constants.DEVICE_TYPE.dianbiao))){ try { //断路器设备参数 d.setDoorNameObj(JSONObject.parseObject(d.getDoorName())); }catch (Exception e){ } } } } return PageData.from(result); } @Override public long count(Device device) { QueryWrapper wrapper = new QueryWrapper<>(device); return deviceMapper.selectCount(wrapper); } @Override public void setBroadcaseBobao(Device model){ List ids = new ArrayList<>(); ids.add(model.getHkId()); CustomBroadcastRequest request = new CustomBroadcastRequest(); request.setAudioPointIndexCode(ids); request.setPlayDuration(15);//单位秒 request.setBroadCastMode("tts"); request.setPriority(1); request.setState(1);//播放/停止标识 1-播放,0-停止 request.setPlayTtsContent(model.getSendInfo()); BaseResponse response = HKService.customBroadcast(request); if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"发送失败:"+ JSONObject.toJSONString(response)); } } @Override public void updateUsedById(Device param){ Device model = deviceMapper.selectById(param.getId()); if(model ==null && Constants.equalsInteger(param.getType(),Constants.DEVICE_TYPE.duanluqi)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } this.updateById(param); } @Override public void dianbaoCmd(Device param){ Device model = deviceMapper.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().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); } @Override @Transactional public void dianbiaoData(Device param){ Device model = deviceMapper.selectById(param.getId()); if(model ==null && Constants.equalsInteger(param.getType(),Constants.DEVICE_TYPE.dianbiao)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } try { Map readData= WaterElectricityUtil.electricityData(model.getIp(),Integer.parseInt(model.getPort()),model.getNo()); if(readData!=null){ String curremak = "【"+param.getLoginUserInfo().getRealname() +"】于"+ DateUtil.getPlusTime2(new Date()) +"进行了数据读取操作"; Date time =(Date) readData.get("time"); String total = (Double) readData.get("total")+""; String status = (String) readData.get("status"); model.setHkDate(new Date());//最近同步时间 model.setOnline(Constants.ONE);//标识设备在线 model.setRemark(curremak); 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.ZERO);// data.setVal1(total); data.setVal2(status); data.setHappenTime(DateUtil.getPlusTime2(data.getCreateDate())); data.setVal3(DateUtil.getPlusTime2(time)); data.setVal4(param.getLoginUserInfo().getRealname()); data.setVal5(param.getNo());//地址域 deviceDataMapper.insert(data); deviceMapper.updateById(model); } }catch (Exception e){ e.printStackTrace(); log.error("电表数据读取失败,"+e.getMessage()); throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"读取电表数据失败!"); } } @Override public void setLedContent(TransparentChannelSingleRequest model) { Device device = findById(model.getDeviceId()); if(device == null || Constants.equalsInteger(device.getIsdeleted(),Constants.ONE) || !Constants.equalsInteger(device.getType(),Constants.TWO)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(model.getSpeed()<=0){ int speed = 13; try { speed = Integer.parseInt(systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.LED_CONTENT_SPEED).getCode()); }catch (Exception e){ } model.setSpeed(speed); } PlatformBroadcastLog log = HkSyncPushServiceImpl.dealLedContentBiz(0,device.getNo(),device.getName(),model.getContent(),model.getSpeed(),1); platformBroadcastLogMapper.insert(log); if(log.getHkStatus() == null || !Constants.equalsInteger(log.getHkStatus(), Constants.TWO)){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,屏幕内容设置失败"+(log!=null?log.getHkInfo():"")); } } private DeviceData getLastDataByVal1(String b, List dataList,double limit) { List list = new ArrayList<>(); for(DeviceData d :dataList){ if(StringUtils.equals(d.getVal1(),b)){ if(limit <= getNumberByStr(d.getVal2())){ //如果有实时电流值大于空闲阈值,则表示工作中,不做处理 return null; } list.add(d); } } return list.size()>0?list.get(0):null; } private double getNumberByStr(String level) { try { return Double.parseDouble(level); }catch (Exception e){ } return 0; } }