// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package query

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"wechat-api/database/dao/wechat/model"
)

func newContact(db *gorm.DB, opts ...gen.DOOption) contact {
	_contact := contact{}

	_contact.contactDo.UseDB(db, opts...)
	_contact.contactDo.UseModel(&model.Contact{})

	tableName := _contact.contactDo.TableName()
	_contact.ALL = field.NewAsterisk(tableName)
	_contact.ID = field.NewInt64(tableName, "id")
	_contact.CreatedAt = field.NewTime(tableName, "created_at")
	_contact.UpdatedAt = field.NewTime(tableName, "updated_at")
	_contact.Status = field.NewInt64(tableName, "status")
	_contact.DeletedAt = field.NewField(tableName, "deleted_at")
	_contact.WxWxid = field.NewString(tableName, "wx_wxid")
	_contact.Type = field.NewInt64(tableName, "type")
	_contact.Wxid = field.NewString(tableName, "wxid")
	_contact.Account = field.NewString(tableName, "account")
	_contact.Nickname = field.NewString(tableName, "nickname")
	_contact.Markname = field.NewString(tableName, "markname")
	_contact.Headimg = field.NewString(tableName, "headimg")
	_contact.Sex = field.NewInt64(tableName, "sex")
	_contact.Starrole = field.NewString(tableName, "starrole")
	_contact.Dontseeit = field.NewInt64(tableName, "dontseeit")
	_contact.Dontseeme = field.NewInt64(tableName, "dontseeme")
	_contact.Lag = field.NewString(tableName, "lag")
	_contact.Gid = field.NewString(tableName, "gid")
	_contact.Gname = field.NewString(tableName, "gname")
	_contact.V3 = field.NewString(tableName, "v3")
	_contact.OrganizationID = field.NewInt64(tableName, "organization_id")
	_contact.Ctype = field.NewInt64(tableName, "ctype")
	_contact.Cc = field.NewString(tableName, "cc")
	_contact.Phone = field.NewString(tableName, "phone")
	_contact.Cname = field.NewString(tableName, "cname")
	_contact.Cage = field.NewInt64(tableName, "cage")
	_contact.Carea = field.NewString(tableName, "carea")
	_contact.Cbirthday = field.NewString(tableName, "cbirthday")
	_contact.Cbirtharea = field.NewString(tableName, "cbirtharea")
	_contact.CidcardNo = field.NewString(tableName, "cidcard_no")
	_contact.Ctitle = field.NewString(tableName, "ctitle")

	_contact.fillFieldMap()

	return _contact
}

type contact struct {
	contactDo

	ALL            field.Asterisk
	ID             field.Int64
	CreatedAt      field.Time   // Create Time | 创建日期
	UpdatedAt      field.Time   // Update Time | 修改日期
	Status         field.Int64  // Status 1: normal 2: ban | 状态 1 正常 2 禁用
	DeletedAt      field.Field  // Delete Time | 删除日期
	WxWxid         field.String // 属主微信id
	Type           field.Int64  // 联系人类型：1好友，2群组，3公众号，4企业微信联系人
	Wxid           field.String // 微信id 公众号微信ID
	Account        field.String // 微信账号
	Nickname       field.String // 微信昵称 群备注名称
	Markname       field.String // 备注名
	Headimg        field.String // 头像
	Sex            field.Int64  // 性别 0未知 1男 2女
	Starrole       field.String // 星标 65/67=星标 1/3=未星标
	Dontseeit      field.Int64  // 不让他看我的朋友圈 0可以看 1不让看
	Dontseeme      field.Int64  // 不看他的朋友圈 0可以看 1不看 1=开启了不看他 128/129=仅聊天
	Lag            field.String // 所属标签id清单，多开会用逗号隔开
	Gid            field.String // 群组id
	Gname          field.String // 群组名称
	V3             field.String // v3数据
	OrganizationID field.Int64  // 机构 ID
	Ctype          field.Int64  // 内容类型：1-微信 2-whatsapp 3-企微
	Cc             field.String // 国家区号
	Phone          field.String // 手机号
	Cname          field.String // 姓名
	Cage           field.Int64  // 年龄
	Carea          field.String // 地区
	Cbirthday      field.String // 出生日期
	Cbirtharea     field.String // 出生地
	CidcardNo      field.String // 身份证号
	Ctitle         field.String // 称呼

	fieldMap map[string]field.Expr
}

