| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 | package svcimport (	"fmt"	"time"		"golib/v4/features/mo"	"golib/v4/infra/ii")// Row 用于 mo.D 的快捷操作type Row struct {	mo.D}func (c *Row) Clone() Row {	r := make(mo.D, len(c.D))	for i, v := range c.D {		r[i] = v	}	return Row{D: r}}func (c *Row) ID() mo.ObjectID {	return c.ObjectID(mo.OID)}func (c *Row) Any(k string) any {	v, _ := c.Get(k)	return v}func (c *Row) Double(k string) float64 {	v, ok := c.Get(k)	if !ok {		return 0	}	return v.(float64)}func (c *Row) Strings(k string) string {	v, ok := c.Get(k)	if !ok {		return ""	}	return v.(string)}func (c *Row) Object(k string) Row {	v, ok := c.Get(k)	if !ok {		return Row{}	}	return Row{D: v.(mo.D)}}func (c *Row) ObjectTo(k string, val any) error {	v, ok := c.Get(k)	if !ok {		return fmt.Errorf("field %v not found in Row", k)	}	return mo.Decode(v, val)}func (c *Row) Array(k string) mo.A {	v, ok := c.Get(k)	if !ok {		return mo.A{}	}	return v.(mo.A)}func (c *Row) Binary(k string) mo.Binary {	v, ok := c.Get(k)	if !ok {		return mo.Binary{}	}	return v.(mo.Binary)}func (c *Row) ObjectID(k string) mo.ObjectID {	v, ok := c.Get(k)	if !ok {		return mo.ObjectID{}	}	return v.(mo.ObjectID)}func (c *Row) Boolean(k string) bool {	v, ok := c.Get(k)	if !ok {		return false	}	return v.(bool)}func (c *Row) Date(k string) mo.DateTime {	v, ok := c.Get(k)	if !ok {		return mo.DateTime(0)	}	return v.(mo.DateTime)}func (c *Row) Int32(k string) int32 {	v, ok := c.Get(k)	if !ok {		return 0	}	return v.(int32)}func (c *Row) Int64(k string) int64 {	v, ok := c.Get(k)	if !ok {		return 0	}	return v.(int64)}func (c *Row) Has(k string) bool {	v, ok := c.Get(k)	if !ok {		return false	}	return v != nil}func (c *Row) Range(f func(i int, e mo.E) bool) {	for i, e := range c.D {		if !f(i, e) {			return		}	}}func (c *Row) CopyToSet(updater *mo.Updater) {	c.Range(func(_ int, e mo.E) bool {		updater.Set(e.Key, e.Value)		return true	})}func (c *Row) Get(k string) (any, bool) {	for _, e := range c.D {		if e.Key == k {			return e.Value, true		}	}	return nil, false}func (c *Row) Add(k string, v any) {	c.D = append(c.D, mo.E{Key: k, Value: v})}func (c *Row) Set(k string, v any) {	set := false	c.Range(func(i int, e mo.E) bool {		if e.Key == k {			c.D[i].Value = v			set = true			return false		}		return true	})	if !set {		c.Add(k, v)	}}func (c *Row) Del(k string) {	for i, e := range c.D {		if e.Key == k {			c.D = append(c.D[:i], c.D[i+1:]...)		}	}}func (c *Row) CreationTime() time.Time {	if creat := c.Date(ii.CreationTime); creat > 0 {		return creat.Time().Local()	}	return time.Time{}}func (c *Row) LastModified() time.Time {	if last := c.Date(ii.LastModified); last > 0 {		return last.Time().Local()	}	return c.CreationTime()}func (c *Row) MarshalJSON() ([]byte, error) {	row := c.Clone()	if row.Has(ii.CreationTime) {		row.Set(ii.CreationTime, c.CreationTime().Format(time.DateTime))	}	if row.Has(ii.LastModified) {		row.Set(ii.LastModified, c.LastModified().Format(time.DateTime))	}	return row.D.MarshalJSON()}func (c *Row) UnmarshalBSON(data []byte) error {	return mo.Unmarshal(data, &c.D)}func (c *Row) MarshalBSON() ([]byte, error) {	return mo.Marshal(c.D)}
 |