// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package garden import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" "property-company/consts" "property-company/errors" dbmodel "property-company/model" "property-company/pb" pb_v1 "property-company/pb/v1" "property-company/utils" ) func checkGardenAddParam(req *pb_v1.CompanyAddGardenRequest) error { switch { case req.Province == "" || req.ProvinceCode == "": return status.Error(10003, "省份不能为空") case req.City == "" || req.CityCode == "": return status.Error(10003, "城市不能为空") case req.Area == "" || req.AreaCode == "": return status.Error(10003, "区域不能为空") case req.Street == "" || req.StreetCode == "": return status.Error(10003, "街道不能为空") case req.Committee == "" || req.CommitteeCode == "": return status.Error(10003, "社区不能为空") case req.PropertyPerson == "": return status.Error(10003, "物业联系人不能为空") case req.PropertyPhone == "": return status.Error(10003, "物业联系人电话不能为空") case req.GardenName == "": return status.Error(10003, "小区名称不能为空") case req.GardenAddr == "": return status.Error(10003, "小区地址不能为空") case req.Cid < 0: return status.Error(10003, "物业公司不能为空") case req.Lnt == 0 || req.Lat == 0: return status.Error(10003, "经纬度不能为空") case len(req.Appendix) == 0: return status.Error(10003, "证明材料不能为空") case req.OrderId == 0: return status.Error(10003, "套餐订单不能为空") case (req.BuildingStart > 0 && req.BuildingEnd < 1) || (req.BuildingStart < 1 && req.BuildingEnd > 0): return status.Error(10003, "建成年份只能同时为空或不为空") case (req.PropertyFeeStart > 0 && req.PropertyFeeEnd < 1) || (req.PropertyFeeStart < 1 && req.PropertyFeeEnd > 0): return status.Error(10003, "物业费只能同时为空或不为空") case (req.GasFeeStart > 0 && req.GasFeeEnd < 1) || (req.GasFeeStart < 1 && req.GasFeeEnd > 0): return status.Error(10003, "燃气费只能同时为空或不为空") } return nil } // func CompanyAddGarden(ctx context.Context, req *pb_v1.CompanyAddGardenRequest) (reply *pb_v1.CompanyAddGardenReply, err error) { reply = &pb_v1.CompanyAddGardenReply{} // 捕获各个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 = checkGardenAddParam(req) if err != nil { return nil, err } // 检查公司 company := dbmodel.TCompany{} where := map[string]interface{}{ "id": req.Cid, } err = company.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if company.ID == 0 { return nil, errors.UserNotExist } if company.ApproveStatus != consts.ApproveStatusSuccess { return nil, errors.UserStatusError } //freeCount := company.FreeGardenCount //if freeCount == 0 { //return nil, errors.GardenLimitError //} // 先更新创建小区个数,以便回滚 db := database.DB().Begin() values := map[string]interface{}{ "garden_count": gorm.Expr("garden_count + ?", 1), } err = company.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } mreq := &pb_v1.GardenAddRequest{ Province: req.Province, ProvinceCode: req.ProvinceCode, City: req.City, CityCode: req.CityCode, Area: req.Area, AreaCode: req.AreaCode, Street: req.Street, StreetCode: req.StreetCode, Committee: req.Committee, CommitteeCode: req.CommitteeCode, PropertyPerson: req.PropertyPerson, PropertyPhone: req.PropertyPhone, GardenName: req.GardenName, GardenAddr: req.GardenAddr, Cid: req.Cid, //FreeGardenCount: int32(freeCount), GardenPic: req.GardenPic, GardenDesc: req.GardenDesc, Lat: req.Lat, Lnt: req.Lnt, Appendix: req.Appendix, OrderId: req.OrderId, GardenPics: req.GardenPics, BuildingStart: req.BuildingStart, BuildingEnd: req.BuildingEnd, PropertyFeeStart: req.PropertyFeeStart, PropertyFeeEnd: req.PropertyFeeEnd, GasFeeStart: req.GasFeeStart, GasFeeEnd: req.GasFeeEnd, BuildingArea: req.BuildingArea, BuildingCompany: req.BuildingCompany, CoveredArea: req.CoveredArea, GreenPercent: req.GreenPercent, AreaPercent: req.AreaPercent, SpaceInfo: req.SpaceInfo, SpaceTotal: req.SpaceTotal, HouseTotal: req.HouseTotal, BuildingType: req.BuildingType, WaterType: req.WaterType, ElectricType: req.ElectricType, AvgPrice: req.AvgPrice, } // 创建小区 mreply, err := pb.System.GardenAdd(ctx, mreq) if err != nil { db.Rollback() return nil, err } db.Commit() reply.Id = mreply.Id utils.RobotMsg(fmt.Sprintf("小区(%s)提交审核,请即时审核", req.GardenName)) return reply, nil }