diff --git a/bonus-common-biz/src/main/java/com/bonus/common/biz/domain/lease/LeaseApplyInfo.java b/bonus-common-biz/src/main/java/com/bonus/common/biz/domain/lease/LeaseApplyInfo.java index c9eb78f1..13d06ba6 100644 --- a/bonus-common-biz/src/main/java/com/bonus/common/biz/domain/lease/LeaseApplyInfo.java +++ b/bonus-common-biz/src/main/java/com/bonus/common/biz/domain/lease/LeaseApplyInfo.java @@ -263,4 +263,6 @@ public class LeaseApplyInfo extends BaseEntity{ @ApiModelProperty(value = "供应地点") private String supplierPlace; + @ApiModelProperty(value = "公司名称") + private String companyName; } diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseTaskController.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseTaskController.java index b9512a4e..02ec1b23 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseTaskController.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/controller/LeaseTaskController.java @@ -11,22 +11,44 @@ import com.bonus.common.log.annotation.SysLog; import com.bonus.common.log.enums.OperaType; import com.bonus.common.security.annotation.RequiresPermissions; import com.bonus.material.common.annotation.PreventRepeatSubmit; +import com.bonus.material.lease.domain.LeaseApplyDetails; import com.bonus.material.lease.domain.vo.LeaseApplyRequestVo; import com.bonus.material.lease.service.ILeaseApplyInfoService; import com.bonus.material.lease.service.ILeaseTaskService; import com.bonus.material.ma.domain.Type; import com.bonus.material.ma.domain.vo.MaTypeVo; import com.bonus.material.ma.domain.vo.StandardConfigDetailsVo; +import com.bonus.system.api.RemoteFileService; +import com.itextpdf.text.Paragraph; + +import feign.Param; +import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; +import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.BooleanUtils; +import org.apache.poi.xwpf.usermodel.*; +import org.springframework.http.MediaType; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; - +import org.springframework.web.multipart.MultipartFile; +import org.springframework.mock.web.MockMultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.constraints.NotNull; -import java.util.ArrayList; +import java.awt.*; +import java.io.*; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.net.URL; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; import java.util.List; +import java.awt.Font; /** * @author hay @@ -41,6 +63,9 @@ public class LeaseTaskController extends BaseController { @Resource private ILeaseTaskService service; + @Resource + private RemoteFileService sysFileService; + /** * 新增领用任务 */ @@ -195,4 +220,228 @@ public class LeaseTaskController extends BaseController { List listByMaType = service.getEquipmentThreeTypes(); return success(listByMaType); } + + + /** + * 业务联系单pdf + */ + @ApiOperation(value = "业务联系单pdf") + @GetMapping("/getCodePDF/{taskId}") + public AjaxResult getCodePDF(@PathVariable("taskId") Long taskId, Map replacements) throws IOException, ParseException { + InputStream in = getClass().getClassLoader().getResourceAsStream("template/yewu.docx"); + XWPFDocument document = new XWPFDocument(in); + + LeaseApplyInfo info = service.getApplyInfo(taskId); + + //订单日期 + replacements.put("${createTime}", String.valueOf(info.getCreateTime())); + + //合同编号 + replacements.put("${createBy}", String.valueOf(info.getCreateBy())); + + replacements.put("${unitName}", String.valueOf(info.getUnitName())); + +// replacements.put("${leasePerson}", String.valueOf(info.getLeasePerson())); + + replacements.put("${companyName}", String.valueOf(info.getCompanyName())); + + replacements.put("${projectName}", String.valueOf(info.getProjectName())); + + replacements.put("${phone}", String.valueOf(info.getPhone())); + + replacements.put("${code}", String.valueOf(info.getCode())); + + // 假设 info.getCreateTime() 返回的是 String 类型的日期时间 + SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); + SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date date1 = inputFormat.parse(String.valueOf(info.getCreateTime())); + String startTime = outputFormat.format(date1); + // 将 startTime 字符串转换为 LocalDate 对象 + LocalDate localDate = LocalDate.parse(startTime); + // 提取年、月、日 + int year = localDate.getYear(); + int month = localDate.getMonthValue(); + int day = localDate.getDayOfMonth(); + + replacements.put("${year}", String.valueOf(year)); + + replacements.put("${month}", String.valueOf(month)); + + replacements.put("${day}", String.valueOf(day)); + + //填充订单详情到表格 + fillOrderTable(document, info); + + //获取附件详情 + List details = service.getApplyDetails(taskId); + + + //填充订单详情到表格 + fillOrderTableTwo(document, details); + + // 将拼接的字符串放入替换的map中 +// replacements.put("${orderTable}", orderDetail.toString()); + + for (IBodyElement element : document.getBodyElements()) { + if (element instanceof XWPFParagraph) { + XWPFParagraph paragraph = (XWPFParagraph) element; + for (XWPFRun run : paragraph.getRuns()) { + String text = run.getText(0); + if (text != null) { + for (Map.Entry entry : replacements.entrySet()) { + String entryValue = entry.getValue(); + if (entryValue == null) {continue;} + text = text.replace(entry.getKey(), entryValue); + } + run.setText(text, 0); + } + } + } + } + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + document.write(outputStream); + byte[] wordBytes = outputStream.toByteArray(); +// // 将 DOCX 转换为 PDF +// ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream(); +// try (XWPFDocument docxDocument = new XWPFDocument(new java.io.ByteArrayInputStream(wordBytes))) { +// PdfOptions options = PdfOptions.create(); +// PdfConverter.getInstance().convert(docxDocument, pdfOutputStream, options); +// } catch (IOException e) { +// e.printStackTrace(); +// // 处理异常 +// } +// byte[] pdfBytes = pdfOutputStream.toByteArray(); + + MultipartFile file = new MockMultipartFile("contract", "contract.docx", MediaType.APPLICATION_OCTET_STREAM_VALUE, wordBytes); +// MultipartFile file = new MockMultipartFile("contract", "contract.pdf", MediaType.APPLICATION_PDF_VALUE, pdfBytes); + + + return sysFileService.upload(file); + } + + private static void fillOrderTable(XWPFDocument document, LeaseApplyInfo info) { + XWPFTable table = null; + int headerRowIndex = 0; // 表头行索引 + int footerRowIndex = 0; // 合计行索引 + int cellCount = 0; + try { + // 获取第一个表格(假设模板中只有一个表格) + table = document.getTables().get(0); + Map replacements = new HashMap<>(); + // 假设 info.getCreateTime() 返回的是 String 类型的日期时间 + SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); + SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date date1 = inputFormat.parse(String.valueOf(info.getCreateTime())); + String startTime = outputFormat.format(date1); + // 将 startTime 字符串转换为 LocalDate 对象 + LocalDate localDate = LocalDate.parse(startTime); + // 提取年、月、日 + int year = localDate.getYear(); + int month = localDate.getMonthValue(); + int day = localDate.getDayOfMonth(); + + replacements.put("${year}", String.valueOf(year)); + + replacements.put("${month}", String.valueOf(month)); + + replacements.put("${day}", String.valueOf(day)); + replacements.put("${companyName}", String.valueOf(info.getCompanyName())); + replacements.put("${leasePerson}", String.valueOf(info.getLeasePerson())); + replacements.put("${unitName}", String.valueOf(info.getUnitName())); + replacements.put("${projectName}", String.valueOf(info.getProjectName())); + replacements.put("${phone}", String.valueOf(info.getPhone())); + replacements.put("${code}", String.valueOf(info.getCode())); + replacements.put("${createTime}", String.valueOf(info.getCreateTime())); + replacements.put("${createBy}", String.valueOf(info.getCreateBy())); + + // 遍历表格的每一行 + for (int i = 0; i < table.getNumberOfRows(); i++) { + XWPFTableRow row = table.getRow(i); + // 遍历行中的每个单元格 + for (int j = 0; j < row.getTableCells().size(); j++) { + XWPFTableCell cell = row.getCell(j); + // 遍历单元格中的每个段落 + for (XWPFParagraph paragraph : cell.getParagraphs()) { + // 遍历段落中的每个运行对象 + for (XWPFRun run : paragraph.getRuns()) { + String text = run.getText(0); + if (text != null) { + // 遍历替换映射 + for (Map.Entry entry : replacements.entrySet()) { + String placeholder = entry.getKey(); + String value = entry.getValue(); + if (text.contains(placeholder)) { + // 替换占位符为实际值 + text = text.replace(placeholder, value); + run.setText(text, 0); + } + } + } + } + } + } + } + + } catch (IndexOutOfBoundsException e) { + throw new RuntimeException("模板表处理异常,请检查合同信息"); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } + + + private static void fillOrderTableTwo(XWPFDocument document, List orderDetails) { + XWPFTable table = null; + int headerRowIndex = 0; // 表头行索引 + int footerRowIndex = 0; // 合计行索引 + int cellCount = 0; + try { + // 获取第一个表格(假设模板中只有一个表格) + table = document.getTables().get(1); + + // 保留第一行(表头)和最后一行(合计行),仅在中间插入数据行 + headerRowIndex = 0; + footerRowIndex = table.getRows().size(); + + // 删除中间的所有行(如果有的话) + for (int i = table.getRows().size() - 2; i > headerRowIndex; i--) { + table.removeRow(i); + } + // 获取表头行的单元格数量 + cellCount = table.getRow(headerRowIndex).getTableCells().size(); + + // 遍历订单详情并动态插入数据行 + SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + } catch (IndexOutOfBoundsException e) { + throw new RuntimeException("模板表处理异常,请检查合同信息"); + } + int num = orderDetails.size(); + for (LeaseApplyDetails detail : orderDetails) { + XWPFTableRow newRow = table.insertNewTableRow(headerRowIndex + 1); + + // 确保新行有足够数量的单元格 + while (newRow.getTableCells().size() < cellCount) { + newRow.createCell(); + } + int cellIndex = 0; + // 序号 + newRow.getCell(cellIndex++).setText(String.valueOf(num)); + num--; + + // 机具名称 + newRow.getCell(cellIndex++).setText(detail.getMaTypeName()); + String typeName = detail.getTypeName(); + // 规格型号 + newRow.getCell(cellIndex++).setText(typeName); + // 单位 + newRow.getCell(cellIndex++).setText(detail.getUnitName()); + // 数量 + newRow.getCell(cellIndex++).setText(String.valueOf(detail.getPreNum())); + // 备注 + newRow.getCell(cellIndex++).setText(detail.getRemark() ); + + + } + } } diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseDeptInfo.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseDeptInfo.java index a4a3e3c6..f68928e6 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseDeptInfo.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/domain/vo/LeaseDeptInfo.java @@ -18,7 +18,7 @@ public class LeaseDeptInfo extends BaseEntity { private static final long serialVersionUID = 1L; /** 部门 */ - private Long dept_id; + private Long deptId; /** 父级id */ @ApiModelProperty(value = "父级id") diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/mapper/LeaseTaskMapper.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/mapper/LeaseTaskMapper.java index 44e6d499..190355b6 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/mapper/LeaseTaskMapper.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/mapper/LeaseTaskMapper.java @@ -94,4 +94,8 @@ public interface LeaseTaskMapper { List getListByTypeName(@Param("typeId") Long typeId); void updateLeaseTaskStatus(@Param("taskId") Long taskId, @Param("taskStatus") int taskStatus); + + LeaseApplyInfo getApplyInfo(Long taskId); + + List getApplyDetails(Long taskId); } diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/ILeaseTaskService.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/ILeaseTaskService.java index b76d4223..61b28d70 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/ILeaseTaskService.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/ILeaseTaskService.java @@ -3,6 +3,7 @@ package com.bonus.material.lease.service; import com.bonus.common.biz.domain.lease.LeaseApplyInfo; import com.bonus.common.core.web.domain.AjaxResult; +import com.bonus.material.lease.domain.LeaseApplyDetails; import com.bonus.material.lease.domain.vo.LeaseApplyRequestVo; import com.bonus.material.lease.domain.vo.LeaseDeptInfo; import com.bonus.material.ma.domain.Type; @@ -87,5 +88,11 @@ public interface ILeaseTaskService { List getEquipmentThreeTypes(); List getListByParentId(Long typeId); + + + LeaseApplyInfo getApplyInfo(Long taskId); + + + List getApplyDetails(Long taskId); } diff --git a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseTaskServiceImpl.java b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseTaskServiceImpl.java index 0a90c265..1bb03d2e 100644 --- a/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseTaskServiceImpl.java +++ b/bonus-modules/bonus-material/src/main/java/com/bonus/material/lease/service/impl/LeaseTaskServiceImpl.java @@ -88,6 +88,27 @@ public class LeaseTaskServiceImpl implements ILeaseTaskService { } leaseApplyRequestVo.getLeaseApplyInfo().setCreateTime(DateUtils.getNowDate()); leaseApplyRequestVo.getLeaseApplyInfo().setCreateBy(SecurityUtils.getUsername()); + Long userId = SecurityUtils.getUserId(); + Long deptId = mapper.getDeptIdByUserId(userId); + LeaseDeptInfo leaseDeptInfo = new LeaseDeptInfo(); + leaseDeptInfo = mapper.getDeptIdByParentId(deptId); + Long parentIdTemp = leaseDeptInfo.getParentId(); + String deptName = ""; + while (leaseDeptInfo != null) { + parentIdTemp = leaseDeptInfo.getParentId(); + String ancestors = leaseDeptInfo.getAncestors(); + if (ancestors != null && StringUtils.countMatches(ancestors, ",") < 1) { + break; + } + if (ancestors != null && StringUtils.countMatches(ancestors, ",") == 1) { + deptName = leaseDeptInfo.getDeptName(); + break; + } + leaseDeptInfo = mapper.getDeptIdByParentId(parentIdTemp); + } + if(!deptName.equals("")){ + leaseApplyRequestVo.getLeaseApplyInfo().setCompanyId(leaseDeptInfo.getDeptId()); + } try { Integer statusFlag = leaseApplyRequestVo.getStatusFlag(); // 根据状态标志选择合适的状态 @@ -479,4 +500,43 @@ public class LeaseTaskServiceImpl implements ILeaseTaskService { public List getListByParentId(Long typeId) { return mapper.getListByTypeName(typeId); } + + + /** + * 根据id查询领用任务信息 + * + * @param taskId + * @return 领用任务 + */ + @Override + public LeaseApplyInfo getApplyInfo(Long taskId) { + try { + LeaseApplyInfo leaseApplyInfo = new LeaseApplyInfo(); + leaseApplyInfo = mapper.getApplyInfo(taskId); + return leaseApplyInfo; + } catch (Exception e) { + // 记录异常日志 + System.err.println("查看失败" + e.getMessage()); + throw new RuntimeException("失败", e); + } + } + + /** + * 获取详情 + * + * @param taskId + * @return 领用任务 + */ + @Override + public List getApplyDetails(Long taskId) { + try { + + List list = mapper.getApplyDetails(taskId); + return list; + } catch (Exception e) { + // 记录异常日志 + System.err.println("查看失败" + e.getMessage()); + throw new RuntimeException("失败", e); + } + } } diff --git a/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseTaskMapper.xml b/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseTaskMapper.xml index 0a07b268..98eb19a5 100644 --- a/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseTaskMapper.xml +++ b/bonus-modules/bonus-material/src/main/resources/mapper/material/lease/LeaseTaskMapper.xml @@ -640,4 +640,29 @@ where task_Id = #{taskId} + + + + diff --git a/bonus-modules/bonus-material/src/main/resources/template/yewu.docx b/bonus-modules/bonus-material/src/main/resources/template/yewu.docx new file mode 100644 index 00000000..33225d29 Binary files /dev/null and b/bonus-modules/bonus-material/src/main/resources/template/yewu.docx differ diff --git a/pom.xml b/pom.xml index 2e7eb03b..f66e3df2 100644 --- a/pom.xml +++ b/pom.xml @@ -240,6 +240,7 @@ bonus-api-system ${bonus.version} + @@ -255,6 +256,32 @@ org.springframework.cloud spring-cloud-starter-bootstrap + + org.springframework + spring-test + 5.3.22 + + + + org.apache.poi + poi-ooxml + 5.2.3 + + + org.apache.poi + poi-ooxml-schemas + 4.1.2 + + + org.apache.poi + poi-scratchpad + 5.2.3 + + + fr.opensagres.xdocreport + fr.opensagres.poi.xwpf.converter.pdf + 2.0.2 +