| | |
| | | package com.linghu.controller; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.handler.SheetWriteHandler; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
| | | import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | 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.Platform; |
| | | import com.linghu.model.entity.Reference; |
| | | import com.linghu.model.entity.Type; |
| | | import com.linghu.model.entity.*; |
| | | import com.linghu.model.excel.ExcelDataWithRow; |
| | | import com.linghu.model.excel.PlatformExcel; |
| | | import com.linghu.model.excel.UserExcel; |
| | | import com.linghu.model.page.CustomPage; |
| | | import com.linghu.service.PlatformService; |
| | | import com.linghu.service.TypeService; |
| | | |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.log4j.Log4j; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.DataValidation; |
| | | import org.apache.poi.ss.usermodel.DataValidationConstraint; |
| | | import org.apache.poi.ss.usermodel.DataValidationHelper; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.util.CellRangeAddressList; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | |
| | | import java.io.IOException; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @RestController |
| | | @RequestMapping("/platform") |
| | | @Api(value = "平台相关接口", tags = "设置-平台") |
| | | @Slf4j |
| | | public class PlatformController { |
| | | |
| | | |
| | | @Autowired |
| | | private PlatformService platformService; |
| | | @Autowired |
| | | private TypeService typeService; |
| | | @Autowired |
| | | private ReferenceMapper referenceMapper; |
| | | |
| | | @PostMapping |
| | | @ApiOperation(value = "添加平台") |
| | | public ResponseResult<Platform> add(@RequestBody Platform platform) { |
| | | // 校验平台名称和域名不能为空 |
| | | if (!StringUtils.hasText(platform.getPlatform_name())) { |
| | | return ResponseResult.error("平台名称不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(platform.getDomain())) { |
| | | return ResponseResult.error("平台域名不能为空"); |
| | | } |
| | | |
| | | boolean success = platformService.save(platform); |
| | | if (success) { |
| | | return ResponseResult.success(platform); |
| | | } |
| | | return ResponseResult.error("添加平台失败"); |
| | | return platformService.addPlatform(platform); |
| | | } |
| | | |
| | | |
| | | @DeleteMapping("/{platformId}") |
| | | @ApiOperation(value = "删除平台") |
| | | public ResponseResult<Void> delete(@PathVariable Integer platformId) { |
| | | //平台被引用了没 |
| | | Integer count = referenceMapper.selectCount(new LambdaQueryWrapper<Reference>().eq(Reference::getPlatform_id, platformId)); |
| | | if (count > 0) { |
| | | return ResponseResult.error("该平台被引用中,不能删除"); |
| | | } |
| | | boolean success = platformService.removeById(platformId); |
| | | if (success) { |
| | | return ResponseResult.success(); |
| | | } |
| | | return ResponseResult.error("删除平台失败"); |
| | | return platformService.deleteByPlatFormId(platformId); |
| | | } |
| | | |
| | | |
| | | |
| | | @PutMapping |
| | | @ApiOperation(value = "更新平台") |
| | | public ResponseResult<Void> update(@RequestBody Platform platform) { |
| | | // 校验平台名称和域名不能为空 |
| | | if (!StringUtils.hasText(platform.getPlatform_name())) { |
| | | return ResponseResult.error("平台名称不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(platform.getDomain())) { |
| | | return ResponseResult.error("平台域名不能为空"); |
| | | } |
| | | |
| | | platformService.updateById(platform); |
| | | |
| | | return ResponseResult.success(); |
| | | return platformService.updatePlatform(platform); |
| | | } |
| | | |
| | | |
| | | @GetMapping("/{platformId}") |
| | | @ApiOperation("根据id获取平台") |
| | |
| | | } |
| | | |
| | | @GetMapping("/list") |
| | | @ApiOperation("查询平台列表") |
| | | public ResponseResult<Page<Platform>> list( |
| | | @RequestParam(required = false,defaultValue = "1") Integer page, |
| | | @RequestParam(required = false,defaultValue = "10") Integer pageSize) { |
| | | |
| | | Page<Platform> pageInfo = new Page<>(page, pageSize); |
| | | Page<Platform> result = platformService.page(pageInfo); |
| | | return ResponseResult.success(result); |
| | | |
| | | @ApiOperation("查询平台列表,不传页数和大小就查全部") |
| | | public ResponseResult<CustomPage<Platform>> list( |
| | | @RequestParam(value = "page",required = false) Integer page, |
| | | @RequestParam(value = "pageSize",required = false) Integer pageSize, |
| | | @RequestParam(value = "type_id",required = false) Integer type_id, |
| | | @RequestParam(value = "keywordId", required = false) Integer keywordId, |
| | | @RequestParam(value = "questionId",required = false) Integer questionId, |
| | | @RequestParam(value = "isNow",required = false) Integer isNow |
| | | ) { |
| | | return platformService.platformPageList(page, pageSize, type_id, keywordId, questionId, isNow); |
| | | } |
| | | |
| | | |
| | | |
| | | @GetMapping("/download") |
| | | @ApiOperation("下载平台模板") |
| | | public ResponseEntity<byte[]> downloadTemplate() throws IOException { |
| | | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| | | EasyExcel.write(out, PlatformExcel.class).sheet("平台模板").doWrite(new ArrayList<>()); |
| | | |
| | | return ResponseEntity.ok() |
| | | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=platform_template.xlsx") |
| | | .contentType(MediaType.APPLICATION_OCTET_STREAM) |
| | | .body(out.toByteArray()); |
| | | return platformService.downloadPlatformExcel(); |
| | | } |
| | | |
| | | |
| | | @PostMapping("/import") |
| | | @ApiOperation("导入平台数据") |
| | | public ResponseResult<String> importPlatforms(@RequestParam("file") MultipartFile file) { |
| | | try { |
| | | // 检查文件是否为空 |
| | | if (file.isEmpty()) { |
| | | return ResponseResult.error("上传文件不能为空"); |
| | | } |
| | | |
| | | // 读取Excel数据 |
| | | List<PlatformExcel> excelList = EasyExcel.read(file.getInputStream()) |
| | | .head(PlatformExcel.class) |
| | | .sheet() |
| | | .doReadSync(); |
| | | |
| | | // 数据转换与验证 |
| | | List<Platform> platforms = new ArrayList<>(); |
| | | List<String> errorMessages = new ArrayList<>(); |
| | | |
| | | for (PlatformExcel excel : excelList) { |
| | | // 检查必要字段 |
| | | if (!StringUtils.hasText(excel.getPlatform_name())) { |
| | | errorMessages.add("平台名称不能为空"); |
| | | continue; |
| | | } |
| | | if (!StringUtils.hasText(excel.getType_name())) { |
| | | errorMessages.add("平台类型不能为空"); |
| | | continue; |
| | | } |
| | | if (!StringUtils.hasText(excel.getDomain())) { |
| | | errorMessages.add("平台域名不能为空"); |
| | | continue; |
| | | } |
| | | |
| | | // 查找类型 |
| | | Type typeByName = typeService.getTypeByName(excel.getType_name()); |
| | | if (typeByName == null) { |
| | | errorMessages.add("未知的平台类型: " + excel.getType_name()); |
| | | continue; |
| | | } |
| | | |
| | | // 构建平台对象 |
| | | Platform platform = new Platform(); |
| | | platform.setPlatform_name(excel.getPlatform_name()); |
| | | platform.setDomain(excel.getDomain()); |
| | | platform.setType_id(typeByName.getType_id()); |
| | | |
| | | // 设置创建时间(解决之前的数据库错误) |
| | | platform.setCreate_time(LocalDateTime.now()); |
| | | |
| | | platforms.add(platform); |
| | | } |
| | | |
| | | // 处理错误 |
| | | if (!errorMessages.isEmpty()) { |
| | | return ResponseResult.error("数据验证失败: " + String.join("; ", errorMessages)); |
| | | } |
| | | |
| | | // 批量保存 |
| | | if (!platforms.isEmpty()) { |
| | | platformService.saveBatch(platforms); |
| | | return ResponseResult.success("成功导入" + platforms.size() + "条数据"); |
| | | } else { |
| | | return ResponseResult.error("没有有效数据可导入"); |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | // 记录详细异常信息 |
| | | |
| | | return ResponseResult.error("文件解析失败:" + e.getMessage()); |
| | | } |
| | | return platformService.importPlatformsExcel(file); |
| | | } |
| | | |
| | | |
| | | } |