func (s *SoExtReplyCreatedWrap) removeExtReplyCreated() error {
	if s.dba == nil {
		return errors.New("database is nil")
	}
	s.initWatcherFlag()
	var oldVal *SoExtReplyCreated
	if s.watcherFlag.AnyWatcher {
		oldVal = s.getExtReplyCreated()
	}
	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 (
	ExtReplyCreatedCreatedOrderTable uint32 = 874612931
	ExtReplyCreatedPostIdUniTable    uint32 = 457271836
	ExtReplyCreatedPostIdRow uint32 = 3284075428
)
type SoExtReplyCreatedWrap struct {
	dba         iservices.IDatabaseRW
	mainKey     *uint64
	watcherFlag *ExtReplyCreatedWatcherFlag
	mKeyFlag    int     
	mKeyBuf     []byte  
	mBuf        []byte  
	mdFuncMap   map[string]interface{}
}
func NewSoExtReplyCreatedWrap(dba iservices.IDatabaseRW, key *uint64) *SoExtReplyCreatedWrap {
	if dba == nil || key == nil {
		return nil
	}
	result := &SoExtReplyCreatedWrap{dba, key, nil, -1, nil, nil, nil}
	result.initWatcherFlag()
	return result
}
func (s *SoExtReplyCreatedWrap) 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 *SoExtReplyCreatedWrap) MustExist(errMsgs ...interface{}) *SoExtReplyCreatedWrap {
	if !s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoExtReplyCreatedWrap.MustExist: %v not found", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoExtReplyCreatedWrap) MustNotExist(errMsgs ...interface{}) *SoExtReplyCreatedWrap {
	if s.CheckExist() {
		panic(bindErrorInfo(fmt.Sprintf("SoExtReplyCreatedWrap.MustNotExist: %v already exists", s.mainKey), errMsgs...))
	}
	return s
}
func (s *SoExtReplyCreatedWrap) initWatcherFlag() {
	if s.watcherFlag == nil {
		s.watcherFlag = new(ExtReplyCreatedWatcherFlag)
		*(s.watcherFlag) = ExtReplyCreatedWatcherFlagOfDb(s.dba.ServiceId())
	}
}
func (s *SoExtReplyCreatedWrap) create(f func(tInfo *SoExtReplyCreated)) 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 := &SoExtReplyCreated{}
	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 *SoExtReplyCreatedWrap) Create(f func(tInfo *SoExtReplyCreated), errArgs ...interface{}) *SoExtReplyCreatedWrap {
	err := s.create(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Errorf("SoExtReplyCreatedWrap.Create failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoExtReplyCreatedWrap) 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 *SoExtReplyCreatedWrap) modify(f func(tInfo *SoExtReplyCreated)) error {
	if !s.CheckExist() {
		return errors.New("the SoExtReplyCreated table does not exist. Please create a table first")
	}
	oriTable := s.getExtReplyCreated()
	if oriTable == nil {
		return errors.New("fail to get origin table SoExtReplyCreated")
	}
	curTable := s.getExtReplyCreated()
	if curTable == nil {
		return errors.New("fail to create current table SoExtReplyCreated")
	}
	f(curTable)
	if !reflect.DeepEqual(curTable.PostId, oriTable.PostId) {
		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.updateExtReplyCreated(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 *SoExtReplyCreatedWrap) Modify(f func(tInfo *SoExtReplyCreated), errArgs ...interface{}) *SoExtReplyCreatedWrap {
	err := s.modify(f)
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoExtReplyCreatedWrap.Modify failed: %s", err.Error()), errArgs...))
	}
	return s
}
func (s *SoExtReplyCreatedWrap) SetCreatedOrder(p *prototype.ReplyCreatedOrder, errArgs ...interface{}) *SoExtReplyCreatedWrap {
	err := s.modify(func(r *SoExtReplyCreated) {
		r.CreatedOrder = p
	})
	if err != nil {
		panic(bindErrorInfo(fmt.Sprintf("SoExtReplyCreatedWrap.SetCreatedOrder( %v ) failed: %s", p, err.Error()), errArgs...))
	}
	return s
}
func (s *SoExtReplyCreatedWrap) checkSortAndUniFieldValidity(curTable *SoExtReplyCreated, fields map[string]bool) error {
	if curTable != nil && fields != nil && len(fields) > 0 {
		if fields["CreatedOrder"] && curTable.CreatedOrder == nil {
			return errors.New("sort field CreatedOrder can't be modified to nil")
		}
	}
	return nil
}
func (s *SoExtReplyCreatedWrap) getModifiedFields(oriTable *SoExtReplyCreated, curTable *SoExtReplyCreated) (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.CreatedOrder, curTable.CreatedOrder) {
		fields["CreatedOrder"] = true
		hasWatcher = hasWatcher || s.watcherFlag.HasCreatedOrderWatcher
	}
	hasWatcher = hasWatcher || s.watcherFlag.WholeWatcher
	return fields, hasWatcher, nil
}
func (s *SoExtReplyCreatedWrap) handleFieldMd(t FieldMdHandleType, so *SoExtReplyCreated, 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["CreatedOrder"] {
		res := true
		if t == FieldMdHandleTypeCheck {
			res = s.mdFieldCreatedOrder(so.CreatedOrder, true, false, false, so)
			errStr = fmt.Sprintf("fail to modify exist value of %v", "CreatedOrder")
		} else if t == FieldMdHandleTypeDel {
			res = s.mdFieldCreatedOrder(so.CreatedOrder, false, true, false, so)
			errStr = fmt.Sprintf("fail to delete  sort or unique field  %v", "CreatedOrder")
		} else if t == FieldMdHandleTypeInsert {
			res = s.mdFieldCreatedOrder(so.CreatedOrder, false, false, true, so)
			errStr = fmt.Sprintf("fail to insert  sort or unique field  %v", "CreatedOrder")
		}
		if !res {
			return errors.New(errStr)
		}
	}
	return nil
}
func (s *SoExtReplyCreatedWrap) delSortKeyCreatedOrder(sa *SoExtReplyCreated) bool {
	if s.dba == nil || s.mainKey == nil {
		return false
	}
	val := SoListExtReplyCreatedByCreatedOrder{}
	if sa == nil {
		val.CreatedOrder = s.GetCreatedOrder()
		val.PostId = *s.mainKey
	} else {
		val.CreatedOrder = sa.CreatedOrder
		val.PostId = sa.PostId
	}
	subBuf, err := val.OpeEncode()
	if err != nil {
		return false
	}
	ordErr := s.dba.Delete(subBuf)
	return ordErr == nil
}
func (s *SoExtReplyCreatedWrap) insertSortKeyCreatedOrder(sa *SoExtReplyCreated) bool {
	if s.dba == nil || sa == nil {
		return false
	}
	val := SoListExtReplyCreatedByCreatedOrder{}
	val.PostId = sa.PostId
	val.CreatedOrder = sa.CreatedOrder
	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 *SoExtReplyCreatedWrap) delAllSortKeys(br bool, val *SoExtReplyCreated) bool {
	if s.dba == nil {
		return false
	}
	res := true
	if !s.delSortKeyCreatedOrder(val) {
		if br {
			return false
		} else {
			res = false
		}
	}
	return res
}
func (s *SoExtReplyCreatedWrap) insertAllSortKeys(val *SoExtReplyCreated) 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 SoExtReplyCreated fail ")
	}
	if !s.insertSortKeyCreatedOrder(val) {
		return errors.New("insert sort Field CreatedOrder fail while insert table ")
	}
	return nil
}