messages_service.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package wechat_service
  2. import (
  3. "SCRM/models"
  4. "SCRM/service"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. )
  8. //GetTextReplyMessagesByKey 通过关键词搜索 user_org_id(机构ID)取信息
  9. func GetTextReplyMessagesByKey(userOrgID int64, keywrods string) (messages []*models.AuthorizationMessageManagements, err error) {
  10. if len(keywrods) == 0 {
  11. return
  12. }
  13. likekey := "%" + keywrods + "%"
  14. err = service.PatientReadDB().Where("user_org_id=? AND message_msg_type='text' AND message_status=1 AND ((message_key_name LIKE ? AND message_key_type=1) OR (message_key_name = ? AND message_key_type=2))", userOrgID, likekey, keywrods).Order("id desc").Find(&messages).Error
  15. return
  16. }
  17. //GetOrgSubscribeReplyMessages 通过user_org_id(机构ID)取信息
  18. func GetOrgSubscribeReplyMessages(orgID int64) (*models.AuthorizationMessageManagements, error) {
  19. var message models.AuthorizationMessageManagements
  20. err := service.PatientReadDB().Where("user_org_id=? AND message_msg_type='event' AND message_msg_event='subscribe' AND message_status=1", orgID).First(&message).Error
  21. if err == gorm.ErrRecordNotFound {
  22. return nil, nil
  23. }
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &message, nil
  28. }
  29. //GetClickButtonReplyMessagesByOrgID 通过user_org_id(机构ID)取信息
  30. func GetClickButtonReplyMessagesByOrgID(orgID int64, key string) (*models.AuthorizationMessageManagements, error) {
  31. var message models.AuthorizationMessageManagements
  32. err := service.PatientReadDB().Where("user_org_id=? AND message_msg_type='event' AND message_msg_event='click' AND message_key_name=? AND message_status=1", orgID, key).First(&message).Error
  33. if err == gorm.ErrRecordNotFound {
  34. return nil, nil
  35. }
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &message, nil
  40. }
  41. func GetOrgAllClickMessages(orgID int64) (messages []*models.AuthorizationMessageManagements, err error) {
  42. err = service.PatientReadDB().Where("user_org_id=? AND message_msg_type='event' AND message_msg_event='click'", orgID).Find(&messages).Error
  43. return
  44. }
  45. func SaveMessage(m *models.AuthorizationMessageManagements) (err error) {
  46. err = service.PatientWriteDB().Save(m).Error
  47. return
  48. }
  49. func GetPageKeyWordMessages(orgID, page, limit int64, search string) (messages []*models.AuthorizationMessageManagements, total int64, err error) {
  50. db := service.PatientReadDB().Model(&models.AuthorizationMessageManagements{}).Where("user_org_id=? and message_msg_type='text' and message_status=1", orgID)
  51. if len(search) > 0 {
  52. searchKey := "%" + search + "%"
  53. db = db.Where("message_key_name LIKE ?", searchKey)
  54. }
  55. offset := (page - 1) * limit
  56. err = db.Count(&total).Order("id desc").Offset(offset).Limit(limit).Find(&messages).Error
  57. return
  58. }
  59. //GetMessageByID 通过ID取信息
  60. func GetMessageByID(orgID, id int64) (*models.AuthorizationMessageManagements, error) {
  61. var message models.AuthorizationMessageManagements
  62. err := service.PatientReadDB().Where("id=? and user_org_id=? and message_status=1", id, orgID).First(&message).Error
  63. if err == gorm.ErrRecordNotFound {
  64. return nil, nil
  65. }
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &message, nil
  70. }
  71. func DeleteMessages(orgID int64, ids []int64) (err error) {
  72. if len(ids) == 1 {
  73. err = service.PatientWriteDB().Model(&models.AuthorizationMessageManagements{}).Where("id =? and user_org_id =?", ids[0], orgID).Update(map[string]interface{}{"MessageStatus": 2, "UpdatedTime": time.Now().Unix()}).Error
  74. } else {
  75. err = service.PatientWriteDB().Model(&models.AuthorizationMessageManagements{}).Where("id IN (?) and user_org_id =?", ids, orgID).Update(map[string]interface{}{"MessageStatus": 2, "UpdatedTime": time.Now().Unix()}).Error
  76. }
  77. return
  78. }