¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.personnel.service.impl; |
| | | |
| | | import cn.hutool.core.util.NumberUtil; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.config.Configure; |
| | | import com.deepoove.poi.data.Pictures; |
| | | import com.ruoyi.common.core.domain.entity.User; |
| | | import com.ruoyi.common.utils.DateImageUtil; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.framework.exception.ErrorException; |
| | | import com.ruoyi.inspect.util.HackLoopTableRenderPolicy; |
| | | import com.ruoyi.personnel.dto.PersonSupervisePlanDto; |
| | | import com.ruoyi.personnel.excel.PersonSupervisePlanDetailsListener; |
| | | import com.ruoyi.personnel.excel.PersonSupervisePlanDetailsUpload; |
| | | import com.ruoyi.personnel.mapper.PersonSupervisePlanDetailsMapper; |
| | | import com.ruoyi.personnel.mapper.PersonSupervisePlanMapper; |
| | | import com.ruoyi.personnel.mapper.PersonSupervisionRecordMapper; |
| | | import com.ruoyi.personnel.pojo.PersonSupervisePlan; |
| | | import com.ruoyi.personnel.pojo.PersonSupervisePlanDetails; |
| | | import com.ruoyi.personnel.pojo.PersonSupervisionRecord; |
| | | import com.ruoyi.personnel.service.PersonSupervisePlanDetailsService; |
| | | import com.ruoyi.personnel.service.PersonSupervisePlanService; |
| | | import com.ruoyi.system.mapper.UserMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.hwpf.HWPFDocument; |
| | | import org.apache.poi.hwpf.usermodel.*; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | import org.apache.poi.xwpf.usermodel.XWPFTable; |
| | | import org.apache.poi.xwpf.usermodel.XWPFTableCell; |
| | | import org.apache.poi.xwpf.usermodel.XWPFTableRow; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.net.URLEncoder; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * <p> |
| | | * çç£è®¡å - ç¶ æå¡å®ç°ç±» |
| | | * </p> |
| | | * |
| | | * @author è¯å¯¼è½¯ä»¶ï¼æ±èï¼æéå
¬å¸ |
| | | * @since 2024-10-09 04:14:45 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public class PersonSupervisePlanServiceImpl extends ServiceImpl<PersonSupervisePlanMapper, PersonSupervisePlan> implements PersonSupervisePlanService { |
| | | |
| | | @Resource |
| | | private PersonSupervisePlanDetailsService personSupervisePlanDetailsService; |
| | | |
| | | @Resource |
| | | private PersonSupervisePlanDetailsMapper personSupervisePlanDetailsMapper; |
| | | |
| | | @Resource |
| | | private PersonSupervisionRecordMapper personSupervisionRecordMapper; |
| | | |
| | | @Value("${file.path}") |
| | | private String imgUrl; |
| | | |
| | | @Autowired |
| | | private UserMapper userMapper; |
| | | |
| | | public IPage<PersonSupervisePlanDto> yearPlanDtoIPage(Page page, String organizationPerson, Integer departId) { |
| | | IPage<PersonSupervisePlanDto> personSupervisePlanDtoIPage = baseMapper.pageByPerson(page, organizationPerson, departId); |
| | | List<PersonSupervisePlanDto> records = personSupervisePlanDtoIPage.getRecords(); |
| | | for(PersonSupervisePlanDto a : records) { |
| | | a.setCurrentId(SecurityUtils.getUserId().intValue()); |
| | | } |
| | | return personSupervisePlanDtoIPage; |
| | | } |
| | | |
| | | @Override |
| | | public void yearPlanDetailImport(MultipartFile file ,String suffix) { |
| | | // 年度计åç¶çº§æ°å¢æ°æ® |
| | | PersonSupervisePlan personSupervisePlan = new PersonSupervisePlan(); |
| | | String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")); |
| | | personSupervisePlan.setFileName(fileName); |
| | | personSupervisePlan.setOrganizationPersonId(SecurityUtils.getUserId().intValue()); |
| | | personSupervisePlan.setOrganizationDate(LocalDateTime.now()); |
| | | baseMapper.insert(personSupervisePlan); |
| | | List<List<String>> tableData = new ArrayList<>(); |
| | | String name = ""; |
| | | // 年度计å详æ
æ°å¢ |
| | | try { |
| | | if(suffix.toLowerCase().contains("xls")) { |
| | | PersonSupervisePlanDetailsListener personSupervisePlanDetailsListener = new PersonSupervisePlanDetailsListener(personSupervisePlanDetailsService); |
| | | personSupervisePlanDetailsListener.setPlanId(personSupervisePlan.getId()); |
| | | EasyExcel.read(file.getInputStream(), PersonSupervisePlanDetailsUpload.class, personSupervisePlanDetailsListener).sheet().doRead(); |
| | | }else if(suffix.toLowerCase().equals("doc")) { |
| | | try(InputStream inputStream = file.getInputStream(); |
| | | HWPFDocument doc = new HWPFDocument(inputStream)) { |
| | | // è·åææ¡£ç Range |
| | | Range range = doc.getRange(); |
| | | // éåææè¡¨æ ¼ |
| | | TableIterator tableIterator = new TableIterator(range); |
| | | while (tableIterator.hasNext()) { |
| | | Table table = tableIterator.next(); |
| | | // éåè¡¨æ ¼çæ¯ä¸è¡ |
| | | for (int rowIdx = 0; rowIdx < table.numRows(); rowIdx++) { |
| | | TableRow row = table.getRow(rowIdx); |
| | | List<String> rowData = new ArrayList<>(); |
| | | |
| | | // éåè¡ä¸çæ¯ä¸ªåå
æ ¼ |
| | | for (int cellIdx = 0; cellIdx < row.numCells(); cellIdx++) { |
| | | TableCell cell = row.getCell(cellIdx); |
| | | String text = cell.text().trim(); |
| | | rowData.add(text); |
| | | } |
| | | tableData.add(rowData); |
| | | } |
| | | } |
| | | for (List<String> list : tableData) { |
| | | // è¿è¡æ·»å |
| | | addPersonSupervisePlanDetails(list,personSupervisePlan.getId()); |
| | | if(StringUtils.isEmpty(name) && NumberUtil.isInteger(list.get(0))) { |
| | | String supervisePerson = list.get(4); // 被çç£äººå |
| | | name = supervisePerson; |
| | | } |
| | | } |
| | | // æ ¹æ®æ·»å æ°æ®è¢«çç£äºº æ¥å¤æè¯¥æä»¶æ¯åªä¸ªé¨é¨ç |
| | | String departLimsId = userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getName, name)).get(0).getDepartLimsId(); |
| | | if(StringUtils.isNotEmpty(departLimsId) && departLimsId.contains("18")) { |
| | | baseMapper.update(null,new LambdaUpdateWrapper<PersonSupervisePlan>() |
| | | .set(PersonSupervisePlan::getDepartId,18) |
| | | .set(PersonSupervisePlan::getOrganizationPersonId,12) // éä¿¡çæ¯åæ¨ ï¼ç¼å¶äººï¼ |
| | | .eq(PersonSupervisePlan::getId,personSupervisePlan.getId())); |
| | | } else if (StringUtils.isNotEmpty(departLimsId) && departLimsId.contains("19")) { |
| | | baseMapper.update(null,new LambdaUpdateWrapper<PersonSupervisePlan>() |
| | | .set(PersonSupervisePlan::getDepartId,19) |
| | | .set(PersonSupervisePlan::getOrganizationPersonId,41) // çµåçæ¯å´æå ï¼ç¼å¶äººï¼ |
| | | .eq(PersonSupervisePlan::getId,personSupervisePlan.getId())); |
| | | } |
| | | |
| | | log.info("读åæä»¶ï¼ {}",tableData); |
| | | }catch (Exception e) { |
| | | throw new ErrorException("导å
¥å¤±è´¥"+ e); |
| | | } |
| | | }else if(suffix.toLowerCase().equals("docx")) { |
| | | try(InputStream fileInputStream = file.getInputStream();XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream)) { |
| | | |
| | | // éåææè¡¨æ ¼ |
| | | for(XWPFTable table : xwpfDocument.getTables()) { |
| | | // é忝ä¸è¡ |
| | | for(XWPFTableRow row : table.getRows()) { |
| | | List<String> list = new ArrayList<>(); |
| | | // é忝ä¸ä¸ªåå
æ ¼ |
| | | for(XWPFTableCell cell : row.getTableCells()) { |
| | | String text = cell.getText(); |
| | | list.add(text); |
| | | } |
| | | log.info("è¯»åæ¯ä¸è¡çå
å®¹ï¼ {}",list); |
| | | // è¿è¡æ·»å |
| | | addPersonSupervisePlanDetails(list,personSupervisePlan.getId()); |
| | | if(StringUtils.isEmpty(name) && NumberUtil.isInteger(list.get(0))) { |
| | | String supervisePerson = list.get(4); // 被çç£äººå |
| | | name = supervisePerson; |
| | | } |
| | | } |
| | | } |
| | | // æ ¹æ®æ·»å æ°æ®è¢«çç£äºº æ¥å¤æè¯¥æä»¶æ¯åªä¸ªé¨é¨ç |
| | | String departLimsId = userMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getName, name)).get(0).getDepartLimsId(); |
| | | if(StringUtils.isNotEmpty(departLimsId) && departLimsId.contains("18")) { |
| | | baseMapper.update(null,new LambdaUpdateWrapper<PersonSupervisePlan>() |
| | | .set(PersonSupervisePlan::getDepartId,18) |
| | | .eq(PersonSupervisePlan::getId,personSupervisePlan.getId())); |
| | | } else if (StringUtils.isNotEmpty(departLimsId) && departLimsId.contains("19")) { |
| | | baseMapper.update(null,new LambdaUpdateWrapper<PersonSupervisePlan>() |
| | | .set(PersonSupervisePlan::getDepartId,18) |
| | | .eq(PersonSupervisePlan::getId,personSupervisePlan.getId())); |
| | | } |
| | | }catch (Exception e){ |
| | | throw new ErrorException("导å
¥å¤±è´¥"+e); |
| | | } |
| | | } |
| | | |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public void addPersonSupervisePlanDetails(List<String> list,Integer planId) { |
| | | if(NumberUtil.isInteger(list.get(0)) ) { |
| | | String superviseDate = list.get(1); // çç£æ¥æ |
| | | String superviseDes = list.get(2); // çç£ç®ç |
| | | String superviseProject = list.get(3); // çç£é¡¹ç® |
| | | String supervisePerson = list.get(4); // 被çç£äººå |
| | | String superviseReason = list.get(5); // çç£åå |
| | | String remarks = list.get(6); // 夿³¨ |
| | | PersonSupervisePlanDetails personSupervisePlanDetails = new PersonSupervisePlanDetails(); |
| | | LocalDate localDate = null; |
| | | String[] parts = superviseDate.split("\\."); |
| | | if (parts.length == 2) { |
| | | try { |
| | | // è·å年份 |
| | | int year = Integer.parseInt(parts[0]); |
| | | // è·åæä»½ |
| | | int month = Integer.parseInt(parts[1]); |
| | | // å建 LocalDate å¯¹è±¡ï¼æ¥æè®¾ä¸ºè¯¥æç¬¬ä¸å¤© |
| | | localDate = LocalDate.of(year, month, 1); |
| | | } catch (NumberFormatException e) { |
| | | throw new ErrorException("è¾å
¥çåç¬¦ä¸²æ æ³è½¬æ¢ä¸ºææç年份ææä»½: " + e.getMessage()); |
| | | } |
| | | } |
| | | personSupervisePlanDetails.setSuperviseDate(localDate); |
| | | personSupervisePlanDetails.setSuperviseDes(superviseDes); |
| | | personSupervisePlanDetails.setSuperviseProject(superviseProject); |
| | | personSupervisePlanDetails.setSupervisePerson(supervisePerson); |
| | | personSupervisePlanDetails.setSuperviseReason(superviseReason); |
| | | personSupervisePlanDetails.setRemarks(remarks); |
| | | personSupervisePlanDetails.setPlanId(planId); // ç¶id |
| | | personSupervisePlanDetailsMapper.insert(personSupervisePlanDetails); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 导åºäººåçç£è®¡å |
| | | * @param id |
| | | * @param response |
| | | */ |
| | | @Override |
| | | public void exportSuperVisePlan(Integer id, HttpServletResponse response) { |
| | | Map<String, Object> data = new HashMap<>(); |
| | | data.put("year",""); |
| | | data.put("supervisor",""); |
| | | // æ¥è¯¢è¯¦æ
|
| | | PersonSupervisePlan personSupervisePlan = baseMapper.selectById(id); |
| | | |
| | | |
| | | //è·åæäº¤äººçç¾åå°å |
| | | String organizationUrl = userMapper.selectById(personSupervisePlan.getOrganizationPersonId()).getSignatureUrl(); |
| | | if (StringUtils.isBlank(organizationUrl)) { |
| | | throw new ErrorException("æ¾ä¸å°æ£éªäººçç¾å"); |
| | | } |
| | | |
| | | //è·åæ¹å人çç¾åå°å |
| | | String approvalUrl = null; |
| | | if (personSupervisePlan.getApprovalId() != null) { |
| | | approvalUrl = userMapper.selectById(personSupervisePlan.getApprovalId()).getSignatureUrl(); |
| | | if (StringUtils.isBlank(approvalUrl)) { |
| | | throw new ErrorException("æ¾ä¸å°æ¹å人çç¾å"); |
| | | } |
| | | } |
| | | |
| | | // æ¥è¯¢è¯¦æ
|
| | | List<PersonSupervisePlanDetails> detailedDtos = personSupervisePlanDetailsService.list(Wrappers.<PersonSupervisePlanDetails>lambdaQuery() |
| | | .eq(PersonSupervisePlanDetails::getPlanId, id)); |
| | | |
| | | |
| | | // æ¥è¯¢çç£è®°å½ |
| | | List<PersonSupervisionRecord> personSupervisionRecords = personSupervisionRecordMapper.selectList(new LambdaQueryWrapper<PersonSupervisionRecord>() |
| | | .orderByAsc(PersonSupervisionRecord::getId)); |
| | | if(CollectionUtils.isNotEmpty(personSupervisionRecords)) { |
| | | Integer supervisorId = personSupervisionRecords.get(0).getSupervisorId(); |
| | | String name = userMapper.selectById(supervisorId).getName(); |
| | | data.put("supervisor",name); |
| | | } |
| | | |
| | | |
| | | int index = 1; |
| | | for (PersonSupervisePlanDetails detailedDto : detailedDtos) { |
| | | detailedDto.setIndex(index); |
| | | index++; |
| | | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| | | String format = detailedDto.getSuperviseDate().format(dateTimeFormatter); |
| | | detailedDto.setSuperviseDateString(format); |
| | | int year = detailedDto.getSuperviseDate().getYear(); |
| | | data.put("year",year); |
| | | } |
| | | System.out.println("data: " + data); |
| | | // è·åè·¯å¾ |
| | | InputStream inputStream = this.getClass().getResourceAsStream("/static/super-vise-plan.docx"); |
| | | Configure configure = Configure.builder() |
| | | .bind("supervisePlanDetailsList", new HackLoopTableRenderPolicy()) |
| | | .build(); |
| | | String finalApprovalUrl = approvalUrl; |
| | | XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render( |
| | | new HashMap<String, Object>() {{ |
| | | put("data",data); |
| | | put("supervisePlanDetailsList", detailedDtos); |
| | | put("organizationUrl", StringUtils.isNotBlank(organizationUrl) ? Pictures.ofLocal(imgUrl + "/" + organizationUrl).create() : null); |
| | | put("approvalUrl", StringUtils.isNotBlank(finalApprovalUrl) ? Pictures.ofLocal(imgUrl + "/" + finalApprovalUrl).create() : null); |
| | | put("writeDateUrl", personSupervisePlan.getOrganizationDate() != null ? |
| | | Pictures.ofStream(DateImageUtil.createDateImage(personSupervisePlan.getOrganizationDate())).create() : null); |
| | | put("ratifyDateUrl", personSupervisePlan.getApprovalDate() != null ? |
| | | Pictures.ofStream(DateImageUtil.createDateImage(personSupervisePlan.getApprovalDate())).create() : null); |
| | | }}); |
| | | try { |
| | | response.setContentType("application/msword"); |
| | | String fileName = URLEncoder.encode( |
| | | "人åçç£è®¡å导åº", "UTF-8"); |
| | | response.setHeader("Content-disposition", |
| | | "attachment;filename=" + fileName + ".docx"); |
| | | OutputStream os = response.getOutputStream(); |
| | | template.write(os); |
| | | os.flush(); |
| | | os.close(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("导åºå¤±è´¥"); |
| | | } |
| | | } |
| | | } |