phone_update.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package user
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "github.com/jinzhu/gorm"
  9. "go.uber.org/zap"
  10. "smart-enterprise-management/errors"
  11. dbmodel "smart-enterprise-management/model"
  12. pb_v1 "smart-enterprise-management/pb/v1"
  13. "time"
  14. )
  15. func PhoneUpdate(ctx context.Context, req *pb_v1.PhoneUpdateRequest)(reply *pb_v1.PhoneUpdateReply, err error) {
  16. p := &dbmodel.TCompany{}
  17. where := map[string]interface{}{
  18. "business_contact_phone":req.Phone,
  19. }
  20. // 状态判断
  21. err = p.Find(database.DB(), where, nil)
  22. if err != nil && err != gorm.ErrRecordNotFound{
  23. logger.Error("PhoneUpdate",
  24. zap.String("err", err.Error()))
  25. return nil, errors.DataBaseError
  26. }
  27. if p.Id > 0 {
  28. return nil, errors.PhoneExist
  29. }
  30. p = &dbmodel.TCompany{}
  31. where = map[string]interface{}{
  32. "id":req.Id,
  33. }
  34. // 状态判断
  35. err = p.Find(database.DB(), where, nil)
  36. if err != nil {
  37. logger.Error("PhoneUpdate",
  38. zap.String("err", err.Error()))
  39. return nil, errors.DataBaseError
  40. }
  41. if p.Id == 0 {
  42. return nil, errors.NoRecordError
  43. }
  44. reply = &pb_v1.PhoneUpdateReply{}
  45. values := map[string]interface{}{
  46. "business_contact_phone":req.Phone,
  47. "updated_at":time.Now(),
  48. }
  49. err = p.Update(database.DB(), where, values)
  50. if err != nil {
  51. return nil, errors.DataBaseError
  52. }
  53. return reply, nil
  54. }