fxkh-txl-service/src/main/java/com/whdc/zhdbaqapi/service/impl/DeviceInfoServiceimpl.java

187 lines
6.9 KiB
Java
Raw Normal View History

2022-07-22 10:22:03 +08:00
package com.whdc.zhdbaqapi.service.impl;
2022-07-22 12:19:58 +08:00
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
2022-07-22 10:22:03 +08:00
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whdc.zhdbaqapi.constant.Constants;
2022-07-22 12:19:58 +08:00
import com.whdc.zhdbaqapi.exception.MyException;
2022-07-22 10:22:03 +08:00
import com.whdc.zhdbaqapi.mapper.DeviceInfoMapper;
import com.whdc.zhdbaqapi.model.dto.FindDeviceDto;
import com.whdc.zhdbaqapi.model.entity.DeviceInfo;
2022-07-22 12:19:58 +08:00
import com.whdc.zhdbaqapi.model.vo.DeviceInfoImpVo;
2022-07-22 10:22:03 +08:00
import com.whdc.zhdbaqapi.service.IDeviceInfoService;
2022-07-26 21:40:58 +08:00
import com.whdc.zhdbaqapi.utils.DataUtils;
2022-07-22 12:19:58 +08:00
import org.apache.commons.lang3.StringUtils;
2022-07-26 21:40:58 +08:00
import org.springframework.beans.factory.annotation.Autowired;
2022-07-22 10:22:03 +08:00
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
2022-07-26 21:40:58 +08:00
import org.springframework.data.redis.core.RedisTemplate;
2022-07-22 10:22:03 +08:00
import org.springframework.stereotype.Service;
2022-07-22 12:19:58 +08:00
import org.springframework.transaction.annotation.Transactional;
2022-07-22 10:22:03 +08:00
2022-07-22 12:19:58 +08:00
import java.io.InputStream;
2022-07-22 10:22:03 +08:00
import java.io.Serializable;
2022-07-22 12:19:58 +08:00
import java.util.*;
2022-07-22 10:22:03 +08:00
/**
* @author
* @date 2022-07-21 23:47
*/
@Service
public class DeviceInfoServiceimpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements IDeviceInfoService {
2022-07-26 21:40:58 +08:00
@Autowired
private RedisTemplate<String, String> redisTemplate;
2022-07-22 10:22:03 +08:00
@Override
public DeviceInfo get(Integer id) {
2022-07-26 21:40:58 +08:00
DeviceInfo ret = baseMapper.get(id);
return ret;
2022-07-22 10:22:03 +08:00
}
2022-07-22 22:18:46 +08:00
@Override
@CacheEvict(Constants.CACHE_NAME)
public boolean save(DeviceInfo entity) {
if (baseMapper.checkValidStationCode(entity.getStationCode()) != null) {
throw new MyException("stationCode=" + entity.getStationCode() + " 已被其他有效数据使用,不能恢复");
}
entity.setCreateDate(new Date());
return super.save(entity);
}
@Override
@CacheEvict(Constants.CACHE_NAME)
public boolean updateById(DeviceInfo entity) {
DeviceInfo old = baseMapper.checkValidStationCode(entity.getStationCode());
if (old != null && !old.getId().equals(entity.getId())) {
throw new MyException("stationCode=" + entity.getStationCode() + " 已被其他有效数据使用,不能恢复");
}
entity.setModificationTime(new Date());
return super.updateById(entity);
}
@Override
@CacheEvict(Constants.CACHE_NAME)
public boolean removeById(Serializable id) {
DeviceInfo entity = baseMapper.selectById(id);
entity.setDel(1);
return super.updateById(entity);
}
2022-07-22 10:22:03 +08:00
@Override
@CacheEvict(Constants.CACHE_NAME)
public boolean restore(Integer id) {
DeviceInfo entity = baseMapper.selectById(id);
2022-07-22 12:19:58 +08:00
if (entity == null) {
return false;
}
if (baseMapper.checkValidStationCode(entity.getStationCode()) != null) {
throw new MyException(entity.getStationCode() + " 已被其他有效数据使用,不能恢复");
}
2022-07-22 10:22:03 +08:00
entity.setDel(0);
return super.updateById(entity);
}
@Override
public List<DeviceInfo> list(String stationCode) {
return baseMapper.list(stationCode);
}
@Override
@Cacheable(Constants.CACHE_NAME)
public List<DeviceInfo> listAll() {
return baseMapper.listAll();
}
@Override
public IPage<DeviceInfo> page(FindDeviceDto findDto) {
return baseMapper.page(findDto.getPage(), findDto);
}
2022-07-22 12:19:58 +08:00
@Override
@Transactional
public DeviceInfoImpVo imp(InputStream file) {
ImportParams params = new ImportParams();
params.setHeadRows(1);
2022-07-26 21:40:58 +08:00
params.setNeedSave(false);
2022-07-22 12:19:58 +08:00
try {
List<DeviceInfo> list = ExcelImportUtil.importExcel(file, DeviceInfo.class, params);
if (list == null || list.isEmpty()) {
return new DeviceInfoImpVo().setLoad(0).setSuccess(0).setFail(0).setMsg("没有读取有有效数据");
}
List<DeviceInfo> err = new ArrayList<>();
List<DeviceInfo> impList = new ArrayList<>();
Set<String> scs = new HashSet<>();
Map<String, DeviceInfo> chkMap = new HashMap<>();
for (DeviceInfo di : list) {
if (StringUtils.isBlank(di.getStationCode())) {
err.add(di);
} else {
scs.add(di.getStationCode());
}
}
if (scs.isEmpty() || err.size() == list.size()) {
return new DeviceInfoImpVo().setLoad(list.size()).setSuccess(0).setFail(err.size()).setErr(err).setMsg("没有读取到有效的测点编号");
}
if (scs.size() != list.size()) {
return new DeviceInfoImpVo().setLoad(list.size()).setSuccess(0).setFail(err.size()).setErr(err).setMsg("有重复的测点编号,请检查后再导入");
}
List<DeviceInfo> chkscs = baseMapper.listBySC(scs);
if (chkscs.size() == scs.size()) {
return new DeviceInfoImpVo().setLoad(list.size()).setSuccess(0).setFail(err.size()).setErr(err).setMsg("测点编号全部已存在");
}
for (DeviceInfo dbdi : chkscs) {
chkMap.put(dbdi.getStationCode(), dbdi);
}
for (DeviceInfo di : list) {
if (chkMap.containsKey(di.getStationCode())) {
err.add(di);
} else {
2022-07-26 21:40:58 +08:00
if (StringUtils.isNotBlank(di.getPicPath())) {
String filepath = System.getProperty("user.dir") + "/" + di.getPicPath();
di.setSketchMap(DataUtils.File2Base64(filepath));
DataUtils.DelFile(filepath);
}
2022-07-22 12:19:58 +08:00
impList.add(di);
}
}
if (impList.isEmpty()) {
return new DeviceInfoImpVo().setLoad(list.size()).setSuccess(0).setFail(err.size()).setErr(err).setMsg("测点编号全部已存在");
}
if (super.saveBatch(impList)) {
2022-07-26 21:40:58 +08:00
2022-07-22 12:19:58 +08:00
return new DeviceInfoImpVo().setLoad(list.size()).setSuccess(impList.size()).setFail(err.size()).setErr(err).setMsg("成功");
} else {
return new DeviceInfoImpVo().setLoad(list.size()).setSuccess(0).setFail(err.size()).setErr(err).setMsg("失败");
}
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new MyException("导入异常!" + e.getMessage());
}
}
2022-07-26 21:40:58 +08:00
@Override
public void clearCache() {
Set<String> keys = redisTemplate.keys(Constants.CACHE_NAME + "*");
for (String k : keys) {
redisTemplate.delete(k);
}
}
2022-07-22 10:22:03 +08:00
}