common.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package utils
  4. import (
  5. "adm-dws/apis"
  6. "adm-dws/common.in/clinit"
  7. "adm-dws/common.in/mq"
  8. "adm-dws/consts"
  9. "adm-dws/model"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/tidwall/gjson"
  13. "go.uber.org/zap"
  14. )
  15. func getKey(content string,keys []string) string{
  16. key := ""
  17. if keys == nil {
  18. return key
  19. }
  20. for _,v := range keys{
  21. key = key+gjson.Get(content, v).String()
  22. }
  23. return key
  24. }
  25. // 获取主键
  26. func GetPrimaryKey(content string,outputSource string ,keys []string ) (string,string) {
  27. key := getKey(content,keys)
  28. if key == ""{
  29. return "",""
  30. }
  31. lockKey := fmt.Sprintf("dws-lock-%s-%s",outputSource,key)
  32. taskKey := fmt.Sprintf("dws-task-%s-%s",outputSource,key)
  33. return lockKey,taskKey
  34. }
  35. func SendAdsMsg(adsMsg *apis.AdsMessage) error {
  36. mqMsg,_ := json.Marshal(adsMsg)
  37. err := mq.AdsMq.PublishMsg(mqMsg)
  38. if err != nil{
  39. // 发送消息失败记录消息到本地库
  40. failMsg := &model.FailMsg{}
  41. failMsg.Msg = string(mqMsg)
  42. failMsg.MsgType = consts.ADSMSGTYPE
  43. err = failMsg.Insert(clinit.DB())
  44. if err != nil {
  45. l.Error("mysql",
  46. zap.String("sql", "insert into t_adm-dws_fail_msg"),
  47. zap.String("fields", failMsg.Msg),
  48. zap.String("error", err.Error()))
  49. }
  50. return err
  51. }
  52. return err
  53. }
  54. func SendDwsMsg(dwsMsg *apis.DwsMessage) error{
  55. mqMsg,_ := json.Marshal(*dwsMsg)
  56. err := mq.DwsMq.PublishMsg(mqMsg)
  57. if err != nil{
  58. // 发送消息失败记录消息到本地库
  59. failMsg := &model.FailMsg{}
  60. failMsg.Msg = string(mqMsg)
  61. failMsg.MsgType = consts.DWSMSGTYPE
  62. err = failMsg.Insert(clinit.DB())
  63. if err != nil {
  64. l.Error("mysql",
  65. zap.String("sql", "insert into t_adm-dws_fail_msg"),
  66. zap.String("fields", failMsg.Msg),
  67. zap.String("error", err.Error()))
  68. }
  69. return err
  70. }
  71. return err
  72. }
  73. func SendMd5Msg(msg map[string]string) error{
  74. mqMsg,_ := json.Marshal(msg)
  75. err := mq.Md5Mq.PublishMsg(mqMsg)
  76. if err != nil{
  77. l.Error("send msg",
  78. zap.String("call", "Md5Mq.PublishMsg"),
  79. zap.String("error", err.Error()))
  80. return err
  81. }
  82. return err
  83. }