| | |
| | | package com.iailab.module.data.channel.modbus.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.iailab.module.data.common.utils.PageUtils; |
| | | import com.iailab.module.data.common.utils.Query; |
| | | import com.iailab.framework.common.pojo.PageResult; |
| | | import com.iailab.framework.common.util.object.BeanUtils; |
| | | import com.iailab.framework.common.util.object.ConvertUtils; |
| | | import com.iailab.module.data.channel.modbus.dto.ChannelModbusTagDTO; |
| | | import com.iailab.module.data.channel.modbus.dao.ChannelModBusTagDao; |
| | | import com.iailab.module.data.channel.modbus.dto.ChannelModbusTagDTO; |
| | | import com.iailab.module.data.channel.modbus.entity.ChannelModBusTagEntity; |
| | | import com.iailab.module.data.channel.modbus.service.ChannelModbusTagService; |
| | | import com.iailab.module.data.channel.modbus.vo.ModBusTagImportExcelVO; |
| | | import com.iailab.module.data.channel.modbus.vo.ModBusTagPageReqVO; |
| | | import com.iailab.module.data.channel.tag.vo.TagImportRespVO; |
| | | import com.iailab.module.data.common.enums.CommonConstant; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static com.iailab.module.data.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * @author DongYukun |
| | | * @author lirm |
| | | * @Description |
| | | * @createTime 2023年05月08日 15:04:00 |
| | | * @createTime 2024年08月27日 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | |
| | | @Value("${iems.upload-dir}") |
| | | private String uploadDir; |
| | | |
| | | /** |
| | | * 分页查询tag |
| | | * |
| | | * @param params |
| | | */ |
| | | @Override |
| | | public PageUtils queryPage(Map<String, Object> params) { |
| | | String tagName = (String) params.get("tagName"); |
| | | String address = (String) params.get("address"); |
| | | String tagDesc = (String) params.get("tagDesc"); |
| | | String device = (String) params.get("device"); |
| | | |
| | | IPage<ChannelModBusTagEntity> page = this.page( |
| | | new Query<ChannelModBusTagEntity>().getPage(params), |
| | | new QueryWrapper<ChannelModBusTagEntity>() |
| | | .like(StringUtils.isNotBlank(tagName), "tag_name", tagName) |
| | | .like(StringUtils.isNotBlank(address), "address", address) |
| | | .like(StringUtils.isNotBlank(tagDesc), "tag_desc", tagDesc) |
| | | .eq(StringUtils.isNotBlank(device), "device", device) |
| | | .orderByDesc("create_time") |
| | | ); |
| | | return new PageUtils(page); |
| | | public PageResult<ChannelModBusTagEntity> queryPage(ModBusTagPageReqVO reqVO) { |
| | | return channelModBusTagDao.selectPage(reqVO); |
| | | } |
| | | |
| | | /** |
| | | * 查询tag详情 |
| | | * |
| | | * @param id |
| | | */ |
| | | @Override |
| | | public ChannelModBusTagEntity info(String id) { |
| | | return channelModBusTagDao.selectById(id); |
| | | } |
| | | |
| | | /** |
| | | * 添加tag |
| | | * |
| | | * @param channelModBusTagEntity |
| | | */ |
| | | @Override |
| | | public void add(ChannelModBusTagEntity channelModBusTagEntity) { |
| | | channelModBusTagDao.insert(channelModBusTagEntity); |
| | | } |
| | | |
| | | /** |
| | | * 修改tag |
| | | * |
| | | * @param channelModBusTagEntity |
| | | */ |
| | | @Override |
| | | public void update(ChannelModBusTagEntity channelModBusTagEntity) { |
| | | channelModBusTagDao.updateById(channelModBusTagEntity); |
| | | } |
| | | |
| | | /** |
| | | * 删除tag |
| | | * |
| | | * @param id |
| | | */ |
| | | @Override |
| | | public void delete(String id) { |
| | | channelModBusTagDao.deleteById(id); |
| | |
| | | return channelModBusTagDao.selectList(queryWrapper); |
| | | } |
| | | |
| | | /** |
| | | * 查询全部 |
| | | * |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<ChannelModbusTagDTO> selectAll() { |
| | | List<ChannelModBusTagEntity> entityList = baseMapper.selectList( |
| | |
| | | return ConvertUtils.sourceToTarget(entityList, ChannelModbusTagDTO.class); |
| | | } |
| | | |
| | | /** |
| | | * listByIds |
| | | * |
| | | * @param ids |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<ChannelModBusTagEntity> listByIds(List<String> ids) { |
| | | return baseMapper.selectList(new QueryWrapper<ChannelModBusTagEntity>().in("id", ids)); |
| | | } |
| | | |
| | | /** |
| | | * deleteByDeviceName |
| | | * |
| | | * @param name |
| | | */ |
| | | @Override |
| | | public void deleteByDeviceName(String name) { |
| | | baseMapper.delete(new QueryWrapper<ChannelModBusTagEntity>().eq("device", name)); |
| | | } |
| | | |
| | | //TODO 使用easyExcel重写 |
| | | // /** |
| | | // * 导入Tag |
| | | // * |
| | | // * @param device |
| | | // * @param file |
| | | // * @throws Exception |
| | | // */ |
| | | // @Override |
| | | // @Transactional(rollbackFor = Exception.class) |
| | | // public void importTag(String device, MultipartFile file) throws Exception { |
| | | // try { |
| | | // String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); |
| | | // String fileName = UUID.randomUUID().toString() + suffix; |
| | | // String path = uploadDir + fileName; |
| | | // file.transferTo(new File(path)); |
| | | // |
| | | // XSSFWorkbook hssfWorkbook = new XSSFWorkbook(new FileInputStream(path)); |
| | | // XSSFSheet sheet = hssfWorkbook.getSheetAt(0); |
| | | // int lastRowNum = sheet.getLastRowNum(); |
| | | // log.info("最后一行:" + lastRowNum); |
| | | // int lastCellNum = 6; |
| | | // List<ChannelModBusTagEntity> dangerList = new ArrayList<>(); |
| | | // for (int i = 1; i <= lastRowNum; i++) { |
| | | // XSSFRow row = sheet.getRow(i); |
| | | // for (int j = row.getFirstCellNum(); j < lastCellNum; j++) { |
| | | // row.getCell(j).setCellType(CellType.STRING); |
| | | // } |
| | | // ChannelModBusTagEntity tagEntity = new ChannelModBusTagEntity(); |
| | | // tagEntity.setId(UUID.randomUUID().toString()); |
| | | // tagEntity.setTagName(row.getCell(1).getStringCellValue()); |
| | | // tagEntity.setDataType(row.getCell(2).getStringCellValue()); |
| | | // tagEntity.setEnabled(true); |
| | | // tagEntity.setFormat(""); |
| | | // tagEntity.setDevice(device); |
| | | // tagEntity.setSamplingRate(0); |
| | | // row.getCell(4).setCellType(CellType.STRING); |
| | | // System.out.println(row.getCell(3).getStringCellValue()); |
| | | // if(row.getCell(3).getStringCellValue().equals("1")){ |
| | | // tagEntity.setAddress(String.format("1%04d",Integer.parseInt(row.getCell(4).getStringCellValue()))); |
| | | // }else if(row.getCell(3).getStringCellValue().equals("3")){ |
| | | // tagEntity.setAddress(String.format("4%04d",Integer.parseInt(row.getCell(4).getStringCellValue()))); |
| | | // } |
| | | // tagEntity.setTagDesc(row.getCell(5).getStringCellValue()); |
| | | // dangerList.add(tagEntity); |
| | | // } |
| | | // if (CollectionUtils.isEmpty(dangerList)) { |
| | | // return; |
| | | // } |
| | | // //getBaseMapper().insertList(dangerList); |
| | | // dangerList.forEach(item -> { |
| | | // try { |
| | | // getBaseMapper().insert(item); |
| | | // } catch (Exception ex) { |
| | | // log.warn("插入异常:" + item.getTagName()); |
| | | // } |
| | | // }); |
| | | // } catch (Exception ex) { |
| | | // ex.printStackTrace(); |
| | | // log.warn("导入失败!"); |
| | | // throw ex; |
| | | // } |
| | | // } |
| | | @Override |
| | | public TagImportRespVO importModBusTagList(List<ModBusTagImportExcelVO> importTags, boolean isUpdateSupport, String device) { |
| | | // 1.1 参数校验 |
| | | if (CollUtil.isEmpty(importTags)) { |
| | | throw exception(TAG_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 遍历,逐个创建 or 更新 |
| | | TagImportRespVO respVO = TagImportRespVO.builder().createTagNames(new ArrayList<>()) |
| | | .updateTagNames(new ArrayList<>()).failureTagNames(new LinkedHashMap<>()).build(); |
| | | importTags.forEach(importTag -> { |
| | | |
| | | // 判断如果不存在,再进行插入 |
| | | ChannelModBusTagEntity existTag = channelModBusTagDao.selectOne(new QueryWrapper<ChannelModBusTagEntity>() |
| | | .eq("device", device) |
| | | .eq("tag_name",importTag.getTagName())); |
| | | if (existTag == null) { |
| | | ChannelModBusTagEntity channelModBusTagEntity = ConvertUtils.sourceToTarget(importTag, ChannelModBusTagEntity.class); |
| | | channelModBusTagEntity.setId(UUID.randomUUID().toString()); |
| | | channelModBusTagEntity.setEnabled(CommonConstant.IS_ENABLE); |
| | | channelModBusTagEntity.setDevice(device); |
| | | channelModBusTagEntity.setCreateTime(new Date()); |
| | | channelModBusTagDao.insert(channelModBusTagEntity); |
| | | |
| | | respVO.getCreateTagNames().add(channelModBusTagEntity.getTagName()); |
| | | return; |
| | | } |
| | | |
| | | // 如果存在,判断是否允许更新 |
| | | if (!isUpdateSupport) { |
| | | respVO.getFailureTagNames().put(importTag.getTagName(), TAG_EXISTS.getMsg()); |
| | | return; |
| | | } |
| | | |
| | | ChannelModBusTagEntity updateTag = BeanUtils.toBean(importTag, ChannelModBusTagEntity.class); |
| | | updateTag.setId(existTag.getId()); |
| | | baseMapper.updateById(updateTag); |
| | | respVO.getUpdateTagNames().add(importTag.getTagName()); |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |