keyvalue.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package keyvalue
  2. import (
  3. "strconv"
  4. "wechat-conf/models"
  5. "wechat-conf/models/model"
  6. "wechat-conf/utils"
  7. "github.com/go-xorm/xorm"
  8. )
  9. // KeyvalueDAO 当前数据库操作对象
  10. type KeyvalueDAO struct {
  11. ctx *utils.Context
  12. db *xorm.Session
  13. }
  14. // NewKeyvalueDAO New Inst
  15. func NewKeyvalueDAO(ctx *utils.Context) *KeyvalueDAO {
  16. return &KeyvalueDAO{
  17. ctx: ctx,
  18. db: ctx.DB,
  19. }
  20. }
  21. func (m *KeyvalueDAO) GetValueByKey(key string) (*model.TaWechatKeyValue, error) {
  22. var keyvalue []model.TaWechatKeyValue
  23. sql := `SELECT
  24. *
  25. FROM
  26. ta_wechat_key_value
  27. WHERE
  28. key_id = '` + key + `'
  29. and status > ` + strconv.Itoa(models.STATUS_DEL)
  30. err := m.db.Sql(sql).Find(&keyvalue)
  31. if len(keyvalue) > 0 {
  32. return &keyvalue[0], err
  33. }
  34. return nil, err
  35. }
  36. func (m *KeyvalueDAO) AddValueKey(valueKey model.TaWechatKeyValue) error {
  37. valueKey.Status = models.STATUS_NORMAL
  38. _, err := m.db.Insert(valueKey)
  39. return err
  40. }
  41. func (m *KeyvalueDAO) DeleteByOrgId(orgId string) error {
  42. _, err := m.db.Delete(&model.TaWechatKeyValue{OrgId: orgId})
  43. return err
  44. }