sync.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "property-garden/pb"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "strings"
  19. )
  20. func checkGardenHouseRentSyncParam(req *pb_v1.GardenHouseRentSyncRequest) error {
  21. switch {
  22. case req.GardenId < 1:
  23. return status.Error(10003, "小区不能为空")
  24. case len(req.Datas) == 0:
  25. return status.Error(10003, "内容不能为空")
  26. }
  27. return nil
  28. }
  29. //
  30. func GardenHouseRentSync(ctx context.Context, req *pb_v1.GardenHouseRentSyncRequest) (reply *pb_v1.GardenHouseRentSyncReply, err error) {
  31. reply = &pb_v1.GardenHouseRentSyncReply{}
  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. // 参数检查
  45. err = checkGardenHouseRentSyncParam(req)
  46. if err != nil {
  47. return nil, err
  48. }
  49. dbname := utils.GetGardenDbName(req.GardenId)
  50. p := dbmodel.THouseRent{}
  51. err = json.Unmarshal(req.Datas, &p)
  52. if err != nil {
  53. return nil, errors.ParamsError
  54. }
  55. if p.ID == 0 {
  56. return nil, status.Error(10003, "id不能为空")
  57. }
  58. p.SetTable(dbname)
  59. db := database.DB().Begin()
  60. oldHousePics := []string{}
  61. oldCertPics := []string{}
  62. if p.ID == 0 {
  63. return nil, status.Error(10003, "同步id不能为空")
  64. }
  65. if req.Insert {
  66. err = p.Insert(db)
  67. } else {
  68. where := map[string]interface{}{
  69. "id": p.ID,
  70. "garden_id": req.GardenId,
  71. }
  72. err = p.Find(db, where)
  73. if err == nil {
  74. oldHousePics = strings.Split(p.HousePic, ";")
  75. oldCertPics = strings.Split(p.CertPic, ";")
  76. values := map[string]interface{}{}
  77. json.Unmarshal(req.Datas, &values)
  78. values["approved_at"] = p.ApprovedAt
  79. values["updated_at"] = p.UpdatedAt
  80. values["created_at"] = p.CreatedAt
  81. err = p.Update(db, where, values)
  82. }
  83. }
  84. if err != nil {
  85. db.Rollback()
  86. return nil, errors.DataBaseError
  87. }
  88. inList := []string{}
  89. outList := []string{}
  90. inList = append(inList, strings.Split(p.HousePic, ";")...)
  91. inList = append(inList, strings.Split(p.CertPic, ";")...)
  92. outList = append(outList, oldHousePics...)
  93. outList = append(outList, oldCertPics...)
  94. if err := oss_utils.OssObjAdd(inList, outList); err != nil {
  95. db.Rollback()
  96. return nil, err
  97. }
  98. if req.Increase != 0 {
  99. mreq := pb_v1.GardenRentSellCountRequest{GardenId: req.GardenId, RentIncrease: req.Increase}
  100. _, err := pb.System.GardenRentSellCount(ctx, &mreq)
  101. if err != nil {
  102. db.Rollback()
  103. return nil, err
  104. }
  105. }
  106. db.Commit()
  107. return reply, nil
  108. }