sendMsg.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package service
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "app.yhyue.com/moapp/MessageCenter/entity"
  7. "app.yhyue.com/moapp/MessageCenter/rpc/message"
  8. )
  9. func SendMsg(this message.SendMsgRequest) (int64, string) {
  10. orm := entity.Engine.NewSession()
  11. defer orm.Close()
  12. err := orm.Begin()
  13. fmt.Println(err)
  14. count, _ := orm.Table("conversation").Select("*").Where("user_id = ? or user_id = ?", this.ReceiveUserId, this.SendUserId).Count()
  15. if count < 1 {
  16. conversation := entity.Conversation{
  17. AppId: this.Appid,
  18. ReceiveId: this.ReceiveUserId,
  19. ReceiveName: this.ReceiveName,
  20. SendId: this.SendUserId,
  21. SendName: this.SendName,
  22. CreateTime: time.Now(),
  23. Sort: 0,
  24. UserId: this.SendUserId,
  25. Key: "",
  26. }
  27. _, err = orm.Table("conversation").Insert(&conversation)
  28. if err != nil {
  29. log.Panicln("会话创建失败:", err)
  30. orm.Rollback()
  31. return -1, "会话创建失败"
  32. }
  33. conversations := entity.Conversation{
  34. AppId: this.Appid,
  35. ReceiveId: this.ReceiveUserId,
  36. ReceiveName: this.ReceiveName,
  37. SendId: this.SendUserId,
  38. SendName: this.SendName,
  39. CreateTime: time.Now(),
  40. Sort: 0,
  41. UserId: this.ReceiveUserId,
  42. Key: "",
  43. }
  44. _, err = orm.Table("conversation").Insert(&conversations)
  45. if err != nil {
  46. log.Panicln("会话创建失败:", err)
  47. orm.Rollback()
  48. return -1, "会话创建失败"
  49. }
  50. }
  51. message := entity.Message{
  52. AppId: this.Appid,
  53. ReceiveUserid: this.ReceiveUserId,
  54. ReceiveName: this.ReceiveName,
  55. SendUserid: this.SendUserId,
  56. SendName: this.SendName,
  57. CreateTime: time.Now(),
  58. Title: this.Title,
  59. MsgType: int(this.MsgType),
  60. Link: this.Link,
  61. CiteId: int(this.CiteId),
  62. Content: this.Content,
  63. IsRead: 0,
  64. Isdel: 1,
  65. }
  66. _, err = orm.Table("message").Insert(&message)
  67. if err != nil {
  68. log.Panicln("消息发送失败:", err)
  69. orm.Rollback()
  70. return -1, "消息发送失败"
  71. }
  72. orm.Commit()
  73. return 1, "消息发送成功"
  74. }