/**
 * 
 */
package com.fuzamei.util.blockchain;

import java.util.Map;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.fuzamei.http.HttpRequest;
import com.fuzamei.util.JSONUtil;
import com.fuzamei.util.ReadConfUtil;

/**
 * 
* <b>Description:区块链的操作类</b><br> 
* @Note
* <b>Author:maamin
* @version 1.0
* @Note
* <b>ProjectName:</b> ccb_fund_trusteeship
* <br><b>PackageName:</b> com.fuzamei.util.blockchain
* <br><b>ClassName:</b> BlockChainUtil
* <br><b>Date:</b> 2018年1月10日 下午4:20:50
 */
@Component
public class BlockChainUtil {
	/**
	 * 
	* <b>Description:将签名转发给区块链</b><br> 
	* @param protobufBean
	* @return
	* @Note
	* <b>Author:maamin
	* <br><b>Date:</b> 2018年1月10日 下午4:21:20
	* <br><b>Version:</b> 1.0
	 */
	public static String  sendPostParam(ProtobufBean protobufBean){
		HttpRequest httpRequest = new HttpRequest();
		String blockUrl = ReadConfUtil.getProperty("url_prefix")+"://"+ReadConfUtil.getProperty("block_ip")+":"+ReadConfUtil.getProperty("block_port");
		String result = "";	//存放最终区块链放回的结果
		String signdata = protobufBean.getSignature();
		 String[] params = new String[1];
         params[0] = signdata;
         String jsonstr = JSON.toJSONString(new RequestAccountBean("2.0", "broadcast_tx_commit", null, params));
         try {
			result =  httpRequest.sendPost(blockUrl, jsonstr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         return result;
	}
	/**
	 * 
	* <b>Description:验证区块链返回的结果是否成功，true：成功，false：失败</b><br> 
	* @param jsonData
	* @return
	* @Note
	* <b>Author:maamin
	* <br><b>Date:</b> 2018年1月10日 下午4:22:37
	* <br><b>Version:</b> 1.0
	 */
	public static boolean vilaResult(String jsonData){
		boolean flag = false;
		Map<String,Object> map = JSONUtil.jsonToMap(jsonData);	//总的map
		String result = map.get("result").toString();	//得到result节点
		Map<String,Object> resultMap = JSONUtil.jsonToMap(result);
		String check_tx = resultMap.get("check_tx").toString();
		Map<String,Object> check_txMap = JSONUtil.jsonToMap(check_tx);
		String code1 = check_txMap.get("code").toString();
		String deliver_tx = resultMap.get("deliver_tx").toString();
		Map<String,Object> deliver_txMap = JSONUtil.jsonToMap(deliver_tx);
		String code2 = deliver_txMap.get("code").toString();
		if(("0".equals(code1))&&("0".equals(code2))){	//两个都等于0才等于成功
			flag = true;
		}
		return flag;
	}
	/**
	 * 
	* @Title: sendBlockChain
	* @Description: TODO 发送签名直接转发到区块链
	* @param @param qianming
	* @param @return
	* @return boolean
	* @author: Ma Amin
	* @date: 2017-10-15 下午1:44:43
	 */
	public boolean sendBlockChain(String qianming){
		boolean flag = false;	//区块链成功返回true，失败返回false，默认返回false
		ProtobufBean protobufBean = new ProtobufBean();		//签名对象
		Map<String, Object> mma = JSONUtil.jsonToMap(qianming);
		long instructionId = Long
				.parseLong(mma.get("sid").toString());
		protobufBean.setInstructionId(instructionId);
		protobufBean.setSignature(mma.get("signdata").toString());
		String result = sendPostParam(protobufBean);
		boolean flag2 = vilaResult(result);	//区块链返回过来的结果
		if(flag2){
			flag = true;
		}
		return flag;
		
	}
	/**
	 * 
	* <b>Description:通过sign拿到protobufBean</b><br> 
	* @param sign
	* @return
	* @Note
	* <b>Author:maamin
	* <br><b>Date:</b> 2018年1月10日 下午4:19:21
	* <br><b>Version:</b> 1.0
	 */
	public static ProtobufBean getProtobufBean(String sign){
		ProtobufBean protobufBean = new ProtobufBean();
		Map<String, Object> signMap = JSONUtil.jsonToMap(sign);
		long instructionId = Long.parseLong(signMap.get("sid").toString());
		protobufBean.setInstructionId(instructionId);
		protobufBean.setSignature(signMap.get("signdata").toString());
		return protobufBean;
	}
	public static void main(String[] args) {
		
	}
}
