update.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package company
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "gorm.io/gorm"
  10. "property-company/errors"
  11. dbmodel "property-company/model"
  12. pb_v1 "property-company/pb/v1"
  13. "strings"
  14. "git.getensh.com/common/gopkgs/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. func checkCompanyUpdateParam(req *pb_v1.CompanyUpdateRequest) error {
  19. switch {
  20. case req.SocialCode == "":
  21. return status.Error(10003, "社会信用码不能为空")
  22. case req.CompanyName == "":
  23. return status.Error(10003, "公司名称不能为空")
  24. case req.LicenseType != 1 && req.LicenseType != 2:
  25. return status.Error(10003, "不支持的营业执照类型")
  26. }
  27. return nil
  28. }
  29. //
  30. func CompanyUpdate(ctx context.Context, req *pb_v1.CompanyUpdateRequest) (reply *pb_v1.CompanyUpdateReply, err error) {
  31. reply = &pb_v1.CompanyUpdateReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. err = checkCompanyUpdateParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. company := &dbmodel.TCompany{}
  49. where := map[string]interface{}{
  50. "id":req.Cid,
  51. }
  52. err = company.Find(database.DB(), where)
  53. if err != nil && err != gorm.ErrRecordNotFound{
  54. return nil, errors.DataBaseError
  55. }
  56. if company.ID == 0 {
  57. return nil, errors.ErrRecordNotFound
  58. }
  59. reply.Origin = &pb_v1.CompanyUpdateRequest{
  60. Cid:company.ID,
  61. CompanyAddr:company.CompanyAddr,
  62. CompanyPhone:company.CompanyPhone,
  63. CompanyName:company.CompanyName,
  64. SocialCode:company.SocialCode,
  65. BusinessLicense:company.BusinessLicense,
  66. LicenseType:int32(company.LicenseType),
  67. Logo:company.Logo,
  68. Desc:company.Desc,
  69. Site:company.Site,
  70. }
  71. values := map[string]interface{}{
  72. "company_name":req.CompanyName,
  73. "company_phone":req.CompanyPhone,
  74. "company_addr":req.CompanyAddr,
  75. "logo":req.Logo,
  76. "desc":req.Desc,
  77. "site":req.Site,
  78. "social_code":req.SocialCode,
  79. "license_type":int64(req.LicenseType),
  80. "business_license":req.BusinessLicense,
  81. }
  82. err = company.Update(database.DB(), where, values)
  83. if err != nil {
  84. msg := strings.ToLower(err.Error())
  85. if strings.Contains(msg, "duplicate") {
  86. return nil, status.Error(10003, "社会信用码已存在")
  87. }
  88. return nil, errors.DataBaseError
  89. }
  90. return reply, nil
  91. }