| | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ycl.dataListener.CurrencyDataListener; |
| | | import com.ycl.platform.base.AbsVo; |
| | | import com.ycl.platform.domain.entity.ReportAuditingRecord; |
| | | import com.ycl.platform.domain.entity.YwPeople; |
| | | import com.ycl.platform.domain.entity.YwPoint; |
| | | import com.ycl.platform.domain.entity.*; |
| | | import com.ycl.platform.domain.excel.PointExport; |
| | | import com.ycl.platform.domain.form.BatchEditPointForm; |
| | | import com.ycl.platform.domain.form.YwPointForm; |
| | |
| | | import com.ycl.platform.mapper.TMonitorMapper; |
| | | import com.ycl.platform.mapper.YwPeopleMapper; |
| | | import com.ycl.platform.mapper.YwPointMapper; |
| | | import com.ycl.platform.service.DynamicColumnService; |
| | | import com.ycl.platform.service.YwPointService; |
| | | import com.ycl.platform.service.YwUnitService; |
| | | import com.ycl.system.Result; |
| | |
| | | import com.ycl.utils.DateUtils; |
| | | import com.ycl.utils.SecurityUtils; |
| | | import com.ycl.utils.poi.EasyExcelImportUtils; |
| | | import com.ycl.utils.uuid.IdUtils; |
| | | import constant.PointHeaderConstant; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.Assert; |
| | |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | @Slf4j |
| | | public class YwPointServiceImpl extends ServiceImpl<YwPointMapper, YwPoint> implements YwPointService { |
| | | |
| | | private final SysDeptMapper sysDeptMapper; |
| | | private final TMonitorMapper monitorMapper; |
| | | private final DynamicColumnMapper dynamicColumnMapper; |
| | | private final ApplicationContext context; |
| | | |
| | | /** |
| | | * 添加 |
| | |
| | | YwPoint entity = baseMapper.selectById(form.getId()); |
| | | Assert.notNull(entity, "记录不存在"); |
| | | BeanUtils.copyProperties(form, entity); |
| | | if (form.getUnitId() != null) { |
| | | entity.setUnitId(Long.valueOf(form.getUnitId())); |
| | | }else { |
| | | entity.setUnitId(null); |
| | | } |
| | | Date now = new Date(); |
| | | entity.setUpdateTime(now); |
| | | baseMapper.updateById(entity); |
| | | baseMapper.myUpdateById(entity); |
| | | return Result.ok("修改成功"); |
| | | } |
| | | |
| | |
| | | List<DynamicColumnVO> dynamics = dynamicColumnMapper.getData(); |
| | | Map<Integer, List<DynamicColumnVO>> dynamicMap = new HashMap<>(); |
| | | if (!CollectionUtils.isEmpty(dynamics)) { |
| | | dynamicMap = dynamics.stream().filter(vo ->vo.getId()!=null).collect(Collectors.groupingBy(AbsVo::getId)); |
| | | dynamicMap = dynamics.stream().filter(vo -> vo.getRefId() != null).collect(Collectors.groupingBy(DynamicColumnVO::getRefId)); |
| | | } |
| | | //定义表头列表 |
| | | LinkedHashSet<String> headers = new LinkedHashSet<>(); |
| | | headers.add(PointHeaderConstant.ID); |
| | | headers.add(PointHeaderConstant.Point_Name); |
| | | headers.add(PointHeaderConstant.Serial_Number); |
| | | headers.add(PointHeaderConstant.IP); |
| | | headers.add(PointHeaderConstant.Area); |
| | | headers.add(PointHeaderConstant.Unit); |
| | | headers.add(PointHeaderConstant.Province_Tag); |
| | | headers.add(PointHeaderConstant.Impotrant_Command_Image); |
| | | headers.add(PointHeaderConstant.Dept_Tag); |
| | |
| | | for (PointExport export : exportData) { |
| | | List<Object> list = new ArrayList<>(); |
| | | //需要保证顺序一致 |
| | | list.add(export.getId()); |
| | | list.add(export.getPointName()); |
| | | list.add(export.getSerialNumber()); |
| | | list.add(export.getPointIP()); |
| | | list.add(export.getArea()); |
| | | list.add(export.getUnitName()); |
| | | list.add(export.getProvinceTagString()); |
| | | list.add(export.getImportantCommandImageTagString()); |
| | | list.add(export.getDeptTag()); |
| | | list.add(export.getDeptTagString()); |
| | | for (String header : headersList) { |
| | | //创建一个变量记录找没找到这个header |
| | | boolean flag = false; |
| | |
| | | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result importData(MultipartFile file, Integer unitId, Date startTime, Date endTime, Boolean |
| | | needUpdateUnit) throws IOException { |
| | | // List<Map<String,String>> list = EasyExcelImportUtils.makeData(file); |
| | | Consumer<List<PointExport>> consumer = (dataList) -> { |
| | | try { |
| | | this.updatePoint(dataList, unitId, startTime, endTime, needUpdateUnit); |
| | | } catch (ExecutionException e) { |
| | | e.printStackTrace(); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | needUpdateUnit) throws IOException, ExecutionException, InterruptedException { |
| | | List<Map<String, String>> list = EasyExcelImportUtils.makeData(file); |
| | | List<PointExport> dataList = new ArrayList<>(); |
| | | // Consumer<List<PointExport>> consumer = (dataList) -> { |
| | | // try { |
| | | // this.updatePoint(dataList, unitId, startTime, endTime, needUpdateUnit); |
| | | // } catch (ExecutionException e) { |
| | | // e.printStackTrace(); |
| | | // } catch (InterruptedException e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // }; |
| | | // EasyExcel.read(file.getInputStream(), PointExport.class, new CurrencyDataListener(consumer)).headRowNumber(1).doReadAll(); |
| | | for (Map<String, String> map : list) { |
| | | PointExport pointExport = new PointExport(); |
| | | pointExport.setId(Integer.valueOf(map.get(PointHeaderConstant.ID))); |
| | | pointExport.setPointName(map.get(PointHeaderConstant.Point_Name)); |
| | | pointExport.setPointIP(map.get(PointHeaderConstant.IP)); |
| | | pointExport.setSerialNumber(map.get(PointHeaderConstant.Serial_Number)); |
| | | pointExport.setProvinceTag("是".equals(map.get(PointHeaderConstant.Province_Tag))); |
| | | pointExport.setImportantCommandImageTag("是".equals(map.get(PointHeaderConstant.Impotrant_Command_Image))); |
| | | pointExport.setDeptTag("是".equals(map.get(PointHeaderConstant.Dept_Tag))); |
| | | List<DynamicColumnVO> dynamics = new ArrayList<>(); |
| | | // 处理动态列 |
| | | for (Map.Entry<String, String> entry : map.entrySet()) { |
| | | String key = entry.getKey(); |
| | | if (!PointHeaderConstant.KNOWN_KEYS.contains(key)) { |
| | | DynamicColumnVO dynamicColumnVO = new DynamicColumnVO(); |
| | | dynamicColumnVO.setRefId(pointExport.getId()); |
| | | dynamicColumnVO.setLabelValue(entry.getKey()); |
| | | dynamicColumnVO.setColumnValue(entry.getValue()); |
| | | dynamicColumnVO.setPropName(IdUtils.randomNO(new Date())); |
| | | dynamicColumnVO.setTableName("t_yw_point"); |
| | | dynamics.add(dynamicColumnVO); |
| | | } |
| | | } |
| | | }; |
| | | EasyExcel.read(file.getInputStream(), PointExport.class, new CurrencyDataListener(consumer)).headRowNumber(1).doReadAll(); |
| | | // for (Map<String, String> map : list) { |
| | | // PointExport pointExport = new PointExport(); |
| | | // pointExport.setPointName(map.get(PointHeaderConstant.Point_Name)); |
| | | // pointExport.setPointIP(map.get(PointHeaderConstant.IP)); |
| | | // pointExport.setSerialNumber(map.get(PointHeaderConstant.Serial_Number)); |
| | | // } |
| | | // this.updatePoint(dataList, unitId, startTime, endTime, needUpdateUnit); |
| | | // System.out.println(list); |
| | | pointExport.setDynamicData(dynamics); |
| | | dataList.add(pointExport); |
| | | } |
| | | YwPointServiceImpl self = context.getBean(YwPointServiceImpl.class); |
| | | self.updatePoint(dataList, unitId, startTime, endTime, needUpdateUnit); |
| | | self.updateDynamic(dataList); |
| | | return Result.ok(); |
| | | } |
| | | |
| | |
| | | * @param dataList |
| | | * @param unitId |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePoint(List<PointExport> dataList, Integer unitId, Date startTime, Date endTime, Boolean |
| | | needUpdateUnit) throws ExecutionException, InterruptedException { |
| | | if (CollectionUtils.isEmpty(dataList)) { |
| | |
| | | } |
| | | List<YwPoint> pointList = dataList.stream().map(item -> { |
| | | YwPoint point = new YwPoint(); |
| | | point.setImportantCommandImageTag("是".equals(item.getImportantCommandImageTagString())); |
| | | point.setProvinceTag("是".equals(item.getProvinceTagString())); |
| | | point.setDeptTag("是".equals(item.getDeptTag())); |
| | | point.setImportantCommandImageTag(item.getImportantCommandImageTag()); |
| | | point.setProvinceTag(item.getProvinceTag()); |
| | | point.setDeptTag(item.getDeptTag()); |
| | | if (needUpdateUnit) { |
| | | if (Objects.nonNull(unitId)) { |
| | | point.setUnitId(Long.valueOf(unitId)); |
| | | } else { |
| | | point.setUnitId(null); |
| | | } |
| | | point.setStartTime(startTime); |
| | | point.setEndTime(endTime); |
| | |
| | | point.setSerialNumber(item.getSerialNumber()); |
| | | return point; |
| | | }).collect(Collectors.toList()); |
| | | this.waitAllFinishAndGetResult(pointList); |
| | | this.waitAllFinishAndGetResult(pointList, needUpdateUnit); |
| | | } |
| | | |
| | | public void waitAllFinishAndGetResult(List<YwPoint> dataList) throws |
| | | InterruptedException, ExecutionException { |
| | | int start = 0; |
| | | Date startTime = new Date(); |
| | | while (true) { |
| | | if (dataList.size() < start + 800) { |
| | | List<YwPoint> list = dataList.subList(start, dataList.size() - 1); |
| | | this.baseMapper.updatePoint(list); |
| | | break; |
| | | @Override |
| | | public void updateDynamic(List<PointExport> dataList) { |
| | | List<DynamicColumnValue> insertList = new ArrayList<>(); |
| | | List<DynamicColumnValue> updateList = new ArrayList<>(); |
| | | List<Integer> deleteList = new ArrayList<>(); |
| | | //查出所有种类动态列作为字典 |
| | | Map<String, Integer> dictionary = new LambdaQueryChainWrapper<>(dynamicColumnMapper) |
| | | .list().stream().collect(Collectors.toMap(DynamicColumn::getLabelValue, DynamicColumn::getId)); |
| | | //查出点位表的所有动态列 |
| | | List<DynamicColumnVO> dynamicVOList = dynamicColumnMapper.getData(); |
| | | Map<Integer, List<DynamicColumnVO>> dynamicVOMap = new HashMap<>(); |
| | | if (!CollectionUtils.isEmpty(dynamicVOList)) { |
| | | dynamicVOMap = dynamicVOList.stream().filter(vo -> vo.getRefId() != null).collect(Collectors.groupingBy(DynamicColumnVO::getRefId)); |
| | | } |
| | | //根据label_value和ref_id判断是否是同一数据 |
| | | for (PointExport pointExport : dataList) { |
| | | //得到excel中动态列名作为key的map |
| | | Map<String, DynamicColumnVO> exportMap = pointExport.getDynamicData().stream().collect(Collectors.toMap(DynamicColumnVO::getLabelValue, Function.identity())); |
| | | //查出数据库中这个点位的动态列 |
| | | List<DynamicColumnVO> dynamicColumnVOS = dynamicVOMap.get(pointExport.getId()); |
| | | if (!CollectionUtils.isEmpty(dynamicColumnVOS)) { |
| | | //遍历数据库中这个点位的动态列,并根据动态列名找出excel中的值 |
| | | for (DynamicColumnVO vo : dynamicColumnVOS) { |
| | | DynamicColumnVO dynamicColumnVO = exportMap.get(vo.getLabelValue()); |
| | | //存在说明excel有这个数据,将数据库这个数据的值改为excel的值 |
| | | if (dynamicColumnVO.getColumnValue() != null) { |
| | | DynamicColumnValue entity = new DynamicColumnValue(); |
| | | entity.setColumnValue(dynamicColumnVO.getColumnValue()); |
| | | entity.setDynamicColumnId(vo.getId()); |
| | | entity.setRefId(vo.getRefId()); |
| | | entity.setId(vo.getValueId()); |
| | | updateList.add(entity); |
| | | |
| | | } else { |
| | | //不存在则取消标签,删除 |
| | | deleteList.add(vo.getValueId()); |
| | | } |
| | | } |
| | | } else { |
| | | List<YwPoint> list = dataList.subList(start, start + 800); |
| | | this.baseMapper.updatePoint(list); |
| | | start += 800; |
| | | //数据库里查不到excel里面的这个点位动态列,全为新增 |
| | | List<DynamicColumnVO> dynamicData = pointExport.getDynamicData(); |
| | | for (DynamicColumnVO excelData : dynamicData) { |
| | | if (excelData.getColumnValue() != null) { |
| | | Integer dynamicId = dictionary.get(excelData.getLabelValue()); |
| | | DynamicColumnValue entity = new DynamicColumnValue(); |
| | | entity.setColumnValue(excelData.getColumnValue()); |
| | | entity.setDynamicColumnId(dynamicId); |
| | | entity.setRefId(pointExport.getId()); |
| | | insertList.add(entity); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | //mybaits方法 |
| | | //插入 |
| | | if (!CollectionUtils.isEmpty(insertList)) dynamicColumnMapper.saveBatch(insertList); |
| | | //更新 |
| | | if (!CollectionUtils.isEmpty(updateList)) dynamicColumnMapper.updateBatch(updateList); |
| | | //删除 |
| | | if (!CollectionUtils.isEmpty(deleteList)) dynamicColumnMapper.deleteBatch(deleteList); |
| | | } |
| | | |
| | | /** |
| | | * 根据设备编码查询点位信息 |
| | | * |
| | | * @param serialNumber 序列号 |
| | | * @return {@link YwPoint } |
| | | * @author |
| | | */ |
| | | @Override |
| | | public YwPoint selectBySerialNumber(String serialNumber) { |
| | | return baseMapper.selectOne(new LambdaQueryWrapper<>(YwPoint.class).eq(YwPoint::getSerialNumber, serialNumber).eq(YwPoint::getDeleted, "0")); |
| | | } |
| | | |
| | | public void waitAllFinishAndGetResult(List<YwPoint> dataList, Boolean needUpdateUnit) { |
| | | Date startTime = new Date(); |
| | | this.baseMapper.updatePoint(dataList, needUpdateUnit); |
| | | Date endTime = new Date(); |
| | | log.error("总共耗时:" + (endTime.getTime() - startTime.getTime())); |
| | | } |