func (c contact) Table(newTableName string) *contact {
	c.contactDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c contact) As(alias string) *contact {
	c.contactDo.DO = *(c.contactDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *contact) updateTableName(table string) *contact {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")
	c.Status = field.NewInt64(table, "status")
	c.DeletedAt = field.NewField(table, "deleted_at")
	c.WxWxid = field.NewString(table, "wx_wxid")
	c.Type = field.NewInt64(table, "type")
	c.Wxid = field.NewString(table, "wxid")
	c.Account = field.NewString(table, "account")
	c.Nickname = field.NewString(table, "nickname")
	c.Markname = field.NewString(table, "markname")
	c.Headimg = field.NewString(table, "headimg")
	c.Sex = field.NewInt64(table, "sex")
	c.Starrole = field.NewString(table, "starrole")
	c.Dontseeit = field.NewInt64(table, "dontseeit")
	c.Dontseeme = field.NewInt64(table, "dontseeme")
	c.Lag = field.NewString(table, "lag")
	c.Gid = field.NewString(table, "gid")
	c.Gname = field.NewString(table, "gname")
	c.V3 = field.NewString(table, "v3")
	c.OrganizationID = field.NewInt64(table, "organization_id")
	c.Ctype = field.NewInt64(table, "ctype")
	c.Cc = field.NewString(table, "cc")
	c.Phone = field.NewString(table, "phone")
	c.Cname = field.NewString(table, "cname")
	c.Cage = field.NewInt64(table, "cage")
	c.Carea = field.NewString(table, "carea")
	c.Cbirthday = field.NewString(table, "cbirthday")
	c.Cbirtharea = field.NewString(table, "cbirtharea")
	c.CidcardNo = field.NewString(table, "cidcard_no")
	c.Ctitle = field.NewString(table, "ctitle")

	c.fillFieldMap()

	return c
}

func (c *contact) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := c.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (c *contact) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 31)
	c.fieldMap["id"] = c.ID
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
	c.fieldMap["status"] = c.Status
	c.fieldMap["deleted_at"] = c.DeletedAt
	c.fieldMap["wx_wxid"] = c.WxWxid
	c.fieldMap["type"] = c.Type
	c.fieldMap["wxid"] = c.Wxid
	c.fieldMap["account"] = c.Account
	c.fieldMap["nickname"] = c.Nickname
	c.fieldMap["markname"] = c.Markname
	c.fieldMap["headimg"] = c.Headimg
	c.fieldMap["sex"] = c.Sex
	c.fieldMap["starrole"] = c.Starrole
	c.fieldMap["dontseeit"] = c.Dontseeit
	c.fieldMap["dontseeme"] = c.Dontseeme
	c.fieldMap["lag"] = c.Lag
	c.fieldMap["gid"] = c.Gid
	c.fieldMap["gname"] = c.Gname
	c.fieldMap["v3"] = c.V3
	c.fieldMap["organization_id"] = c.OrganizationID
	c.fieldMap["ctype"] = c.Ctype
	c.fieldMap["cc"] = c.Cc
	c.fieldMap["phone"] = c.Phone
	c.fieldMap["cname"] = c.Cname
	c.fieldMap["cage"] = c.Cage
	c.fieldMap["carea"] = c.Carea
	c.fieldMap["cbirthday"] = c.Cbirthday
	c.fieldMap["cbirtharea"] = c.Cbirtharea
	c.fieldMap["cidcard_no"] = c.CidcardNo
	c.fieldMap["ctitle"] = c.Ctitle
}

func (c contact) clone(db *gorm.DB) contact {
	c.contactDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c contact) replaceDB(db *gorm.DB) contact {
	c.contactDo.ReplaceDB(db)
	return c
}

type contactDo struct{ gen.DO }

type IContactDo interface {
	gen.SubQuery
	Debug() IContactDo
	WithContext(ctx context.Context) IContactDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IContactDo
	WriteDB() IContactDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IContactDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IContactDo
	Not(conds ...gen.Condition) IContactDo
	Or(conds ...gen.Condition) IContactDo
	Select(conds ...field.Expr) IContactDo
	Where(conds ...gen.Condition) IContactDo
	Order(conds ...field.Expr) IContactDo
	Distinct(cols ...field.Expr) IContactDo
	Omit(cols ...field.Expr) IContactDo
	Join(table schema.Tabler, on ...field.Expr) IContactDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IContactDo
	RightJoin(table schema.Tabler, on ...field.Expr) IContactDo
	Group(cols ...field.Expr) IContactDo
	Having(conds ...gen.Condition) IContactDo
	Limit(limit int) IContactDo
	Offset(offset int) IContactDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IContactDo
	Unscoped() IContactDo
	Create(values ...*model.Contact) error
	CreateInBatches(values []*model.Contact, batchSize int) error
	Save(values ...*model.Contact) error
	First() (*model.Contact, error)
	Take() (*model.Contact, error)
	Last() (*model.Contact, error)
	Find() ([]*model.Contact, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Contact, err error)
	FindInBatches(result *[]*model.Contact, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.Contact) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IContactDo
	Assign(attrs ...field.AssignExpr) IContactDo
	Joins(fields ...field.RelationField) IContactDo
	Preload(fields ...field.RelationField) IContactDo
	FirstOrInit() (*model.Contact, error)
	FirstOrCreate() (*model.Contact, error)
	FindByPage(offset int, limit int) (result []*model.Contact, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IContactDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (c contactDo) Debug() IContactDo {
	return c.withDO(c.DO.Debug())
}

func (c contactDo) WithContext(ctx context.Context) IContactDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c contactDo) ReadDB() IContactDo {
	return c.Clauses(dbresolver.Read)
}

func (c contactDo) WriteDB() IContactDo {
	return c.Clauses(dbresolver.Write)
}

func (c contactDo) Session(config *gorm.Session) IContactDo {
	return c.withDO(c.DO.Session(config))
}

func (c contactDo) Clauses(conds ...clause.Expression) IContactDo {
	return c.withDO(c.DO.Clauses(conds...))
}

func (c contactDo) Returning(value interface{}, columns ...string) IContactDo {
	return c.withDO(c.DO.Returning(value, columns...))
}

func (c contactDo) Not(conds ...gen.Condition) IContactDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c contactDo) Or(conds ...gen.Condition) IContactDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c contactDo) Select(conds ...field.Expr) IContactDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c contactDo) Where(conds ...gen.Condition) IContactDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c contactDo) Order(conds ...field.Expr) IContactDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c contactDo) Distinct(cols ...field.Expr) IContactDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c contactDo) Omit(cols ...field.Expr) IContactDo {
	return c.withDO(c.DO.Omit(cols...))
}

