| | |
| | | import com.gkhy.exam.system.mapper.CompanySummaryMapper; |
| | | import com.gkhy.exam.system.service.CompanySummaryService; |
| | | import com.gkhy.exam.system.service.SysCompanyService; |
| | | import org.apache.poi.schemas.vmldrawing.XmlDocument; |
| | | import org.apache.poi.xwpf.extractor.XWPFWordExtractor; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | |
| | |
| | | |
| | | @Override |
| | | public CommonPage selectCompanySummaryList(Integer companyId) { |
| | | boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId()); |
| | | if (!admin){ |
| | | if (!SecurityUtils.adminUser()){ |
| | | if (companyId==null){ |
| | | throw new ApiException("非管理员操作,查询条件不可为空"); |
| | | } |
| | |
| | | } |
| | | return CommonResult.failed(); |
| | | } |
| | | |
| | | @Override |
| | | public CommonResult uploadSummary(MultipartFile file,Integer companyId) { |
| | | |
| | | List<CompanySummary> companySummaries = companySummaryMapper.selectCompanySummaryList(Math.toIntExact(companyId == null ? SecurityUtils.getCompanyId() : companyId)); |
| | | if (companySummaries.size()>0){ |
| | | throw new ApiException("当前企业已有相关数据,请删除原有数据后重试"); |
| | | } |
| | | try { |
| | | String fileData = readWordToString(file); |
| | | CompanySummary companySummary = new CompanySummary(); |
| | | companySummary.setCompanySummary(fileData); |
| | | if (companyId==null){ |
| | | companySummary.setCompanyId(Math.toIntExact(SecurityUtils.getCompanyId())); |
| | | }else { |
| | | companySummary.setCompanyId(companyId); |
| | | } |
| | | companySummary.setCreateTime(LocalDateTime.now()); |
| | | companySummary.setCreateBy(SecurityUtils.getUsername()); |
| | | companySummaryMapper.insert(companySummary); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | return CommonResult.success(); |
| | | } |
| | | |
| | | /** |
| | | * 读取Word文档内容并转为字符串 |
| | | * @param file Word文件 |
| | | * @return 文档内容的字符串 |
| | | */ |
| | | public static String readWordToString(MultipartFile file) throws Exception { |
| | | try (InputStream inputStream = file.getInputStream(); |
| | | XWPFDocument document = new XWPFDocument(inputStream); |
| | | XWPFWordExtractor extractor = new XWPFWordExtractor(document)) { |
| | | return extractor.getText(); |
| | | } |
| | | } |
| | | } |