add.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. "gorm.io/gorm"
  9. "property-system/errors"
  10. "property-system/impl/v1/oss_utils"
  11. dbmodel "property-system/model"
  12. pb_v1 "property-system/pb/v1"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. )
  21. const (
  22. GardenStatusWait = 1
  23. GardenStatusApproved = 2
  24. GardenStatusApprovedFail = 3
  25. )
  26. func int32ArrayToString(data []int32) string {
  27. if len(data) == 0 {
  28. return ""
  29. }
  30. ret := []string{}
  31. for _, v := range data {
  32. if v == 0 {
  33. continue
  34. }
  35. tmp := strconv.Itoa(int(v))
  36. ret = append(ret, tmp)
  37. }
  38. return strings.Join(ret, ";")
  39. }
  40. func stringToInt32Array(data string) []int32 {
  41. if data == "" {
  42. return []int32{}
  43. }
  44. src := strings.Split(data, ";")
  45. ret := []int32{}
  46. for _, v := range src {
  47. tmp, _ := strconv.Atoi(v)
  48. ret = append(ret, int32(tmp))
  49. }
  50. return ret
  51. }
  52. func checkGardenAddParam(req *pb_v1.GardenAddRequest) error {
  53. switch {
  54. case req.Province == "" || req.ProvinceCode == "":
  55. return status.Error(10003, "省份不能为空")
  56. case req.City == "" || req.CityCode == "":
  57. return status.Error(10003, "城市不能为空")
  58. case req.Area == "" || req.AreaCode == "":
  59. return status.Error(10003, "区域不能为空")
  60. case req.Street == "" || req.StreetCode == "":
  61. return status.Error(10003, "街道不能为空")
  62. case req.Committee == "" || req.CommitteeCode == "":
  63. return status.Error(10003, "社区不能为空")
  64. case req.PropertyPerson == "":
  65. return status.Error(10003, "物业联系人不能为空")
  66. case req.PropertyPhone == "":
  67. return status.Error(10003, "物业联系人电话不能为空")
  68. case req.GardenName == "":
  69. return status.Error(10003, "小区名称不能为空")
  70. case req.GardenAddr == "":
  71. return status.Error(10003, "小区地址不能为空")
  72. case req.Cid < 1:
  73. return status.Error(10003, "物业公司不能为空")
  74. case req.Lnt == 0 || req.Lat == 0:
  75. return status.Error(10003, "经纬度不能为空")
  76. case len(req.Appendix) == 0:
  77. return status.Error(10003, "证明材料不能为空")
  78. case req.OrderId == 0:
  79. return status.Error(10003, "套餐订单不能为空")
  80. case (req.BuildingStart > 0 && req.BuildingEnd < 1) ||
  81. (req.BuildingStart < 1 && req.BuildingEnd > 0):
  82. return status.Error(10003, "建成年份只能同时为空或不为空")
  83. case (req.PropertyFeeStart > 0 && req.PropertyFeeEnd < 1) ||
  84. (req.PropertyFeeStart < 1 && req.PropertyFeeEnd > 0):
  85. return status.Error(10003, "物业费只能同时为空或不为空")
  86. case (req.GasFeeStart > 0 && req.GasFeeEnd < 1) ||
  87. (req.GasFeeStart < 1 && req.GasFeeEnd > 0):
  88. return status.Error(10003, "燃气费只能同时为空或不为空")
  89. }
  90. return nil
  91. }
  92. //
  93. func GardenAdd(ctx context.Context, req *pb_v1.GardenAddRequest) (reply *pb_v1.GardenAddReply, err error) {
  94. reply = &pb_v1.GardenAddReply{}
  95. // 捕获各个task中的异常并返回给调用者
  96. defer func() {
  97. if r := recover(); r != nil {
  98. err = fmt.Errorf("%+v", r)
  99. e := &status.Status{}
  100. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  101. logger.Error("err",
  102. zap.String("system_err", err.Error()),
  103. zap.Stack("stacktrace"))
  104. }
  105. }
  106. }()
  107. err = checkGardenAddParam(req)
  108. if err != nil {
  109. return nil, err
  110. }
  111. gardenApprove := dbmodel.TGardenApproved{}
  112. filter := map[string]interface{}{
  113. "street_code": req.StreetCode,
  114. "garden_name": req.GardenName,
  115. "in_use": true,
  116. }
  117. count, err := gardenApprove.Count(database.DB(), filter, nil)
  118. if err != nil {
  119. return nil, errors.DataBaseError
  120. }
  121. if count > 0 {
  122. return nil, errors.GardenExist
  123. }
  124. now := time.Now()
  125. garden := dbmodel.TGarden{
  126. Province: req.Province,
  127. ProvinceCode: req.ProvinceCode,
  128. City: req.City,
  129. CityCode: req.CityCode,
  130. Area: req.Area,
  131. AreaCode: req.AreaCode,
  132. Street: req.Street,
  133. StreetCode: req.StreetCode,
  134. Committee: req.Committee,
  135. CommitteeCode: req.CommitteeCode,
  136. PropertyPerson: req.PropertyPerson,
  137. PropertyPhone: req.PropertyPhone,
  138. GardenName: req.GardenName,
  139. GardenAddr: req.GardenAddr,
  140. Cid: req.Cid,
  141. UniqFlag: 1,
  142. GardenDesc: req.GardenDesc,
  143. GardenPic: req.GardenPic,
  144. UpdatedAt: now,
  145. CreatedAt: now,
  146. Lat: req.Lat,
  147. Lnt: req.Lnt,
  148. Status: GardenStatusWait,
  149. Appendix: strings.Join(req.Appendix, ";"),
  150. GardenPics: strings.Join(req.GardenPics, ";"),
  151. BuildingStart: req.BuildingStart,
  152. BuildingEnd: req.BuildingEnd,
  153. PropertyFeeStart: req.PropertyFeeStart,
  154. PropertyFeeEnd: req.PropertyFeeEnd,
  155. GasFeeStart: req.GasFeeStart,
  156. GasFeeEnd: req.GasFeeEnd,
  157. BuildingArea: req.BuildingArea,
  158. BuildingCompany: req.BuildingCompany,
  159. CoveredArea: req.CoveredArea,
  160. GreenPercent: req.GreenPercent,
  161. AreaPercent: req.AreaPercent,
  162. SpaceInfo: req.SpaceInfo,
  163. SpaceTotal: req.SpaceTotal,
  164. HouseTotal: req.HouseTotal,
  165. BuildingType: req.BuildingType,
  166. WaterType: int32ArrayToString(req.WaterType),
  167. ElectricType: int32ArrayToString(req.ElectricType),
  168. AvgPrice: req.AvgPrice,
  169. }
  170. db := database.DB().Begin()
  171. defer func() {
  172. if err != nil {
  173. db.Rollback()
  174. }
  175. }()
  176. err = garden.Insert(db)
  177. if err != nil {
  178. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  179. return nil, status.Error(10003, "小区已存在")
  180. }
  181. return nil, errors.DataBaseError
  182. }
  183. pac := dbmodel.TPackageOrder{}
  184. where := map[string]interface{}{
  185. "id": req.OrderId,
  186. "status": 2,
  187. }
  188. err = pac.Find(database.DB(), where)
  189. if err != nil && err != gorm.ErrRecordNotFound {
  190. return nil, errors.DataBaseError
  191. }
  192. if pac.ID == 0 {
  193. return nil, errors.ErrRecordNotFound
  194. }
  195. if pac.GardenId > 0 {
  196. return nil, status.Error(10003, "套餐已占用")
  197. }
  198. values := map[string]interface{}{
  199. "garden_id": garden.ID,
  200. }
  201. err = pac.Update(db, where, values)
  202. if err != nil {
  203. return nil, errors.DataBaseError
  204. }
  205. reply.Id = garden.ID
  206. // 检查小区数量
  207. /* garden.ID = 0
  208. where := map[string]interface{}{
  209. "cid": req.Cid,
  210. "status":GardenStatusWait,
  211. }
  212. waitCount, err := garden.Count(db, where, nil)
  213. if err != nil {
  214. return nil, errors.DataBaseError
  215. }
  216. where = map[string]interface{}{
  217. "cid": req.Cid,
  218. }
  219. gardenApprove.ID = 0
  220. approvedCount, err := gardenApprove.Count(db, where, nil)
  221. if err != nil {
  222. return nil, errors.DataBaseError
  223. }
  224. if req.FreeGardenCount <= int32(waitCount+approvedCount) && req.FreeGardenCount >= 0 {
  225. return nil, status.Error(10003, "超过数量")
  226. }
  227. */
  228. inList := []string{}
  229. inList = append(inList, req.Appendix...)
  230. inList = append(inList, req.GardenPic)
  231. inList = append(inList, req.GardenPics...)
  232. if err := oss_utils.OssObjAdd(inList, nil); err != nil {
  233. db.Rollback()
  234. return nil, err
  235. }
  236. db.Commit()
  237. return reply, nil
  238. }