autoreply.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  104. // GetAutoReplayByAppID 获取微信对应的自动回复信息
  105. func (m *AutoreplyDAO) GetAutoReplayByAppID(appid, val string) (*model.TaAutoReply, error) {
  106. var reply []model.TaAutoReply
  107. // 先查询精确查询
  108. sql := `SELECT
  109. a.*
  110. FROM
  111. ta_auto_reply a
  112. INNER JOIN sys_org b ON a.org_id = b.org_id
  113. INNER JOIN sys_wechat_conf c ON b.wechat_id = c.conf_id
  114. INNER JOIN ta_auto_reply_keywords d ON a.auto_reply_id = d.auto_reply_id
  115. WHERE
  116. c.appid = ?
  117. AND a.is_use = ?
  118. AND a.status > ?
  119. AND b.status > ?
  120. AND a.auto_type = ?
  121. AND a.pair_type = ?
  122. AND d.keywords = ? order by a.create_date desc`
  123. err := m.db.Sql(sql, appid, models.AUTOREPLY_IS_USE_ON, models.STATUS_DEL, models.STATUS_DEL, models.AUTOREPLY_KEYWORDS, models.PAIR_TYPE_ACU, val).Find(&reply)
  124. if err != nil {
  125. return nil, err
  126. }
  127. if len(reply) > 0 {
  128. return &reply[0], nil
  129. }
  130. // 模糊查询
  131. sql = `SELECT DISTINCT
  132. a.*
  133. FROM
  134. ta_auto_reply a
  135. INNER JOIN sys_org b ON a.org_id = b.org_id
  136. INNER JOIN sys_wechat_conf c ON b.wechat_id = c.conf_id
  137. INNER JOIN ta_auto_reply_keywords d ON a.auto_reply_id = d.auto_reply_id
  138. WHERE
  139. c.appid = ?
  140. AND a.is_use = ?
  141. AND a.status > ?
  142. AND b.status > ?
  143. AND a.auto_type = ?
  144. AND a.pair_type = ?
  145. AND d.keywords like CONCAT('%',?, '%') order by a.create_date desc`
  146. err = m.db.Sql(sql, appid, models.AUTOREPLY_IS_USE_ON, models.STATUS_DEL, models.STATUS_DEL, models.AUTOREPLY_KEYWORDS, models.PAIR_TYPE_BLUR, val).Find(&reply)
  147. if err != nil {
  148. return nil, err
  149. }
  150. if len(reply) > 0 {
  151. return &reply[0], nil
  152. }
  153. return nil, nil
  154. }
  155. // GetSubscribeByAppID 获取微信关注自动回复
  156. func (m *AutoreplyDAO) GetSubscribeByAppID(appid string) (*model.TaAutoReply, error) {
  157. var replys []model.TaAutoReply
  158. sql := `select a.* from ta_auto_reply a inner join sys_org b on a.org_id=b.org_id
  159. inner join sys_wechat_conf c on b.wechat_id = c.conf_id where c.appid=? and a.is_use=? and a.auto_type=? and a.status>? and b.status>? order by a.create_date desc`
  160. err := m.db.Sql(sql, appid, models.AUTOREPLY_IS_USE_ON, models.AUTOREPLY_SUBSCRIBE, models.STATUS_DEL, models.STATUS_DEL).Find(&replys)
  161. if err != nil {
  162. return nil, err
  163. }
  164. if len(replys) > 0 {
  165. return &replys[0], nil
  166. }
  167. return nil, nil
  168. }
  169. func (m *AutoreplyDAO) DisableAutoreply(autoType, orgId, isUse string) error {
  170. var auto = model.TaAutoReply{
  171. AutoType: autoType,
  172. OrgId: orgId,
  173. IsUse: isUse,
  174. }
  175. var cols = []string{
  176. "is_use",
  177. }
  178. _, err := m.db.Cols(cols...).Where("auto_type = ?", auto.AutoType).And("org_id = ?", auto.OrgId).Update(auto)
  179. return err
  180. }