autoreply.go 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/astaxie/beego"
  9. "github.com/go-xorm/xorm"
  10. )
  11. // AutoreplyDAO 当前数据库操作对象
  12. type AutoreplyDAO struct {
  13. ctx *utils.Context
  14. db *xorm.Session
  15. }
  16. // NewAutoreplyDAO New Inst
  17. func NewAutoreplyDAO(ctx *utils.Context) *AutoreplyDAO {
  18. return &AutoreplyDAO{
  19. ctx: ctx,
  20. db: ctx.DB,
  21. }
  22. }
  23. type AutoReply struct {
  24. model.TaAutoReply `xorm:"extends"`
  25. Url string
  26. }
  27. func (m *AutoreplyDAO) GetAutoReplyList(orgId, autoType string, page, pagesize int) ([]AutoReply, error) {
  28. var autoreply []AutoReply
  29. sql := `SELECT
  30. a.*,
  31. b.url
  32. FROM
  33. ta_auto_reply a
  34. LEFT JOIN ta_wechat_img b ON a.message_img = b.media_id
  35. WHERE
  36. a.org_id = '` + orgId + `'
  37. AND a.auto_type = '` + autoType + `'
  38. and a.status >` + strconv.Itoa(models.STATUS_DEL)
  39. sql += ` order by create_date desc limit ` + strconv.Itoa((page-1)*pagesize) + `, ` + strconv.Itoa(pagesize)
  40. err := m.db.Sql(sql).Find(&autoreply)
  41. return autoreply, err
  42. }
  43. func (m *AutoreplyDAO) GetAutoReplyListCount(orgId, autoType string) (int, error) {
  44. var autoreply []model.TaAutoReply
  45. sql := `SELECT
  46. *
  47. FROM
  48. ta_auto_reply
  49. WHERE
  50. org_id = '` + orgId + `'
  51. AND auto_type = '` + autoType + `'`
  52. err := m.db.Sql(sql).Find(&autoreply)
  53. return len(autoreply), err
  54. }
  55. func (m *AutoreplyDAO) AddAutoReply(auto model.TaAutoReply) (*model.TaAutoReply, error) {
  56. auto.CreateDate = time.Now()
  57. auto.Status = models.STATUS_NORMAL
  58. auto.AutoReplyId = utils.GetGUID()
  59. _, err := m.db.Insert(auto)
  60. return &auto, err
  61. }
  62. func (m *AutoreplyDAO) AddKeyword(keyword model.TaAutoReplyKeywords) error {
  63. keyword.KeywordsId = utils.GetGUID()
  64. keyword.Status = models.STATUS_NORMAL
  65. _, err := m.db.Insert(keyword)
  66. return err
  67. }
  68. func (m *AutoreplyDAO) UpdateAutoRelpy(auto model.TaAutoReply) error {
  69. var cols = []string{
  70. "auto_type",
  71. "rule_name",
  72. "pair_type",
  73. "message_type",
  74. "message_paragraph",
  75. "message_img",
  76. "message_content",
  77. "keywords",
  78. }
  79. _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto)
  80. return err
  81. }
  82. func (m *AutoreplyDAO) DeleteKeywords(autoReplyId string) error {
  83. _, err := m.db.Delete(&model.TaAutoReplyKeywords{AutoReplyId: autoReplyId})
  84. return err
  85. }
  86. func (m *AutoreplyDAO) DeleteAutoReply(autoReplyId string) error {
  87. var auto = model.TaAutoReply{
  88. AutoReplyId: autoReplyId,
  89. Status: models.STATUS_DEL,
  90. }
  91. var cols = []string{
  92. "status",
  93. }
  94. _, err := m.db.Cols(cols...).Where("auto_reply_id = ?", auto.AutoReplyId).Update(auto)
  95. return err
  96. }
  97. func (m *AutoreplyDAO) GetAutoReplyById(autoReplyId string) (*AutoReply, error) {
  98. var autoReply []AutoReply
  99. sql := `SELECT
  100. a.*,
  101. b.url
  102. FROM
  103. ta_auto_reply a
  104. LEFT JOIN ta_wechat_img b ON a.message_img = b.media_id
  105. WHERE
  106. a.auto_reply_id = '` + autoReplyId + `'`
  107. err := m.db.Sql(sql).Find(&autoReply)
  108. if len(autoReply) > 0 {
  109. return &autoReply[0], err
  110. }
  111. return nil, err
  112. }
  113. // GetAutoReplayByAppID 获取微信对应的自动回复信息
  114. func (m *AutoreplyDAO) GetAutoReplayByAppID(appid, val string) (*model.TaAutoReply, error) {
  115. var reply []model.TaAutoReply
  116. // 先查询精确查询
  117. sql := `SELECT
  118. a.*
  119. FROM
  120. ta_auto_reply a
  121. INNER JOIN sys_org b ON a.org_id = b.org_id
  122. INNER JOIN sys_wechat_conf c ON b.wechat_id = c.conf_id
  123. INNER JOIN ta_auto_reply_keywords d ON a.auto_reply_id = d.auto_reply_id
  124. WHERE
  125. c.appid = ?
  126. AND a.is_use = ?
  127. AND a.status > ?
  128. AND b.status > ?
  129. AND a.auto_type = ?
  130. AND a.pair_type = ?
  131. AND d.keywords = ? order by a.create_date desc`
  132. 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)
  133. if err != nil {
  134. return nil, err
  135. }
  136. if len(reply) > 0 {
  137. return &reply[0], nil
  138. }
  139. // 模糊查询
  140. sql = `SELECT DISTINCT
  141. a.*
  142. FROM
  143. ta_auto_reply a
  144. INNER JOIN sys_org b ON a.org_id = b.org_id
  145. INNER JOIN sys_wechat_conf c ON b.wechat_id = c.conf_id
  146. INNER JOIN ta_auto_reply_keywords d ON a.auto_reply_id = d.auto_reply_id
  147. WHERE
  148. c.appid = ?
  149. AND a.is_use = ?
  150. AND a.status > ?
  151. AND b.status > ?
  152. AND a.auto_type = ?
  153. AND a.pair_type = ?
  154. AND ? like CONCAT('%',d.keywords, '%')
  155. order by a.create_date desc`
  156. 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)
  157. if err != nil {
  158. return nil, err
  159. }
  160. if len(reply) > 0 {
  161. return &reply[0], nil
  162. beego.Info("__________________________REPLY__________________________")
  163. beego.Info(reply[0])
  164. }
  165. return nil, nil
  166. }
  167. // GetSubscribeByAppID 获取微信关注自动回复
  168. func (m *AutoreplyDAO) GetSubscribeByAppID(appid string) (*model.TaAutoReply, error) {
  169. var replys []model.TaAutoReply
  170. sql := `select a.* from ta_auto_reply a inner join sys_org b on a.org_id=b.org_id
  171. 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`
  172. err := m.db.Sql(sql, appid, models.AUTOREPLY_IS_USE_ON, models.AUTOREPLY_SUBSCRIBE, models.STATUS_DEL, models.STATUS_DEL).Find(&replys)
  173. if err != nil {
  174. return nil, err
  175. }
  176. if len(replys) > 0 {
  177. return &replys[0], nil
  178. }
  179. return nil, nil
  180. }
  181. func (m *AutoreplyDAO) DisableAutoreply(autoType, orgId, isUse string) error {
  182. var auto = model.TaAutoReply{
  183. AutoType: autoType,
  184. OrgId: orgId,
  185. IsUse: isUse,
  186. }
  187. var cols = []string{
  188. "is_use",
  189. }
  190. _, err := m.db.Cols(cols...).Where("auto_type = ?", auto.AutoType).And("org_id = ?", auto.OrgId).Update(auto)
  191. return err
  192. }