package models import ( "eta_gn/eta_task/global" "eta_gn/eta_task/utils" "fmt" "html" "strings" "time" ) const ( BusinessConfUseXf = "UseXf" BusinessConfXfAppid = "XfAppid" BusinessConfXfApiKey = "XfApiKey" BusinessConfXfApiSecret = "XfApiSecret" BusinessConfXfVcn = "XfVcn" BusinessConfEnPptCoverImgs = "EnPptCoverImgs" ) type BusinessConf struct { Id int `gorm:"column:id;primaryKey"` // `orm:"column(id);pk"` ConfKey string `description:"配置Key"` ConfVal string `description:"配置值"` ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"` Necessary int `description:"是否必填:0-否;1-是"` Remark string `description:"备注"` CreateTime time.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["eta"].Create(m).Error return } func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) { if len(items) == 0 { return } err = global.DmSQL["eta"].CreateInBatches(items, utils.MultiAddNum).Error return } func (m *BusinessConf) Update(cols []string) (err error) { err = global.DmSQL["eta"].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["eta"].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["eta"].Raw(sql, id).Find(&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["eta"].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) AS count FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = global.DmSQL["eta"].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["eta"].Raw(sql, pars...).Find(&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["eta"].Raw(sql, pars...).Find(&items).Error return } func GetBusinessConf() (list map[string]string, err error) { list = make(map[string]string) var items []*BusinessConf sql := `SELECT * FROM business_conf` err = global.DmSQL["eta"].Raw(sql).Find(&items).Error if err != nil { return } for _, v := range items { if v.ValType == 4 { list[v.ConfKey] = html.UnescapeString(v.ConfVal) continue } list[v.ConfKey] = v.ConfVal } return } type BusinessConfUpdate struct { ConfKey string ConfVal string } func (m *BusinessConf) GetItemByConfKey(key string) (item *BusinessConf, err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE conf_key = ? LIMIT 1`, m.TableName()) err = global.DmSQL["eta"].Raw(sql, key).First(&item).Error return } type EdbStopRefreshRule struct { IsOpen int `description:"是否开启自动禁用1,开启,0未开启"` BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"` EdbStopDays int `description:"指标库间隔天数未引用则停用"` }