| | |
| | | package com.gkhy.exam.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.gkhy.exam.common.api.CommonPage; |
| | | import com.gkhy.exam.common.api.CommonResult; |
| | |
| | | import com.gkhy.exam.common.exception.ApiException; |
| | | import com.gkhy.exam.common.utils.PageUtils; |
| | | import com.gkhy.exam.common.utils.SecurityUtils; |
| | | import com.gkhy.exam.common.utils.StringUtils; |
| | | import com.gkhy.exam.system.domain.CompanyBasic; |
| | | import com.gkhy.exam.system.domain.ExQuestion; |
| | | import com.gkhy.exam.system.domain.SysCompany; |
| | | import com.gkhy.exam.system.mapper.CompanyBasicMapper; |
| | | import com.gkhy.exam.system.service.CompanyBasicService; |
| | | import com.gkhy.exam.system.service.SysCompanyService; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | |
| | | } |
| | | return CommonResult.failed(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public CommonResult uploadBasic(MultipartFile file) { |
| | | Workbook workbook = null; |
| | | List<CompanyBasic> companyBasicslist = new ArrayList<>(); |
| | | StringBuffer stringBuffer = new StringBuffer(); |
| | | Integer t=0; |
| | | try { |
| | | workbook = WorkbookFactory.create(file.getInputStream()); |
| | | Sheet sheetAt = workbook.getSheetAt(0); |
| | | LoginUserDetails loginUser = SecurityUtils.getLoginUser(); |
| | | for (int i = 1; i <= sheetAt.getLastRowNum(); i++) { |
| | | Row row = sheetAt.getRow(i); |
| | | String cellValueAsString = getCellValueAsString(row.getCell(1)); |
| | | if (StringUtils.isEmpty(cellValueAsString)){ |
| | | continue; |
| | | } |
| | | CompanyBasic companyBasic = new CompanyBasic(); |
| | | if (row!=null ){ |
| | | |
| | | companyBasic.setBasic(getCellValueAsString(row.getCell(1))); |
| | | companyBasic.setIntroduce(getCellValueAsString(row.getCell(2))); |
| | | companyBasic.setTarget(getCellValueAsString(row.getCell(3))); |
| | | companyBasic.setQuality(getCellValueAsString(row.getCell(4))); |
| | | companyBasic.setActivity(getCellValueAsString(row.getCell(5))); |
| | | companyBasic.setAudit(getCellValueAsString(row.getCell(6))); |
| | | companyBasic.setEpiboly(getCellValueAsString(row.getCell(7))); |
| | | companyBasic.setResource(getCellValueAsString(row.getCell(8))); |
| | | companyBasic.setCreateTime(LocalDateTime.now()); |
| | | companyBasic.setCreateBy(SecurityUtils.getUsername()); |
| | | SysCompany sysCompany = sysCompanyService.selectCompanyByName(getCellValueAsString(row.getCell(0))); |
| | | companyBasic.setCompanyName(sysCompany.getName()); |
| | | companyBasic.setCompanyId(Math.toIntExact(sysCompany.getId())); |
| | | |
| | | } |
| | | companyBasicslist.add(companyBasic); |
| | | } |
| | | for (CompanyBasic companyBasic : companyBasicslist) { |
| | | List<CompanyBasic> companyBasics = companyBasicMapper.selectCompanyBasicList(companyBasic.getCompanyId()); |
| | | if (companyBasics.size()>0){ |
| | | stringBuffer.append(companyBasic.getCompanyName()).append(","); |
| | | }else { |
| | | if (!SecurityUtils.adminUser()){ |
| | | if (companyBasic.getCompanyId() != null && SecurityUtils.getCompanyId() == companyBasic.getCompanyId().longValue()) { |
| | | t+=companyBasicMapper.insert(companyBasic); |
| | | } else { |
| | | stringBuffer.append(companyBasic.getCompanyName()).append(","); |
| | | } |
| | | }else { |
| | | t+=companyBasicMapper.insert(companyBasic); |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (t<1){ |
| | | throw new ApiException("导入公司基本信息失败"); |
| | | } |
| | | workbook.close(); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | if (stringBuffer.length()>0){ |
| | | return CommonResult.failed("只能导入本企业数据或该企业已有数据 ["+stringBuffer.toString()+"]"); |
| | | } |
| | | return CommonResult.success(); |
| | | } |
| | | |
| | | /** |
| | | * 安全获取单元格值 |
| | | */ |
| | | private String getCellValueAsString(Cell cell) { |
| | | if (cell == null) return ""; |
| | | |
| | | switch (cell.getCellType()) { |
| | | case STRING: |
| | | return cell.getStringCellValue().trim(); |
| | | case NUMERIC: |
| | | if (DateUtil.isCellDateFormatted(cell)) { |
| | | return cell.getDateCellValue().toString(); |
| | | } else { |
| | | return String.valueOf((long) cell.getNumericCellValue()); |
| | | } |
| | | case BOOLEAN: |
| | | return String.valueOf(cell.getBooleanCellValue()); |
| | | case FORMULA: |
| | | try { |
| | | return cell.getStringCellValue(); |
| | | } catch (IllegalStateException e) { |
| | | return String.valueOf(cell.getNumericCellValue()); |
| | | } |
| | | default: |
| | | return ""; |
| | | } |
| | | } |
| | | |
| | | } |