Demo entry 6741879

cbq

   

Submitted by anonymous on May 16, 2018 at 17:49
Language: Go. Code size: 20.6 kB.

package main 

import (
	"fmt"
	"github.com/hyperledger/fabric/common/flogging"
	"github.com/hyperledger/fabric/core/chaincode/shim"
	pb "github.com/hyperledger/fabric/protos/peer"
	"encoding.json"
)

// logger
var chaincodeLogger = flogging.MustGetLogger("ChainnovaChaincode")

/*****************定义复合键search表的映射名*****************/
const IndexName = "username~landid"
const HostIndexName ="Host~deal.Product+deal.Landid"
const FarmerIndexName = "farmerid~order.Product+order.Landid"
const PlatIndexName = "landid~username"
/*********************end**********************************/

/*****************定义结构体*********************/
//平台发布信息
type Deal struct {
	FarmerID string `json:farmerID` //农民账户
	Product  string `json:product`  //农产品类型
	Landid string `json:landid` //土地使用权id
	eatid string `json:eatid`//食用合格证id
	Prize string `json:prize` //单价
	Proarea  string `json:proarea`  //产地
	Reserve  string `json:reserve`  //库存量
}
//农民发布预约交易
type Order struct{
	FarmerID string `json:farmerID` //农民账户
	Product string `json:product`  //农产品类型
	Landid string `json:landid` //土地使用权id
	ForeReserve string `json:foreReserve`//预估产量
	Date string `json:date` //生产周期
	Proarea string `json:proarea` //产地
	Prize string `json:prize`//预期单价
}
//消费者购买信息
type Purchase struct{
	Username string `json:username`//顾客账户id
	FarmerID string `json:farmerID` //农民账户
	Product  string `json:product`  //农产品类型
	Landid string `json:landid` //土地使用权id
	Proarea  string `json:proarea`  //产地
	Number string `json:number` //购买数量
	State string `json:state` //订单状态:已付款、未付款、退货中、已退款
}

/*********************end***********************/

/*****************Define chaincode**************/
type DealChaincode struct {
}
/*********************end***********************/

// chaincode Init 接口
func (a *DealChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
	return shim.Success(nil)
}

// chaincode Invoke 接口
func (a *DealChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	function,args := stub.GetFunctionAndParameters()//参数必须要解析后才可使用
	chaincodeLogger.Info("%s%s","ChainnovaChaincode function=",function)
	chaincodeLogger.Info("%s%s","ChainnovaChaincode args=",args)

 	if function == "issueSpotGoods" {
		return a.issueinvode(stub, args)
   	} else if function == "queryMyTech" {
		return a.endorse(stub, args)
	} else if function == "changeMyTech" {
	        	return a.accept(stub, args)
	} else if function == "deleteMyTech" {
		return a.reject(stub, args)
	}

	res := getResString(1,"ChainnovaChaincode Unkown method!") //交易失败
	chaincodeLogger.Info("%s",res)
	chaincodeLogger.Infof("%s",res)
	return shim.Error(res)
}

/**************************************************************************
*函数名:issueSpotGoods
*功能:平台发布现货交易单
*输入参数:农民id、农产品类型、土地使用id、食用合格证id、单价、产地、库存量
*存储到堆栈的格式:以农产品类型和土地使用id作为主键,结构体Deal的json格式作为值
*返回参数:发布成功信息
***************************************************************************/
func (a *DealChaincode) issueSpotGoods(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 7{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=5")
		return shim.Error(res)
	}

	var deal Deal
	deal.FarmerID=args[0]
	deal.Product=args[1]
	deal.LandID=args[2]
	deal.eatid=args[3]
	deal.Prize=args[4]
	deal.Proarea=args[5]
	deal.Reserve=args[6]

	var b []byte
	b,err := json.Marshal(deal)
	err = stub.PutState(deal.Product+deal.Landid, b)
	if err != nil {
		return shim.Error(err.Error())
	}

	indexKey,err := stub.CreateCompositeKey(HostIndexName,[]string{Host_,deal.Product+deal.Landid})
	value := []byte{0x00}
	err1 = stub.PutState(indexKey,value)
	if err1 != nil {
		return shim.Error(err1.Error())
	}
	return shim.Success([]byte{"issue success!"})
}