func (c contactDo) Join(table schema.Tabler, on ...field.Expr) IContactDo {
	return c.withDO(c.DO.Join(table, on...))
}

func (c contactDo) LeftJoin(table schema.Tabler, on ...field.Expr) IContactDo {
	return c.withDO(c.DO.LeftJoin(table, on...))
}

func (c contactDo) RightJoin(table schema.Tabler, on ...field.Expr) IContactDo {
	return c.withDO(c.DO.RightJoin(table, on...))
}

func (c contactDo) Group(cols ...field.Expr) IContactDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c contactDo) Having(conds ...gen.Condition) IContactDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c contactDo) Limit(limit int) IContactDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c contactDo) Offset(offset int) IContactDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c contactDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IContactDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c contactDo) Unscoped() IContactDo {
	return c.withDO(c.DO.Unscoped())
}

func (c contactDo) Create(values ...*model.Contact) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

func (c contactDo) CreateInBatches(values []*model.Contact, batchSize int) error {
	return c.DO.CreateInBatches(values, batchSize)
}

// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (c contactDo) Save(values ...*model.Contact) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Save(values)
}

func (c contactDo) First() (*model.Contact, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Contact), nil
	}
}

func (c contactDo) Take() (*model.Contact, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Contact), nil
	}
}

func (c contactDo) Last() (*model.Contact, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Contact), nil
	}
}

func (c contactDo) Find() ([]*model.Contact, error) {
	result, err := c.DO.Find()
	return result.([]*model.Contact), err
}

func (c contactDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Contact, err error) {
	buf := make([]*model.Contact, 0, batchSize)
	err = c.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
		defer func() { results = append(results, buf...) }()
		return fc(tx, batch)
	})
	return results, err
}

func (c contactDo) FindInBatches(result *[]*model.Contact, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return c.DO.FindInBatches(result, batchSize, fc)
}

func (c contactDo) Attrs(attrs ...field.AssignExpr) IContactDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c contactDo) Assign(attrs ...field.AssignExpr) IContactDo {
	return c.withDO(c.DO.Assign(attrs...))
}

func (c contactDo) Joins(fields ...field.RelationField) IContactDo {
	for _, _f := range fields {
		c = *c.withDO(c.DO.Joins(_f))
	}
	return &c
}

func (c contactDo) Preload(fields ...field.RelationField) IContactDo {
	for _, _f := range fields {
		c = *c.withDO(c.DO.Preload(_f))
	}
	return &c
}

func (c contactDo) FirstOrInit() (*model.Contact, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Contact), nil
	}
}

func (c contactDo) FirstOrCreate() (*model.Contact, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Contact), nil
	}
}

func (c contactDo) FindByPage(offset int, limit int) (result []*model.Contact, count int64, err error) {
	result, err = c.Offset(offset).Limit(limit).Find()
	if err != nil {
		return
	}

	if size := len(result); 0 < limit && 0 < size && size < limit {
		count = int64(size + offset)
		return
	}

	count, err = c.Offset(-1).Limit(-1).Count()
	return
}

func (c contactDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = c.Count()
	if err != nil {
		return
	}

	err = c.Offset(offset).Limit(limit).Scan(result)
	return
}

func (c contactDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c contactDo) Delete(models ...*model.Contact) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *contactDo) withDO(do gen.Dao) *contactDo {
	c.DO = *do.(*gen.DO)
	return c
}
