up_chain.go 1.9 KB
package domain

import "time"

// 上链数据
type UpChain struct {
	// 上链数据唯一标识
	UpChainId int64 `json:"upChainId,string"`
	// 数据来源 例如:app.model
	Source string `json:"source"`
	// 来源数据唯一ID
	PrimaryId string `json:"primaryId"`
	// 溯源ID 标记同一个系列的数据;例如订单相关事件
	IssueId string `json:"issueId"`
	// 数据体
	Data string `json:"data"`
	// 数据块hash
	Hash string `json:"hash"`
	// 上链状态 1:成功 2:失败
	UpChainStatus int `json:"upChainStatus"`
	// 创建时间
	CreatedAt time.Time `json:"createdAt"`
}

type UpChainRepository interface {
	Save(upChain *UpChain) (*UpChain, error)
	Remove(upChain *UpChain) (*UpChain, error)
	FindOne(queryOptions map[string]interface{}) (*UpChain, error)
	Find(queryOptions map[string]interface{}) (int64, []*UpChain, error)
}

func (upChain *UpChain) Identify() interface{} {
	if upChain.UpChainId == 0 {
		return nil
	}
	return upChain.UpChainId
}

func (upChain *UpChain) Update(data map[string]interface{}) error {
	//if source, ok := data["source"]; ok {
	//	upChain.Source = source.(string)
	//}
	//if primaryId, ok := data["primaryId"]; ok {
	//	upChain.PrimaryId = primaryId.(string)
	//}
	//if issueId, ok := data["issueId"]; ok {
	//	upChain.IssueId = issueId.(string)
	//}
	//if data, ok := data["data"]; ok {
	//	upChain.Data = data.(string)
	//}
	if hash, ok := data["hash"]; ok {
		upChain.Hash = hash.(string)
	}
	if upChainStatus, ok := data["upChainStatus"]; ok {
		upChain.UpChainStatus = upChainStatus.(int)
	}
	if createdAt, ok := data["createdAt"]; ok {
		upChain.CreatedAt = createdAt.(time.Time)
	}
	return nil
}

func (upChain *UpChain) UpSuccess(hashData string) {
	if len(upChain.Hash) > 0 {
		return
	}
	upChain.Hash = hashData
	upChain.UpChainStatus = 1
}

func (upChain *UpChain) UpFail() {
	if len(upChain.Hash) > 0 {
		return
	}
	upChain.Hash = ""
	upChain.UpChainStatus = 2
}