sync.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house_rent
  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. "property-garden/errors"
  13. "property-garden/impl/v1/oss_utils"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. "strings"
  18. )
  19. func checkGardenHouseRentSyncParam(req *pb_v1.GardenHouseRentSyncRequest) error {
  20. switch {
  21. case req.GardenId < 1:
  22. return status.Error(10003, "小区不能为空")
  23. case len(req.Datas) == 0:
  24. return status.Error(10003, "内容不能为空")
  25. }
  26. return nil
  27. }
  28. //
  29. func GardenHouseRentSync(ctx context.Context, req *pb_v1.GardenHouseRentSyncRequest) (reply *pb_v1.GardenHouseRentSyncReply, err error) {
  30. reply = &pb_v1.GardenHouseRentSyncReply{}
  31. // 捕获各个task中的异常并返回给调用者
  32. defer func() {
  33. if r := recover(); r != nil {
  34. err = fmt.Errorf("%+v", r)
  35. e := &status.Status{}
  36. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  37. logger.Error("err",
  38. zap.String("system_err", err.Error()),
  39. zap.Stack("stacktrace"))
  40. }
  41. }
  42. }()
  43. // 参数检查
  44. err = checkGardenHouseRentSyncParam(req)
  45. if err != nil {
  46. return nil, err
  47. }
  48. dbname := utils.GetGardenDbName(req.GardenId)
  49. p := dbmodel.THouseRent{}
  50. err = json.Unmarshal(req.Datas, &p)
  51. if err != nil {
  52. return nil, errors.ParamsError
  53. }
  54. if p.ID == 0 {
  55. return nil, status.Error(10003, "id不能为空")
  56. }
  57. p.SetTable(dbname)
  58. db := database.DB().Begin()
  59. oldHousePics := []string{}
  60. oldCertPics := []string{}
  61. if req.Insert {
  62. err = p.Insert(db)
  63. } else {
  64. where := map[string]interface{}{
  65. "id":p.ID,
  66. "garden_id":req.GardenId,
  67. }
  68. err = p.Find(db, where)
  69. if err == nil {
  70. oldHousePics = strings.Split(p.HousePic, ";")
  71. oldCertPics = strings.Split(p.CertPic, ";")
  72. values := map[string]interface{}{}
  73. json.Unmarshal(req.Datas, &values)
  74. values["approved_at"] = p.ApprovedAt
  75. values["updated_at"] = p.UpdatedAt
  76. values["created_at"] = p.CreatedAt
  77. err = p.Update(db, where, values)
  78. }
  79. }
  80. if err != nil {
  81. db.Rollback()
  82. return nil, errors.DataBaseError
  83. }
  84. inList := []string{}
  85. outList := []string{}
  86. inList = append(inList, strings.Split(p.HousePic, ";")...)
  87. inList = append(inList, strings.Split(p.CertPic, ";")...)
  88. outList = append(outList, oldHousePics...)
  89. outList = append(outList, oldCertPics...)
  90. if err := oss_utils.OssObjAdd(inList, outList); err != nil {
  91. db.Rollback()
  92. return nil, err
  93. }
  94. db.Commit()
  95. return reply, nil
  96. }