B00029Service.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package com.miniframe.bisiness.system;
  2. import com.miniframe.constant.MFConstant;
  3. import com.miniframe.core.ExecProcessFlow;
  4. import com.miniframe.core.exception.BusinessException;
  5. import com.miniframe.core.ext.UtilTools;
  6. import com.miniframe.generate.business.system.model.B00029BaseModel;
  7. import com.miniframe.model.system.SysFile;
  8. import com.miniframe.model.system.SysFileSQLBuilder;
  9. import com.miniframe.model.system.dao.SysFileMapper;
  10. import com.miniframe.tools.XIFileUtils;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.math.BigDecimal;
  16. import java.nio.file.Files;
  17. import java.nio.file.Path;
  18. import java.nio.file.Paths;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * 基础系统,“文件分片合并”逻辑处理(重新生成不覆盖)。
  23. */
  24. public class B00029Service extends B00029BaseModel implements ExecProcessFlow {
  25. private static final long serialVersionUID = -7051358269847459502L;
  26. /**
  27. * 基础系统,“文件分片合并”业务核心处理
  28. */
  29. public void transExecute() throws Exception {
  30. String bfid =this.getA_b00029().getBfid();
  31. SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
  32. SysFile bfile = sysFileDAO.selectByPrimaryKey(bfid);
  33. if(bfile==null){
  34. throw new BusinessException("EB4000002");
  35. }
  36. //本地是否存在
  37. SysFileSQLBuilder fsb = new SysFileSQLBuilder();
  38. //先使用loginName查询用户,再用手机号查询
  39. fsb.createCriteria().andParentidEqualTo(bfid);
  40. fsb.setOrderByClause("chunk");
  41. List<SysFile> sonFiles =sysFileDAO.selectByExample(fsb);
  42. if(sonFiles.isEmpty()){
  43. throw new BusinessException("EB4000003");
  44. }
  45. if(bfile.getChunks()!=sonFiles.size()){
  46. throw new BusinessException("EB4000003");
  47. }
  48. merge(bfile, sonFiles);
  49. for (SysFile tmp:sonFiles) {
  50. File file =new File(XIFileUtils.getRootPathStr()+ MFConstant.separator+tmp.getFilepath());
  51. file.delete();
  52. sysFileDAO.deleteByPrimaryKey(tmp.getId());
  53. }
  54. bfile.setNeedop("1");
  55. Path path = Paths.get(XIFileUtils.getRootPathStr() + MFConstant.separator + bfile.getFilepath());
  56. bfile.setFilesize(BigDecimal.valueOf(Files.size(path)));
  57. sysFileDAO.updateByPrimaryKey(bfile);
  58. }
  59. private void merge(SysFile bfile, List<SysFile> sonFiles) throws BusinessException, IOException {
  60. FileOutputStream outputStream = null;
  61. FileInputStream fileInputStream = null; //分片文件
  62. try {
  63. byte[] byt = new byte[10 * 1024 * 1024];
  64. int len;
  65. outputStream = new FileOutputStream(XIFileUtils.getRootPathStr()+ MFConstant.separator+ bfile.getFilepath(), true); // 文件追加写入
  66. for (SysFile tmp : sonFiles) {
  67. fileInputStream = new FileInputStream(XIFileUtils.getRootPathStr()+ MFConstant.separator+tmp.getFilepath());
  68. while ((len = fileInputStream.read(byt)) != -1) {
  69. outputStream.write(byt, 0, len);
  70. }
  71. fileInputStream.close();
  72. }
  73. outputStream.close();
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. throw new BusinessException("EB8100015");
  77. } finally {
  78. if (outputStream != null) {
  79. outputStream.close();
  80. }
  81. if (fileInputStream != null) {
  82. fileInputStream.close();
  83. }
  84. }
  85. }
  86. /**
  87. * 基础系统,“文件分片合并”业务前处理
  88. */
  89. public void preTransFlow() throws Exception {
  90. this.validater();
  91. }
  92. /**
  93. * 基础系统,“文件分片合并”业务后处理
  94. */
  95. public void afterTransFlow() throws Exception {
  96. }
  97. /**
  98. * 基础系统,“文件分片合并”逻辑入口处理方法
  99. */
  100. @SuppressWarnings("rawtypes")
  101. @Override
  102. public Map execute(Map vars) throws Exception {
  103. this.setTransMap(vars);
  104. preTransFlow();// 执行业务开始的规则检查和校验
  105. transExecute();// 执行核心业务段
  106. afterTransFlow();// 执行核心逻辑完成后的收尾逻辑
  107. return this.getTransMap();
  108. }
  109. }