/***************************************************************************
*函数名:purchaseSpotGoods
*功能:消费者购买农产品
*输入参数:顾客账户id、农民账户、农产品类型、土地使用权id、产地、购买数量、状态
*存储到堆栈的格式:以顾客账户id+土地使用权id作为主键,结构体Purchase的json格式作为值
*返回参数:交易成功信息
***************************************************************************/
func (a *DealChaincode) purchaseSpotGoods(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 7{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=7")
		return shim.Error(res)
	}

	var purc Purchase
	purc.Username = args[0]
	purc.FarmerID = args[1]
	purc.Product = args[2]
	purc.Landid = args[3]
	purc.Proarea = args[4]
	purc.Number  = args[5]
	purc.State = args[6]

	var b []byte
	b,err := json.Marshal(purc)
	err = stub.PutState(purc.Username+purc.Landid, b)
	if err != nil {
		return shim.Error(err.Error())
	}

	
	indexKey,err := stub.CreateCompositeKey(IndexName,[]string{purc.Username,purc.Landid})
	value := []byte{0x00}
	err1 = stub.PutState(indexKey,value)
	if err1 != nil {
		return shim.Error(err1.Error())
	}

	indexKey1,err := stub.CreateCompositeKey(PlatIndexName,[]string{purc.Landid,purc.Username})
	err2 = stub.PutState(indexKey1,value)
	if err2 != nil {
		return shim.Error(err1.Error())
	}

	return shim.Success([]byte{"purchase success!"})
}

