package com.miniframe.bisiness.system; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigDecimal; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Map; import com.miniframe.constant.MFConstant; import com.miniframe.core.ExecProcessFlow; import com.miniframe.core.exception.BusinessException; import com.miniframe.core.ext.UtilTools; import com.miniframe.generate.appcode.UserState; import com.miniframe.generate.business.system.model.B00029BaseModel; import com.miniframe.model.system.SysFile; import com.miniframe.model.system.SysFileSQLBuilder; import com.miniframe.model.system.SysUser; import com.miniframe.model.system.SysUserSQLBuilder; import com.miniframe.model.system.dao.SysFileMapper; import com.miniframe.tools.XIFileUtils; /** * 基础系统,“文件分片合并”逻辑处理(重新生成不覆盖)。 */ public class B00029Service extends B00029BaseModel implements ExecProcessFlow { private static final long serialVersionUID = -7051358269847459502L; /** * 基础系统,“文件分片合并”业务核心处理 */ public void transExecute() throws Exception { String bfid =this.getA_b00029().getBfid(); SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class); SysFile bfile = sysFileDAO.selectByPrimaryKey(bfid); if(bfile==null){ throw new BusinessException("EB4000002"); } //本地是否存在 SysFileSQLBuilder fsb = new SysFileSQLBuilder(); //先使用loginName查询用户,再用手机号查询 fsb.createCriteria().andParentidEqualTo(bfid); fsb.setOrderByClause("chunk"); List sonFiles =sysFileDAO.selectByExample(fsb); if(sonFiles.isEmpty()){ throw new BusinessException("EB4000003"); } if(bfile.getChunks()!=sonFiles.size()){ throw new BusinessException("EB4000003"); } merge(bfile, sonFiles); for (SysFile tmp:sonFiles) { File file =new File(XIFileUtils.getRootPathStr()+ MFConstant.separator+tmp.getFilepath()); file.delete(); sysFileDAO.deleteByPrimaryKey(tmp.getId()); } bfile.setNeedop("1"); Path path = Paths.get(XIFileUtils.getRootPathStr() + MFConstant.separator + bfile.getFilepath()); bfile.setFilesize(BigDecimal.valueOf(Files.size(path))); sysFileDAO.updateByPrimaryKey(bfile); } private void merge(SysFile bfile, List sonFiles) throws BusinessException, IOException { FileOutputStream outputStream = null; FileInputStream fileInputStream = null; //分片文件 try { byte[] byt = new byte[10 * 1024 * 1024]; int len; outputStream = new FileOutputStream(XIFileUtils.getRootPathStr()+ MFConstant.separator+ bfile.getFilepath(), true); // 文件追加写入 for (SysFile tmp : sonFiles) { fileInputStream = new FileInputStream(XIFileUtils.getRootPathStr()+ MFConstant.separator+tmp.getFilepath()); while ((len = fileInputStream.read(byt)) != -1) { outputStream.write(byt, 0, len); } fileInputStream.close(); } outputStream.close(); } catch (Exception e) { e.printStackTrace(); throw new BusinessException("EB8100015"); } finally { if (outputStream != null) { outputStream.close(); } if (fileInputStream != null) { fileInputStream.close(); } } } /** * 基础系统,“文件分片合并”业务前处理 */ public void preTransFlow() throws Exception { this.validater(); } /** * 基础系统,“文件分片合并”业务后处理 */ public void afterTransFlow() throws Exception { } /** * 基础系统,“文件分片合并”逻辑入口处理方法 */ @SuppressWarnings("rawtypes") @Override public Map execute(Map vars) throws Exception { this.setTransMap(vars); preTransFlow();// 执行业务开始的规则检查和校验 transExecute();// 执行核心业务段 afterTransFlow();// 执行核心逻辑完成后的收尾逻辑 return this.getTransMap(); } }