dengzedong
2024-11-06 db184afd0c5bf3359b44eb0251fa5b07386eb3ff
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
package com.iailab.module.data.channel.opcua.service.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.opcua.dao.ChannelOPCUATagDao;
import com.iailab.module.data.channel.opcua.dto.ChannelOPCUATagDTO;
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity;
import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService;
import com.iailab.module.data.channel.opcua.vo.OpcUaTagImportExcelVO;
import com.iailab.module.data.channel.opcua.vo.OpcUaTagPageReqVO;
import com.iailab.module.data.channel.tag.vo.TagImportRespVO;
import com.iailab.module.data.common.enums.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
 
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.iailab.module.data.enums.ErrorCodeConstants.*;
 
/**
 * @author lirm
 * @Description
 * @createTime 2024年08月26日
 */
@Slf4j
@Service
public class ChannelOPCUATagServiceImpl extends ServiceImpl<ChannelOPCUATagDao, ChannelOPCUATagEntity> implements ChannelOPCUATagService {
    @Resource
    private ChannelOPCUATagDao channelOPCUATagDao;
 
    @Value("${iems.upload-dir}")
    private String uploadDir;
 
    @Override
    public PageResult<ChannelOPCUATagEntity> queryPage(OpcUaTagPageReqVO reqVO) {
        return channelOPCUATagDao.selectPage(reqVO);
    }
 
    @Override
    public ChannelOPCUATagEntity info(String id) {
        return channelOPCUATagDao.selectById(id);
    }
 
    @Override
    public void add(ChannelOPCUATagEntity channelOPCUATagEntity) {
        channelOPCUATagDao.insert(channelOPCUATagEntity);
    }
 
    @Override
    public void update(ChannelOPCUATagEntity channelOPCUATagEntity) {
        channelOPCUATagDao.updateById(channelOPCUATagEntity);
    }
 
    @Override
    public void delete(String id) {
        channelOPCUATagDao.deleteById(id);
    }
 
 
    @Override
    public List<ChannelOPCUATagEntity> getByDevice(String device) {
        QueryWrapper<ChannelOPCUATagEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device", device).orderByDesc ("create_time");
        return channelOPCUATagDao.selectList(queryWrapper);
    }
 
    @Override
    public List<ChannelOPCUATagDTO> selectAll() {
        List<ChannelOPCUATagEntity> entityList = baseMapper.selectList(
                null
        );
        return ConvertUtils.sourceToTarget(entityList, ChannelOPCUATagDTO.class);
    }
 
    @Override
    public List<ChannelOPCUATagEntity> listByIds(List<String> ids) {
        return baseMapper.selectList(new QueryWrapper<ChannelOPCUATagEntity>().in("id", ids));
    }
 
    @Override
    public void deleteByDeviceName(String name) {
        baseMapper.delete(new QueryWrapper<ChannelOPCUATagEntity>().eq("device",name));
    }
 
    @Override
    @DSTransactional(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 = 4;
            List<ChannelOPCUATagEntity> dangerList = new ArrayList<>();
            for (int i = 2; i <= lastRowNum; i++) {
                XSSFRow row = sheet.getRow(i);
                for (int j = row.getFirstCellNum(); j < lastCellNum; j++) {
                    row.getCell(j).setCellType(CellType.STRING);
                }
                ChannelOPCUATagEntity tagEntity = new ChannelOPCUATagEntity();
                tagEntity.setId(UUID.randomUUID().toString());
                tagEntity.setTagName(row.getCell(1).getStringCellValue());
                tagEntity.setDataType(row.getCell(2).getStringCellValue());
                row.getCell(4).setCellType(CellType.STRING);
                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.setEnabled(1);
                tagEntity.setDevice(device);
                tagEntity.setSamplingRate(1);
                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 importOpcUaTagList(List<OpcUaTagImportExcelVO> 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 -> {
            // 判断如果不存在,再进行插入
            ChannelOPCUATagEntity existTag = channelOPCUATagDao.selectOne(new QueryWrapper<ChannelOPCUATagEntity>()
                    .eq("device", device)
                    .eq("tag_name",importTag.getTagName()));
            if (existTag == null) {
                ChannelOPCUATagEntity channelOpCuaTagEntity = ConvertUtils.sourceToTarget(importTag, ChannelOPCUATagEntity.class);
                channelOpCuaTagEntity.setId(UUID.randomUUID().toString());
                channelOpCuaTagEntity.setEnabled(CommonConstant.IS_ENABLE);
                channelOpCuaTagEntity.setDevice(device);
                channelOpCuaTagEntity.setCreateTime(new Date());
                channelOPCUATagDao.insert(channelOpCuaTagEntity);
 
                respVO.getCreateTagNames().add(channelOpCuaTagEntity.getTagName());
                return;
            }
 
            // 如果存在,判断是否允许更新
            if (!isUpdateSupport) {
                respVO.getFailureTagNames().put(importTag.getTagName(), TAG_EXISTS.getMsg());
                return;
            }
 
            ChannelOPCUATagEntity updateTag = BeanUtils.toBean(importTag, ChannelOPCUATagEntity.class);
            updateTag.setId(existTag.getId());
            baseMapper.updateById(updateTag);
            respVO.getUpdateTagNames().add(importTag.getTagName());
        });
        return respVO;
    }
}