123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- package user_merchant
- import (
- "context"
- "gd_management/apis"
- "gd_management/errors"
- "gd_management/impl/pubsub"
- "gd_management/rpc_apis"
- "gd_management/rpc_apis/gd_access_log"
- "strings"
- "time"
- "gd_management/common.in/storage"
- "gd_management/common.in/utils"
- "github.com/astaxie/beego/orm"
- "go.uber.org/zap"
- )
- func getUserMerchantInfo(o orm.Ormer, merchantId int64) (*apis.TGdUser, *apis.TGdMerchants, error) {
- userInfo := apis.TGdUser{}
- merchantInfo := apis.TGdMerchants{}
- err := o.QueryTable("t_gd_merchants").Filter("id", merchantId).One(&merchantInfo)
- if err != nil {
- if err == orm.ErrNoRows {
- return nil, nil, errors.DataBaseNoRecord
- }
- return nil, nil, errors.DataBaseError
- }
- err = o.QueryTable("t_gd_user").Filter("id", merchantInfo.UserId).One(&userInfo)
- if err != nil {
- if err == orm.ErrNoRows {
- return nil, nil, errors.DataBaseNoRecord
- }
- return nil, nil, errors.DataBaseError
- }
- return &userInfo, &merchantInfo, nil
- }
- func checkUpdateParam(o orm.Ormer, phone, name, socialCode string) error {
- if phone != "" {
- exist := o.QueryTable("t_gd_user").Filter("phone", phone).Exist()
- if exist {
- return errors.UserPhoneExist
- }
- }
- if name != "" {
- exist := o.QueryTable("t_gd_user").Filter("name", name).Exist()
- if exist {
- return errors.UserNameExist
- }
- }
- /*if socialCode != "" {
- exist := o.QueryTable("t_gd_merchants").Filter("social_code", socialCode).Exist()
- if exist {
- return errors.UserNameExist
- }
- }*/
- return nil
- }
- func updateUser(req *apis.ManagementUpdateUserMerchantReq, reply *apis.ManagementUpdateUserMerchantReply) error {
- if req.MerchantId == 0 {
- return errors.ArgsError
- }
- //验证ip地址
- if req.MerchantInfo.IpWhitelist != "" {
- ip := strings.Split(req.MerchantInfo.IpWhitelist, ",")
- for _, v := range ip {
- ips := strings.Split(v, "-")
- for _, realIp := range ips {
- if !utils.VerifyIp(realIp) {
- return errors.IpAddressErr
- }
- }
- }
- }
- updateTask := func(db orm.Ormer) error {
- // TODO 检查是否存在 checkParam
- userInfo, merchantInfo, err := getUserMerchantInfo(db, req.MerchantId)
- if err != nil {
- return err
- }
- //user info
- now := time.Now().Format("2006-01-02 15:04:05")
- req.UserInfo.UpdateTime = now
- req.UserInfo.Id = userInfo.Id
- if req.UserInfo.Password == "" {
- req.UserInfo.Password = userInfo.Password
- } else {
- req.UserInfo.Password = encryptPassword(req.UserInfo.Password)
- }
- if req.UserInfo.Name != userInfo.Name {
- err = checkUpdateParam(db, "", req.UserInfo.Name, "")
- if err != nil {
- return err
- }
- }
- if req.UserInfo.Name == "" {
- req.UserInfo.Name = userInfo.Name
- }
- if req.UserInfo.Phone != userInfo.Phone {
- err = checkUpdateParam(db, req.UserInfo.Phone, "", "")
- if err != nil {
- return err
- }
- }
- if req.UserInfo.Phone == "" {
- req.UserInfo.Phone = userInfo.Phone
- }
- if req.UserInfo.Email == "" {
- req.UserInfo.Email = userInfo.Email
- }
- req.UserInfo.CreateTime = userInfo.CreateTime
- _, err = db.Update(&req.UserInfo)
- if err != nil {
- return errors.DataBaseError
- }
- //merchant info
- req.MerchantInfo.UpdateTime = now
- req.MerchantInfo.Id = req.MerchantId
- req.MerchantInfo.AppKey = merchantInfo.AppKey
- req.MerchantInfo.UserId = merchantInfo.UserId
- req.MerchantInfo.AppSecret = merchantInfo.AppSecret
- req.MerchantInfo.AuthStatus = merchantInfo.AuthStatus
- req.MerchantInfo.MerchantType = merchantInfo.MerchantType
- req.MerchantInfo.Arrearage = merchantInfo.Arrearage
- if req.MerchantInfo.MerchantName == "" {
- req.MerchantInfo.MerchantName = merchantInfo.MerchantName
- }
- if req.MerchantInfo.SocialCode == "" {
- /*if req.MerchantInfo.SocialCode != merchantInfo.SocialCode {
- err = checkUpdateParam(db, "", "", req.MerchantInfo.SocialCode)
- if err != nil {
- return err
- }
- }*/
- req.MerchantInfo.SocialCode = merchantInfo.SocialCode
- }
- if req.MerchantInfo.Address == "" {
- req.MerchantInfo.Address = merchantInfo.Address
- }
- if req.MerchantInfo.ContactName == "" {
- req.MerchantInfo.ContactName = merchantInfo.ContactName
- }
- if req.MerchantInfo.ContactNumber == "" {
- req.MerchantInfo.ContactNumber = merchantInfo.ContactNumber
- }
- if req.MerchantInfo.MerchantLicense == "" {
- req.MerchantInfo.MerchantLicense = merchantInfo.MerchantLicense
- }
- _, 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")
- if err != nil {
- return errors.DataBaseError
- }
- if req.MerchantInfo.MerchantName != merchantInfo.MerchantName {
- dreq := gd_access_log.LogUpdateApiNameReq{
- Type: 2,
- Name: req.MerchantInfo.MerchantName,
- Id: req.MerchantId,
- }
- go func() error {
- _, err := rpc_apis.AccessLog.LogUpdateApiName(context.Background(), &dreq)
- return err
- }()
- }
- //redis
- utils.RedisSet("t_gd_merchants", req.MerchantInfo.AppKey, req.MerchantInfo)
- if err := pubsub.PublishMerchantNotify(merchantInfo.AppKey); err != nil {
- return err
- }
- if err := pubsub.PublishNameNotify("merchant_id", req.MerchantId, 0); err != nil {
- return err
- }
- return nil
- }
- tasks := []storage.DbaTasker{}
- tasks = append(tasks, storage.GenerateDbaTask(updateTask))
- storage.ExecTrans(tasks...)
- return nil
- }
- func ManagementUpdateUserMerchant(ctx context.Context, req *apis.ManagementUpdateUserMerchantReq, reply *apis.ManagementUpdateUserMerchantReply) (err error) {
- err = updateUser(req, reply)
- if err != nil {
- l.Error("func",
- zap.String("call", "ManagementUpdateUserMerchant"),
- zap.String("args", utils.MarshalJsonString(req)),
- zap.String("error", err.Error()))
- }
- l.Debug(utils.MarshalJsonString(req, reply))
- return
- }
|