package models import ( "eta_gn/eta_index_lib/global" "eta_gn/eta_index_lib/utils" "fmt" "strings" "time" ) // BusinessConf 商户配置表 type BusinessConf struct { Id int `gorm:"column:id;primaryKey;autoIncrement"` ConfKey string `gorm:"column:conf_key;type:varchar(128);not null;default:'';uniqueIndex:idx_conf_key"` ConfVal string `gorm:"column:conf_val;type:text"` ValType int `gorm:"column:val_type;not null;default:0"` Necessary int `gorm:"column:necessary;not null;default:0"` Remark string `gorm:"column:remark;type:varchar(128);not null;default:''"` CreateTime *time.Time `gorm:"column:create_time"` } func (m *BusinessConf) TableName() string { return "business_conf" } func (m *BusinessConf) PrimaryId() string { return "id" } func (m *BusinessConf) Create() (err error) { err = global.DmSQL["master"].Create(m).Error if err != nil { return } return } func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) { if len(items) == 0 { return } err = global.DmSQL["master"].CreateInBatches(items, 500).Error return } func (m *BusinessConf) Update(cols []string) (err error) { err = global.DmSQL["master"].Model(m).Select(cols).Updates(m).Error return } func (m *BusinessConf) Del() (err error) { sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId()) err = global.DmSQL["master"].Exec(sql, m.Id).Error return } func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId()) err = global.DmSQL["master"].Raw(sql, id).First(&item).Error return } func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition) err = global.DmSQL["master"].Raw(sql, pars...).First(&item).Error return } func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) { sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = global.DmSQL["master"].Raw(sql, pars...).Scan(&count).Error return } func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) err = global.DmSQL["master"].Raw(sql, pars...).Scan(&items).Error return } func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order) pars = append(pars, startSize, pageSize) err = global.DmSQL["master"].Raw(sql, pars...).Scan(&items).Error return } // BusinessConfUpdate 更新配置 type BusinessConfUpdate struct { ConfKey string ConfVal string } func GetBusinessConfByKey(key string) (item *BusinessConf, err error) { sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`) err = global.DmSQL["master"].Raw(sql, key).First(&item).Error return } // InitUseMongoConf // @Description: // @author: Roc // @datetime 2024-07-01 13:49:09 func InitUseMongoConf() { useMongo, e := GetBusinessConfByKey("UseMongo") if e != nil { return } if useMongo.ConfVal == `true` { utils.UseMongo = true } } func InitSmmDataMethodConf() { utils.SmmDataMethod = "terminal" useMongo, e := GetBusinessConfByKey("SmmDataMethod") if e != nil { return } if useMongo.ConfVal == "api" { utils.SmmDataMethod = "api" } }