func (s *SoBlockSummaryObjectWrap) removeBlockSummaryObject() error {
	if s.dba == nil {
		return errors.New("database is nil")
	}
	s.initWatcherFlag()
	var oldVal *SoBlockSummaryObject
	if s.watcherFlag.AnyWatcher {
		oldVal = s.getBlockSummaryObject()
	}
	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())
	}
}
//////////// SECTION LKeys delete/insert //////////////
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 (
	BlockSummaryObjectIdUniTable uint32 = 1471985338
	BlockSummaryObjectIdRow uint32 = 172113035
)
type SoBlockSummaryObjectWrap struct {
	dba         iservices.IDatabaseRW
	mainKey     *uint32
	watcherFlag *BlockSummaryObjectWatcherFlag
	mKeyFlag    int     
	mKeyBuf     []byte  
	mBuf        []byte  
	mdFuncMap   map[string]interface{}
}
func NewSoBlockSummaryObjectWrap(dba iservices.IDatabaseRW, key *uint32) *SoBlockSummaryObjectWrap {
	if dba == nil || key == nil {
		return nil
	}
	result := &SoBlockSummaryObjectWrap{dba, key, nil, -1, nil, nil, nil}
	result.initWatcherFlag()
	return result
}
func (s *SoBlockSummaryObjectWrap) 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 *SoBlockSummaryObjectWrap) MustExist(errMsgs ...interface{}) *SoBlockSummaryObjectWrap {
	if !s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockSummaryObjectWrap.MustExist: %v not found", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoBlockSummaryObjectWrap) MustNotExist(errMsgs ...interface{}) *SoBlockSummaryObjectWrap {
	if s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockSummaryObjectWrap.MustNotExist: %v already exists", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoBlockSummaryObjectWrap) initWatcherFlag() {
	if s.watcherFlag == nil {
		s.watcherFlag = new(BlockSummaryObjectWatcherFlag)
		*(s.watcherFlag) = BlockSummaryObjectWatcherFlagOfDb(s.dba.ServiceId())
	}
}
func (s *SoBlockSummaryObjectWrap) create(f func(tInfo *SoBlockSummaryObject)) 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 := &SoBlockSummaryObject{}
	f(val)
	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 *SoBlockSummaryObjectWrap) Create(f func(tInfo *SoBlockSummaryObject), errArgs ...interface{}) *SoBlockSummaryObjectWrap {
	err := s.create(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Errorf("SoBlockSummaryObjectWrap.Create failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockSummaryObjectWrap) 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 *SoBlockSummaryObjectWrap) modify(f func(tInfo *SoBlockSummaryObject)) error {
	if !s.CheckExist() {
		return errors.New("the SoBlockSummaryObject table does not exist. Please create a table first")
	}
	oriTable := s.getBlockSummaryObject()
	if oriTable == nil {
		return errors.New("fail to get origin table SoBlockSummaryObject")
	}
	curTable := s.getBlockSummaryObject()
	if curTable == nil {
		return errors.New("fail to create current table SoBlockSummaryObject")
	}
	f(curTable)
	if !reflect.DeepEqual(curTable.Id, oriTable.Id) {
		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.updateBlockSummaryObject(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 *SoBlockSummaryObjectWrap) Modify(f func(tInfo *SoBlockSummaryObject), errArgs ...interface{}) *SoBlockSummaryObjectWrap {
	err := s.modify(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockSummaryObjectWrap.Modify failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockSummaryObjectWrap) SetBlockId(p *prototype.Sha256, errArgs ...interface{}) *SoBlockSummaryObjectWrap {
	err := s.modify(func(r *SoBlockSummaryObject) {
		r.BlockId = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoBlockSummaryObjectWrap.SetBlockId( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoBlockSummaryObjectWrap) checkSortAndUniFieldValidity(curTable *SoBlockSummaryObject, fields map[string]bool) error {
	if curTable != nil && fields != nil && len(fields) > 0 {
	}
	return nil
}
func (s *SoBlockSummaryObjectWrap) getModifiedFields(oriTable *SoBlockSummaryObject, curTable *SoBlockSummaryObject) (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.BlockId, curTable.BlockId) {
		fields["BlockId"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasBlockIdWatcher
	}
	hasWatcher = hasWatcher || s.watcherFlag.WholeWatcher
	return fields, hasWatcher, nil
}
func (s *SoBlockSummaryObjectWrap) handleFieldMd(t FieldMdHandleType, so *SoBlockSummaryObject, 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["BlockId"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldBlockId(so.BlockId, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "BlockId")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldBlockId(so.BlockId, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "BlockId")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldBlockId(so.BlockId, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "BlockId")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	return nil
}
func (s *SoBlockSummaryObjectWrap) delAllSortKeys(br bool, val *SoBlockSummaryObject) bool {
	if s.dba == nil {
		return false
	}
	res := true
	return res
}
func (s *SoBlockSummaryObjectWrap) insertAllSortKeys(val *SoBlockSummaryObject) 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 SoBlockSummaryObject fail ")
	}
	return nil
}