/*************************************************************************
*函数名:purchaseOrderGoods
*功能:消费者预定农产品
*输入参数:顾客账户id、农民账户、农产品类型、土地使用权id、产地、预定数量、状态
*存储到堆栈的格式:以顾客账户id+土地使用权id作为主键,结构体Purchase的json格式作为值
*返回参数:交易成功信息
***************************************************************************/
func (a *DealChaincode) purchaseOrderGoods(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 6{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=5")
		return shim.Error(res)
	}

	var purc Purchase
	purc.Username = args[0]
	purc.FarmerID = args[1]
	purc.Product = args[2]
	purc.Landid = args[3]
	purc.Proarea = args[4]
	purc.Number  = args[5]
	purc.State = args[6]

	var b []byte
	b,err := json.Marshal(purc)
	err = stub.PutState(purc.Username+purc.Landid, b)
	if err != nil {
		return shim.Error(err.Error())
	}

	indexKey,err := stub.CreateCompositeKey(IndexName,[]string{purc.Username,purc.Landid})
	value := []byte{0x00}
	err1 = stub.PutState(indexKey,value)
	if err1 != nil {
		return shim.Error(err1.Error())
	}

	return shim.Success([]byte{"purchase success!"})
}
/***************************************************************************
*函数名:issueOrderGoods
*功能:农民发布预约交易
*输入参数:农民id、农产品类型、土地使用id、预估产量、生产周期、产地、预期单价
*存储到堆栈的格式:以农产品类型和土地使用id作为主键,结构体Order的json格式作为值
*返回参数:发布成功信息
***************************************************************************/
func (a *DealChaincode) issueOrderGoods(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 7{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=7")
		return shim.Error(res)
	}

	var order Order
	order.FarmerID=args[0]
	order.Product=args[1]
	order.Landid=args[2]
	order.ForeReserve=args[3]
	order.Date=args[4]
	order.Proarea=args[5]
	order.Prize=args[6]

	var b []byte
	b,err := json.Marshal(order)
	err = stub.PutState(order.Product+order.Landid, b)
	if err != nil {
		return shim.Error(err.Error())
	}

	indexKey,err := stub.CreateCompositeKey(FarmerIndexName,[]string{order.FarmerID,order.Product+order.Landid})
	value := []byte{0x00}
	err1 = stub.PutState(indexKey,value)
	if err1 != nil {
		return shim.Error(err1.Error())
	}
	return shim.Success([]byte{"issue success!"})
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*订单界面基础显示*/
/*************************************************************************************
*函数名:queryDeals
*功能:平台显示发布记录
*输入参数:nil
*返回参数:Deal结构体的json数组
*************************************************************************************/
func (a *DealChaincode) queryDeals(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 1{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=1")
		return shim.Error(res)
	}

	//批量查询
	hostIterator, err := stub.GetStateByPartialCompositeKey(HostIndexName, []string{Host_})
	if err != nil {
		res := getResString(1,"ChainnovaChaincode queryMyBill get tech list error")
		return shim.Error(res)
	}
	defer hostIterator.Close()

	var hostList = []Deal{}

	//开始遍历查询出所有发布信息
	for hostIterator.HasNext() { 
		kv, _ := hostIterator.Next()
		// 从查询表里取得满足条件的学生学号
		_, compositeKeyParts, err := stub.SplitCompositeKey(kv.Key)//将组合键分开成一个字符串数组,长度为2
		if err != nil {
			res := getResString(1,"ChainnovaChaincode queryMyBill SplitCompositeKey error")
			return shim.Error(res)
		}
		//根据学号取得交易单信息
		hostbill, bl := a.getTech(stub,compositeKeyParts[1])
		if !bl {
			res := getRetString(1,"ChainnovaChaincode queryMyBill get bill error")
			return shim.Error(res)
		}
		hostList = append(hostList,hostbill)
		}
	// 返回交易信息给用户
	b, err := json.Marshal(hostList)
	if err != nil {
		res := getRetString(1,"ChainnovaChaincode Marshal queryMyBill billList error")
		return shim.Error(res)
	}
	return shim.Success(b)
}

/**************************************************************************************
*函数名:queryUserBuy
*功能:用户显示购买的历史记录
*输入参数:顾客账户id
*返回参数:Purchase结构体的json数组
**************************************************************************************/
func (a *DealChaincode) queryUserBuy(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 1{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=1")
		return shim.Error(res)
	}

	//批量查询
	userIterator, err := stub.GetStateByPartialCompositeKey(IndexName, []string{arg[0]})
	if err != nil {
		res := getResString(1,"ChainnovaChaincode queryMyBill get tech list error")
		return shim.Error(res)
	}
	defer userIterator.Close()

	var billList = []Purchase{}

	
	for userIterator.HasNext() { 
		kv, _ := userIterator.Next()
		
		_, compositeKeyParts, err := stub.SplitCompositeKey(kv.Key)//将组合键分开成一个字符串数组,长度为2
		if err != nil {
			res := getResString(1,"ChainnovaChaincode queryMyBill SplitCompositeKey error")
			return shim.Error(res)
		}
		
		userbill, bl := a.getTech(stub, compositeKeyParts[0]+compositeKeyParts[1])
		if !bl {
			res := getRetString(1,"ChainnovaChaincode queryMyBill get bill error")
			return shim.Error(res)
		}
		billList = append(billList,userbill)
		}
	// 返回交易信息给用户
	b, err := json.Marshal(billList)
	if err != nil {
		res := getRetString(1,"ChainnovaChaincode Marshal queryMyBill billList error")
		return shim.Error(res)
	}
	return shim.Success(b)
}

/*****************************************************************************************
*功能:农民显示发布的预订交易单的记录
*输入参数为农民id
*返回参数为Order结构体的json数组
******************************************************************************************/
func (a *DealChaincode) queryFarmerOrder(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 1{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=1")
		return shim.Error(res)
	}

	//批量查询
	farmerIterator, err := stub.GetStateByPartialCompositeKey(FarmerIndexName, []string{arg[0]})
	if err != nil {
		res := getResString(1,"ChainnovaChaincode queryMyBill get tech list error")
		return shim.Error(res)
	}
	defer farmerIterator.Close()

	var ordersList = []Order{}

	
	for farmerIterator.HasNext() { 
		kv, _ := farmerIterator.Next()
		
		_, compositeKeyParts, err := stub.SplitCompositeKey(kv.Key)//将组合键分开成一个字符串数组,长度为3
		if err != nil {
			res := getResString(1,"ChainnovaChaincode queryMyBill SplitCompositeKey error")
			return shim.Error(res)
		}
		
		orderbill, bl := a.getTech(stub, compositeKeyParts[0]+compositeKeyParts[1])
		if !bl {
			res := getRetString(1,"ChainnovaChaincode queryMyBill get bill error")
			return shim.Error(res)
		}
		billList = append(ordersList,orderbill)
		}
	// 返回交易信息给用户
	b, err := json.Marshal(ordersList)
	if err != nil {
		res := getRetString(1,"ChainnovaChaincode Marshal queryMyBill billList error")
		return shim.Error(res)
	}
	return shim.Success(b)
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*主界面查询操作*/
/******************************************************************************************
*功能:用户查询需要的农产品
*输入参数为农产品类型
*返回参数为Deal或Order结构体的json数组
******************************************************************************************/
func (a *SimpleChaincode) queryDealByProduct(stub shim.ChaincodeStubInterface, args []string) pb.Response{
 	if len(args) != 1{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=1")
		return shim.Error(res)
	}

	product := args[0]
  	queryString := fmt.Sprintf("{\"selector\":{\"Product\":\"%s\"}}", product)
  	resultsIterator,err:= stub.GetQueryResult(queryString)//必须是CouchDB才行
 	if err!=nil{
    		return shim.Error("Rich query failed")
 	 }
 	 deals,err:=getListResult(resultsIterator)
  	if err!=nil{
    		  return shim.Error("Rich query failed")
 	 }
  	return shim.Success(deals)
}

/*****************************************************************************************
*功能:用户查询需要的农产品
*输入参数为产地
*返回参数为Deal或Order结构体的json数组
*****************************************************************************************/
func (a *SimpleChaincode) queryDealByProarea(stub shim.ChaincodeStubInterface, args []string) pb.Response{
 	if len(args) != 1{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=1")
		return shim.Error(res)
	}

	proarea := args[0]
  	queryString := fmt.Sprintf("{\"selector\":{\"Proarea\":\"%s\"}}", proarea)
  	resultsIterator,err:= stub.GetQueryResult(queryString)//必须是CouchDB才行
 	if err!=nil{
    		return shim.Error("Rich query failed")
 	 }
 	 deals,err:=getListResult(resultsIterator)
  	if err!=nil{
    		  return shim.Error("Rich query failed")
 	 }
  	return shim.Success(deals)
}

/*平台订单界面操作*/
/*****************************************************************************************
*功能:平台查询已有订单
*输入参数:土地使用id
*返回参数为Purchase结构体的json数组
******************************************************************************************/
func (a *DealChaincode) queryExistDeal(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 1{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=1")
		return shim.Error(res)
	}

	//批量查询
	userIterator, err := stub.GetStateByPartialCompositeKey(PlatIndexName, []string{arg[0]})
	if err != nil {
		res := getResString(1,"ChainnovaChaincode queryMyBill get tech list error")
		return shim.Error(res)
	}
	defer userIterator.Close()

	var billList = []Purchase{}

	
	for userIterator.HasNext() { 
		kv, _ := userIterator.Next()
		
		_, compositeKeyParts, err := stub.SplitCompositeKey(kv.Key)//将组合键分开成一个字符串数组,长度为2
		if err != nil {
			res := getResString(1,"ChainnovaChaincode queryMyBill SplitCompositeKey error")
			return shim.Error(res)
		}
		
		userbill, bl := a.getTech(stub, compositeKeyParts[1]+compositeKeyParts[0])
		if !bl {
			res := getRetString(1,"ChainnovaChaincode queryMyBill get bill error")
			return shim.Error(res)
		}
		billList = append(billList,userbill)
		}
	// 返回交易信息给用户
	b, err := json.Marshal(billList)
	if err != nil {
		res := getRetString(1,"ChainnovaChaincode Marshal queryMyBill billList error")
		return shim.Error(res)
	}
	return shim.Success(b)
}

/*****************************************************************************************
*功能:平台取消订单
*输入参数:农产品类型和土地使用id
*返回参数为Purchase结构体的json数组
******************************************************************************************/
func (a *TechChaincode) deletePlatDeal(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args)!=2 {
		res := getRetString(1,"ChainnovaChaincode deletePlatDeal args!=1")
		return shim.Error(res)
	}
	
	err != stub.DelState(args[0]+args[1])
	if err != nil {
		res := getResString(1,"ChainnovaChaincode deletePlatDeal SplitCompositeKey error")
			return shim.Error(res)
	}

	return shim.Success([]byte{"delete accept success"})
}

/*农民订单界面操作*/
/*****************************************************************************************
*功能:农民查询已有订单
*输入参数:土地使用id
*返回参数为Purchase结构体的json数组
******************************************************************************************/
func (a *DealChaincode) queryExistOrder(stub shim.ChaincodeStubInterface,args []string) pb.Response{
	if len(args) != 1{
		res := getResString(1,"ChainnovaChaincode Invoke issue args!=1")
		return shim.Error(res)
	}

	//批量查询
	userIterator, err := stub.GetStateByPartialCompositeKey(PlatIndexName, []string{arg[0]})
	if err != nil {
		res := getResString(1,"ChainnovaChaincode queryMyBill get tech list error")
		return shim.Error(res)
	}
	defer userIterator.Close()

	var billList = []Purchase{}

	
	for userIterator.HasNext() { 
		kv, _ := userIterator.Next()
		
		_, compositeKeyParts, err := stub.SplitCompositeKey(kv.Key)//将组合键分开成一个字符串数组,长度为2
		if err != nil {
			res := getResString(1,"ChainnovaChaincode queryMyBill SplitCompositeKey error")
			return shim.Error(res)
		}
		
		userbill, bl := a.getTech(stub, compositeKeyParts[1]+compositeKeyParts[0])
		if !bl {
			res := getRetString(1,"ChainnovaChaincode queryMyBill get bill error")
			return shim.Error(res)
		}
		billList = append(billList,userbill)
		}
	// 返回交易信息给用户
	b, err := json.Marshal(billList)
	if err != nil {
		res := getRetString(1,"ChainnovaChaincode Marshal queryMyBill billList error")
		return shim.Error(res)
	}
	return shim.Success(b)
}

/*****************************************************************************************
*功能:农民取消订单
*输入参数:农产品类型和土地使用id
*返回参数为Purchase结构体的json数组
******************************************************************************************/
func (a *DealChaincode) deleteFarmerOrder(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args)!=2 {
		res := getRetString(1,"ChainnovaChaincode deletePlatDeal args!=1")
		return shim.Error(res)
	}
	
	err != stub.DelState(args[0]+args[1])
	if err != nil {
		res := getResString(1,"ChainnovaChaincode deletePlatDeal SplitCompositeKey error")
			return shim.Error(res)
	}

	return shim.Success([]byte{"delete accept success"})
}

