123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. auto.IsUse = models.AUTOREPLY_IS_USE_ON
  53. _, err := m.db.Insert(auto)
  54. return &auto, err
  55. }
  56. func (m *AutoreplyDAO) AddKeyword(keyword model.TaAutoReplyKeywords) error {
  57. keyword.KeywordsId = utils.GetGUID()
  58. keyword.Status = models.STATUS_NORMAL
  59. _, err := m.db.Insert(keyword)
  60. return err
  61. }
  62. func (m *AutoreplyDAO) UpdateAutoRelpy(auto model.TaAutoReply) error {
  63. var cols = []string{
  64. "auto_type",
  65. "rule_name",
  66. "pair_type",
  67. "message_type",
  68. "message_paragraph",
  69. "message_img",
  70. "message_content",
  71. "keywords",
  72. }
  73. _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto)
  74. return err
  75. }
  76. func (m *AutoreplyDAO) DeleteKeywords(autoReplyId string) error {
  77. _, err := m.db.Delete(&model.TaAutoReplyKeywords{AutoReplyId: autoReplyId})
  78. return err
  79. }
  80. func (m *AutoreplyDAO) DeleteAutoReply(autoReplyId string) error {
  81. var auto = model.TaAutoReply{
  82. AutoReplyId: autoReplyId,
  83. Status: models.STATUS_DEL,
  84. }
  85. var cols = []string{
  86. "status",
  87. }
  88. _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto)
  89. return err
  90. }
  91. func (m *AutoreplyDAO) GetAutoReplyById(autoReplyId string) (*model.TaAutoReply, error) {
  92. var autoReply []model.TaAutoReply
  93. sql := `SELECT
  94. *
  95. FROM
  96. ta_auto_reply
  97. WHERE
  98. auto_reply_id = '` + autoReplyId + `'`
  99. err := m.db.Sql(sql).Find(&autoReply)
  100. if len(autoReply) > 0 {
  101. return &autoReply[0], err
  102. }
  103. return nil, err
  104. }
  105. // GetAutoReplayByAppID 获取微信对应的自动回复信息
  106. func (m *AutoreplyDAO) GetAutoReplayByAppID(appid, val string) (*model.TaAutoReply, error) {
  107. var reply []model.TaAutoReply
  108. // 先查询精确查询
  109. sql := `SELECT
  110. a.*
  111. FROM
  112. ta_auto_reply a
  113. INNER JOIN sys_org b ON a.org_id = b.org_id
  114. INNER JOIN sys_wechat_conf c ON b.wechat_id = c.conf_id
  115. INNER JOIN ta_auto_reply_keywords d ON a.auto_reply_id = d.auto_reply_id
  116. WHERE
  117. c.appid = ?
  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.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
  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.status > ?
  141. AND b.status > ?
  142. AND a.auto_type = ?
  143. AND a.pair_type = ?
  144. AND ? like CONCAT('%', d.keywords, '%') order by a.create_date desc`
  145. err = m.db.Sql(sql, appid, models.STATUS_DEL, models.STATUS_DEL, models.AUTOREPLY_KEYWORDS, models.PAIR_TYPE_BLUR, val).Find(&reply)
  146. if err != nil {
  147. return nil, err
  148. }
  149. if len(reply) > 0 {
  150. return &reply[0], nil
  151. }
  152. return nil, nil
  153. }
  154. // GetSubscribeByAppID 获取微信关注自动回复
  155. func (m *AutoreplyDAO) GetSubscribeByAppID(appid string) (*model.TaAutoReply, error) {
  156. var replys []model.TaAutoReply
  157. sql := `select a.* from ta_auto_reply a inner join sys_org b on a.org_id=b.org_id
  158. inner join sys_wechat_conf c on b.wechat_id = c.conf_id where c.appid=? and a.auto_type=? and a.status>? and b.status>? order by a.create_date desc`
  159. err := m.db.Sql(sql, appid, models.AUTOREPLY_SUBSCRIBE, models.STATUS_DEL, models.STATUS_DEL).Find(&replys)
  160. if err != nil {
  161. return nil, err
  162. }
  163. if len(replys) > 0 {
  164. return &replys[0], nil
  165. }
  166. return nil, nil
  167. }
  168. func (m *AutoreplyDAO) DisableAutoreply(autoType, orgId, isUse string) error {
  169. var auto = model.TaAutoReply{
  170. AutoType: autoType,
  171. OrgId: orgId,
  172. IsUse: isUse,
  173. }
  174. var cols = []string{
  175. "is_use",
  176. }
  177. _, err := m.db.Cols(cols...).Where("auto_type = ?", auto.AutoType).And("org_id = ?", auto.OrgId).Update(auto)
  178. return err
  179. }