123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package dao
- import (
- "errors"
- "fmt"
- "github.com/astaxie/beego/logs"
- "github.com/gogf/gf/v2/util/gconv"
- "gorm.io/gorm"
- )
- type Condition struct {
- Table string
- Where map[string]any
- OrderBy string
- Limit int
- Fields []string
- Offset int
- GroupBy string
- Having string
- }
- type Base struct {
- TableName string `gorm:"-" json:"-"`
- Db *gorm.DB
- }
- func newBase(tableName string, db *gorm.DB) *Base {
- b := Base{TableName: tableName, Db: db}
- return &b
- }
- func (c Base) DB() *gorm.DB {
- return c.Db
- }
- func (c Base) Split(sqlSplit *Condition) *gorm.DB {
- dbcon := c.DB()
- //if cfg.Debug.Mysql {
- // dbcon = dbcon.Debug()
- //}
- dbcon = dbcon.Table(c.TableName)
- if sqlSplit == nil {
- return dbcon
- }
- if sqlSplit.Table != "" {
- dbcon = dbcon.Table(sqlSplit.Table)
- }
- for key, val := range sqlSplit.Where {
- dbcon = dbcon.Where(key, val)
- }
- if len(sqlSplit.Fields) > 0 {
- dbcon = dbcon.Select(sqlSplit.Fields)
- }
- if sqlSplit.OrderBy != "" {
- dbcon = dbcon.Order(sqlSplit.OrderBy)
- }
- if sqlSplit.Limit != 0 {
- dbcon = dbcon.Limit(sqlSplit.Limit)
- }
- if sqlSplit.Offset != 0 {
- dbcon = dbcon.Offset(sqlSplit.Offset)
- }
- if sqlSplit.GroupBy != "" {
- dbcon = dbcon.Group(sqlSplit.GroupBy)
- }
- if sqlSplit.Having != "" {
- dbcon = dbcon.Having(sqlSplit.Having)
- }
- return dbcon
- }
- // Find
- // @Description: 根据条件查询数据库
- // @receiver c
- // @param sqlCondition 条件
- // @param data 对象指针,或者对象切片
- // @return error
- func (c Base) Find(sqlCondition *Condition, data any) error {
- return c.Split(sqlCondition).Find(data).Error
- }
- func (c Base) Raw(sql string, data any) error {
- return c.DB().Raw(sql).Scan(data).Error
- }
- func (c Base) Count(sqlCondition *Condition, num *int64) error {
- sqlCondition.Limit = -1
- sqlCondition.Offset = -1
- return c.Split(sqlCondition).Count(num).Error
- }
- func (c Base) Update(sqlCondition *Condition, data any) error {
- return c.Split(sqlCondition).Updates(data).Error
- }
- func (c Base) Delete(data map[string]any) error {
- if data == nil || len(data) == 0 {
- return errors.New("删除参数异常")
- }
- return c.DB().Table(c.TableName).Where(data).Delete(c).Error
- }
- func (c Base) Deletes(cond *Condition) error {
- return c.Split(cond).Delete(c).Error
- }
- func (c Base) Save(data any) error {
- return c.DB().Table(c.TableName).Save(data).Error
- }
- // Replace
- // @Description: 1、替代insert;2、原子更新某个字段
- // @receiver c
- // @param cond
- // @return error
- func (c Base) Replace(cond map[string]any) error {
- if id, ok := cond["id"]; !ok || gconv.Int(id) == 0 {
- delete(cond, "id")
- }
- field := ""
- values := ""
- updateStr := ""
- for key, val := range cond {
- field = fmt.Sprintf("%s,`%s`", field, key)
- values = fmt.Sprintf("%s,'%s'", values, gconv.String(val))
- updateStr = fmt.Sprintf("%s,`%s` = values(`%s`)", updateStr, key, key)
- }
- field = field[1:]
- values = values[1:]
- updateStr = updateStr[1:]
- sql := fmt.Sprintf(`insert into %s (%s) values (%s) ON DUPLICATE KEY UPDATE %s`, c.TableName, field, values, updateStr)
- return c.Exec(sql)
- }
- func (c Base) Insert(data any) error {
- return c.DB().Table(c.TableName).Create(data).Error
- }
- func (c Base) Exec(sql string) error {
- return c.DB().Exec(sql).Error
- }
- func (c Base) Truncate(table string) error {
- sql := fmt.Sprintf(`truncate table %s`, table)
- return c.Exec(sql)
- }
- func (c Base) GetValueByField(tableName, field, value, GetField string) string {
- var (
- resp = ""
- err error
- )
- sql := fmt.Sprintf(`select %s from %s where %s = "%s"`, GetField, tableName, field, value)
- err = c.Raw(sql, &resp)
- if err != nil {
- logs.Error("sql:%s查询数据异常 err:%s", sql, err.Error())
- }
- return resp
- }
|