liriming
2024-08-27 c7f70933adf89a163d0049c907492a6df60cb45f
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
package com.iailab.module.data.channel.modbus.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.common.util.object.ConvertUtils;
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.ModBusTagPageReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
 
/**
 * @author lirm
 * @Description
 * @createTime 2024年08月27日
 */
@Slf4j
@Service
public class ChannelModbusTagServiceImpl extends ServiceImpl<ChannelModBusTagDao, ChannelModBusTagEntity> implements ChannelModbusTagService {
    @Resource
    private ChannelModBusTagDao channelModBusTagDao;
 
    @Value("${iems.upload-dir}")
    private String uploadDir;
 
    @Override
    public PageResult<ChannelModBusTagEntity> queryPage(ModBusTagPageReqVO reqVO) {
        return channelModBusTagDao.selectPage(reqVO);
    }
 
    @Override
    public ChannelModBusTagEntity info(String id) {
        return channelModBusTagDao.selectById(id);
    }
 
    @Override
    public void add(ChannelModBusTagEntity channelModBusTagEntity) {
        channelModBusTagDao.insert(channelModBusTagEntity);
    }
 
    @Override
    public void update(ChannelModBusTagEntity channelModBusTagEntity) {
        channelModBusTagDao.updateById(channelModBusTagEntity);
    }
 
    @Override
    public void delete(String id) {
        channelModBusTagDao.deleteById(id);
    }
 
    @Override
    public List<ChannelModBusTagEntity> getByDevice(String device) {
        QueryWrapper<ChannelModBusTagEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device", device);
        queryWrapper.orderByDesc("create_time");
        return channelModBusTagDao.selectList(queryWrapper);
    }
 
    @Override
    public List<ChannelModbusTagDTO> selectAll() {
        List<ChannelModBusTagEntity> entityList = baseMapper.selectList(
                null
        );
        return ConvertUtils.sourceToTarget(entityList, ChannelModbusTagDTO.class);
    }
 
    @Override
    public List<ChannelModBusTagEntity> listByIds(List<String> ids) {
        return baseMapper.selectList(new QueryWrapper<ChannelModBusTagEntity>().in("id", ids));
    }
 
    @Override
    public void deleteByDeviceName(String name) {
        baseMapper.delete(new QueryWrapper<ChannelModBusTagEntity>().eq("device", name));
    }
 
//    /**
//     * 导入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;
//        }
//    }
 
}