package com.platform.http.gfs;

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



import net.sf.json.JSONArray;

import org.apache.log4j.Logger;

import com.base.MyException;
import com.base.PostData;
import com.base.TaskOperateData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.http.HttpUtils;
import com.platform.http.VolumeEntity;
import com.platform.utils.DateForm;


/**
 * <一句话功能简述> 复制数据
 * <功能详细描述>
 * @author    chen
 * @version   [版本号，2016年9月8日]
 * @see       [相关类/方法]
 * @since     [产品/模块版本]
 */

public class CopyData {
	public static Logger log = Logger.getLogger(CopyData.class);
	HttpUtils ht = new HttpUtils();
	
	/**
	 * 将sourceFolderName拷贝到destFolderName
	 * 如果拷贝正常返回1，如果sourceFolderName不存在返回-2 ，如果destFolderName不存在返回-3
	 * @param sourceFolderName
	 * @param destFolderName
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public int copyFolder(String sourceFolderName, String destFolderName)  throws Exception{
		int reslut = 0;
		log.info(" copyFolder     " + sourceFolderName + " --> " + destFolderName);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sourcePath", sourceFolderName);
		map.put("destPath", destFolderName);
		//请求web
		String rest = ht.sendPost("data/copyData", map);
		if (null == rest || "".equals(rest)) {
			log.error(" ---    gluster is disconnect !   \n");
			return 0;
		}
		Gson gs = new Gson();
		PostData data = gs.fromJson(rest, PostData.class);
		// 1 : 成功
		double copyResult = (double) data.getData();
		reslut = (int) copyResult;
		getExcept(data);
		return reslut;
	}
	
	
	/**
	 * 将sourceFolderName拷贝到destFolderName
	 * 如果拷贝正常返回1，如果sourceFolderName不存在返回-2 ，如果destFolderName不存在返回-3
	 * @param sourceFolderName
	 * @param destFolderName
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	public List<TaskOperateData> operationTask()  throws Exception{
		List<TaskOperateData> li = new ArrayList<TaskOperateData>();
		Map<String, Object> map = new HashMap<String, Object>();
		Calendar c2 = Calendar.getInstance();
		map.put("endTime", DateForm.date2StringBysecond(c2.getTime()));
		 // 时间设置为 10天 前的时间
		c2.set(Calendar.DAY_OF_YEAR, c2.get(Calendar.DAY_OF_YEAR) -10);
		String time = DateForm.date2StringBysecond(c2.getTime());
		map.put("startTime", time);
		List<Integer> typeList = new ArrayList<Integer>();
		typeList.add(0);
		typeList.add(1);
		typeList.add(2);
		typeList.add(3);
		typeList.add(-1);
		typeList.add(-2);
		map.put("statusList", typeList);
		
		//请求web
		String rest = ht.sendPost("data/getOperateTasks", map);
		if (null == rest || "".equals(rest)) {
			log.error(" ---    gluster is disconnect !   \n");
			return li;
		}
		Gson gs = new Gson();
		PostData data = gs.fromJson(rest, (new PostData(new ArrayList<TaskOperateData>()).getClass()));
		li = (List<TaskOperateData>) data.getData();
		//解析 PostData
		if(li.size() > 0){
			JSONArray jsonarr = JSONArray.fromObject(data.getData());
			try {
				li = gs.fromJson(jsonarr.toString(), new TypeToken<List<TaskOperateData>>(){}.getType());
			} catch (Exception e) {
				log.error(e);
			}
		}
		getExcept(data);
		return li;
	}
	
	/** 记录异常信息
	 * @param pd
	 */
	private void getExcept(PostData pd) {
		StringBuffer sb = new StringBuffer();
		for ( MyException object : pd.getExceptionsStack()) {
			sb.append(object.getMess()).append(",");
		}
		if (!"".equals(sb.toString())) {
			log.error(sb.toString());
		}
	}
}


