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.dao.VolumeDao;
import com.platform.entities.DataInfoEntity;
import com.platform.entities.DataInfoEntityMoveTmp;
import com.platform.entities.FolderNode;
import com.platform.entities.VolumeInitEntity;
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;
	
	@Resource(name = "volumeDao")
	private VolumeDao volumeDao;

	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<DataInfoEntity> data, FolderNode node)
			throws Exception {
		boolean isSuccess = false;
		if (null ==node.getName() ||"".equals(node.getName())) {
			return false;
		}
		if (null ==node.getPath() ||"".equals(node.getPath())) {
			return false;
		}
		List<VolumeInitEntity> listVolume = volumeDao.findAll();
		if (null == listVolume || listVolume.size() == 0) {
			return false;
		}
		String tailPath = "";
		if (null != data) {
			// XXX/320198_16/1,or XXX/320122KFQ_15/1 ---> /320198_16/1, or
			// /320122KFQ_15/1
			List<DataInfoEntityMoveTmp> exist = dataInfoMoveTmpDao.findAll();
			List<String> existIds = new ArrayList<String>();
			List<Integer> fkIds = new ArrayList<Integer>();
			if (null != exist) {
				for (DataInfoEntityMoveTmp dataInfoEntityMoveTmp : exist) {
					if ("0".equals(dataInfoEntityMoveTmp.getCompleteStatus())
							|| "1".equals(dataInfoEntityMoveTmp
									.getCompleteStatus())) {
						if (null != dataInfoEntityMoveTmp.getDataPath()) {
							existIds.add(dataInfoEntityMoveTmp.getDataPath());
						}
						fkIds.add(dataInfoEntityMoveTmp.getFkid());
					}
				}
			}
			Pattern pattern = Pattern
					.compile("\\/\\d+[a-z]*[A-Z]*_\\d+\\/\\d*\\/*$");
			// 末尾 含有 /
			Pattern pattern2 = Pattern.compile("\\/$");
			String dstPath = node.getPath();
			Matcher matcher2 = pattern2.matcher(dstPath);
			// 去掉 最后 的 / 符合
			if (matcher2.find()) {
				dstPath = dstPath.substring(0, dstPath.length() - 1);
			}
			List<DataInfoEntityMoveTmp> moveList = new ArrayList<DataInfoEntityMoveTmp>();
			for (DataInfoEntity dataInfoEntity : data) {
				if (existIds.contains(dataInfoEntity.getDataPath())) {
					continue;
				}
				if (dataInfoEntity.getId() == 0) {
					continue;
				}
				if (fkIds.contains(dataInfoEntity.getId())) {
					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());
				// 末尾 含有 /
				Matcher matcher3 = pattern2.matcher(node.getPath());
				// 去掉 最后 的 / 符合
				String volumePath = ""; 
				if (!matcher3.find()) {
					volumePath = node.getPath()+"/";
				}
				else {
					volumePath = node.getPath();
				}
				for ( VolumeInitEntity ve : listVolume) {
					if (volumePath.contains(ve.getPath())) {
						dataMove.setDstVolumePath(ve.getPath());
						dataMove.setDstVolumeIp(ve.getIp());
						break;
					}
				}
				moveList.add(dataMove);
			}
			if (moveList.size() > 0) {
				dataInfoMoveTmpDao.insertBatch(moveList);
//				for (DataInfoEntityMoveTmp dataInfoEntityMoveTmp2 : moveList) {
//					dataInfoMoveTmpDao.save(dataInfoEntityMoveTmp2);
//				}
				isSuccess = true;
			}
		}
		return isSuccess;
	}

	@Override
	public List<DataInfoEntityMoveTmp> findAll() throws Exception {
		List<DataInfoEntityMoveTmp> result = new ArrayList<DataInfoEntityMoveTmp>();
		try {
			result = dataInfoMoveTmpDao.findAll();

		} catch (Exception e) {
			System.err.println(e);
		}
		return result;
	}

	@Override
	public int delete(DataInfoEntityMoveTmp dataMove) throws Exception {
		List<DataInfoEntityMoveTmp> list = dataInfoMoveTmpDao.findAll();
		if (null != list) {
			for (DataInfoEntityMoveTmp dataInfoEntityMoveTmp : list) {
				if (dataMove.getId() == dataInfoEntityMoveTmp.getId()) {
					dataMove = dataInfoEntityMoveTmp;
					break;
				}
			}
			
		}
		if ("3".equals(dataMove.getCompleteStatus())) {
			removeservice.deleteFolder(dataMove.getDstPath());
		}
		int result = 0;
		if (!"1".equals(dataMove.getCompleteStatus())) {
			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<DataInfoEntityMoveTmp> 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;
	}

}
