PageRenderTime 43ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/wellpt-basicdata/src/main/java/com/wellsoft/pt/basicdata/printtemplate/service/impl/PrintTemplateServiceImpl.java

https://github.com/silentjesse/code_java_wellpt
Java | 1672 lines | 1049 code | 122 blank | 501 comment | 159 complexity | b5d2adb6047e536f733a27112631560e MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, AGPL-3.0
  1. package com.wellsoft.pt.basicdata.printtemplate.service.impl;
  2. import java.beans.PropertyDescriptor;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.lang.annotation.Annotation;
  10. import java.text.SimpleDateFormat;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Calendar;
  14. import java.util.Collection;
  15. import java.util.Date;
  16. import java.util.HashMap;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import javax.persistence.Table;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.commons.io.IOUtils;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.apache.poi.hwpf.HWPFDocument;
  27. import org.apache.poi.hwpf.model.FieldsDocumentPart;
  28. import org.apache.poi.hwpf.usermodel.Field;
  29. import org.apache.poi.hwpf.usermodel.Fields;
  30. import org.apache.poi.hwpf.usermodel.Range;
  31. import org.springframework.beans.BeanWrapperImpl;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.security.acls.domain.BasePermission;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import com.jacob.activeX.ActiveXComponent;
  37. import com.jacob.com.ComThread;
  38. import com.jacob.com.Dispatch;
  39. import com.jacob.com.Variant;
  40. import com.wellsoft.pt.basicdata.printtemplate.bean.PrintTemplateBean;
  41. import com.wellsoft.pt.basicdata.printtemplate.dao.PrintRecordDao;
  42. import com.wellsoft.pt.basicdata.printtemplate.dao.PrintTemplateDao;
  43. import com.wellsoft.pt.basicdata.printtemplate.entity.PrintRecord;
  44. import com.wellsoft.pt.basicdata.printtemplate.entity.PrintTemplate;
  45. import com.wellsoft.pt.basicdata.printtemplate.service.PrintTemplateService;
  46. import com.wellsoft.pt.common.component.jqgrid.JqGridQueryData;
  47. import com.wellsoft.pt.common.component.jqgrid.JqGridQueryInfo;
  48. import com.wellsoft.pt.common.enums.IdPrefix;
  49. import com.wellsoft.pt.common.enums.Separator;
  50. import com.wellsoft.pt.common.utils.DateUtil;
  51. import com.wellsoft.pt.core.dao.Page;
  52. import com.wellsoft.pt.core.entity.IdEntity;
  53. import com.wellsoft.pt.core.resource.Config;
  54. import com.wellsoft.pt.core.template.TemplateEngine;
  55. import com.wellsoft.pt.core.template.TemplateEngineFactory;
  56. import com.wellsoft.pt.org.entity.Department;
  57. import com.wellsoft.pt.org.entity.User;
  58. import com.wellsoft.pt.org.facade.OrgApiFacade;
  59. import com.wellsoft.pt.repository.entity.mongo.file.MongoFileEntity;
  60. import com.wellsoft.pt.repository.service.MongoFileService;
  61. import com.wellsoft.pt.security.acl.entity.AclSid;
  62. import com.wellsoft.pt.security.acl.service.AclService;
  63. import com.wellsoft.pt.security.core.userdetails.UserDetails;
  64. import com.wellsoft.pt.utils.bean.BeanUtils;
  65. import com.wellsoft.pt.utils.security.SpringSecurityUtils;
  66. import freemarker.template.Configuration;
  67. /**
  68. *
  69. * Description: 打印模板实现类
  70. *
  71. * @author zhouyq
  72. * @date 2013-3-21
  73. * @version 1.0
  74. *
  75. * <pre>
  76. * 修改记录:
  77. * 修改后版本 修改人 修改日期 修改内容
  78. * 2013-3-21.1 zhouyq 2013-3-21 Create
  79. * </pre>
  80. *
  81. */
  82. @Service
  83. @Transactional
  84. public class PrintTemplateServiceImpl implements PrintTemplateService {
  85. @Autowired
  86. private PrintTemplateDao printTemplateDao;
  87. @Autowired
  88. private PrintRecordDao printRecordDao;
  89. @Autowired
  90. private AclService aclService;
  91. @Autowired
  92. private OrgApiFacade orgApiFacade;
  93. @Autowired
  94. private MongoFileService mongoFileService;
  95. private Configuration configuration = null;
  96. private Dispatch wordObject;
  97. /**
  98. * 通过uuid查找打印模板
  99. * @see com.wellsoft.pt.message.service.PrintTemplateService#getById(java.lang.String)
  100. */
  101. @Override
  102. public PrintTemplate getByUuid(String uuid) {
  103. return printTemplateDao.get(uuid);
  104. }
  105. /**
  106. * 根据uuid查找对应的打印模板bean
  107. * @see com.wellsoft.pt.message.service.PrintTemplateService#getBeanById(java.lang.String)
  108. */
  109. @Override
  110. public PrintTemplateBean getBeanByUuid(String uuid) {
  111. PrintTemplate printTemplate = this.printTemplateDao.get(uuid);
  112. PrintTemplateBean bean = new PrintTemplateBean();
  113. BeanUtils.copyProperties(printTemplate, bean);
  114. //设置打印模板使用人
  115. List<AclSid> aclSids = aclService.getSid(printTemplate);
  116. List<String> sids = new ArrayList<String>();
  117. for (AclSid sid : aclSids) {
  118. if (ACL_SID.equals(sid.getSid())) {
  119. continue;
  120. }
  121. sids.add(sid.getSid());
  122. }
  123. StringBuilder ownerIds = new StringBuilder();
  124. StringBuilder ownerNames = new StringBuilder();
  125. Iterator<String> it = sids.iterator();
  126. while (it.hasNext()) {
  127. String sid = it.next();
  128. if (sid.startsWith(IdPrefix.USER.getValue())) {
  129. User user = orgApiFacade.getUserById(sid);
  130. ownerIds.append(user.getId());
  131. ownerNames.append(user.getUserName());
  132. } else if (sid.startsWith(IdPrefix.ROLE.getValue())) {
  133. sid = sid.substring(IdPrefix.ROLE.getName().length() + 1);
  134. Department department = orgApiFacade.getDepartmentById(sid);
  135. ownerIds.append(department.getId());
  136. ownerNames.append(department.getName());
  137. }
  138. if (it.hasNext()) {
  139. ownerIds.append(Separator.SEMICOLON.getValue());
  140. ownerNames.append(Separator.SEMICOLON.getValue());
  141. }
  142. }
  143. bean.setOwnerIds(ownerIds.toString());
  144. bean.setOwnerNames(ownerNames.toString());
  145. return bean;
  146. }
  147. /**
  148. * 保存打印模板bean
  149. * @throws IOException
  150. * @see com.wellsoft.pt.message.service.PrintTemplateService#saveBean(com.wellsoft.pt.message.bean.PrintTemplateBean)
  151. */
  152. @Override
  153. public PrintTemplate saveBean(PrintTemplateBean bean) throws IOException {
  154. PrintTemplate printTemplate = new PrintTemplate();
  155. // 保存新printTemplate 设置id值
  156. if (StringUtils.isBlank(bean.getUuid())) {
  157. bean.setUuid(null);
  158. } else {
  159. printTemplate = this.printTemplateDao.get(bean.getUuid());
  160. }
  161. BeanUtils.copyProperties(bean, printTemplate);
  162. //设置所有者
  163. if (StringUtils.isNotBlank(bean.getOwnerIds())) {
  164. String[] ownerIds = StringUtils.split(bean.getOwnerIds(), Separator.SEMICOLON.getValue());
  165. printTemplate.setOwners(Arrays.asList(ownerIds));
  166. }
  167. this.printTemplateDao.save(printTemplate);
  168. this.saveAcl(printTemplate);
  169. return printTemplate;
  170. }
  171. /**
  172. * 删除打印模板
  173. * @see com.wellsoft.pt.message.service.PrintTemplateService#remove(java.lang.String)
  174. */
  175. @Override
  176. public void remove(String uuid) {
  177. this.printTemplateDao.delete(uuid);
  178. }
  179. /**
  180. * (non-Javadoc)
  181. * @see com.wellsoft.pt.message.service.PrintTemplateService#query(com.wellsoft.pt.common.component.jqgrid.JqGridQueryInfo)
  182. */
  183. @Override
  184. public JqGridQueryData query(JqGridQueryInfo queryInfo) {
  185. Page<PrintTemplate> pageData = new Page<PrintTemplate>();
  186. pageData.setPageNo(queryInfo.getPage());
  187. pageData.setPageSize(queryInfo.getRows());
  188. this.printTemplateDao.findPage(pageData);
  189. List<PrintTemplate> printTemplates = pageData.getResult();
  190. List<PrintTemplate> jqUsers = new ArrayList<PrintTemplate>();
  191. for (PrintTemplate printTemplate : printTemplates) {
  192. PrintTemplate jqPrintTemplate = new PrintTemplate();
  193. BeanUtils.copyProperties(printTemplate, jqPrintTemplate);
  194. jqUsers.add(jqPrintTemplate);
  195. }
  196. JqGridQueryData queryData = new JqGridQueryData();
  197. queryData.setCurrentPage(queryInfo.getPage());
  198. queryData.setDataList(jqUsers);
  199. queryData.setRepeatitems(false);
  200. queryData.setTotalPages(pageData.getTotalPages());
  201. queryData.setTotalRows(pageData.getTotalCount());
  202. return queryData;
  203. }
  204. @Override
  205. public PrintTemplate saveAcl(PrintTemplate printTemplate) {
  206. List<AclSid> aclSids = aclService.getSid(printTemplate);
  207. List<String> existSids = new ArrayList<String>();
  208. for (AclSid aclSid : aclSids) {
  209. existSids.add(aclSid.getSid());
  210. }
  211. List<String> sids = getAclSid(printTemplate);
  212. //新的SID
  213. List<String> newSids = new ArrayList<String>();
  214. for (String newSid : sids) {
  215. if (!existSids.contains(newSid)) {
  216. newSids.add(newSid);
  217. }
  218. }
  219. //要删除的SID
  220. List<String> delSids = new ArrayList<String>();
  221. for (String newSid : existSids) {
  222. if (!sids.contains(newSid)) {
  223. delSids.add(newSid);
  224. }
  225. }
  226. //删除
  227. for (String sid : delSids) {
  228. aclService.removePermission(printTemplate, BasePermission.ADMINISTRATION, sid);
  229. }
  230. /*// 新增
  231. if (printTemplate.getParent() != null) {
  232. aclService.save(printTemplate, printTemplate.getParent(), sids.get(0), BasePermission.ADMINISTRATION);
  233. }*/
  234. for (String sid : sids) {
  235. aclService.addPermission(printTemplate, BasePermission.ADMINISTRATION, sid);
  236. }
  237. return aclService.get(PrintTemplate.class, printTemplate.getUuid(), BasePermission.ADMINISTRATION);
  238. }
  239. /**
  240. * 返回打印模板使用者在ACL中的SID
  241. *
  242. * @param printTemplate
  243. *
  244. * @return
  245. */
  246. private List<String> getAclSid(PrintTemplate printTemplate) {
  247. List<String> newOwners = new ArrayList<String>();
  248. if (printTemplate.getOwners().isEmpty()) {
  249. printTemplate.getOwners().add(ACL_SID);
  250. return printTemplate.getOwners();
  251. } else {
  252. List<String> owners = printTemplate.getOwners();
  253. for (String owner : owners) {
  254. if (owner.startsWith(IdPrefix.DEPARTMENT.getValue())) {
  255. owner = "ROLE_" + owner;
  256. }
  257. newOwners.add(owner);
  258. }
  259. }
  260. // 返回组织部门中选择的角色作为SID
  261. return newOwners;
  262. }
  263. /**
  264. * 判断当前登录用户是否在指定的组织部门中
  265. *
  266. * @param printTemplate
  267. * @param sid
  268. */
  269. private Boolean hasPermission(PrintTemplate printTemplate) {
  270. Boolean hasPermission = false;
  271. // 获取该打印模板的所有SID,判断是否有访问权限
  272. List<AclSid> aclSids = aclService.getSid(printTemplate);
  273. for (AclSid aclSid : aclSids) {
  274. String sid = aclSid.getSid();
  275. // 如果所有者是默认的则有权限
  276. if (sid.equals(ACL_SID)) {
  277. hasPermission = true;
  278. break;
  279. } else {// 由组织部门提供接口,判断当前登录用户是否在指定的SID(组织部门)中
  280. if (sid.startsWith(IdPrefix.USER.getValue())) {
  281. if (StringUtils.equals(((UserDetails) SpringSecurityUtils.getCurrentUser()).getUserId(), sid)) {
  282. hasPermission = true;
  283. break;
  284. }
  285. } else {
  286. hasPermission = false;
  287. }
  288. }
  289. }
  290. return hasPermission;
  291. }
  292. /**
  293. *
  294. * 打印模板调用接口,返回文件流(模板ID,单份工作,动态表单集合,输入文件(正文))
  295. *
  296. * @param templateId
  297. * @param entities
  298. */
  299. @Override
  300. public <ENTITY extends IdEntity> InputStream getPrintTemplateInputStream(String templateId,
  301. Collection<ENTITY> entities, Map<String, Object> dytableMap, File textFile) throws Exception {
  302. List<Collection<ENTITY>> allEntities = new ArrayList<Collection<ENTITY>>();
  303. allEntities.add(entities);
  304. List<Map<String, Object>> dytableMaps = new ArrayList<Map<String, Object>>();
  305. dytableMaps.add(dytableMap);
  306. File finalFile = getPrintTemplateFile(templateId, allEntities, dytableMaps, textFile);
  307. InputStream fileInStream = new FileInputStream(finalFile);
  308. return fileInStream;
  309. }
  310. /**
  311. *
  312. * 打印模板调用接口,返回文件(模板ID,单份工作,动态表单集合,输入文件(正文))
  313. *
  314. * (non-Javadoc)
  315. * @throws Exception
  316. * @see com.wellsoft.pt.basicdata.printtemplate.service.PrintTemplateService#getPrintTemplate(java.lang.String, java.util.Collection, java.io.InputStream)
  317. */
  318. @Override
  319. public <ENTITY extends IdEntity> File getPrintTemplateFile(String templateId, Collection<ENTITY> entities,
  320. Map<String, Object> dytableMap, File textFile) throws Exception {
  321. List<Collection<ENTITY>> allEntities = new ArrayList<Collection<ENTITY>>();
  322. allEntities.add(entities);
  323. List<Map<String, Object>> dytableMaps = new ArrayList<Map<String, Object>>();
  324. dytableMaps.add(dytableMap);
  325. return getPrintTemplateFile(templateId, allEntities, dytableMaps, textFile);
  326. }
  327. /**
  328. *
  329. * 打印模板调用接口,返回文件流(模板ID,多份工作,动态表单集合,输入文件(正文))
  330. *
  331. * @param templateId
  332. * @param entities
  333. */
  334. @Override
  335. public <ENTITY extends IdEntity> InputStream getPrintTemplateInputStream(String templateId,
  336. Collection<Collection<ENTITY>> allEntities, List<Map<String, Object>> dytableMaps, File textFile)
  337. throws Exception {
  338. File finalFile = getPrintTemplateFile(templateId, allEntities, dytableMaps, textFile);
  339. InputStream fileInStream = new FileInputStream(finalFile);
  340. return fileInStream;
  341. }
  342. /**
  343. *
  344. * 打印模板调用接口,返回文件(模板ID,多份工作,动态表单集合,输入文件(正文))
  345. *
  346. * (non-Javadoc)
  347. * @throws Exception
  348. * @see com.wellsoft.pt.basicdata.printtemplate.service.PrintTemplateService#getPrintTemplate(java.lang.String, java.util.Collection, java.io.InputStream)
  349. */
  350. @Override
  351. public <ENTITY extends IdEntity> File getPrintTemplateFile(String templateId,
  352. Collection<Collection<ENTITY>> allEntities, List<Map<String, Object>> dytableMaps, File textFile)
  353. throws Exception {
  354. PrintTemplate printTemplate = printTemplateDao.findUniqueBy("id", templateId);
  355. File finalFile = null;
  356. //word模板类型
  357. if ("wordType".equals(printTemplate.getTemplateType())) {
  358. String uuid = printTemplate.getUuid();
  359. //下载该打印模板对应的模板文件
  360. List<MongoFileEntity> files = mongoFileService.getFilesFromFolder(uuid, "attach");
  361. MongoFileEntity mongoFileEntity = files.get(0);
  362. InputStream inputStreamOut = mongoFileEntity.getInputstream();
  363. //将文件输出流转为临时文件,使用"./"表示放在eclipseWorkspace\basic下
  364. FileUtils.copyInputStreamToFile(inputStreamOut, new File("./", "temp.doc"));
  365. System.out.println("临时文件输出成功!");
  366. //判断是否需要多次套打
  367. if (allEntities.size() == 1) {
  368. //判断是否保存到源文档
  369. if (printTemplate.getIsSaveSource()) {
  370. for (Collection<ENTITY> entities : allEntities) {
  371. //文件名定义格式
  372. String fileNameFormat = printTemplate.getFileNameFormat();
  373. fileNameFormat = fileNameFormat.replaceAll("}", "!}");//防止freemarker模板处理null出错
  374. Map<String, Object> templateMap = getMapValue(entities, dytableMaps.get(0));//获得模板集合
  375. TemplateEngine templateEngine = TemplateEngineFactory.getDefaultTemplateEngine();
  376. String fileName = templateEngine.process(fileNameFormat, templateMap);
  377. String outputPath = Config.HOME_DIR + "/" + fileName + ".doc";//文件输出路径
  378. Map<String, String> replaceMap = new HashMap<String, String>();//用来存放解析后的键值对
  379. File tempFile = getTempFile();//获取下载的临时文件
  380. String text = getWordText(tempFile);//获取临时word文件文本内容
  381. String reg = "\\$\\{.*?(?=\\}.*)";//文本内容中的关键字定义部分正则
  382. Pattern pattern = Pattern.compile(reg);
  383. Matcher matcher = pattern.matcher(text);
  384. String keyWords = "";
  385. while (matcher.find()) {
  386. //取得关键字
  387. String keyWord = matcher.group() + "!}";
  388. keyWords = keyWords + keyWord + ",";
  389. System.out.println("关键字:" + keyWord);
  390. //模板解析关键字
  391. String result = templateEngine.process(keyWord, templateMap);
  392. System.out.println("关键字替换后:" + result);
  393. String newKeyWord = keyWord.replaceAll("!", "");
  394. replaceMap.put(newKeyWord, result);
  395. }
  396. //读取关键字后保存在数据库中的关键字列
  397. String newKeyWords = keyWords.replaceAll("!", "");
  398. printTemplate.setKeyWords(newKeyWords);
  399. printTemplateDao.save(printTemplate);
  400. //完成关键字解析后对word进行操作
  401. finalFile = exportDocByMutiLine(tempFile.getCanonicalPath(), outputPath, replaceMap, textFile,
  402. printTemplate);
  403. }
  404. } else {
  405. //多个工作数量
  406. Integer docNumber = allEntities.size();
  407. Integer subScript = 0;
  408. //对多个工作进行遍历
  409. for (Collection<ENTITY> entities : allEntities) {
  410. Map<String, String> replaceMap = new HashMap<String, String>();//用来存放解析后的键值对
  411. Map<String, Object> templateMap = getMapValue(entities, dytableMaps.get(subScript));//获得模板集合
  412. subScript = subScript + 1;
  413. TemplateEngine templateEngine = TemplateEngineFactory.getDefaultTemplateEngine();
  414. File tempFile = getTempFile();//获取下载的临时文件
  415. String text = getWordText(tempFile);//获取临时word文件文本内容
  416. String reg = "\\$\\{.*?(?=\\}.*)";//文本内容中的关键字定义部分正则
  417. Pattern pattern = Pattern.compile(reg);
  418. Matcher matcher = pattern.matcher(text);
  419. String keyWords = "";
  420. while (matcher.find()) {
  421. //取得关键字
  422. String keyWord = matcher.group() + "!}";
  423. keyWords = keyWords + keyWord + ",";
  424. System.out.println("关键字:" + keyWord);
  425. //模板解析关键字
  426. String result = templateEngine.process(keyWord, templateMap);
  427. System.out.println("关键字替换后:" + result);
  428. String newKeyWord = keyWord.replaceAll("!", "");
  429. replaceMap.put(newKeyWord, result);
  430. }
  431. //读取关键字后保存在数据库中的关键字列
  432. String newKeyWords = keyWords.replaceAll("!", "");
  433. printTemplate.setKeyWords(newKeyWords);
  434. printTemplateDao.save(printTemplate);
  435. //完成关键字解析后对word进行操作
  436. exportDoc(tempFile.getCanonicalPath(), Config.HOME_DIR + "/" + docNumber + ".doc", replaceMap,
  437. textFile, printTemplate);
  438. docNumber = docNumber - 1;
  439. }
  440. //判断多次套打是分页还是多行
  441. if ("paging".equals(printTemplate.getPrintInterval())) {
  442. //合并word文档(分页)
  443. finalFile = uniteDocByPage(allEntities.size(), printTemplate);
  444. } else if ("multi_line".equals(printTemplate.getPrintInterval())) {
  445. //合并word文档(多行)
  446. finalFile = uniteDocByMultiLine(allEntities.size(), printTemplate);
  447. }
  448. }
  449. } else {
  450. //多个工作数量
  451. Integer docNumber = allEntities.size();
  452. Integer subScript = 0;
  453. //对多个工作进行遍历
  454. for (Collection<ENTITY> entities : allEntities) {
  455. Map<String, String> replaceMap = new HashMap<String, String>();//用来存放解析后的键值对
  456. Map<String, Object> templateMap = getMapValue(entities, dytableMaps.get(subScript));//获得模板集合
  457. subScript = subScript + 1;
  458. TemplateEngine templateEngine = TemplateEngineFactory.getDefaultTemplateEngine();
  459. File tempFile = getTempFile();//获取下载的临时文件
  460. String text = getWordText(tempFile);//获取临时word文件文本内容
  461. String reg = "\\$\\{.*?(?=\\}.*)";//文本内容中的关键字定义部分正则
  462. Pattern pattern = Pattern.compile(reg);
  463. Matcher matcher = pattern.matcher(text);
  464. String keyWords = "";
  465. while (matcher.find()) {
  466. //取得关键字
  467. String keyWord = matcher.group() + "!}";
  468. keyWords = keyWords + keyWord + ",";
  469. System.out.println("关键字:" + keyWord);
  470. //模板解析关键字
  471. String result = templateEngine.process(keyWord, templateMap);
  472. System.out.println("关键字替换后:" + result);
  473. String newKeyWord = keyWord.replaceAll("!", "");
  474. replaceMap.put(newKeyWord, result);
  475. }
  476. //读取关键字后保存在数据库中的关键字列
  477. String newKeyWords = keyWords.replaceAll("!", "");
  478. printTemplate.setKeyWords(newKeyWords);
  479. printTemplateDao.save(printTemplate);
  480. //完成关键字解析后对word进行操作
  481. exportDoc(tempFile.getCanonicalPath(), Config.HOME_DIR + "/" + docNumber + ".doc", replaceMap,
  482. textFile, printTemplate);
  483. docNumber = docNumber - 1;
  484. }
  485. //判断多次套打是分页还是多行
  486. if ("paging".equals(printTemplate.getPrintInterval())) {
  487. //合并word文档(分页)
  488. finalFile = uniteDocByPage(allEntities.size(), printTemplate);
  489. } else if ("multi_line".equals(printTemplate.getPrintInterval())) {
  490. //合并word文档(多行)
  491. finalFile = uniteDocByMultiLine(allEntities.size(), printTemplate);
  492. }
  493. }
  494. //判断是否保存打印记录
  495. if (printTemplate.getIsSavePrintRecord()) {
  496. for (Collection<ENTITY> entities : allEntities) {
  497. PrintRecord printRecord = new PrintRecord();
  498. String printObjectUuid = "";
  499. String printObjectType = "";
  500. for (ENTITY entity : entities) {
  501. printObjectUuid = printObjectUuid + entity.getUuid() + ",";
  502. printObjectType = printObjectType + entity.getClass().getName() + ",";
  503. }
  504. //打印记录历史次数集合
  505. List<Integer> oldPrintTimes = new ArrayList<Integer>();
  506. List<PrintRecord> oldPrintRecords = printRecordDao.findBy("printObject", printObjectUuid);
  507. for (PrintRecord oldPrintRecord : oldPrintRecords) {
  508. Integer oldPrintTimes1 = oldPrintRecord.getPrintTimes();
  509. oldPrintTimes.add(oldPrintTimes1);
  510. }
  511. //取得打印次数的最大值
  512. int maxOldPrintTimes = 0;
  513. for (int i = 0; i < oldPrintTimes.size(); i++) {
  514. if (oldPrintTimes.get(i) > maxOldPrintTimes) {
  515. maxOldPrintTimes = oldPrintTimes.get(i);
  516. }
  517. }
  518. //如果打印记录不为null的话
  519. if (oldPrintRecords != null) {
  520. printRecord.setPrintTimes(maxOldPrintTimes + 1);
  521. } else {
  522. printRecord.setPrintTimes(1);
  523. }
  524. printRecord.setPrintObject(printObjectUuid);
  525. printRecord.setPrintObjectType(printObjectType);
  526. //获取调用人
  527. String userName = SpringSecurityUtils.getCurrentUserName();
  528. System.out.println("当前用户名:" + userName);
  529. printRecord.setUserName(userName);
  530. printRecord.setCode(templateId);
  531. printRecordDao.save(printRecord);
  532. }
  533. }
  534. } else if ("htmlType".equals(printTemplate.getTemplateType())) {
  535. //判断是否需要多次套打
  536. if (allEntities.size() == 1) {
  537. //判断是否保存到源文档
  538. if (printTemplate.getIsSaveSource()) {
  539. for (Collection<ENTITY> entities : allEntities) {
  540. //文件流每次被读取的话都要新下载该文件流
  541. String uuid = printTemplate.getUuid();
  542. //下载该打印模板对应的html模板文件
  543. List<MongoFileEntity> files = mongoFileService.getFilesFromFolder(uuid, "attach");
  544. MongoFileEntity mongoFileEntity = files.get(0);
  545. InputStream inputStreamOut = mongoFileEntity.getInputstream();
  546. //文件名定义格式
  547. String fileNameFormat = printTemplate.getFileNameFormat();
  548. fileNameFormat = fileNameFormat.replaceAll("}", "!}");//防止freemarker模板处理null出错
  549. Map<String, Object> templateMap = getMapValue(entities, dytableMaps.get(0));//获得模板集合
  550. TemplateEngine templateEngine = TemplateEngineFactory.getDefaultTemplateEngine();
  551. String fileName = templateEngine.process(fileNameFormat, templateMap);
  552. String outputPath = Config.HOME_DIR + "/" + fileName + ".doc";//文件输出路径
  553. Map<String, String> replaceMap = new HashMap<String, String>();//用来存放解析后的键值对
  554. //读取html模板流并替换为freemarker模板数据内容
  555. String beforeReplaceHtml = IOUtils.toString(inputStreamOut);
  556. String afterReplaceHtml = templateEngine.process(beforeReplaceHtml, templateMap);
  557. InputStream afterReplaceInputStream = new ByteArrayInputStream(
  558. afterReplaceHtml.getBytes("UTF-8"));//html模板替换后的文件流
  559. // File tempHtmlFile = new File("./", "tempHtml.doc");
  560. FileUtils.copyInputStreamToFile(afterReplaceInputStream, new File(outputPath));
  561. finalFile = new File(outputPath);
  562. /* //如果套打模板设置为只读,则最终的结果以pdf来展示
  563. if (printTemplate.getIsReadOnly()) {
  564. File afterReplaceHtmlToWord = new File("./", "tempHtmlToWord.doc");
  565. PrinttemplateUtil.htmlToWord(finalFile.getCanonicalPath(),
  566. afterReplaceHtmlToWord.getCanonicalPath());
  567. //删除临时html文件流转成的word文档tempHtml.doc
  568. finalFile.delete();
  569. //html转为word之后样式会有变化,所以这时要对页面进行设置(纸张大小、页边距等)
  570. ActiveXComponent word = PrinttemplateUtil.getActiveXComponent();
  571. Dispatch wordDispatchDocument = PrinttemplateUtil.getWordDispatchDocument(word);
  572. Dispatch currentDispatch = PrinttemplateUtil.open(wordDispatchDocument,
  573. afterReplaceHtmlToWord.getCanonicalPath());
  574. PrinttemplateUtil.setPageSetup(currentDispatch, 1, 8, 45, 45, 40, 40);
  575. PrinttemplateUtil.save(word, afterReplaceHtmlToWord.getCanonicalPath());
  576. PrinttemplateUtil.closeActiveXComponent(word);
  577. fileService.convert2PDF(FileUtils.openInputStream(afterReplaceHtmlToWord),
  578. printTemplate.getUuid());
  579. //删除临时word文档tempHtmlToWord.doc
  580. afterReplaceHtmlToWord.delete();
  581. finalFile = new File(Config.APP_DATA_DIR + "/pdf2swf/" + printTemplate.getUuid() + ".pdf");
  582. }*/
  583. }
  584. } else {
  585. //多个工作数量
  586. Integer docNumber = allEntities.size();
  587. Integer subScript = 0;
  588. //对多个工作进行遍历
  589. for (Collection<ENTITY> entities : allEntities) {
  590. //文件流每次被读取的话都要新下载该文件流
  591. String uuid = printTemplate.getUuid();
  592. //下载该打印模板对应的html模板文件
  593. List<MongoFileEntity> files = mongoFileService.getFilesFromFolder(uuid, "attach");
  594. MongoFileEntity mongoFileEntity = files.get(0);
  595. InputStream inputStreamOut = mongoFileEntity.getInputstream();
  596. Map<String, String> replaceMap = new HashMap<String, String>();//用来存放解析后的键值对
  597. Map<String, Object> templateMap = getMapValue(entities, dytableMaps.get(subScript));//获得模板集合
  598. subScript = subScript + 1;
  599. TemplateEngine templateEngine = TemplateEngineFactory.getDefaultTemplateEngine();
  600. //读取html模板流并替换为freemarker模板数据内容
  601. String beforeReplaceHtml = IOUtils.toString(inputStreamOut);
  602. String afterReplaceHtml = templateEngine.process(beforeReplaceHtml, templateMap);
  603. InputStream afterReplaceInputStream = new ByteArrayInputStream(
  604. afterReplaceHtml.getBytes("UTF-8"));//html模板替换后的文件流
  605. // File tempHtmlFile = new File(Config.HOME_DIR + "/" + docNumber + ".doc");//多份工作临时存放位置,存放格式为1.doc,2.doc……
  606. String outputPath = Config.HOME_DIR + "/" + getFileName() + ".doc";//文件输出路径
  607. FileUtils.copyInputStreamToFile(afterReplaceInputStream, new File(outputPath));
  608. finalFile = new File(outputPath);
  609. docNumber = docNumber - 1;
  610. }
  611. /*//判断多次套打是分页还是多行
  612. if ("paging".equals(printTemplate.getPrintInterval())) {
  613. //合并word文档(分页)
  614. finalFile = uniteDocByPage(allEntities.size(), printTemplate);
  615. } else if ("multi_line".equals(printTemplate.getPrintInterval())) {
  616. //合并word文档(多行)
  617. finalFile = uniteDocByMultiLine(allEntities.size(), printTemplate);
  618. }
  619. */
  620. /*//如果套打模板设置为只读,则最终的结果以pdf来展示
  621. if (printTemplate.getIsReadOnly()) {
  622. File afterReplaceHtmlToWord = new File("./", "tempHtmlToWord.doc");
  623. PrinttemplateUtil.htmlToWord(finalFile.getCanonicalPath(),
  624. afterReplaceHtmlToWord.getCanonicalPath());
  625. //删除临时html文件流转成的word文档tempHtml.doc
  626. finalFile.delete();
  627. //html转为word之后样式会有变化,所以这时要对页面进行设置(纸张大小、页边距等)
  628. ActiveXComponent word = PrinttemplateUtil.getActiveXComponent();
  629. Dispatch wordDispatchDocument = PrinttemplateUtil.getWordDispatchDocument(word);
  630. Dispatch currentDispatch = PrinttemplateUtil.open(wordDispatchDocument,
  631. afterReplaceHtmlToWord.getCanonicalPath());
  632. PrinttemplateUtil.setPageSetup(currentDispatch, 1, 8, 45, 45, 40, 40);
  633. PrinttemplateUtil.save(word, afterReplaceHtmlToWord.getCanonicalPath());
  634. PrinttemplateUtil.closeActiveXComponent(word);
  635. fileService.convert2PDF(FileUtils.openInputStream(afterReplaceHtmlToWord),
  636. printTemplate.getUuid());
  637. //删除临时word文档tempHtmlToWord.doc
  638. afterReplaceHtmlToWord.delete();
  639. finalFile = new File(Config.APP_DATA_DIR + "/pdf2swf/" + printTemplate.getUuid() + ".pdf");
  640. }*/
  641. }
  642. } else {
  643. //多个工作数量
  644. Integer docNumber = allEntities.size();
  645. Integer subScript = 0;
  646. //对多个工作进行遍历
  647. for (Collection<ENTITY> entities : allEntities) {
  648. //文件流每次被读取的话都要新下载该文件流
  649. String uuid = printTemplate.getUuid();
  650. //下载该打印模板对应的html模板文件
  651. List<MongoFileEntity> files = mongoFileService.getFilesFromFolder(uuid, "attach");
  652. MongoFileEntity mongoFileEntity = files.get(0);
  653. InputStream inputStreamOut = mongoFileEntity.getInputstream();
  654. Map<String, String> replaceMap = new HashMap<String, String>();//用来存放解析后的键值对
  655. Map<String, Object> templateMap = getMapValue(entities, dytableMaps.get(subScript));//获得模板集合
  656. subScript = subScript + 1;
  657. TemplateEngine templateEngine = TemplateEngineFactory.getDefaultTemplateEngine();
  658. //读取html模板流并替换为freemarker模板数据内容
  659. String beforeReplaceHtml = IOUtils.toString(inputStreamOut);
  660. String afterReplaceHtml = templateEngine.process(beforeReplaceHtml, templateMap);
  661. InputStream afterReplaceInputStream = new ByteArrayInputStream(afterReplaceHtml.getBytes("UTF-8"));//html模板替换后的文件流
  662. // File tempHtmlFile = new File(Config.HOME_DIR + "/" + docNumber + ".doc");//多份工作临时存放位置,存放格式为1.doc,2.doc……
  663. String outputPath = Config.HOME_DIR + "/" + getFileName() + ".doc";//文件输出路径
  664. FileUtils.copyInputStreamToFile(afterReplaceInputStream, new File(outputPath));
  665. finalFile = new File(outputPath);
  666. docNumber = docNumber - 1;
  667. }
  668. /*//判断多次套打是分页还是多行
  669. if ("paging".equals(printTemplate.getPrintInterval())) {
  670. //合并word文档(分页)
  671. finalFile = uniteDocByPage(allEntities.size(), printTemplate);
  672. } else if ("multi_line".equals(printTemplate.getPrintInterval())) {
  673. //合并word文档(多行)
  674. finalFile = uniteDocByMultiLine(allEntities.size(), printTemplate);
  675. }
  676. //如果套打模板设置为只读,则最终的结果以pdf来展示
  677. if (printTemplate.getIsReadOnly()) {
  678. File afterReplaceHtmlToWord = new File("./", "tempHtmlToWord.doc");
  679. PrinttemplateUtil.htmlToWord(finalFile.getCanonicalPath(),
  680. afterReplaceHtmlToWord.getCanonicalPath());
  681. //删除临时html文件流转成的word文档tempHtml.doc
  682. finalFile.delete();
  683. //html转为word之后样式会有变化,所以这时要对页面进行设置(纸张大小、页边距等)
  684. ActiveXComponent word = PrinttemplateUtil.getActiveXComponent();
  685. Dispatch wordDispatchDocument = PrinttemplateUtil.getWordDispatchDocument(word);
  686. Dispatch currentDispatch = PrinttemplateUtil.open(wordDispatchDocument,
  687. afterReplaceHtmlToWord.getCanonicalPath());
  688. PrinttemplateUtil.setPageSetup(currentDispatch, 1, 8, 45, 45, 40, 40);
  689. PrinttemplateUtil.save(word, afterReplaceHtmlToWord.getCanonicalPath());
  690. PrinttemplateUtil.closeActiveXComponent(word);
  691. fileService.convert2PDF(FileUtils.openInputStream(afterReplaceHtmlToWord), printTemplate.getUuid());
  692. //删除临时word文档tempHtmlToWord.doc
  693. afterReplaceHtmlToWord.delete();
  694. finalFile = new File(Config.APP_DATA_DIR + "/pdf2swf/" + printTemplate.getUuid() + ".pdf");
  695. }*/
  696. }
  697. //判断是否保存打印记录
  698. if (printTemplate.getIsSavePrintRecord()) {
  699. for (Collection<ENTITY> entities : allEntities) {
  700. PrintRecord printRecord = new PrintRecord();
  701. String printObjectUuid = "";
  702. String printObjectType = "";
  703. for (ENTITY entity : entities) {
  704. printObjectUuid = printObjectUuid + entity.getUuid() + ",";
  705. printObjectType = printObjectType + entity.getClass().getName() + ",";
  706. }
  707. //打印记录历史次数集合
  708. List<Integer> oldPrintTimes = new ArrayList<Integer>();
  709. List<PrintRecord> oldPrintRecords = printRecordDao.findBy("printObject", printObjectUuid);
  710. for (PrintRecord oldPrintRecord : oldPrintRecords) {
  711. Integer oldPrintTimes1 = oldPrintRecord.getPrintTimes();
  712. oldPrintTimes.add(oldPrintTimes1);
  713. }
  714. //取得打印次数的最大值
  715. int maxOldPrintTimes = 0;
  716. for (int i = 0; i < oldPrintTimes.size(); i++) {
  717. if (oldPrintTimes.get(i) > maxOldPrintTimes) {
  718. maxOldPrintTimes = oldPrintTimes.get(i);
  719. }
  720. }
  721. //如果打印记录不为null的话
  722. if (oldPrintRecords != null) {
  723. printRecord.setPrintTimes(maxOldPrintTimes + 1);
  724. } else {
  725. printRecord.setPrintTimes(1);
  726. }
  727. printRecord.setPrintObject(printObjectUuid);
  728. printRecord.setPrintObjectType(printObjectType);
  729. //获取调用人
  730. String userName = SpringSecurityUtils.getCurrentUserName();
  731. System.out.println("当前用户名:" + userName);
  732. printRecord.setUserName(userName);
  733. printRecord.setCode(templateId);
  734. printRecordDao.save(printRecord);
  735. }
  736. }
  737. }
  738. return finalFile;
  739. }
  740. /**
  741. *
  742. * 合并文件(多行)
  743. *
  744. */
  745. private <ENTITY extends IdEntity> File uniteDocByMultiLine(Integer docNumber, PrintTemplate printTemplate) {
  746. Integer rows = printTemplate.getRowNumber();//获取行数
  747. String outputPath = Config.HOME_DIR + "/" + getFileName() + ".doc";//合并文件输出路径
  748. System.out.println("合并文件输出路径:" + outputPath);
  749. List<String> fileList = new ArrayList<String>();
  750. for (int i = 1; i <= docNumber; i++) {
  751. String file = Config.HOME_DIR + "/" + i + ".doc";
  752. fileList.add(file);
  753. System.out.println("当前文件名为:" + file);
  754. }
  755. if (fileList.size() == 0 || fileList == null) {
  756. return null;
  757. }
  758. //打开word
  759. ActiveXComponent active = new ActiveXComponent("Word.Application");//启动word
  760. try {
  761. // 设置word不可见
  762. active.setProperty("Visible", new Variant(false));
  763. //获得documents对象
  764. Object docs = active.getProperty("Documents").toDispatch();
  765. //打开第一个文件
  766. Dispatch doc = Dispatch.invoke((Dispatch) docs, "Open", Dispatch.Method,
  767. new Object[] { (String) fileList.get(0), new Variant(false), new Variant(true) }, new int[3])
  768. .toDispatch();
  769. //选择区域
  770. Dispatch selection = this.select(active);
  771. //判断文本内容中是否包含{BOTTOM}
  772. if (getWordText(getTempFile()).contains("{BOTTOM}")) {
  773. //置底部分不用显示痕迹
  774. Dispatch.put(doc, "TrackRevisions", new Variant(false));
  775. Dispatch.put(doc, "PrintRevisions", new Variant(false));
  776. Dispatch.put(doc, "ShowRevisions", new Variant(false));
  777. System.out.println("文本中包含{BOTTOM},需要置底");
  778. moveStart(selection);
  779. Boolean result = find("{BOTTOM}", selection, active);//查找{BOTTOM}关键字并选中
  780. if (result) {
  781. //替换{BOTTOM}为空
  782. this.replaceAll(selection, "{BOTTOM}", "");
  783. System.out.println("{BOTTOM}已经替换为空");
  784. // // 插入分页符
  785. // Dispatch.call(selection, "InsertBreak", new Variant(7));
  786. // moveUp(2, selection);//将光标往上移动两行
  787. int pages1 = Integer.parseInt(Dispatch.call(selection, "information", 4).toString());//总页数 //显示修订内容的最终状态
  788. System.out.println("移动前总页数:" + pages1);
  789. boolean flag = false;
  790. while (pages1 == Integer.parseInt(Dispatch.call(selection, "information", 4).toString()) && !flag) {
  791. insertBlank(selection);//插入空白行
  792. while (Integer.parseInt(Dispatch.call(selection, "information", 4).toString()) > pages1) {
  793. moveUp(1, selection, active);//超过当前页光标上移
  794. // goToEnd(selection);//光标移动至末尾
  795. Dispatch.call(selection, "Delete");//删除当前空行
  796. flag = true;
  797. break;
  798. }
  799. }
  800. }
  801. }
  802. //追加文件
  803. for (int i = 1; i < fileList.size(); i++) {
  804. goToEnd(selection);
  805. if (i == 1) {
  806. for (int j = 0; j <= rows; j++) {
  807. insertBlank(selection);
  808. }
  809. } else {
  810. //第二份文件开始追加时少一个空行
  811. for (int j = 0; j <= rows - 1; j++) {
  812. insertBlank(selection);
  813. }
  814. }
  815. Dispatch.invoke(active.getProperty("Selection").toDispatch(), "insertFile", Dispatch.Method,
  816. new Object[] { (String) fileList.get(i), "", new Variant(false), new Variant(false),
  817. new Variant(false) }, new int[3]);
  818. //判断文本内容中是否包含{BOTTOM}
  819. if (getWordText(getTempFile()).contains("{BOTTOM}")) {
  820. //置底部分不用显示痕迹
  821. Dispatch.put(doc, "TrackRevisions", new Variant(false));
  822. Dispatch.put(doc, "PrintRevisions", new Variant(false));
  823. Dispatch.put(doc, "ShowRevisions", new Variant(false));
  824. System.out.println("文本中包含{BOTTOM},需要置底");
  825. moveStart(selection);
  826. Boolean result = find("{BOTTOM}", selection, active);//查找{BOTTOM}关键字并选中
  827. if (result) {
  828. //替换{BOTTOM}为空
  829. this.replaceAll(selection, "{BOTTOM}", "");
  830. System.out.println("{BOTTOM}已经替换为空");
  831. // // 插入分页符
  832. // Dispatch.call(selection, "InsertBreak", new Variant(7));
  833. // moveUp(2, selection);//将光标往上移动两行
  834. int pages1 = Integer.parseInt(Dispatch.call(selection, "information", 4).toString());//总页数 //显示修订内容的最终状态
  835. System.out.println("移动前总页数:" + pages1);
  836. boolean flag = false;
  837. while (pages1 == Integer.parseInt(Dispatch.call(selection, "information", 4).toString())
  838. && !flag) {
  839. insertBlank(selection);//插入空白行
  840. while (Integer.parseInt(Dispatch.call(selection, "information", 4).toString()) > pages1) {
  841. flag = true;
  842. break;
  843. }
  844. }
  845. }
  846. }
  847. }
  848. //删除最后一页空白页
  849. goToEnd(selection);//光标移动至末尾
  850. Dispatch.call(selection, "Delete");//删除当前空行
  851. //保存新的word文件
  852. Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method, new Object[] { outputPath, new Variant(1) },
  853. new int[3]);
  854. if (printTemplate.getIsReadOnly()) {
  855. new File(outputPath).setWritable(false);//将输出文档设为只读
  856. }
  857. Variant f = new Variant(false);
  858. Dispatch.call((Dispatch) doc, "Close", f);
  859. //合并完毕后删除原来文件
  860. for (int i = 1; i <= docNumber; i++) {
  861. String file = Config.HOME_DIR + "/" + i + ".doc";
  862. File delFile = new File(file);
  863. delFile.delete();
  864. System.out.println("原来文件删除成功!");
  865. }
  866. //删除临时文件temp.doc
  867. new File("./", "temp.doc").delete();
  868. System.out.println("临时文件删除成功!");
  869. } catch (Exception e) {
  870. throw new RuntimeException("合并word文件出错.原因:" + e);
  871. } finally {
  872. active.invoke("Quit", new Variant[] {});
  873. }
  874. System.out.println("多行合并成功!");
  875. return new File(outputPath);
  876. }
  877. /**
  878. *
  879. * 合并文件(分页)
  880. *
  881. */
  882. public <ENTITY extends IdEntity> File uniteDocByPage(Integer docNumber, PrintTemplate printTemplate) {
  883. String outputPath = Config.HOME_DIR + "/" + getFileName() + ".doc";//合并文件输出路径
  884. System.out.println("合并文件输出路径:" + outputPath);
  885. List<String> fileList = new ArrayList<String>();
  886. for (int i = 1; i <= docNumber; i++) {
  887. String file = Config.HOME_DIR + "/" + i + ".doc";
  888. fileList.add(file);
  889. System.out.println("当前文件名为:" + file);
  890. }
  891. if (fileList.size() == 0 || fileList == null) {
  892. return null;
  893. }
  894. //打开word
  895. ActiveXComponent active = new ActiveXComponent("Word.Application");//启动word
  896. try {
  897. // 设置word不可见
  898. active.setProperty("Visible", new Variant(false));
  899. //获得documents对象
  900. Object docs = active.getProperty("Documents").toDispatch();
  901. //打开第一个文件
  902. Object doc = Dispatch.invoke((Dispatch) docs, "Open", Dispatch.Method,
  903. new Object[] { (String) fileList.get(0), new Variant(false), new Variant(true) }, new int[3])
  904. .toDispatch();
  905. //选择区域
  906. Dispatch selection = this.select(active);
  907. //追加文件
  908. for (int i = 1; i < fileList.size(); i++) {
  909. goToEnd(selection);//光标移动至末尾
  910. PageBreak(selection);//插入分页符
  911. Dispatch.invoke(active.getProperty("Selection").toDispatch(), "insertFile", Dispatch.Method,
  912. new Object[] { (String) fileList.get(i), "", new Variant(false), new Variant(false),
  913. new Variant(false) }, new int[3]);
  914. }
  915. //删除最后一页空白页
  916. goToEnd(selection);//光标移动至末尾
  917. Dispatch.call(selection, "Delete");//删除当前空行
  918. //保存新的word文件
  919. Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method, new Object[] { outputPath, new Variant(1) },
  920. new int[3]);
  921. if (printTemplate.getIsReadOnly()) {
  922. new File(outputPath).setWritable(false);//将输出文档设为只读
  923. }
  924. Variant f = new Variant(false);
  925. Dispatch.call((Dispatch) doc, "Close", f);
  926. //合并完毕后删除原来文件
  927. for (int i = 1; i <= docNumber; i++) {
  928. String file = Config.HOME_DIR + "/" + i + ".doc";
  929. File delFile = new File(file);
  930. delFile.delete();
  931. System.out.println("原来文件删除成功!");
  932. }
  933. //删除临时文件temp.doc
  934. new File("./", "temp.doc").delete();
  935. System.out.println("临时文件删除成功!");
  936. } catch (Exception e) {
  937. throw new RuntimeException("合并word文件出错.原因:" + e);
  938. } finally {
  939. active.invoke("Quit", new Variant[] {});
  940. }
  941. System.out.println("分页合并成功!");
  942. return new File(outputPath);
  943. }
  944. /**
  945. * 解析关键字后操作word(单工作多行)
  946. * @param inputDocPath
  947. * @param outPutDocPath
  948. * @param map
  949. * @param isPrint
  950. * @throws IOException
  951. */
  952. public File exportDocByMutiLine(String inputDocPath, String outPutDocPath, Map<String, String> map, File textFile,
  953. PrintTemplate printTemplate) {
  954. //初始化com的线程
  955. ComThread.InitSTA();
  956. //word运行程序对象
  957. ActiveXComponent word = new ActiveXComponent("Word.Application");
  958. //文档对象
  959. Dispatch wordObject = (Dispatch) word.getObject();
  960. //设置属性 Variant(true)表示word应用程序可见
  961. Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false));
  962. //word所有文档
  963. Dispatch documents = word.getProperty("Documents").toDispatch();
  964. //打开模板文档
  965. Dispatch document = this.open(documents, inputDocPath);
  966. //选择区域
  967. Dispatch selection = this.select(word);
  968. Iterator keys = map.keySet().iterator();
  969. String oldText;
  970. Object newValue;
  971. while (keys.hasNext()) {
  972. oldText = (String) keys.next();//替换前关键字
  973. newValue = map.get(oldText);//替换后的关键字
  974. this.replaceAll(selection, oldText, newValue);
  975. }
  976. //判断是否包含{正文}及正文内容的文件
  977. if (getWordText(getTempFile()).contains("{正文}") && textFile.exists()) {
  978. //判断是否需要保留正文修改痕迹
  979. if (printTemplate.getIsSaveTrace()) {
  980. Dispatch.put(document, "TrackRevisions", new Variant(true));
  981. Dispatch.put(document, "PrintRevisions", new Variant(true));
  982. Dispatch.put(document, "ShowRevisions", new Variant(true));
  983. }
  984. //打开含正文内容文档
  985. Dispatch textDocument;
  986. try {
  987. textDocument = this.open(documents, textFile.getCanonicalPath());
  988. Dispatch textSelection = this.select(word);
  989. Dispatch wordContent = Dispatch.get(textDocument, "Content").toDispatch(); //取得正文word文件的内容
  990. Dispatch.call(wordContent, "Select");//全选
  991. Dispatch.call(textSelection, "Copy");//复制
  992. this.close(textDocument);//关闭正文word
  993. //替换{正文}为空
  994. this.replaceAll(selection, "{正文}", "");
  995. System.out.println("{正文}已经替换为空");
  996. paste(selection);//粘贴至源文档
  997. } catch (IOException e) {
  998. e.printStackTrace();
  999. this.close(document);//出异常则关闭当前word
  1000. word.invoke("Quit", new Variant[0]);
  1001. }
  1002. }
  1003. //判断文本内容中是否包含{BOTTOM}
  1004. if (getWordText(getTempFile()).contains("{BOTTOM}")) {
  1005. //置底部分不用显示痕迹
  1006. Dispatch.put(document, "TrackRevisions", new Variant(false));
  1007. Dispatch.put(document, "PrintRevisions", new Variant(false));
  1008. Dispatch.put(document, "ShowRevisions", new Variant(false));
  1009. System.out.println("文本中包含{BOTTOM},需要置底");
  1010. moveStart(selection);
  1011. Boolean result = find("{BOTTOM}", selection, word);//查找{BOTTOM}关键字并选中
  1012. if (result) {
  1013. //替换{BOTTOM}为空
  1014. this.replaceAll(selection, "{BOTTOM}", "");
  1015. System.out.println("{BOTTOM}已经替换为空");
  1016. // // 插入分页符
  1017. // Dispatch.call(selection, "InsertBreak", new Variant(7));
  1018. // moveUp(2, selection);//将光标往上移动两行
  1019. int pages1 = Integer.parseInt(Dispatch.call(selection, "information", 4).toString());//总页数 //显示修订内容的最终状态
  1020. System.out.println("移动前总页数:" + pages1);
  1021. boolean flag = false;
  1022. while (pages1 == Integer.parseInt(Dispatch.call(selection, "information", 4).toString()) && !flag) {
  1023. insertBlank(selection);//插入空白行
  1024. while (Integer.parseInt(Dispatch.call(selection, "information", 4).toString()) > pages1) {
  1025. moveUp(1, selection, word);//超过当前页光标上移
  1026. // goToEnd(selection);//光标移动至末尾
  1027. Dispatch.call(selection, "Delete");//删除当前空行
  1028. flag = true;
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. }
  1034. this.save(word, outPutDocPath, printTemplate, document);
  1035. this.close(document);
  1036. word.invoke("Quit", new Variant[0]);
  1037. //关闭com的线程
  1038. ComThread.Release();
  1039. return new File(outPutDocPath);
  1040. }
  1041. /**
  1042. * 解析关键字后操作word
  1043. * @param inputDocPath
  1044. * @param outPutDocPath
  1045. * @param map
  1046. * @param isPrint
  1047. * @throws IOException
  1048. */
  1049. public File exportDoc(String inputDocPath, String outPutDocPath, Map<String, String> map, File textFile,
  1050. PrintTemplate printTemplate) {
  1051. //初始化com的线程
  1052. ComThread.InitSTA();
  1053. //word运行程序对象
  1054. ActiveXComponent word = new ActiveXComponent("Word.Application");
  1055. //文档对象
  1056. Dispatch wordObject = (Dispatch) word.getObject();
  1057. //设置属性 Variant(true)表示word应用程序可见
  1058. Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false));
  1059. //word所有文档
  1060. Dispatch documents = word.getProperty("Documents").toDispatch();
  1061. //打开模板文档
  1062. Dispatch document = this.open(documents, inputDocPath);
  1063. //选择区域
  1064. Dispatch selection = this.select(word);
  1065. Iterator keys = map.keySet().iterator();
  1066. String oldText;
  1067. Object newValue;
  1068. while (keys.hasNext()) {
  1069. oldText = (String) keys.next();//替换前关键字
  1070. newValue = map.get(oldText);//替换后的关键字
  1071. this.replaceAll(selection, oldText, newValue);
  1072. }
  1073. //判断是否包含{正文}及正文内容的文件
  1074. if (getWordText(getTempFile()).contains("{正文}") && textFile.exists()) {
  1075. //判断是否需要保留正文修改痕迹
  1076. if (printTemplate.getIsSaveTrace()) {
  1077. Dispatch.put(document, "TrackRevisions", new Variant(true));
  1078. Dispatch.put(document, "PrintRevisions", new Variant(true));
  1079. Dispatch.put(document, "ShowRevisions", new Variant(true));
  1080. }
  1081. //打开含正文内容文档
  1082. Dispatch textDocument;
  1083. try {
  1084. textDocument = this.open(documents, textFile.getCanonicalPath());
  1085. Dispatch textSelection = this.select(word);
  1086. Dispatch wordContent = Dispatch.get(textDocument, "Content").toDispatch(); //取得正文word文件的内容
  1087. Dispatch.call(wordContent, "Select");//全选
  1088. Dispatch.call(textSelection, "Copy");//复制
  1089. this.close(textDocument);//关闭正文word
  1090. //替换{正文}为空
  1091. this.replaceAll(selection, "{正文}", "");
  1092. System.out.println("{正文}已经替换为空");
  1093. paste(selection);//粘贴至源文档
  1094. } catch (IOException e) {
  1095. e.printStackTrace();
  1096. this.close(document);//出异常则关闭当前word
  1097. word.invoke("Quit", new Variant[0]);
  1098. }
  1099. }
  1100. //如果是分页先执行置底然后再合并
  1101. if ("paging".equals(printTemplate.getPrintInterval())) {
  1102. //判断文本内容中是否包含{BOTTOM}
  1103. if (getWordText(getTempFile()).contains("{BOTTOM}")) {
  1104. //置底部分不用显示痕迹
  1105. Dispatch.put(document, "TrackRevisions", new Variant(false));
  1106. Dispatch.put(document, "PrintRevisions", new Variant(false));
  1107. Dispatch.put(document, "ShowRevisions", new Variant(false));
  1108. System.out.println("文本中包含{BOTTOM},需要置底");
  1109. moveStart(selection);
  1110. Boolean result = find("{BOTTOM}", selection, word);//查找{BOTTOM}关键字并选中
  1111. if (result) {
  1112. //替换{BOTTOM}为空
  1113. this.replaceAll(selection, "{BOTTOM}", "");
  1114. System.out.println("{BOTTOM}已经替换为空");
  1115. // // 插入分页符
  1116. // Dispatch.call(selection, "InsertBreak", new Variant(7));
  1117. // moveUp(2, selection);//将光标往上移动两行
  1118. int pages1 = Integer.parseInt(Dispatch.call(selection, "information", 4).toString());//总页数 //显示修订内容的最终状态
  1119. System.out.println("移动前总页数:" + pages1);
  1120. boolean flag = false;
  1121. while (pages1 == Integer.parseInt(Dispatch.call(selection, "information", 4).toString()) && !flag) {
  1122. insertBlank(selection);//插入空白行
  1123. while (Integer.parseInt(Dispatch.call(selection, "information", 4).toString()) > pages1) {
  1124. moveUp(1, selection, word);//超过当前页光标上移
  1125. // goToEnd(selection);//光标移动至末尾
  1126. Dispatch.call(selection, "Delete");//删除当前空行
  1127. flag = true;
  1128. break;
  1129. }
  1130. }
  1131. }
  1132. }
  1133. }
  1134. this.save(word, outPutDocPath, printTemplate, document);
  1135. this.close(document);
  1136. word.invoke("Quit", new Variant[0]);
  1137. //关闭com的线程
  1138. ComThread.Release();
  1139. return new File(outPutDocPath);
  1140. }
  1141. /**
  1142. *
  1143. * 插入空行
  1144. *
  1145. * @param selection
  1146. */
  1147. public void insertBlank(Dispatch selection) {
  1148. Dispatch.call(selection, "TypeParagraph");//插入空行
  1149. }
  1150. /**
  1151. * 插入分页符
  1152. *
  1153. * @param selection
  1154. */
  1155. public void PageBreak(Dispatch selection) {
  1156. Dispatch.call(selection, "InsertBreak", new Variant(7));
  1157. }
  1158. /**
  1159. * 按下Ctrl + End键
  1160. */
  1161. public void goToEnd(Dispatch selection) {
  1162. Dispatch.call(selection, "EndKey", "6");
  1163. }
  1164. /**
  1165. * 执行某条宏指令
  1166. *
  1167. * @param cmd
  1168. */
  1169. private void cmd(String cmd, Dispatch selection) {
  1170. Dispatch.call(selection, cmd);
  1171. }
  1172. /**
  1173. * 在当前光标处做粘贴
  1174. */
  1175. public void paste(Dispatch selection) {
  1176. Dispatch.call(selection, "Paste");
  1177. }
  1178. /**
  1179. * 把选定的内容或插入点向上移动
  1180. *
  1181. * @param pos
  1182. * 移动的距离
  1183. */
  1184. public void moveUp(int pos, Dispatch selection, ActiveXComponent word) {
  1185. if (selection == null)
  1186. selection = Dispatch.get(word, "Selection").toDispatch();
  1187. for (int i = 0; i < pos; i++)
  1188. Dispatch.call(selection, "MoveUp");
  1189. }
  1190. /**
  1191. * 把选定的内容或者插入点向下移动
  1192. *
  1193. * @param pos
  1194. * 移动的距离
  1195. */
  1196. public void moveDown(Dispatch selection, ActiveXComponent word) {
  1197. if (selection == null)
  1198. selection = Dispatch.get(word, "Selection").toDispatch();
  1199. // for (int i = 0; i < pos; i++)
  1200. Dispatch.call(selection, "MoveDown");
  1201. }
  1202. /**
  1203. * 从选定内容或插入点开始查找文本
  1204. *
  1205. * @param toFindText
  1206. * 要查找的文本
  1207. * @return boolean true-查找到并选中该文本,false-未查找到文本
  1208. */
  1209. public boolean find(String toFindText, Dispatch selection, ActiveXComponent word) {
  1210. if (toFindText == null || toFindText.equals(""))
  1211. return false;
  1212. // 从selection所在位置开始查询
  1213. Dispatch find = word.call(selection, "Find").toDispatch();
  1214. // 设置要查找的内容
  1215. Dispatch.put(find, "Text", toFindText);
  1216. // 向前查找
  1217. Dispatch.put(find, "Forward", "True");
  1218. // 设置格式
  1219. Dispatch.put(find, "Format", "True");
  1220. // 大小写匹配
  1221. Dispatch.put(find, "MatchCase", "True");
  1222. // 全字匹配
  1223. Dispatch.put(find, "MatchWholeWord", "True");
  1224. // 查找并选中
  1225. return Dispatch.call(find, "Execute").getBoolean();
  1226. }
  1227. /**
  1228. *
  1229. * 获取临时文件
  1230. *
  1231. * @return
  1232. */
  1233. public File getTempFile() {
  1234. File tempFile = new File("./", "temp.doc");
  1235. return tempFile;
  1236. }
  1237. /**
  1238. *
  1239. * POI读取word文本内容
  1240. *
  1241. * @return
  1242. */
  1243. public String getWordText(File tempFile) {
  1244. //POI读取word模板
  1245. FileInputStream in = null;
  1246. try {
  1247. in = new FileInputStream(tempFile);
  1248. } catch (FileNotFoundException e1) {
  1249. e1.printStackTrace();
  1250. }
  1251. HWPFDocument hdt = null;
  1252. try {
  1253. hdt = new HWPFDocument(in);
  1254. } catch (IOException e1) {
  1255. e1.printStackTrace();
  1256. }
  1257. Fields fields = hdt.getFields();
  1258. Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN).iterator();
  1259. while (it.hasNext()) {
  1260. System.out.println(it.next().getType());
  1261. }
  1262. //读取word文本内容
  1263. Range range = hdt.getRange();
  1264. String text = range.text();
  1265. System.out.println("word文本内容:" + text);
  1266. return text;
  1267. }
  1268. /**
  1269. * 打开文件
  1270. * @param documents
  1271. * @param inputDocPath
  1272. * @return
  1273. */
  1274. private Dispatch open(Dispatch documents, String inputDocPath) {
  1275. return Dispatch.call(documents, "Open", inputDocPath).toDispatch();
  1276. }
  1277. /**
  1278. * 选定内容
  1279. * @param word
  1280. * @return
  1281. */
  1282. private Dispatch select(ActiveXComponent word) {
  1283. return word.getProperty("Selection").toDispatch();
  1284. }
  1285. /**
  1286. * 把插入点移动到文件首位置
  1287. * @param selection
  1288. */
  1289. private void moveStart(Dispatch selection) {
  1290. Dispatch.call(selection, "HomeKey", new Variant(6));
  1291. }
  1292. /**
  1293. * 从选定内容或插入点开始查找文本
  1294. * @param selection 选定内容
  1295. * @param toFindText 要查找的文本
  1296. * @return true:查找到并选中该文本;false:未查找到文本。
  1297. */
  1298. private boolean find(Dispatch selection, String toFindText) {
  1299. //从selection所在位置开始查询
  1300. Dispatch find = Dispatch.call(selection, "Find").toDispatch();
  1301. //设置要查找的内容
  1302. Dispatch.put(find, "Text", toFindText);
  1303. //向前查找
  1304. Dispatch.put(find, "Forward", "True");
  1305. //设置格式
  1306. Dispatch.put(find, "format", "True");
  1307. //大小写匹配
  1308. Dispatch.put(find, "MatchCase", "True");
  1309. //全字匹配
  1310. Dispatch.put(find, "MatchWholeWord", "True");
  1311. //查找并选中
  1312. return Dispatch.call(find, "Execute").getBoolean();
  1313. }
  1314. /**
  1315. * 把选定内容替换为设定文本
  1316. * @param selection
  1317. * @param newText
  1318. */
  1319. private void replace(Dispatch selection, String newText) {
  1320. Dispatch.put(selection, "Text", newText);
  1321. }
  1322. /**
  1323. * 全局替换
  1324. * @param selection
  1325. * @param oldText
  1326. * @param replaceObj
  1327. */
  1328. private void replaceAll(Dispatch selection, String oldText, Object replaceObj) {
  1329. moveStart(selection);
  1330. String newText = (String) replaceObj;
  1331. while (find(selection, oldText)) {
  1332. replace(selection, newText);
  1333. Dispatch.call(selection, "MoveRight");
  1334. }
  1335. }
  1336. /**
  1337. * 打印
  1338. * @param document
  1339. */
  1340. private void print(Dispatch document) {
  1341. Dispatch.call(document, "PrintOut");
  1342. }
  1343. /**
  1344. * 保存文件
  1345. * @param word
  1346. * @param outputPath
  1347. */
  1348. private void save(ActiveXComponent word, String outputPath, PrintTemplate printTemplate, Dispatch document) {
  1349. Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(), "FileSaveAs", outputPath);
  1350. if (printTemplate.getIsReadOnly()) {
  1351. new File(outputPath).setWritable(false);//将输出文档设为只读
  1352. }
  1353. }
  1354. /**
  1355. * 关闭文件
  1356. * @param doc
  1357. */
  1358. private void close(Dispatch doc) {
  1359. Dispatch.call(doc, "Close", new Variant(true));
  1360. }
  1361. /**
  1362. *
  1363. * 关键字定义(实体集合)
  1364. *
  1365. * @return
  1366. * @throws Exception
  1367. */
  1368. private static <ENTITY extends IdEntity> Map<String, Object> getMapValue(Collection<ENTITY> entities,
  1369. Map<String, Object> dytableMap) throws Exception {
  1370. Calendar cal = Calendar.getInstance();
  1371. int year = cal.get(Calendar.YEAR);//获取年份
  1372. int month = cal.get(Calendar.MONTH) + 1;//获取月份
  1373. int day = cal.get(Calendar.DATE);//获取日
  1374. int hour = cal.get(Calendar.HOUR_OF_DAY);//小时
  1375. int minute = cal.get(Calendar.MINUTE);//分
  1376. int second = cal.get(Calendar.SECOND);//秒
  1377. Map<String, Object> keyWordMap = new HashMap<String, Object>();
  1378. keyWordMap.put("年", String.valueOf(year));
  1379. keyWordMap.put("月", DateUtil.getFormatDate(month));
  1380. keyWordMap.put("日", DateUtil.getFormatDate(day));
  1381. keyWordMap.put("时", DateUtil.getFormatDate(hour));
  1382. keyWordMap.put("分", DateUtil.getFormatDate(minute));
  1383. keyWordMap.put("秒", DateUtil.getFormatDate(second));
  1384. keyWordMap.put("简年", String.valueOf(year).substring(2));
  1385. keyWordMap.put("大写年", toChinese(String.valueOf(year)));
  1386. keyWordMap.put("大写月", toChinese(String.valueOf(month)));
  1387. keyWordMap.put("大写日", toChinese(String.valueOf(day)));
  1388. keyWordMap.put("YEAR", String.valueOf(year));
  1389. keyWordMap.put("MONTH", String.valueOf(month));
  1390. keyWordMap.put("DAY", String.valueOf(day));
  1391. keyWordMap.put("HOUR", String.valueOf(hour));
  1392. keyWordMap.put("MINUTE", String.valueOf(minute));
  1393. keyWordMap.put("SECOND", String.valueOf(second));
  1394. keyWordMap.put("SHORTYEAR", String.valueOf(year).substring(2));
  1395. keyWordMap.put("当前登录人", SpringSecurityUtils.getCurrentUserName());
  1396. //动态表单解析
  1397. keyWordMap.put("dytable", dytableMap);
  1398. //只传入${属性}时解析
  1399. for (Object obj : entities) {
  1400. //反射取得实体类的属性及值存入keyWordMap集合中
  1401. BeanWrapperImpl wrapper = new BeanWrapperImpl(obj);
  1402. PropertyDescriptor[] descriptors = wrapper.getPropertyDescriptors();
  1403. for (PropertyDescriptor descriptor : descriptors) {
  1404. String propertyName = descriptor.getName();
  1405. System.out.println(propertyName);
  1406. keyWordMap.put(propertyName, wrapper.getPropertyValue(propertyName));
  1407. }
  1408. }
  1409. //传入${类.属性}时解析
  1410. for (Object obj : entities) {
  1411. String className = obj.getClass().getSimpleName();
  1412. String lowerCaseName = className.substring(0, 1).toLowerCase() + className.substring(1);
  1413. System.out.println("首字母小写类名:" + lowerCaseName);
  1414. keyWordMap.put(lowerCaseName, obj);
  1415. }
  1416. return keyWordMap;
  1417. }
  1418. /**
  1419. * 日期转为中文大写
  1420. */
  1421. private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  1422. public static synchronized String toChinese(String str) {
  1423. StringBuffer sb = new StringBuffer();
  1424. sb.append(getSplitDateStr(str, 0));
  1425. return sb.toString();
  1426. }
  1427. public static String getSplitDateStr(String str, int unit) {
  1428. // unit是单位 0=年 1=月 2日
  1429. String[] DateStr = str.split("-");
  1430. if (unit > DateStr.length)
  1431. unit = 0;
  1432. StringBuffer sb = new StringBuffer();
  1433. for (int i = 0; i < DateStr[unit].length(); i++) {
  1434. if ((unit == 1 || unit == 2) && Integer.valueOf(DateStr[unit]) > 9) {
  1435. sb.append(convertNum(DateStr[unit].substring(0, 1))).append("拾")
  1436. .append(convertNum(DateStr[unit].substring(1, 2)));
  1437. break;
  1438. } else {
  1439. sb.append(convertNum(DateStr[unit].substring(i, i + 1)));
  1440. }
  1441. }
  1442. if (unit == 1 || unit == 2) {
  1443. return sb.toString().replaceAll("^壹", "").replace("零", "");
  1444. }
  1445. return sb.toString();
  1446. }
  1447. private static String convertNum(String str) {
  1448. return NUMBERS[Integer.valueOf(str)];
  1449. }
  1450. public static boolean isNumeric(String str) {
  1451. Pattern pattern = Pattern.compile("[0-9]*");
  1452. Matcher isNum = pattern.matcher(str);
  1453. if (!isNum.matches()) {
  1454. return false;
  1455. }
  1456. return true;
  1457. }
  1458. /**
  1459. * 根据类的完全限定名获取表名
  1460. *
  1461. * @param classtype
  1462. * @return
  1463. */
  1464. public String getTableName(Class classtype) {
  1465. Annotation[] anno = classtype.getAnnotations();
  1466. String tableName = "";
  1467. for (int i = 0; i < anno.length; i++) {
  1468. if (anno[i] instanceof Table) {
  1469. Table table = (Table) anno[i];
  1470. System.out.println(table.name());
  1471. tableName = table.name();
  1472. }
  1473. }
  1474. return tableName;
  1475. }
  1476. /**
  1477. * 是否保存到源文档
  1478. */
  1479. @Override
  1480. public Boolean isSaveToSource(String templateId) {
  1481. PrintTemplate printTemplate = printTemplateDao.findUniqueBy("id", templateId);
  1482. Boolean isSaveSource = printTemplate.getIsSaveSource();
  1483. return isSaveSource;
  1484. }
  1485. /**
  1486. *
  1487. * 获取随机数(年月日时分秒+五位随机数)
  1488. *
  1489. * @return
  1490. */
  1491. public static String getFileName() {
  1492. Date date = new Date();
  1493. SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
  1494. int temp = (int) (Math.random() * 100000);
  1495. while (temp / 10000 < 1) {
  1496. temp = (int) (Math.random() * 100000);
  1497. }
  1498. return sf.format(date) + temp;
  1499. }
  1500. /**
  1501. *
  1502. * 获取所有可用的打印模板定义
  1503. *
  1504. * @return
  1505. */
  1506. @Override
  1507. public List<PrintTemplate> findAll() {
  1508. return printTemplateDao.getAll();
  1509. }
  1510. @Override
  1511. public void save(PrintTemplate printTemplate) {
  1512. printTemplateDao.save(printTemplate);
  1513. }
  1514. /**
  1515. *
  1516. * 批量删除
  1517. *
  1518. * (non-Javadoc)
  1519. * @see com.wellsoft.pt.basicdata.printtemplate.service.PrintTemplateService#deleteAllById(java.lang.String[])
  1520. */
  1521. @Override
  1522. public void deleteAllById(String[] ids) {
  1523. for (int i = 0; i < ids.length; i++) {
  1524. PrintTemplate printTemplate = printTemplateDao.getById(ids[i]);
  1525. printTemplateDao.delete(printTemplate);
  1526. }
  1527. }
  1528. /**
  1529. * 根据打印模板ID获取对应的模板实体对象
  1530. *
  1531. * @param printTemplateId
  1532. * @return
  1533. */
  1534. public PrintTemplate getPrintTemplateById(String printTemplateId) {
  1535. return printTemplateDao.getById(printTemplateId);
  1536. }
  1537. /**
  1538. * 如何描述该方法
  1539. *
  1540. * @param example
  1541. * @return
  1542. */
  1543. public List<PrintTemplate> findByExample(PrintTemplate printTemplate) {
  1544. return printTemplateDao.findByExample(printTemplate);
  1545. }
  1546. }