autoreply.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package autoreply
  2. import (
  3. "strconv"
  4. "time"
  5. "wechat-conf/models"
  6. "wechat-conf/models/model"
  7. "wechat-conf/utils"
  8. "github.com/go-xorm/xorm"
  9. )
  10. // AutoreplyDAO 当前数据库操作对象
  11. type AutoreplyDAO struct {
  12. ctx *utils.Context
  13. db *xorm.Session
  14. }
  15. // NewAutoreplyDAO New Inst
  16. func NewAutoreplyDAO(ctx *utils.Context) *AutoreplyDAO {
  17. return &AutoreplyDAO{
  18. ctx: ctx,
  19. db: ctx.DB,
  20. }
  21. }
  22. func (m *AutoreplyDAO) GetAutoReplyList(orgId, autoType string, page, pagesize int) ([]model.TaAutoReply, error) {
  23. var autoreply []model.TaAutoReply
  24. sql := `SELECT
  25. *
  26. FROM
  27. ta_auto_reply
  28. WHERE
  29. org_id = '` + orgId + `'
  30. AND auto_type = '` + autoType + `'
  31. and status >`+ strconv.Itoa(models.STATUS_DEL)
  32. sql += ` order by create_date desc limit ` + strconv.Itoa((page-1)*pagesize) + `, ` + strconv.Itoa(pagesize)
  33. err := m.db.Sql(sql).Find(&autoreply)
  34. return autoreply, err
  35. }
  36. func (m *AutoreplyDAO) GetAutoReplyListCount(orgId, autoType string) (int, error) {
  37. var autoreply []model.TaAutoReply
  38. sql := `SELECT
  39. *
  40. FROM
  41. ta_auto_reply
  42. WHERE
  43. org_id = '` + orgId + `'
  44. AND auto_type = '` + autoType + `'`
  45. err := m.db.Sql(sql).Find(&autoreply)
  46. return len(autoreply), err
  47. }
  48. func (m *AutoreplyDAO) AddAutoReply(auto model.TaAutoReply) (*model.TaAutoReply, error) {
  49. auto.CreateDate = time.Now()
  50. auto.Status = models.STATUS_NORMAL
  51. auto.AutoReplyId = utils.GetGUID()
  52. _, err := m.db.Insert(auto)
  53. return &auto, err
  54. }
  55. func (m *AutoreplyDAO) AddKeyword(keyword model.TaAutoReplyKeywords) error {
  56. keyword.KeywordsId = utils.GetGUID()
  57. keyword.Status = models.STATUS_NORMAL
  58. _, err := m.db.Insert(keyword)
  59. return err
  60. }
  61. func (m *AutoreplyDAO) UpdateAutoRelpy(auto model.TaAutoReply) error {
  62. var cols = []string{
  63. "auto_type",
  64. "rule_name",
  65. "pair_type",
  66. "message_type",
  67. "message_paragraph",
  68. "message_img",
  69. "message_content",
  70. "keywords",
  71. }
  72. _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto)
  73. return err
  74. }
  75. func (m *AutoreplyDAO) DeleteKeywords(autoReplyId string) error {
  76. _, err := m.db.Delete(&model.TaAutoReplyKeywords{AutoReplyId: autoReplyId})
  77. return err
  78. }
  79. func (m *AutoreplyDAO) DeleteAutoReply(autoReplyId string) error {
  80. var auto = model.TaAutoReply{
  81. AutoReplyId: autoReplyId,
  82. Status: models.STATUS_DEL,
  83. }
  84. var cols = []string{
  85. "status",
  86. }
  87. _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto)
  88. return err
  89. }
  90. func (m *AutoreplyDAO) GetAutoReplyById(autoReplyId string) (*model.TaAutoReply, error) {
  91. var autoReply []model.TaAutoReply
  92. sql := `SELECT
  93. *
  94. FROM
  95. ta_auto_reply
  96. WHERE
  97. auto_reply_id = '` + autoReplyId + `'`
  98. err := m.db.Sql(sql).Find(&autoReply)
  99. if len(autoReply) > 0 {
  100. return &autoReply[0], err
  101. }
  102. return nil, err
  103. }