| | |
| | | package com.linghu.service.impl; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.linghu.listener.PlatformExcelListener; |
| | | import com.linghu.listener.TypeDropdownWriteHandler; |
| | | import com.linghu.mapper.KeywordMapper; |
| | | import com.linghu.mapper.ReferenceMapper; |
| | | import com.linghu.model.common.ResponseResult; |
| | | import com.linghu.model.entity.Keyword; |
| | | import com.linghu.model.entity.Platform; |
| | | import com.linghu.model.entity.Reference; |
| | | import com.linghu.model.entity.Type; |
| | | import com.linghu.model.excel.ExcelDataWithRow; |
| | | import com.linghu.model.excel.PlatformExcel; |
| | | import com.linghu.model.page.CustomPage; |
| | | import com.linghu.service.PlatformService; |
| | | import com.linghu.mapper.PlatformMapper; |
| | | import com.linghu.service.TypeService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author xy |
| | |
| | | public class PlatformServiceImpl extends ServiceImpl<PlatformMapper, Platform> |
| | | implements PlatformService { |
| | | |
| | | @Autowired |
| | | private TypeService typeService; |
| | | @Resource |
| | | private PlatformMapper platformMapper; |
| | | @Autowired |
| | | private ReferenceMapper referenceMapper; |
| | | @Autowired |
| | | private KeywordMapper keywordMapper; |
| | | |
| | | @Override |
| | | public Platform getPlatformByDomain(String domain,String platformName) { |
| | | return platformMapper.getPlatformByDomain(domain,platformName); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public Platform getPlatformByDomain(String domain) { |
| | | return platformMapper.getPlatformByDomainWeiYi(domain); |
| | | } |
| | | |
| | | @Override |
| | | public ResponseResult<Platform> addPlatform(Platform platform) { |
| | | // 校验平台名称和域名不能为空 |
| | | if (!StringUtils.hasText(platform.getPlatform_name())) { |
| | | return ResponseResult.error("平台名称不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(platform.getDomain())) { |
| | | return ResponseResult.error("平台域名不能为空"); |
| | | } |
| | | platform.setCreate_time(LocalDateTime.now()); |
| | | |
| | | boolean success = this.save(platform); |
| | | if (success) { |
| | | return ResponseResult.success(platform); |
| | | } |
| | | return ResponseResult.error("添加平台失败"); |
| | | } |
| | | |
| | | @Override |
| | | public ResponseResult<Void> deleteByPlatFormId(Integer platformId) { |
| | | //平台被引用了没 |
| | | Integer count = Math.toIntExact(referenceMapper.selectCount(new LambdaQueryWrapper<Reference>().eq(Reference::getPlatform_id, platformId))); |
| | | if (count > 0) { |
| | | return ResponseResult.error("该平台被引用中,不能删除"); |
| | | } |
| | | boolean success = this.removeById(platformId); |
| | | if (success) { |
| | | return ResponseResult.success(); |
| | | } |
| | | return ResponseResult.error("删除平台失败"); |
| | | } |
| | | |
| | | @Override |
| | | public ResponseResult<CustomPage<Platform>> platformPageList(Integer page, Integer pageSize, Integer type_id, Integer keywordId, Integer questionId, Integer isNow) { |
| | | List<Integer> platForm=new ArrayList<>(); |
| | | //先查找当前关键词下,所有的回答 的 所有的平台名称 |
| | | Keyword keyword=new Keyword(); |
| | | if (keywordId !=null){ |
| | | keyword = keywordMapper.selectById(keywordId); |
| | | } |
| | | |
| | | if (keywordId != null && questionId == null) { |
| | | |
| | | List<Reference> references = referenceMapper.selectList(new LambdaQueryWrapper<Reference>() |
| | | .eq(Reference::getKeyword_id, keywordId) |
| | | .eq(Reference::getNum, isNow == 0 ? 1 : keyword.getNum()) |
| | | ); |
| | | platForm = references.stream().map(Reference::getPlatform_id).filter(Objects::nonNull).distinct().collect(Collectors.toList()); |
| | | } |
| | | if (keywordId != null && questionId != null) { |
| | | List<Reference> references = referenceMapper.selectList(new LambdaQueryWrapper<Reference>() |
| | | .eq(Reference::getKeyword_id, keywordId) |
| | | .eq(Reference::getNum, isNow == 0 ? 1 : keyword.getNum()) |
| | | .eq(Reference::getQuestion_id, questionId) |
| | | ); |
| | | platForm = references.stream().map(Reference::getPlatform_id).filter(Objects::nonNull).distinct().collect(Collectors.toList()); |
| | | } |
| | | |
| | | // 构建查询条件并添加排序(按创建时间倒序) |
| | | LambdaQueryWrapper<Platform> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.orderByDesc(Platform::getCreate_time); // 新增的排序条件 |
| | | if (!platForm.isEmpty()){ |
| | | queryWrapper.in(Platform::getPlatform_id, platForm); |
| | | } |
| | | if (page != null && pageSize != null && type_id == null){ |
| | | Page<Platform> pageInfo = new Page<>(page, pageSize); |
| | | Page<Platform> result = this.page(pageInfo, queryWrapper); |
| | | return ResponseResult.success(new CustomPage<>(result)); |
| | | } else if (page != null && pageSize != null && type_id != null) { |
| | | Page<Platform> pageInfo = new Page<>(page, pageSize); |
| | | queryWrapper.eq(Platform::getType_id, type_id); |
| | | Page<Platform> result = this.page(pageInfo, queryWrapper); |
| | | return ResponseResult.success(new CustomPage<>(result)); |
| | | |
| | | } else { |
| | | CustomPage<Platform> customPage = new CustomPage<>(new Page<>()); |
| | | customPage.setRecords(new ArrayList<>()); |
| | | customPage.setTotal(0); |
| | | return ResponseResult.success(customPage); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public ResponseResult<String> importPlatformsExcel(MultipartFile file) { |
| | | try { |
| | | if (file.isEmpty()) { |
| | | return ResponseResult.error("上传文件不能为空"); |
| | | } |
| | | |
| | | // 获取数据库中的旧数据 |
| | | List<Platform> oldPlatforms = this.list(); |
| | | |
| | | // 使用自定义监听器读取数据(包含行号) |
| | | PlatformExcelListener listener = new PlatformExcelListener(); |
| | | EasyExcel.read(file.getInputStream()) |
| | | .head(PlatformExcel.class) |
| | | .sheet() |
| | | .registerReadListener(listener) |
| | | .doRead(); |
| | | |
| | | List<ExcelDataWithRow<PlatformExcel>> excelList = listener.getDataList(); |
| | | List<Platform> platforms = new ArrayList<>(); |
| | | List<String> errorMessages = new ArrayList<>(); |
| | | |
| | | // 遍历数据并验证 |
| | | for (ExcelDataWithRow<PlatformExcel> excelData : excelList) { |
| | | int rowNum = excelData.getRowNumber(); |
| | | PlatformExcel excel = excelData.getData(); |
| | | List<String> rowErrors = new ArrayList<>(); |
| | | |
| | | // 验证逻辑 |
| | | if (!StringUtils.hasText(excel.getPlatform_name())) { |
| | | rowErrors.add("平台名称不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(excel.getDomain())) { |
| | | rowErrors.add("平台域名不能为空"); |
| | | } |
| | | |
| | | // 查找类型(仅当类型名称不为空时检查,避免空指针) |
| | | Integer typeId = null; |
| | | if (StringUtils.hasText(excel.getType_name())) { |
| | | Type typeByName = typeService.getTypeByName(excel.getType_name()); |
| | | if (typeByName == null) { |
| | | rowErrors.add("未知的平台类型: " + excel.getType_name()); |
| | | } else { |
| | | typeId = typeByName.getType_id(); |
| | | } |
| | | } else { |
| | | rowErrors.add("平台类型不能为空"); |
| | | } |
| | | |
| | | if (!rowErrors.isEmpty()) { |
| | | errorMessages.add(String.format("第%d行错误: %s", rowNum, String.join(";", rowErrors))); |
| | | } else { |
| | | // 构建Platform对象 |
| | | Platform platform = new Platform(); |
| | | platform.setPlatform_name(excel.getPlatform_name()); |
| | | platform.setDomain(excel.getDomain()); |
| | | platform.setType_id(typeId); |
| | | platform.setCreate_time(LocalDateTime.now()); |
| | | |
| | | // 检查重复性 |
| | | boolean isDuplicate = false; |
| | | for (Platform oldPlatform : oldPlatforms) { |
| | | // 判断重复的条件,这里假设平台名称和域名都相同才算重复 |
| | | if (oldPlatform.getPlatform_name().equals(platform.getPlatform_name()) && |
| | | oldPlatform.getDomain().equals(platform.getDomain())) { |
| | | isDuplicate = true; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 如果不重复才添加到导入列表 |
| | | if (!isDuplicate) { |
| | | platforms.add(platform); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 错误处理 |
| | | if (!errorMessages.isEmpty()) { |
| | | StringBuilder errorMsg = new StringBuilder(); |
| | | errorMsg.append("导入失败,共").append(errorMessages.size()).append("条数据存在错误:\n"); |
| | | errorMessages.forEach(msg -> errorMsg.append(msg).append("\n")); |
| | | return ResponseResult.error(400, errorMsg.toString()); |
| | | } |
| | | |
| | | // 无错误时导入 |
| | | if (!platforms.isEmpty()) { |
| | | this.saveBatch(platforms); |
| | | return ResponseResult.success("导入成功,新增" + platforms.size() + "条数据"); |
| | | } else { |
| | | return ResponseResult.error("导入完成,没有新增数据(所有数据均已存在)"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | log.error("文件解析失败", e); |
| | | return ResponseResult.error("文件解析失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public ResponseEntity<byte[]> downloadPlatformExcel() { |
| | | // 获取所有类型名称 |
| | | List<Type> typeList = typeService.list(); |
| | | List<String> typeNames = typeList.stream() |
| | | .map(Type::getType_name) |
| | | .collect(Collectors.toList()); |
| | | |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | | |
| | | // 注册下拉框拦截器 |
| | | EasyExcel.write(out, PlatformExcel.class) |
| | | .registerWriteHandler(new TypeDropdownWriteHandler(typeNames)) |
| | | .sheet("平台模板") |
| | | .doWrite(new ArrayList<>()); |
| | | |
| | | return ResponseEntity.ok() |
| | | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=platform_template.xlsx") |
| | | .contentType(MediaType.APPLICATION_OCTET_STREAM) |
| | | .body(out.toByteArray()); |
| | | } |
| | | |
| | | @Override |
| | | public ResponseResult<Void> updatePlatform(Platform platform) { |
| | | // 校验平台名称和域名不能为空 |
| | | if (!StringUtils.hasText(platform.getPlatform_name())) { |
| | | return ResponseResult.error("平台名称不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(platform.getDomain())) { |
| | | return ResponseResult.error("平台域名不能为空"); |
| | | } |
| | | |
| | | this.updateById(platform); |
| | | |
| | | return ResponseResult.success(); |
| | | } |
| | | |
| | | |
| | | |
| | | } |