add.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package contact
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "time"
  13. "xingjia-management-gateway/apis"
  14. "xingjia-management-gateway/consts"
  15. "xingjia-management-gateway/errors"
  16. "xingjia-management-gateway/impl/v1/operation_log"
  17. dbmodel "xingjia-management-gateway/model"
  18. )
  19. func checkContactAddParam(req *apis.ContactAddRequest) error {
  20. switch {
  21. case req.SelfId == 0:
  22. return status.Error(10003, "未知操作者")
  23. }
  24. return nil
  25. }
  26. func getContactContent(p dbmodel.TContact) string {
  27. m := map[string]interface{}{
  28. "电话": p.Phone,
  29. "地址": p.Addr,
  30. "邮箱": p.Email,
  31. }
  32. bytes, _ := json.Marshal(m)
  33. return string(bytes)
  34. }
  35. //
  36. func ContactAdd(ctx context.Context, req *apis.ContactAddRequest) (reply *apis.ContactAddReply, err error) {
  37. reply = &apis.ContactAddReply{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. if err = checkContactAddParam(req); err != nil {
  51. return nil, err
  52. }
  53. now := time.Now()
  54. p := dbmodel.TContact{}
  55. p.Phone = req.Phone
  56. p.Addr = req.Addr
  57. p.Email = req.Email
  58. p.CreatedAt = now
  59. p.UpdatedAt = now
  60. exist := false
  61. operation := consts.OperationActionAdd
  62. if req.Id > 0 {
  63. exist = true
  64. operation = consts.OperationActionUpdate
  65. }
  66. db := database.DB().Begin()
  67. defer func() {
  68. if err != nil {
  69. db.Rollback()
  70. }
  71. }()
  72. originContent := ""
  73. targetContent := ""
  74. if !exist {
  75. p.ID = 1
  76. err = p.Insert(db)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. targetContent = getContactContent(p)
  81. } else {
  82. where := map[string]interface{}{
  83. "id": req.Id,
  84. }
  85. values := map[string]interface{}{
  86. "addr": req.Addr,
  87. "phone": req.Phone,
  88. "email": req.Email,
  89. "updated_at": time.Now(),
  90. }
  91. old := dbmodel.TContact{}
  92. err = old.Find(db, where)
  93. if err != nil {
  94. return nil, errors.DataBaseError
  95. }
  96. err = p.Update(db, where, values)
  97. if err != nil {
  98. return nil, errors.DataBaseError
  99. }
  100. originContent = getContactContent(old)
  101. targetContent = getContactContent(p)
  102. }
  103. // 操作日志
  104. logReq := &apis.OperationLogAddRequest{
  105. OriginContent: originContent,
  106. TargetContent: targetContent,
  107. Uid: req.SelfId,
  108. User: req.SelfName,
  109. Module: consts.OperationModuleContact,
  110. OperationTime: now.Unix(),
  111. Operation: int32(operation),
  112. }
  113. err = operation_log.OperationLogAddHandle(logReq, db)
  114. if err != nil {
  115. return nil, err
  116. }
  117. db.Commit()
  118. return reply, nil
  119. }