add.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package garden
  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. "gorm.io/gorm"
  13. "property-company/consts"
  14. "property-company/errors"
  15. dbmodel "property-company/model"
  16. "property-company/pb"
  17. pb_v1 "property-company/pb/v1"
  18. "property-company/utils"
  19. )
  20. func checkGardenAddParam(req *pb_v1.CompanyAddGardenRequest) error {
  21. switch {
  22. case req.Province == "" || req.ProvinceCode == "":
  23. return status.Error(10003, "省份不能为空")
  24. case req.City == "" || req.CityCode == "":
  25. return status.Error(10003, "城市不能为空")
  26. case req.Area == "" || req.AreaCode == "":
  27. return status.Error(10003, "区域不能为空")
  28. case req.Street == "" || req.StreetCode == "":
  29. return status.Error(10003, "街道不能为空")
  30. case req.Committee == "" || req.CommitteeCode == "":
  31. return status.Error(10003, "社区不能为空")
  32. case req.PropertyPerson == "":
  33. return status.Error(10003, "物业联系人不能为空")
  34. case req.PropertyPhone == "":
  35. return status.Error(10003, "物业联系人电话不能为空")
  36. case req.GardenName == "":
  37. return status.Error(10003, "小区名称不能为空")
  38. case req.GardenAddr == "":
  39. return status.Error(10003, "小区地址不能为空")
  40. case req.Cid < 0:
  41. return status.Error(10003, "物业公司不能为空")
  42. case req.Lnt == 0 || req.Lat == 0:
  43. return status.Error(10003, "经纬度不能为空")
  44. case len(req.Appendix) == 0:
  45. return status.Error(10003, "证明材料不能为空")
  46. case req.OrderId == 0:
  47. return status.Error(10003, "套餐订单不能为空")
  48. case (req.BuildingStart > 0 && req.BuildingEnd < 1) ||
  49. (req.BuildingStart < 1 && req.BuildingEnd > 0):
  50. return status.Error(10003, "建成年份只能同时为空或不为空")
  51. case (req.PropertyFeeStart > 0 && req.PropertyFeeEnd < 1) ||
  52. (req.PropertyFeeStart < 1 && req.PropertyFeeEnd > 0):
  53. return status.Error(10003, "物业费只能同时为空或不为空")
  54. case (req.GasFeeStart > 0 && req.GasFeeEnd < 1) ||
  55. (req.GasFeeStart < 1 && req.GasFeeEnd > 0):
  56. return status.Error(10003, "燃气费只能同时为空或不为空")
  57. }
  58. return nil
  59. }
  60. //
  61. func CompanyAddGarden(ctx context.Context, req *pb_v1.CompanyAddGardenRequest) (reply *pb_v1.CompanyAddGardenReply, err error) {
  62. reply = &pb_v1.CompanyAddGardenReply{}
  63. // 捕获各个task中的异常并返回给调用者
  64. defer func() {
  65. if r := recover(); r != nil {
  66. err = fmt.Errorf("%+v", r)
  67. e := &status.Status{}
  68. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  69. logger.Error("err",
  70. zap.String("system_err", err.Error()),
  71. zap.Stack("stacktrace"))
  72. }
  73. }
  74. }()
  75. // 检查参数
  76. err = checkGardenAddParam(req)
  77. if err != nil {
  78. return nil, err
  79. }
  80. // 检查公司
  81. company := dbmodel.TCompany{}
  82. where := map[string]interface{}{
  83. "id": req.Cid,
  84. }
  85. err = company.Find(database.DB(), where)
  86. if err != nil && err != gorm.ErrRecordNotFound {
  87. return nil, errors.DataBaseError
  88. }
  89. if company.ID == 0 {
  90. return nil, errors.UserNotExist
  91. }
  92. if company.ApproveStatus != consts.ApproveStatusSuccess {
  93. return nil, errors.UserStatusError
  94. }
  95. //freeCount := company.FreeGardenCount
  96. //if freeCount == 0 {
  97. //return nil, errors.GardenLimitError
  98. //}
  99. // 先更新创建小区个数,以便回滚
  100. db := database.DB().Begin()
  101. values := map[string]interface{}{
  102. "garden_count": gorm.Expr("garden_count + ?", 1),
  103. }
  104. err = company.Update(db, where, values)
  105. if err != nil {
  106. db.Rollback()
  107. return nil, errors.DataBaseError
  108. }
  109. mreq := &pb_v1.GardenAddRequest{
  110. Province: req.Province,
  111. ProvinceCode: req.ProvinceCode,
  112. City: req.City,
  113. CityCode: req.CityCode,
  114. Area: req.Area,
  115. AreaCode: req.AreaCode,
  116. Street: req.Street,
  117. StreetCode: req.StreetCode,
  118. Committee: req.Committee,
  119. CommitteeCode: req.CommitteeCode,
  120. PropertyPerson: req.PropertyPerson,
  121. PropertyPhone: req.PropertyPhone,
  122. GardenName: req.GardenName,
  123. GardenAddr: req.GardenAddr,
  124. Cid: req.Cid,
  125. //FreeGardenCount: int32(freeCount),
  126. GardenPic: req.GardenPic,
  127. GardenDesc: req.GardenDesc,
  128. Lat: req.Lat,
  129. Lnt: req.Lnt,
  130. Appendix: req.Appendix,
  131. OrderId: req.OrderId,
  132. GardenPics: req.GardenPics,
  133. BuildingStart: req.BuildingStart,
  134. BuildingEnd: req.BuildingEnd,
  135. PropertyFeeStart: req.PropertyFeeStart,
  136. PropertyFeeEnd: req.PropertyFeeEnd,
  137. GasFeeStart: req.GasFeeStart,
  138. GasFeeEnd: req.GasFeeEnd,
  139. BuildingArea: req.BuildingArea,
  140. BuildingCompany: req.BuildingCompany,
  141. CoveredArea: req.CoveredArea,
  142. GreenPercent: req.GreenPercent,
  143. AreaPercent: req.AreaPercent,
  144. SpaceInfo: req.SpaceInfo,
  145. SpaceTotal: req.SpaceTotal,
  146. HouseTotal: req.HouseTotal,
  147. BuildingType: req.BuildingType,
  148. WaterType: req.WaterType,
  149. ElectricType: req.ElectricType,
  150. AvgPrice: req.AvgPrice,
  151. }
  152. // 创建小区
  153. mreply, err := pb.System.GardenAdd(ctx, mreq)
  154. if err != nil {
  155. db.Rollback()
  156. return nil, err
  157. }
  158. db.Commit()
  159. reply.Id = mreply.Id
  160. utils.RobotMsg(fmt.Sprintf("小区(%s)提交审核,请即时审核", req.GardenName))
  161. return reply, nil
  162. }