approve.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/permission"
  11. dbmodel "property-system/model"
  12. "property-system/pb"
  13. pb_v1 "property-system/pb/v1"
  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. func checkGardenApproveParam(req *pb_v1.GardenApproveRequest) error {
  22. switch {
  23. case req.Id == 0:
  24. return status.Error(10003, "小区不能为空")
  25. }
  26. return nil
  27. }
  28. func addSuperGroup(db *gorm.DB, gardenId int64, cid int64) error {
  29. now := time.Now()
  30. group := &dbmodel.TGroup{
  31. GardenId: gardenId,
  32. Cid: cid,
  33. PermissionCodes: "",
  34. IsSuper: 1,
  35. GroupName: "超级管理组",
  36. UpdatedAt: now,
  37. CreatedAt: now,
  38. GroupDesc: "超级管理组",
  39. }
  40. err := group.Insert(db)
  41. if err != nil {
  42. return errors.DataBaseError
  43. }
  44. return nil
  45. }
  46. func initDb(gardenId int64) error {
  47. mreq := pb_v1.GardenInitDbRequest{GardenId: gardenId}
  48. _, err := pb.Garden.GardenInitDb(context.Background(), &mreq)
  49. return err
  50. }
  51. //
  52. func GardenApprove(ctx context.Context, req *pb_v1.GardenApproveRequest) (reply *pb_v1.GardenApproveReply, err error) {
  53. reply = &pb_v1.GardenApproveReply{}
  54. // 捕获各个task中的异常并返回给调用者
  55. defer func() {
  56. if r := recover(); r != nil {
  57. err = fmt.Errorf("%+v", r)
  58. e := &status.Status{}
  59. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  60. logger.Error("err",
  61. zap.String("system_err", err.Error()),
  62. zap.Stack("stacktrace"))
  63. }
  64. }
  65. }()
  66. err = checkGardenApproveParam(req)
  67. if err != nil {
  68. return nil, err
  69. }
  70. p := dbmodel.TGarden{}
  71. where := map[string]interface{}{
  72. "id": req.Id,
  73. }
  74. err = p.Find(database.DB(), where)
  75. if err != nil && err != gorm.ErrRecordNotFound {
  76. return nil, errors.DataBaseError
  77. }
  78. if p.ID == 0 {
  79. return nil, errors.GardenNotExist
  80. }
  81. now := time.Now()
  82. if !req.Status {
  83. values := map[string]interface{}{
  84. "uniq_flag": nil,
  85. "status": GardenStatusApprovedFail,
  86. "approved_at": now,
  87. "feedback": req.Feedback,
  88. }
  89. db := database.DB().Begin()
  90. garden := dbmodel.TGarden{}
  91. err = garden.Update(db, where, values)
  92. if err != nil {
  93. db.Rollback()
  94. return nil, errors.DataBaseError
  95. }
  96. mreq := pb_v1.CompanyGardenDecreaseRequest{Cid: p.Cid}
  97. _, err := pb.Company.CompanyGardenDecrease(ctx, &mreq)
  98. if err != nil {
  99. db.Rollback()
  100. return nil, err
  101. }
  102. pac := dbmodel.TPackageOrder{}
  103. where := map[string]interface{}{
  104. "garden_id": req.Id,
  105. "order_type": 1,
  106. }
  107. values = map[string]interface{}{
  108. "garden_id": 0,
  109. }
  110. err = pac.Update(db, where, values)
  111. if err != nil {
  112. db.Rollback()
  113. return nil, errors.DataBaseError
  114. }
  115. db.Commit()
  116. return reply, nil
  117. }
  118. garden := dbmodel.TGardenApproved{
  119. Province: p.Province,
  120. ProvinceCode: p.ProvinceCode,
  121. City: p.City,
  122. CityCode: p.CityCode,
  123. Area: p.Area,
  124. AreaCode: p.AreaCode,
  125. Street: p.Street,
  126. StreetCode: p.StreetCode,
  127. Committee: p.Committee,
  128. CommitteeCode: p.CommitteeCode,
  129. PropertyPerson: p.PropertyPerson,
  130. PropertyPhone: p.PropertyPhone,
  131. GardenName: p.GardenName,
  132. GardenAddr: p.GardenAddr,
  133. Cid: p.Cid,
  134. InUse: true,
  135. GardenDesc: p.GardenDesc,
  136. GardenPic: p.GardenPic,
  137. UpdatedAt: now,
  138. CreatedAt: now,
  139. Lat: p.Lat,
  140. Lnt: p.Lnt,
  141. Appendix: p.Appendix,
  142. PayMode: 1,
  143. GardenPics: p.GardenPics,
  144. BuildingStart: p.BuildingStart,
  145. BuildingEnd: p.BuildingEnd,
  146. PropertyFeeStart: p.PropertyFeeStart,
  147. PropertyFeeEnd: p.PropertyFeeEnd,
  148. GasFeeStart: p.GasFeeStart,
  149. GasFeeEnd: p.GasFeeEnd,
  150. BuildingArea: p.BuildingArea,
  151. BuildingCompany: p.BuildingCompany,
  152. CoveredArea: p.CoveredArea,
  153. GreenPercent: p.GreenPercent,
  154. AreaPercent: p.AreaPercent,
  155. SpaceInfo: p.SpaceInfo,
  156. SpaceTotal: p.SpaceTotal,
  157. HouseTotal: p.HouseTotal,
  158. BuildingType: p.BuildingType,
  159. WaterType: p.WaterType,
  160. ElectricType: p.ElectricType,
  161. AvgPrice: p.AvgPrice,
  162. }
  163. db := database.DB().Begin()
  164. defer func() {
  165. if err != nil {
  166. db.Rollback()
  167. }
  168. }()
  169. // 检查套餐
  170. pac := dbmodel.TPackageOrder{}
  171. pacwhere := map[string]interface{}{
  172. "garden_id": req.Id,
  173. "status": 2,
  174. "order_type": 1,
  175. }
  176. err = pac.Find(database.DB(), pacwhere)
  177. if err != nil && err != gorm.ErrRecordNotFound {
  178. return nil, errors.DataBaseError
  179. }
  180. if pac.ID == 0 {
  181. return nil, errors.ErrRecordNotFound
  182. }
  183. pacinfo := pb_v1.PackageInfoReply{}
  184. json.Unmarshal([]byte(pac.PackageContent), &pacinfo)
  185. // 填充套餐信息到小区
  186. garden.HouseCountLimit = pacinfo.HouseCount
  187. garden.ExpireAt = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(int(pac.Period), 0, 0).Unix()
  188. codes := []string{}
  189. m := map[string]bool{}
  190. for _, application := range pacinfo.Applications {
  191. for _, v := range application.Permissions {
  192. if m[v.Code] {
  193. continue
  194. }
  195. m[v.Code] = true
  196. codes = append(codes, v.Code)
  197. }
  198. }
  199. garden.PermissionCodes = strings.Join(codes, ";")
  200. err = garden.Insert(db)
  201. if err != nil {
  202. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  203. return nil, status.Error(10003, "小区已存在")
  204. }
  205. return nil, errors.DataBaseError
  206. }
  207. values := map[string]interface{}{
  208. "garden_id": garden.ID,
  209. }
  210. err = pac.Update(db, pacwhere, values)
  211. if err != nil {
  212. return nil, errors.DataBaseError
  213. }
  214. err = p.Delete(db, where)
  215. if err != nil {
  216. return nil, errors.DataBaseError
  217. }
  218. err = addSuperGroup(db, garden.ID, p.Cid)
  219. if err != nil {
  220. return nil, err
  221. }
  222. err = initDb(garden.ID)
  223. if err != nil {
  224. return nil, err
  225. }
  226. mreq := pb_v1.CompanyObjStatisticSetRequest{Cid: garden.Cid, TotalIncrease: 1, ObjType: 3}
  227. _, err = pb.Company.CompanyObjStatisticSet(ctx, &mreq)
  228. if err != nil {
  229. return nil, err
  230. }
  231. db.Commit()
  232. permission.GardenPermissionTreeClearRedis(garden.ID)
  233. return reply, nil
  234. }