/*用户订单界面操作*/
/*****************************************************************************************
*功能:确认收货
*输入参数:顾客账户id和土地使用权id
*返回参数为Purchase结构体的json数组
******************************************************************************************/
func (a *DealChaincode) changeState(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args)!=2 {
		res := getRetString(1,"ChainnovaChaincode queryMyBill args!=2")
		return shim.Error(res)
	}

	key := args[0]+args[1]
	dbPurchaseBytes,err:= stub.GetState(key)
	var purchase Purchase;
	err=json.Unmarshal(dbPurchaseBytes,&purchase)//反序列化
	if err != nil {
		return shim.Error("{\"Error\":\"Failed to decode JSON of: " + string(dbPurchaseBytes)+ "\" to Purchase}")
	}
	
	purchase.State = string("receives")

	var b []byte
	b,err := json.Marshal(purchase)
	err = stub.PutState(purchase.Username+purchase.Landid, b)
	if err != nil {
		return shim.Error(err.Error())
	}

	return shim.Success(b)
}

/*****************************************************************************************
*功能:申请退货
*输入参数:顾客账户id和土地使用权id
*返回参数为Purchase结构体的json数组
******************************************************************************************/
func (a *DealChaincode) changeState(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args)!=2 {
		res := getRetString(1,"ChainnovaChaincode queryMyBill args!=2")
		return shim.Error(res)
	}

	key := args[0]+args[1]
	dbPurchaseBytes,err:= stub.GetState(key)
	var purchase Purchase;
	err=json.Unmarshal(dbPurchaseBytes,&purchase)//反序列化
	if err != nil {
		return shim.Error("{\"Error\":\"Failed to decode JSON of: " + string(dbPurchaseBytes)+ "\" to Purchase}")
	}
	
	purchase.State = string("cancelling goods")

	var b []byte
	b,err := json.Marshal(purchase)
	err = stub.PutState(purchase.Username+purchase.Landid, b)
	if err != nil {
		return shim.Error(err.Error())
	}

	return shim.Success(b)
}

func main() {
    if err := shim.Start(new(DealChaincode)); err != nil {
        fmt.Printf("Error starting DealChaincode: %s", err)
    }
}

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).