func (s *SoStakeRecordWrap) GetLastStakeBlockNum() uint64 {
	res := true
	msg := &SoStakeRecord{}
	if s.dba == nil {
		res = false
	} else {
		key, err := s.encodeMainKey()
		if err != nil {
			res = false
		} else {
			buf, err := s.dba.Get(key)
			if err != nil {
				res = false
			}
			err = proto.Unmarshal(buf, msg)
			if err != nil {
				res = false
			} else {
				return msg.LastStakeBlockNum
			}
		}
	}
	if !res {
		var tmpValue uint64
		return tmpValue
	}
	return msg.LastStakeBlockNum
}
//////////// SECTION Members Get/Modify ///////////////
package table
import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"reflect"
	"sync"
	"github.com/coschain/contentos-go/common/encoding/kope"
	"github.com/coschain/contentos-go/iservices"
	prototype "github.com/coschain/contentos-go/prototype"
	proto "github.com/golang/protobuf/proto"
)
var (
	StakeRecordRecordTable        uint32 = 265171955
	StakeRecordRecordReverseTable uint32 = 2606609996
	StakeRecordRecordUniTable     uint32 = 832689285
	StakeRecordRecordRow uint32 = 957572259
)
type SoStakeRecordWrap struct {
	dba         iservices.IDatabaseRW
	mainKey     *prototype.StakeRecord
	watcherFlag *StakeRecordWatcherFlag
	mKeyFlag    int     
	mKeyBuf     []byte  
	mBuf        []byte  
	mdFuncMap   map[string]interface{}
}
func NewSoStakeRecordWrap(dba iservices.IDatabaseRW, key *prototype.StakeRecord) *SoStakeRecordWrap {
	if dba == nil || key == nil {
		return nil
	}
	result := &SoStakeRecordWrap{dba, key, nil, -1, nil, nil, nil}
	result.initWatcherFlag()
	return result
}
func (s *SoStakeRecordWrap) CheckExist() bool {
	if s.dba == nil {
		return false
	}
	if s.mKeyFlag != -1 {
		if s.mKeyFlag == 0 {
			return false
		}
		return true
	}
	keyBuf, err := s.encodeMainKey()
	if err != nil {
		return false
	}
	res, err := s.dba.Has(keyBuf)
	if err != nil {
		return false
	}
	if res == false {
		s.mKeyFlag = 0
	} else {
		s.mKeyFlag = 1
	}
	return res
}
func (s *SoStakeRecordWrap) MustExist(errMsgs ...interface{}) *SoStakeRecordWrap {
	if !s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoStakeRecordWrap.MustExist: %v not found", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoStakeRecordWrap) MustNotExist(errMsgs ...interface{}) *SoStakeRecordWrap {
	if s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoStakeRecordWrap.MustNotExist: %v already exists", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoStakeRecordWrap) initWatcherFlag() {
	if s.watcherFlag == nil {
		s.watcherFlag = new(StakeRecordWatcherFlag)
		*(s.watcherFlag) = StakeRecordWatcherFlagOfDb(s.dba.ServiceId())
	}
}
func (s *SoStakeRecordWrap) create(f func(tInfo *SoStakeRecord)) error {
	if s.dba == nil {
		return errors.New("the db is nil")
	}
	if s.mainKey == nil {
		return errors.New("the main key is nil")
	}
	val := &SoStakeRecord{}
	f(val)
	if val.Record == nil {
		val.Record = s.mainKey
	}
	if s.CheckExist() {
		return errors.New("the main key is already exist")
	}
	keyBuf, err := s.encodeMainKey()
	if err != nil {
		return err
	}
	buf, err := proto.Marshal(val)
	if err != nil {
		return err
	}
	err = s.dba.Put(keyBuf, buf)
	if err != nil {
		return err
	}
	if err = s.insertAllSortKeys(val); err != nil {
		s.delAllSortKeys(false, val)
		s.dba.Delete(keyBuf)
		return err
	}
	if sucNames, err := s.insertAllUniKeys(val); err != nil {
		s.delAllSortKeys(false, val)
		s.delUniKeysWithNames(sucNames, val)
		s.dba.Delete(keyBuf)
		return err
	}
	s.mKeyFlag = 1
	s.initWatcherFlag()
	if s.watcherFlag.AnyWatcher {
		ReportTableRecordInsert(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, val)
	}
	return nil
}
func (s *SoStakeRecordWrap) Create(f func(tInfo *SoStakeRecord), errArgs ...interface{}) *SoStakeRecordWrap {
	err := s.create(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Errorf("SoStakeRecordWrap.Create failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoStakeRecordWrap) getMainKeyBuf() ([]byte, error) {
	if s.mainKey == nil {
		return nil, errors.New("the main key is nil")
	}
	if s.mBuf == nil {
		var err error = nil
		s.mBuf, err = kope.Encode(s.mainKey)
		if err != nil {
			return nil, err
		}
	}
	return s.mBuf, nil
}
func (s *SoStakeRecordWrap) modify(f func(tInfo *SoStakeRecord)) error {
	if !s.CheckExist() {
		return errors.New("the SoStakeRecord table does not exist. Please create a table first")
	}
	oriTable := s.getStakeRecord()
	if oriTable == nil {
		return errors.New("fail to get origin table SoStakeRecord")
	}
	curTable := s.getStakeRecord()
	if curTable == nil {
		return errors.New("fail to create current table SoStakeRecord")
	}
	f(curTable)
	if !reflect.DeepEqual(curTable.Record, oriTable.Record) {
		return errors.New("primary key does not support modification")
	}
	s.initWatcherFlag()
	modifiedFields, hasWatcher, err := s.getModifiedFields(oriTable, curTable)
	if err != nil {
		return err
	}
	if modifiedFields == nil || len(modifiedFields) < 1 {
		return nil
	}
	err = s.checkSortAndUniFieldValidity(curTable, modifiedFields)
	if err != nil {
		return err
	}
	err = s.handleFieldMd(FieldMdHandleTypeCheck, curTable, modifiedFields)
	if err != nil {
		return err
	}
	err = s.handleFieldMd(FieldMdHandleTypeDel, oriTable, modifiedFields)
	if err != nil {
		return err
	}
	err = s.updateStakeRecord(curTable)
	if err != nil {
		return err
	}
	err = s.handleFieldMd(FieldMdHandleTypeInsert, curTable, modifiedFields)
	if err != nil {
		return err
	}
	if hasWatcher {
		ReportTableRecordUpdate(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, oriTable, curTable, modifiedFields)
	}
	return nil
}
func (s *SoStakeRecordWrap) Modify(f func(tInfo *SoStakeRecord), errArgs ...interface{}) *SoStakeRecordWrap {
	err := s.modify(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoStakeRecordWrap.Modify failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoStakeRecordWrap) SetLastStakeBlockNum(p uint64, errArgs ...interface{}) *SoStakeRecordWrap {
	err := s.modify(func(r *SoStakeRecord) {
		r.LastStakeBlockNum = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoStakeRecordWrap.SetLastStakeBlockNum( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoStakeRecordWrap) SetRecordReverse(p *prototype.StakeRecordReverse, errArgs ...interface{}) *SoStakeRecordWrap {
	err := s.modify(func(r *SoStakeRecord) {
		r.RecordReverse = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoStakeRecordWrap.SetRecordReverse( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoStakeRecordWrap) SetStakeAmount(p *prototype.Vest, errArgs ...interface{}) *SoStakeRecordWrap {
	err := s.modify(func(r *SoStakeRecord) {
		r.StakeAmount = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoStakeRecordWrap.SetStakeAmount( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoStakeRecordWrap) checkSortAndUniFieldValidity(curTable *SoStakeRecord, fields map[string]bool) error {
	if curTable != nil && fields != nil && len(fields) > 0 {
		if fields["RecordReverse"] && curTable.RecordReverse == nil {
			return errors.New("sort field RecordReverse can't be modified to nil")
		}
	}
	return nil
}
func (s *SoStakeRecordWrap) getModifiedFields(oriTable *SoStakeRecord, curTable *SoStakeRecord) (map[string]bool, bool, error) {
	if oriTable == nil {
		return nil, false, errors.New("table info is nil, can't get modified fields")
	}
	hasWatcher := false
	fields := make(map[string]bool)
	if !reflect.DeepEqual(oriTable.LastStakeBlockNum, curTable.LastStakeBlockNum) {
		fields["LastStakeBlockNum"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasLastStakeBlockNumWatcher
	}
	if !reflect.DeepEqual(oriTable.RecordReverse, curTable.RecordReverse) {
		fields["RecordReverse"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasRecordReverseWatcher
	}
	if !reflect.DeepEqual(oriTable.StakeAmount, curTable.StakeAmount) {
		fields["StakeAmount"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasStakeAmountWatcher
	}
	hasWatcher = hasWatcher || s.watcherFlag.WholeWatcher
	return fields, hasWatcher, nil
}
func (s *SoStakeRecordWrap) handleFieldMd(t FieldMdHandleType, so *SoStakeRecord, fields map[string]bool) error {
	if so == nil {
		return errors.New("fail to modify empty table")
	}
	if fields == nil || len(fields) < 1 {
		return nil
	}
	errStr := ""
	if fields["LastStakeBlockNum"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldLastStakeBlockNum(so.LastStakeBlockNum, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "LastStakeBlockNum")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldLastStakeBlockNum(so.LastStakeBlockNum, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "LastStakeBlockNum")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldLastStakeBlockNum(so.LastStakeBlockNum, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "LastStakeBlockNum")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["RecordReverse"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldRecordReverse(so.RecordReverse, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "RecordReverse")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldRecordReverse(so.RecordReverse, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "RecordReverse")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldRecordReverse(so.RecordReverse, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "RecordReverse")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	if fields["StakeAmount"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldStakeAmount(so.StakeAmount, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "StakeAmount")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldStakeAmount(so.StakeAmount, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "StakeAmount")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldStakeAmount(so.StakeAmount, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "StakeAmount")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	return nil
}
func (s *SoStakeRecordWrap) delSortKeyRecord(sa *SoStakeRecord) bool {
	if s.dba == nil || s.mainKey == nil {
		return false
	}
	val := SoListStakeRecordByRecord{}
	if sa == nil {
		val.Record = s.GetRecord()
	} else {
		val.Record = sa.Record
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Delete(subBuf)
	return ordErr == nil
}
func (s *SoStakeRecordWrap) insertSortKeyRecord(sa *SoStakeRecord) bool {
	if s.dba == nil || sa == nil {
		return false
	}
	val := SoListStakeRecordByRecord{}
	val.Record = sa.Record
	buf, err := proto.Marshal(&val)
	if err != nil {
		return false
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Put(subBuf, buf)
	return ordErr == nil
}
func (s *SoStakeRecordWrap) delSortKeyRecordReverse(sa *SoStakeRecord) bool {
	if s.dba == nil || s.mainKey == nil {
		return false
	}
	val := SoListStakeRecordByRecordReverse{}
	if sa == nil {
		val.RecordReverse = s.GetRecordReverse()
		val.Record = s.mainKey
	} else {
		val.RecordReverse = sa.RecordReverse
		val.Record = sa.Record
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Delete(subBuf)
	return ordErr == nil
}
func (s *SoStakeRecordWrap) insertSortKeyRecordReverse(sa *SoStakeRecord) bool {
	if s.dba == nil || sa == nil {
		return false
	}
	val := SoListStakeRecordByRecordReverse{}
	val.Record = sa.Record
	val.RecordReverse = sa.RecordReverse
	buf, err := proto.Marshal(&val)
	if err != nil {
		return false
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Put(subBuf, buf)
	return ordErr == nil
}
func (s *SoStakeRecordWrap) delAllSortKeys(br bool, val *SoStakeRecord) bool {
	if s.dba == nil {
		return false
	}
	res := true
	if !s.delSortKeyRecord(val) {
		if br {
			return false
		} else {
			res = false
		}
	}
	if !s.delSortKeyRecordReverse(val) {
		if br {
			return false
		} else {
			res = false
		}
	}
	return res
}
func (s *SoStakeRecordWrap) insertAllSortKeys(val *SoStakeRecord) error {
	if s.dba == nil {
		return errors.New("insert sort Field fail,the db is nil ")
	}
	if val == nil {
		return errors.New("insert sort Field fail,get the SoStakeRecord fail ")
	}
	if !s.insertSortKeyRecord(val) {
		return errors.New("insert sort Field Record fail while insert table ")
	}
	if !s.insertSortKeyRecordReverse(val) {
		return errors.New("insert sort Field RecordReverse fail while insert table ")
	}
	return nil
}
func (s *SoStakeRecordWrap) removeStakeRecord() error {
	if s.dba == nil {
		return errors.New("database is nil")
	}
	s.initWatcherFlag()
	var oldVal *SoStakeRecord
	if s.watcherFlag.AnyWatcher {
		oldVal = s.getStakeRecord()
	}
	if res := s.delAllSortKeys(true, nil); !res {
		return errors.New("delAllSortKeys failed")
	}
	if res := s.delAllUniKeys(true, nil); !res {
		return errors.New("delAllUniKeys failed")
	}
	key, err := s.encodeMainKey()
	if err != nil {
		return fmt.Errorf("encodeMainKey failed: %s", err.Error())
	}
	err = s.dba.Delete(key)
	if err == nil {
		s.mKeyBuf = nil
		s.mKeyFlag = -1
		if s.watcherFlag.AnyWatcher && oldVal != nil {
			ReportTableRecordDelete(s.dba.ServiceId(), s.dba.BranchId(), s.mainKey, oldVal)
		}
		return nil
	} else {
		return fmt.Errorf("database.Delete failed: %s", err.Error())
	}
}
func (s *SoStakeRecordWrap) RemoveStakeRecord(errMsgs ...interface{}) *SoStakeRecordWrap {
	err := s.removeStakeRecord()
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoStakeRecordWrap.RemoveStakeRecord failed: %s", err.Error()), errMsgs...))
	}
	return s
}