187 lines
6.9 KiB
Java
187 lines
6.9 KiB
Java
package com.whdc.zhdbaqapi.service.impl;
|
|
|
|
import cn.afterturn.easypoi.excel.ExcelImportUtil;
|
|
import cn.afterturn.easypoi.excel.entity.ImportParams;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.whdc.zhdbaqapi.constant.Constants;
|
|
import com.whdc.zhdbaqapi.exception.MyException;
|
|
import com.whdc.zhdbaqapi.mapper.DeviceInfoMapper;
|
|
import com.whdc.zhdbaqapi.model.dto.FindDeviceDto;
|
|
import com.whdc.zhdbaqapi.model.entity.DeviceInfo;
|
|
import com.whdc.zhdbaqapi.model.vo.DeviceInfoImpVo;
|
|
import com.whdc.zhdbaqapi.service.IDeviceInfoService;
|
|
import com.whdc.zhdbaqapi.utils.DataUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.Serializable;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* @author 李赛
|
|
* @date 2022-07-21 23:47
|
|
*/
|
|
@Service
|
|
public class DeviceInfoServiceimpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements IDeviceInfoService {
|
|
@Autowired
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
@Override
|
|
public DeviceInfo get(Integer id) {
|
|
DeviceInfo ret = baseMapper.get(id);
|
|
|
|
return ret;
|
|
}
|
|
|
|
@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);
|
|
}
|
|
|
|
@Override
|
|
@CacheEvict(Constants.CACHE_NAME)
|
|
public boolean restore(Integer id) {
|
|
DeviceInfo entity = baseMapper.selectById(id);
|
|
|
|
if (entity == null) {
|
|
return false;
|
|
}
|
|
|
|
if (baseMapper.checkValidStationCode(entity.getStationCode()) != null) {
|
|
throw new MyException(entity.getStationCode() + " 已被其他有效数据使用,不能恢复");
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
@Override
|
|
@Transactional
|
|
public DeviceInfoImpVo imp(InputStream file) {
|
|
ImportParams params = new ImportParams();
|
|
params.setHeadRows(1);
|
|
params.setNeedSave(false);
|
|
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 {
|
|
if (StringUtils.isNotBlank(di.getPicPath())) {
|
|
String filepath = System.getProperty("user.dir") + "/" + di.getPicPath();
|
|
di.setSketchMap(DataUtils.File2Base64(filepath));
|
|
DataUtils.DelFile(filepath);
|
|
}
|
|
|
|
impList.add(di);
|
|
}
|
|
}
|
|
|
|
if (impList.isEmpty()) {
|
|
return new DeviceInfoImpVo().setLoad(list.size()).setSuccess(0).setFail(err.size()).setErr(err).setMsg("测点编号全部已存在");
|
|
}
|
|
|
|
if (super.saveBatch(impList)) {
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void clearCache() {
|
|
Set<String> keys = redisTemplate.keys(Constants.CACHE_NAME + "*");
|
|
for (String k : keys) {
|
|
redisTemplate.delete(k);
|
|
}
|
|
}
|
|
}
|