dao.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package manager
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/up/conf"
  10. "go-common/app/admin/main/up/model/signmodel"
  11. "go-common/app/admin/main/up/model/upcrmmodel"
  12. "go-common/library/database/sql"
  13. "go-common/library/log"
  14. bm "go-common/library/net/http/blademaster"
  15. xtime "go-common/library/time"
  16. "go-common/library/xstr"
  17. )
  18. const (
  19. // URLUNames url for usernames to uid
  20. URLUNames = "/x/admin/manager/users/unames"
  21. // URLUids url for uids to username
  22. URLUids = "/x/admin/manager/users/uids"
  23. // URLAuditLog url for sign up change log
  24. URLAuditLog = "/x/admin/search/log"
  25. )
  26. // Dao is redis dao.
  27. type Dao struct {
  28. c *conf.Config
  29. managerDB *sql.DB
  30. HTTPClient *bm.Client
  31. }
  32. // New fn
  33. func New(c *conf.Config) (d *Dao) {
  34. d = &Dao{
  35. c: c,
  36. managerDB: sql.NewMySQL(c.DB.Manager),
  37. // http client
  38. HTTPClient: bm.NewClient(c.HTTPClient.Normal),
  39. }
  40. return d
  41. }
  42. // Close fn
  43. func (d *Dao) Close() {
  44. if d.managerDB != nil {
  45. d.managerDB.Close()
  46. }
  47. }
  48. // Ping ping cpdb
  49. func (d *Dao) Ping(c context.Context) (err error) {
  50. return d.managerDB.Ping(c)
  51. }
  52. // GetUNamesByUids get name by uid
  53. func (d *Dao) GetUNamesByUids(c context.Context, uids []int64) (res map[int64]string, err error) {
  54. var param = url.Values{}
  55. var uidStr = xstr.JoinInts(uids)
  56. param.Set("uids", uidStr)
  57. var httpRes struct {
  58. Code int `json:"code"`
  59. Data map[int64]string `json:"data"`
  60. Message string `json:"message"`
  61. }
  62. err = d.HTTPClient.Get(c, d.c.Host.Manager+URLUNames, "", param, &httpRes)
  63. if err != nil {
  64. log.Error("d.client.Get(%s) error(%v)", d.c.Host.Manager+URLUNames+"?"+param.Encode(), err)
  65. return
  66. }
  67. if httpRes.Code != 0 {
  68. log.Error("url(%s) error(%v), code(%d), message(%s)", d.c.Host.Manager+URLUNames+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
  69. }
  70. res = httpRes.Data
  71. return
  72. }
  73. // GetUIDByNames get uid by name
  74. func (d *Dao) GetUIDByNames(c context.Context, names []string) (res map[string]int64, err error) {
  75. var param = url.Values{}
  76. var namesStr = strings.Join(names, ",")
  77. param.Set("unames", namesStr)
  78. var httpRes struct {
  79. Code int `json:"code"`
  80. Data map[string]int64 `json:"data"`
  81. Message string `json:"message"`
  82. }
  83. err = d.HTTPClient.Get(c, d.c.Host.Manager+URLUids, "", param, &httpRes)
  84. if err != nil {
  85. log.Error("d.client.Get(%s) error(%v)", d.c.Host.Manager+URLUids+"?"+param.Encode(), err)
  86. return
  87. }
  88. if httpRes.Code != 0 {
  89. log.Error("url(%s) error(%v), code(%d), message(%s)", d.c.Host.Manager+URLUids+"?"+param.Encode(), err, httpRes.Code, httpRes.Message)
  90. }
  91. res = httpRes.Data
  92. return
  93. }
  94. // SignUpAuditLogs get sign up audit log .
  95. func (d *Dao) SignUpAuditLogs(c context.Context, arg *signmodel.SignOpSearchArg) (res *signmodel.SignAuditListReply, err error) {
  96. params := url.Values{}
  97. params.Set("appid", "log_audit")
  98. params.Set("business", fmt.Sprintf("%d", signmodel.SignUpLogBizID))
  99. params.Set("order", arg.Order)
  100. params.Set("sort", arg.Sort)
  101. params.Set("int_0", fmt.Sprintf("%d", arg.SignID))
  102. params.Set("oid", fmt.Sprintf("%d", arg.Mid))
  103. params.Set("type", fmt.Sprintf("%d", arg.Tp))
  104. params.Set("pn", fmt.Sprintf("%d", arg.PN))
  105. params.Set("ps", fmt.Sprintf("%d", arg.PS))
  106. var httpRes struct {
  107. Code int `json:"code"`
  108. Data *signmodel.BaseAuditListReply `json:"data"`
  109. Message string `json:"message"`
  110. }
  111. err = d.HTTPClient.Get(c, d.c.Host.Manager+URLAuditLog, "", params, &httpRes)
  112. if err != nil {
  113. log.Error("d.client.Get(%s) error(%v)", d.c.Host.Manager+URLAuditLog+"?"+params.Encode(), err)
  114. return
  115. }
  116. if httpRes.Code != 0 {
  117. log.Error("url(%s) error(%v), code(%d), message(%s)", d.c.Host.Manager+URLAuditLog+"?"+params.Encode(), err, httpRes.Code, httpRes.Message)
  118. }
  119. res = new(signmodel.SignAuditListReply)
  120. res.Page = arg.PN
  121. res.Size = arg.PS
  122. res.Order = arg.Order
  123. res.Sort = arg.Sort
  124. if httpRes.Data != nil && httpRes.Data.Pager != nil {
  125. res.TotalCount = httpRes.Data.Pager.TotalCount
  126. }
  127. for _, v := range httpRes.Data.Result {
  128. ctime, _ := time.ParseInLocation(upcrmmodel.TimeFmtDateTime, v.CTime, time.Local)
  129. var signAuit = &signmodel.SignAuditReply{
  130. Mid: v.OID,
  131. SignID: v.IntOne,
  132. Tp: v.Tp,
  133. OperID: v.UID,
  134. OperName: v.UName,
  135. CTime: xtime.Time(ctime.Unix()),
  136. }
  137. var content signmodel.SignContentReply
  138. json.Unmarshal([]byte(v.ExtraData), &content)
  139. signAuit.Content = &content
  140. if signAuit.Content.New != nil {
  141. buildSignContractURL(signAuit.Content.New)
  142. }
  143. if signAuit.Content.Old != nil {
  144. buildSignContractURL(signAuit.Content.Old)
  145. }
  146. res.Result = append(res.Result, signAuit)
  147. }
  148. return
  149. }
  150. func buildSignContractURL(su *signmodel.SignUpArg) {
  151. if su.ContractInfo == nil {
  152. return
  153. }
  154. for _, v := range su.ContractInfo {
  155. v.Filelink = signmodel.BuildOrcBfsURL(v.Filelink)
  156. v.Filelink = signmodel.BuildDownloadURL(v.Filename, v.Filelink)
  157. }
  158. }