user_update.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package user_merchant
  2. import (
  3. "context"
  4. "gd_management/apis"
  5. "gd_management/errors"
  6. "gd_management/impl/pubsub"
  7. "gd_management/rpc_apis"
  8. "gd_management/rpc_apis/gd_access_log"
  9. "strings"
  10. "time"
  11. "gd_management/common.in/storage"
  12. "gd_management/common.in/utils"
  13. "github.com/astaxie/beego/orm"
  14. "go.uber.org/zap"
  15. )
  16. func getUserMerchantInfo(o orm.Ormer, merchantId int64) (*apis.TGdUser, *apis.TGdMerchants, error) {
  17. userInfo := apis.TGdUser{}
  18. merchantInfo := apis.TGdMerchants{}
  19. err := o.QueryTable("t_gd_merchants").Filter("id", merchantId).One(&merchantInfo)
  20. if err != nil {
  21. if err == orm.ErrNoRows {
  22. return nil, nil, errors.DataBaseNoRecord
  23. }
  24. return nil, nil, errors.DataBaseError
  25. }
  26. err = o.QueryTable("t_gd_user").Filter("id", merchantInfo.UserId).One(&userInfo)
  27. if err != nil {
  28. if err == orm.ErrNoRows {
  29. return nil, nil, errors.DataBaseNoRecord
  30. }
  31. return nil, nil, errors.DataBaseError
  32. }
  33. return &userInfo, &merchantInfo, nil
  34. }
  35. func checkUpdateParam(o orm.Ormer, phone, name, socialCode string) error {
  36. if phone != "" {
  37. exist := o.QueryTable("t_gd_user").Filter("phone", phone).Exist()
  38. if exist {
  39. return errors.UserPhoneExist
  40. }
  41. }
  42. if name != "" {
  43. exist := o.QueryTable("t_gd_user").Filter("name", name).Exist()
  44. if exist {
  45. return errors.UserNameExist
  46. }
  47. }
  48. /*if socialCode != "" {
  49. exist := o.QueryTable("t_gd_merchants").Filter("social_code", socialCode).Exist()
  50. if exist {
  51. return errors.UserNameExist
  52. }
  53. }*/
  54. return nil
  55. }
  56. func updateUser(req *apis.ManagementUpdateUserMerchantReq, reply *apis.ManagementUpdateUserMerchantReply) error {
  57. if req.MerchantId == 0 {
  58. return errors.ArgsError
  59. }
  60. //验证ip地址
  61. if req.MerchantInfo.IpWhitelist != "" {
  62. ip := strings.Split(req.MerchantInfo.IpWhitelist, ",")
  63. for _, v := range ip {
  64. ips := strings.Split(v, "-")
  65. for _, realIp := range ips {
  66. if !utils.VerifyIp(realIp) {
  67. return errors.IpAddressErr
  68. }
  69. }
  70. }
  71. }
  72. updateTask := func(db orm.Ormer) error {
  73. // TODO 检查是否存在 checkParam
  74. userInfo, merchantInfo, err := getUserMerchantInfo(db, req.MerchantId)
  75. if err != nil {
  76. return err
  77. }
  78. //user info
  79. now := time.Now().Format("2006-01-02 15:04:05")
  80. req.UserInfo.UpdateTime = now
  81. req.UserInfo.Id = userInfo.Id
  82. if req.UserInfo.Password == "" {
  83. req.UserInfo.Password = userInfo.Password
  84. } else {
  85. req.UserInfo.Password = encryptPassword(req.UserInfo.Password)
  86. }
  87. if req.UserInfo.Name != userInfo.Name {
  88. err = checkUpdateParam(db, "", req.UserInfo.Name, "")
  89. if err != nil {
  90. return err
  91. }
  92. }
  93. if req.UserInfo.Name == "" {
  94. req.UserInfo.Name = userInfo.Name
  95. }
  96. if req.UserInfo.Phone != userInfo.Phone {
  97. err = checkUpdateParam(db, req.UserInfo.Phone, "", "")
  98. if err != nil {
  99. return err
  100. }
  101. }
  102. if req.UserInfo.Phone == "" {
  103. req.UserInfo.Phone = userInfo.Phone
  104. }
  105. if req.UserInfo.Email == "" {
  106. req.UserInfo.Email = userInfo.Email
  107. }
  108. req.UserInfo.CreateTime = userInfo.CreateTime
  109. _, err = db.Update(&req.UserInfo)
  110. if err != nil {
  111. return errors.DataBaseError
  112. }
  113. //merchant info
  114. req.MerchantInfo.UpdateTime = now
  115. req.MerchantInfo.Id = req.MerchantId
  116. req.MerchantInfo.AppKey = merchantInfo.AppKey
  117. req.MerchantInfo.UserId = merchantInfo.UserId
  118. req.MerchantInfo.AppSecret = merchantInfo.AppSecret
  119. req.MerchantInfo.AuthStatus = merchantInfo.AuthStatus
  120. req.MerchantInfo.MerchantType = merchantInfo.MerchantType
  121. req.MerchantInfo.Arrearage = merchantInfo.Arrearage
  122. if req.MerchantInfo.MerchantName == "" {
  123. req.MerchantInfo.MerchantName = merchantInfo.MerchantName
  124. }
  125. if req.MerchantInfo.SocialCode == "" {
  126. /*if req.MerchantInfo.SocialCode != merchantInfo.SocialCode {
  127. err = checkUpdateParam(db, "", "", req.MerchantInfo.SocialCode)
  128. if err != nil {
  129. return err
  130. }
  131. }*/
  132. req.MerchantInfo.SocialCode = merchantInfo.SocialCode
  133. }
  134. if req.MerchantInfo.Address == "" {
  135. req.MerchantInfo.Address = merchantInfo.Address
  136. }
  137. if req.MerchantInfo.ContactName == "" {
  138. req.MerchantInfo.ContactName = merchantInfo.ContactName
  139. }
  140. if req.MerchantInfo.ContactNumber == "" {
  141. req.MerchantInfo.ContactNumber = merchantInfo.ContactNumber
  142. }
  143. if req.MerchantInfo.MerchantLicense == "" {
  144. req.MerchantInfo.MerchantLicense = merchantInfo.MerchantLicense
  145. }
  146. _, err = db.Update(&req.MerchantInfo, "merchant_name", "social_code", "address", "contact_name", "contact_number", "merchant_license", "ip_whitelist", "update_time", "warning_enable", "company_name", "email", "is_http_code")
  147. if err != nil {
  148. return errors.DataBaseError
  149. }
  150. if req.MerchantInfo.MerchantName != merchantInfo.MerchantName {
  151. dreq := gd_access_log.LogUpdateApiNameReq{
  152. Type: 2,
  153. Name: req.MerchantInfo.MerchantName,
  154. Id: req.MerchantId,
  155. }
  156. go func() error {
  157. _, err := rpc_apis.AccessLog.LogUpdateApiName(context.Background(), &dreq)
  158. return err
  159. }()
  160. }
  161. //redis
  162. utils.RedisSet("t_gd_merchants", req.MerchantInfo.AppKey, req.MerchantInfo)
  163. if err := pubsub.PublishMerchantNotify(merchantInfo.AppKey); err != nil {
  164. return err
  165. }
  166. if err := pubsub.PublishNameNotify("merchant_id", req.MerchantId, 0); err != nil {
  167. return err
  168. }
  169. return nil
  170. }
  171. tasks := []storage.DbaTasker{}
  172. tasks = append(tasks, storage.GenerateDbaTask(updateTask))
  173. storage.ExecTrans(tasks...)
  174. return nil
  175. }
  176. func ManagementUpdateUserMerchant(ctx context.Context, req *apis.ManagementUpdateUserMerchantReq, reply *apis.ManagementUpdateUserMerchantReply) (err error) {
  177. err = updateUser(req, reply)
  178. if err != nil {
  179. l.Error("func",
  180. zap.String("call", "ManagementUpdateUserMerchant"),
  181. zap.String("args", utils.MarshalJsonString(req)),
  182. zap.String("error", err.Error()))
  183. }
  184. l.Debug(utils.MarshalJsonString(req, reply))
  185. return
  186. }