123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package garden
- import (
- "context"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "property-system/errors"
- "property-system/impl/v1/permission"
- dbmodel "property-system/model"
- "property-system/pb"
- pb_v1 "property-system/pb/v1"
- "strings"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func checkGardenApproveParam(req *pb_v1.GardenApproveRequest) error {
- switch {
- case req.Id == 0:
- return status.Error(10003, "小区不能为空")
- }
- return nil
- }
- func addSuperGroup(db *gorm.DB, gardenId int64, cid int64) error {
- now := time.Now()
- group := &dbmodel.TGroup{
- GardenId: gardenId,
- Cid: cid,
- PermissionCodes: "",
- IsSuper: 1,
- GroupName: "超级管理组",
- UpdatedAt: now,
- CreatedAt: now,
- GroupDesc: "超级管理组",
- }
- err := group.Insert(db)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func initDb(gardenId int64) error {
- mreq := pb_v1.GardenInitDbRequest{GardenId: gardenId}
- _, err := pb.Garden.GardenInitDb(context.Background(), &mreq)
- return err
- }
- //
- func GardenApprove(ctx context.Context, req *pb_v1.GardenApproveRequest) (reply *pb_v1.GardenApproveReply, err error) {
- reply = &pb_v1.GardenApproveReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- err = checkGardenApproveParam(req)
- if err != nil {
- return nil, err
- }
- p := dbmodel.TGarden{}
- where := map[string]interface{}{
- "id": req.Id,
- }
- err = p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if p.ID == 0 {
- return nil, errors.GardenNotExist
- }
- now := time.Now()
- if !req.Status {
- values := map[string]interface{}{
- "uniq_flag": nil,
- "status": GardenStatusApprovedFail,
- "approved_at": now,
- "feedback": req.Feedback,
- }
- db := database.DB().Begin()
- garden := dbmodel.TGarden{}
- err = garden.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- mreq := pb_v1.CompanyGardenDecreaseRequest{Cid: p.Cid}
- _, err := pb.Company.CompanyGardenDecrease(ctx, &mreq)
- if err != nil {
- db.Rollback()
- return nil, err
- }
- pac := dbmodel.TPackageOrder{}
- where := map[string]interface{}{
- "garden_id": req.Id,
- "order_type": 1,
- }
- values = map[string]interface{}{
- "garden_id": 0,
- }
- err = pac.Update(db, where, values)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- db.Commit()
- return reply, nil
- }
- garden := dbmodel.TGardenApproved{
- Province: p.Province,
- ProvinceCode: p.ProvinceCode,
- City: p.City,
- CityCode: p.CityCode,
- Area: p.Area,
- AreaCode: p.AreaCode,
- Street: p.Street,
- StreetCode: p.StreetCode,
- Committee: p.Committee,
- CommitteeCode: p.CommitteeCode,
- PropertyPerson: p.PropertyPerson,
- PropertyPhone: p.PropertyPhone,
- GardenName: p.GardenName,
- GardenAddr: p.GardenAddr,
- Cid: p.Cid,
- InUse: true,
- GardenDesc: p.GardenDesc,
- GardenPic: p.GardenPic,
- UpdatedAt: now,
- CreatedAt: now,
- Lat: p.Lat,
- Lnt: p.Lnt,
- Appendix: p.Appendix,
- PayMode: 1,
- GardenPics: p.GardenPics,
- BuildingStart: p.BuildingStart,
- BuildingEnd: p.BuildingEnd,
- PropertyFeeStart: p.PropertyFeeStart,
- PropertyFeeEnd: p.PropertyFeeEnd,
- GasFeeStart: p.GasFeeStart,
- GasFeeEnd: p.GasFeeEnd,
- BuildingArea: p.BuildingArea,
- BuildingCompany: p.BuildingCompany,
- CoveredArea: p.CoveredArea,
- GreenPercent: p.GreenPercent,
- AreaPercent: p.AreaPercent,
- SpaceInfo: p.SpaceInfo,
- SpaceTotal: p.SpaceTotal,
- HouseTotal: p.HouseTotal,
- BuildingType: p.BuildingType,
- WaterType: p.WaterType,
- ElectricType: p.ElectricType,
- AvgPrice: p.AvgPrice,
- }
- db := database.DB().Begin()
- defer func() {
- if err != nil {
- db.Rollback()
- }
- }()
- // 检查套餐
- pac := dbmodel.TPackageOrder{}
- pacwhere := map[string]interface{}{
- "garden_id": req.Id,
- "status": 2,
- "order_type": 1,
- }
- err = pac.Find(database.DB(), pacwhere)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if pac.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- pacinfo := pb_v1.PackageInfoReply{}
- json.Unmarshal([]byte(pac.PackageContent), &pacinfo)
- // 填充套餐信息到小区
- garden.HouseCountLimit = pacinfo.HouseCount
- garden.ExpireAt = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(int(pac.Period), 0, 0).Unix()
- codes := []string{}
- m := map[string]bool{}
- for _, application := range pacinfo.Applications {
- for _, v := range application.Permissions {
- if m[v.Code] {
- continue
- }
- m[v.Code] = true
- codes = append(codes, v.Code)
- }
- }
- garden.PermissionCodes = strings.Join(codes, ";")
- err = garden.Insert(db)
- if err != nil {
- if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
- return nil, status.Error(10003, "小区已存在")
- }
- return nil, errors.DataBaseError
- }
- values := map[string]interface{}{
- "garden_id": garden.ID,
- }
- err = pac.Update(db, pacwhere, values)
- if err != nil {
- return nil, errors.DataBaseError
- }
- err = p.Delete(db, where)
- if err != nil {
- return nil, errors.DataBaseError
- }
- err = addSuperGroup(db, garden.ID, p.Cid)
- if err != nil {
- return nil, err
- }
- err = initDb(garden.ID)
- if err != nil {
- return nil, err
- }
- mreq := pb_v1.CompanyObjStatisticSetRequest{Cid: garden.Cid, TotalIncrease: 1, ObjType: 3}
- _, err = pb.Company.CompanyObjStatisticSet(ctx, &mreq)
- if err != nil {
- return nil, err
- }
- db.Commit()
- permission.GardenPermissionTreeClearRedis(garden.ID)
- return reply, nil
- }
|