| | |
| | | package com.ycl.platform.service.impl; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | 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.domain.entity.YwPeople; |
| | | import com.ycl.platform.domain.entity.YwPoint; |
| | | import com.ycl.platform.domain.entity.YwUnit; |
| | | 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.domain.query.YwPointQuery; |
| | | import com.ycl.platform.domain.vo.YwPointVO; |
| | | import com.ycl.platform.mapper.YwPeopleMapper; |
| | | import com.ycl.platform.mapper.YwPointMapper; |
| | | import com.ycl.platform.mapper.YwUnitMapper; |
| | | import com.ycl.platform.service.YwPointService; |
| | | import com.ycl.platform.service.YwUnitService; |
| | | import com.ycl.system.Result; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ycl.platform.domain.form.YwPointForm; |
| | | import com.ycl.platform.domain.vo.YwPointVO; |
| | | import com.ycl.platform.domain.query.YwPointQuery; |
| | | import java.util.List; |
| | | |
| | | import com.ycl.system.entity.SysDept; |
| | | import com.ycl.system.service.ISysDeptService; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.ycl.system.mapper.SysDeptMapper; |
| | | import com.ycl.system.page.PageUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ycl.thread.PointImportCallable; |
| | | import com.ycl.utils.DateUtils; |
| | | import com.ycl.utils.SecurityUtils; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.Assert; |
| | | import java.util.ArrayList; |
| | | import java.util.Objects; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | import java.util.function.Consumer; |
| | | import java.util.stream.Collectors; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | /** |
| | | * 运维点位 服务实现类 |
| | |
| | | @RequiredArgsConstructor |
| | | public class YwPointServiceImpl extends ServiceImpl<YwPointMapper, YwPoint> implements YwPointService { |
| | | |
| | | private final YwPointMapper ywPointMapper; |
| | | @Autowired |
| | | private ISysDeptService deptService; |
| | | @Autowired |
| | | private YwUnitService unitService; |
| | | private final YwUnitMapper ywUnitMapper; |
| | | private final SysDeptMapper sysDeptMapper; |
| | | private final YwUnitService unitService; |
| | | private final YwPeopleMapper ywPeopleMapper; |
| | | |
| | | private static final ExecutorService executorService = new ThreadPoolExecutor(8, |
| | | 24, |
| | | 5000, |
| | | TimeUnit.SECONDS, |
| | | new ArrayBlockingQueue<>(10), |
| | | new ThreadPoolExecutor.CallerRunsPolicy() |
| | | ); |
| | | |
| | | /** |
| | | * 添加 |
| | |
| | | @Override |
| | | public Result add(YwPointForm form) { |
| | | YwPoint entity = YwPointForm.getEntityByForm(form, null); |
| | | Date now = new Date(); |
| | | entity.setCreateTime(now); |
| | | entity.setUpdateTime(now); |
| | | if(baseMapper.insert(entity) > 0) { |
| | | return Result.ok("添加成功"); |
| | | } |
| | |
| | | .forEach(form -> { |
| | | YwPoint entity = YwPointForm.getEntityByForm(form, null); |
| | | entity.setStatus("未开始"); |
| | | Date now = new Date(); |
| | | entity.setCreateTime(now); |
| | | entity.setUpdateTime(now); |
| | | baseMapper.insert(entity); |
| | | }); |
| | | return Result.ok("添加成功"); |
| | |
| | | new LambdaUpdateChainWrapper<>(baseMapper) |
| | | .in(YwPoint::getId, form.getIds()) |
| | | .set(YwPoint::getUnitId, form.getUnitId()) |
| | | .set(YwPoint::getStartTime, form.getStartTime()) |
| | | .set(YwPoint::getEndTime, form.getEndTime()) |
| | | .set(YwPoint::getUpdateTime, new Date()) |
| | | .update(); |
| | | } else { |
| | | new LambdaUpdateChainWrapper<>(baseMapper) |
| | |
| | | .set(YwPoint::getUnitId, form.getUnitId()) |
| | | .set(YwPoint::getStartTime, null) |
| | | .set(YwPoint::getEndTime, null) |
| | | .set(YwPoint::getUpdateTime, new Date()) |
| | | .update(); |
| | | } |
| | | return Result.ok("修改成功"); |
| | |
| | | */ |
| | | @Override |
| | | public Result update(YwPointForm form) { |
| | | |
| | | YwPoint entity = baseMapper.selectById(form.getId()); |
| | | |
| | | // 为空抛IllegalArgumentException,做全局异常处理 |
| | | Assert.notNull(entity, "记录不存在"); |
| | | BeanUtils.copyProperties(form, entity); |
| | | if (baseMapper.updateById(entity) > 0) { |
| | | return Result.ok("修改成功"); |
| | | } |
| | | return Result.error("修改失败"); |
| | | Date now = new Date(); |
| | | entity.setUpdateTime(now); |
| | | baseMapper.updateById(entity); |
| | | return Result.ok("修改成功"); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public Result page(YwPointQuery query) { |
| | | |
| | | IPage<YwPoint> page = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .like(StringUtils.hasText(query.getPointName()), YwPoint::getPointName, query.getPointName()) |
| | | .orderByDesc(YwPoint::getCreateTime) |
| | | .page(PageUtil.getPage(query, YwPoint.class)); |
| | | |
| | | List<YwPointVO> vos = page.getRecords().stream() |
| | | .map( |
| | | entity -> YwPointVO.getVoByEntity(entity, null) |
| | | .setDeptName(deptService.selectDeptById(entity.getDeptId()).getDeptName()) |
| | | .setUnitName(unitService.getById(entity.getUnitId()).getUnitName()) |
| | | ) |
| | | .collect(Collectors.toList()); |
| | | |
| | | return Result.ok().data(vos).total(page.getTotal()); |
| | | IPage<YwPointVO> page = PageUtil.getPage(query, YwPointVO.class); |
| | | baseMapper.page(page, query); |
| | | return Result.ok().data(page.getRecords()).total(page.getTotal()); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public Result detail(String id) { |
| | | |
| | | YwPoint entity = baseMapper.selectById(id); |
| | | Assert.notNull(entity, "记录不存在"); |
| | | YwPointVO vo = YwPointVO.getVoByEntity(entity, null); |
| | | if (Objects.nonNull(entity.getDeptId())) { |
| | | vo.setDeptIds(sysDeptMapper.selectParents(entity.getDeptId())); |
| | | } |
| | | return Result.ok().data(vo); |
| | | } |
| | | |
| | |
| | | .collect(Collectors.toList()); |
| | | return Result.ok().data(vos); |
| | | } |
| | | |
| | | @Override |
| | | public Result select(String keyword) { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | // 获取当前运维人员的运维单位 |
| | | YwPeople people = new LambdaQueryChainWrapper<>(ywPeopleMapper) |
| | | .eq(YwPeople::getUserId, userId) |
| | | .one(); |
| | | Integer unitId = null; |
| | | if (Objects.nonNull(people)) { |
| | | unitId = people.getBelongUnit(); |
| | | } |
| | | |
| | | List<YwPoint> list = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .eq(YwPoint::getUnitId, unitId) |
| | | .like(YwPoint::getPointName, keyword) |
| | | .or() |
| | | .eq(YwPoint::getUnitId, unitId) |
| | | .like(YwPoint::getSerialNumber, keyword) |
| | | .list(); |
| | | List data = list.stream().map(item -> { |
| | | Map map = new HashMap<String, Object>(); |
| | | map.put("id", item.getSerialNumber()); |
| | | map.put("value", item.getPointName()); |
| | | return map; |
| | | }).collect(Collectors.toList()); |
| | | return Result.ok().data(data); |
| | | } |
| | | |
| | | @Override |
| | | public boolean updateRecovery(List<String> serialNumberList, int recovery) { |
| | | return new LambdaUpdateChainWrapper<>(baseMapper) |
| | | .in(YwPoint::getSerialNumber, serialNumberList) |
| | | .set(YwPoint::getRecovery, recovery) |
| | | .set(YwPoint::getUpdateTime, new Date()) |
| | | .set(recovery == 0, YwPoint::getRecoveryTime, DateUtils.getNowDate()) |
| | | .update(); |
| | | } |
| | | |
| | | @Override |
| | | public List<YwPoint> home() { |
| | | return baseMapper.home(); |
| | | } |
| | | |
| | | @Override |
| | | public void export(YwPointQuery query, HttpServletResponse response) throws IOException { |
| | | // 导出数据 |
| | | List<PointExport> exportData = baseMapper.export(query); |
| | | EasyExcel.write(response.getOutputStream(), PointExport.class) |
| | | .sheet("点位更换运维单位") |
| | | .doWrite(exportData); |
| | | } |
| | | |
| | | @Override |
| | | public Result importData(MultipartFile file, Integer unitId, Date startTime, Date endTime, Boolean needUpdateUnit) throws IOException { |
| | | 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(); |
| | | 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)) { |
| | | throw new RuntimeException("导入数据不能为空"); |
| | | } |
| | | 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())); |
| | | if (needUpdateUnit) { |
| | | if (Objects.nonNull(unitId)) { |
| | | point.setUnitId(Long.valueOf(unitId)); |
| | | } |
| | | point.setStartTime(startTime); |
| | | point.setEndTime(endTime); |
| | | } else { |
| | | point.setUnitId(null); |
| | | point.setStartTime(null); |
| | | point.setEndTime(null); |
| | | } |
| | | |
| | | point.setPointName(item.getPointName()); |
| | | point.setSerialNumber(item.getSerialNumber()); |
| | | return point; |
| | | }).collect(Collectors.toList()); |
| | | this.waitAllFinishAndGetResult(pointList); |
| | | } |
| | | |
| | | 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; |
| | | } else { |
| | | List<YwPoint> list = dataList.subList(start, start + 800); |
| | | this.baseMapper.updatePoint(list); |
| | | start += 800; |
| | | } |
| | | } |
| | | Date endTime = new Date(); |
| | | log.error("总共耗时:" + (endTime.getTime() - startTime.getTime())); |
| | | } |
| | | |
| | | } |