package com.platform.service.impl; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Resource; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import com.platform.dao.DataInfoDao; import com.platform.dao.DataInfoMoveTmpDao; import com.platform.entities.DataInfoEntity; import com.platform.entities.DataInfoEntityMoveTmp; import com.platform.glusterfs.CheckoutMD5; import com.platform.glusterfs.CopyData; import com.platform.glusterfs.RemoveData; import com.platform.glusterfs.ShowData; import com.platform.service.IMoveDataService; import com.platform.utils.Bean2MapUtils; import com.platform.utils.Constant; import com.platform.utils.DateForm; @Component @Service(value = "moveDataService") public class MoveDataServiceImpl implements IMoveDataService{ @Resource(name = "dataInfoDao") private DataInfoDao dataInfoDao; private RemoveData removeservice = new RemoveData(); /** * 迁移 */ CopyData copy = new CopyData(); /** * MD5校验 */ CheckoutMD5 check = new CheckoutMD5(); @Resource(name = "dataInfoMoveTmpDao") private DataInfoMoveTmpDao dataInfoMoveTmpDao; /** * 查看数据 */ ShowData show = new ShowData(); @Override public boolean moveData(List data, String dstPath) throws Exception { boolean isSuccess = false; String tailPath = ""; if (null != data) { //XXX/320198_16/1,or XXX/320122KFQ_15/1 ---> /320198_16/1, or /320122KFQ_15/1 List exist = dataInfoMoveTmpDao.findAll(); List existIds = new ArrayList(); if (null != exist) { for (DataInfoEntityMoveTmp dataInfoEntityMoveTmp : exist) { if ("0".equals(dataInfoEntityMoveTmp.getCompleteStatus()) || "1".equals(dataInfoEntityMoveTmp.getCompleteStatus())) { if (null != dataInfoEntityMoveTmp.getDataPath()) { existIds.add(dataInfoEntityMoveTmp.getDataPath()); } } } } Pattern pattern = Pattern.compile("\\/\\d+[a-z]*[A-Z]*_\\d+\\/\\d*\\/*$"); // 末尾 含有 / Pattern pattern2 = Pattern.compile("\\/$"); Matcher matcher2 = pattern2.matcher(dstPath); //去掉 最后 的 / 符合 if (matcher2.find()) { dstPath = dstPath.substring(0, dstPath.length()-1); } List moveList = new ArrayList(); for (DataInfoEntity dataInfoEntity : data) { if (existIds.contains(dataInfoEntity.getDataPath())) { continue; } //TODO 正则:取出 data 的后面 的 路径,eg: XXX/320198_16/1,or XXX/320122KFQ_15/1) Matcher matcher = pattern.matcher(dataInfoEntity.getDataPath()); // tailPath 第一个字符是 / 符号取尾:"\\/\\d+[a-z]*[A-Z]*_\\d+\\/\\d*\\/*$" if (matcher.find()) { tailPath = matcher.group(); } String finalDestPath = dstPath + tailPath; Matcher matchertmp2 = pattern2.matcher(finalDestPath); //去掉 最后 的 / 符合 if (!matchertmp2.find()) { finalDestPath = finalDestPath + "/"; } DataInfoEntityMoveTmp dataMove = new DataInfoEntityMoveTmp(); dataMove.setSystemCode(dataInfoEntity.getSystemCode()); dataMove.setRegionalismCode(dataInfoEntity.getRegionalismCode()); dataMove.setDstPath(finalDestPath); dataMove.setLastTime(DateForm.date2StringBysecond(new Date())); dataMove.setFkid(dataInfoEntity.getId()); moveList.add(dataMove); } if (moveList.size() > 0) { dataInfoMoveTmpDao.insertBatch(moveList); isSuccess = true; } } return isSuccess; } @Override public List findAll() throws Exception { List result = new ArrayList(); try { result = dataInfoMoveTmpDao.findAll(); } catch (Exception e) { System.err.println(e); } return result; } @Override public int delete(DataInfoEntityMoveTmp dataMove) throws Exception { if ("1".equals(dataMove.getCompleteStatus())) { removeservice.deleteFolder(dataMove.getDstPath()); } int result = dataInfoMoveTmpDao.remove(dataMove.getId()); return result; } @Override public int save(DataInfoEntityMoveTmp data) throws Exception { dataInfoMoveTmpDao.save(data); return 0; } @Override public int insertBatch(List list) throws Exception { // TODO Auto-generated method stub return 0; } @Override public int update(DataInfoEntityMoveTmp data) throws Exception { int result = dataInfoMoveTmpDao.update(data); return result; } }