
/**
 * 文件名   :   GfsServiceImpl.java
 * 版权       :   <版权/公司名>
 * 描述       :   <描述>
 * @author  chen
 * 版本       :   <版本>
 * 修改时间：      2016年9月8日
 * 修改内容：      <修改内容>
 */
package com.platform.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.platform.entities.Brick;
import com.platform.entities.FolderNode;
import com.platform.entities.VolumeEntity;
import com.platform.glusterfs.CopyData;
import com.platform.glusterfs.GetTreeData;
import com.platform.glusterfs.VolumeInfo;
import com.platform.service.IGfsService;
import com.platform.utils.CacheTreeData;
import com.platform.utils.ThreadVolume;
import com.platform.utils.getTreeDataByPath;

/**
 * <一句话功能简述> gfs功能实现类
 * <功能详细描述>
 * @author    chen
 * @version   [版本号，2016年9月8日]
 * @see       [相关类/方法]
 * @since     [产品/模块版本]
 */
@Service(value = "gfsService")
public class GfsServiceImpl implements IGfsService {
	
	/** gfs目录树形查询  */
	private getTreeDataByPath getFolder = new getTreeDataByPath();

	/** 数据迁移实现  */
	private CopyData copydata = new CopyData();
	
	/**  Volume信息查询  */
	private VolumeInfo volumeInfo = new VolumeInfo();

	@Override
	public FolderNode getFolder(String path) {
		FolderNode result = getFolder.findByPath(path);
		return result;
	}

	@Override
	public int copyFolder(List<String> srcFolders, String dstFolder, String name)
			throws Exception {
		int status = 0 ;
		if (null != srcFolders) {
			
			for (String string : srcFolders) {
				status	= copydata.copyFolderFilesAnyway(string, dstFolder, name);
			}
		}
		return status;
	}

	/* (non-Javadoc)
	 * @see com.platform.service.IGfsService#getAllVolume()
	 */
	@Override
	public List<VolumeEntity> getAllVolumes() throws Exception {
		List<VolumeEntity> volumeList = CacheTreeData.getVolumeList();
		if (null == volumeList) {
			return new ArrayList<VolumeEntity>();
		}
		for (VolumeEntity volume : volumeList) {
//			VolumeEntity volume = new VolumeEntity();
//			volume.setName(volumeName);
//			List<String> path = volumeInfo.getVolumeMountPoint(volumeName);
//			//默认加载第一个路径
//			if (null != path && path.size() > 0) {
//				volume.setPath(path.get(0));
//			}
//			volume.setAllSize(volumeInfo.getVolumeAllSize(volumeName));
//			volume.setUsedSize(volumeInfo.getVolumeUseSize(volumeName));
//			//TODO 查询brick--
//			//返回     ip:path
//			List<String> brickStrs = volumeInfo.getVolumeBricks(volumeName);
//			//brick已用大小：
//			Map<String, Double> usedSize = volumeInfo.getVolumebricksDataSize(volumeName);
//			Map<String, Double> availableSize = volumeInfo.getVolumebricksAvailableSize(volumeName);
//			
//			
//			List<Brick> brickList = new ArrayList<Brick>();
//			for (String brickIpPath : brickStrs) {
//				Brick b = new Brick();
//				String ipAndpath[] = brickIpPath.split(":");
//				String brickip = ipAndpath[0];
//				String brickpath = ipAndpath[1];
//				//iP ,  path ,  
//				b.setIp(brickip);
//				b.setPath(brickpath);
//				b.setAvailableSize(availableSize.get(brickIpPath));
//				b.setUsedSize(usedSize.get(brickIpPath));
//				brickList.add(b);
//			}
//			volume.setBrick(brickList);
			if (null != volume.getPath()) {
//			获得   folder      目录
				List<FolderNode> list = new ArrayList<FolderNode>();
					FolderNode currNode = getFolder(volume.getPath());
					if (null != currNode && null != currNode.getChildNodes()) {
						list.addAll(currNode.getChildNodes());
					}
				volume.setFolder(list);
			}
		}
		return volumeList;
	}

	@Override
	public VolumeEntity getOneVolume(String volumeName) throws Exception {
		VolumeEntity volume = new VolumeEntity();
		volume.setName(volumeName);
		List<String> path = volumeInfo.getVolumeMountPoint(volumeName);
		if (null != path && path.size() > 0) {
			volume.setPath(path.get(0));
		}
		volume.setAllSize((double) volumeInfo.getVolumeAvailableSize(volumeName));
		volume.setUsedSize((double) volumeInfo.getVolumeUseSize(volumeName));
//		volume.setBrick(brick);
		if (null != volume.getPath()) {
			List<FolderNode> list = new ArrayList<FolderNode>();
			if (null != path && path.size() > 0) {
				FolderNode currNode = getFolder(path.get(0));
				if (null != currNode && null != currNode.getChildNodes()) {
					list.addAll(currNode.getChildNodes());
				}
			}
			volume.setFolder(list);
		}
		return volume;
	}

	@Override
	public int moveData(String volumeName, String srcPath, String dstPath)
			throws Exception {
		
		int result = copydata.copyFolderFilesAnyway(srcPath, dstPath, "app");
		return result;
	}

	@Override
	public int deleteData(String volumeName, String srcPath) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

}
