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 org.apache.commons.lang3.StringUtils; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; 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 implements IDeviceInfoService { @Override public DeviceInfo get(Integer id) { return baseMapper.get(id); } @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 list(String stationCode) { return baseMapper.list(stationCode); } @Override @Cacheable(Constants.CACHE_NAME) public List listAll() { return baseMapper.listAll(); } @Override public IPage page(FindDeviceDto findDto) { return baseMapper.page(findDto.getPage(), findDto); } @Override @Transactional public DeviceInfoImpVo imp(InputStream file) { ImportParams params = new ImportParams(); params.setHeadRows(1); try { List list = ExcelImportUtil.importExcel(file, DeviceInfo.class, params); if (list == null || list.isEmpty()) { return new DeviceInfoImpVo().setLoad(0).setSuccess(0).setFail(0).setMsg("没有读取有有效数据"); } List err = new ArrayList<>(); List impList = new ArrayList<>(); Set scs = new HashSet<>(); Map 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 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 { 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()); } } }