// 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/oss_utils" dbmodel "property-system/model" pb_v1 "property-system/pb/v1" "strconv" "strings" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) const ( GardenStatusWait = 1 GardenStatusApproved = 2 GardenStatusApprovedFail = 3 ) func int32ArrayToString(data []int32) string { if len(data) == 0 { return "" } ret := []string{} for _, v := range data { if v == 0 { continue } tmp := strconv.Itoa(int(v)) ret = append(ret, tmp) } return strings.Join(ret, ";") } func stringToInt32Array(data string) []int32 { if data == "" { return []int32{} } src := strings.Split(data, ";") ret := []int32{} for _, v := range src { tmp, _ := strconv.Atoi(v) ret = append(ret, int32(tmp)) } return ret } func checkGardenAddParam(req *pb_v1.GardenAddRequest) 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 < 1: 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 GardenAdd(ctx context.Context, req *pb_v1.GardenAddRequest) (reply *pb_v1.GardenAddReply, err error) { reply = &pb_v1.GardenAddReply{} // 捕获各个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 } gardenApprove := dbmodel.TGardenApproved{} filter := map[string]interface{}{ "street_code": req.StreetCode, "garden_name": req.GardenName, "in_use": true, } count, err := gardenApprove.Count(database.DB(), filter, nil) if err != nil { return nil, errors.DataBaseError } if count > 0 { return nil, errors.GardenExist } now := time.Now() garden := dbmodel.TGarden{ 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, UniqFlag: 1, GardenDesc: req.GardenDesc, GardenPic: req.GardenPic, UpdatedAt: now, CreatedAt: now, Lat: req.Lat, Lnt: req.Lnt, Status: GardenStatusWait, Appendix: strings.Join(req.Appendix, ";"), GardenPics: strings.Join(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: int32ArrayToString(req.WaterType), ElectricType: int32ArrayToString(req.ElectricType), AvgPrice: req.AvgPrice, } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() err = garden.Insert(db) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, status.Error(10003, "小区已存在") } return nil, errors.DataBaseError } pac := dbmodel.TPackageOrder{} where := map[string]interface{}{ "id": req.OrderId, "status": 2, } err = pac.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if pac.ID == 0 { return nil, errors.ErrRecordNotFound } if pac.GardenId > 0 { return nil, status.Error(10003, "套餐已占用") } values := map[string]interface{}{ "garden_id": garden.ID, } err = pac.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } reply.Id = garden.ID // 检查小区数量 /* garden.ID = 0 where := map[string]interface{}{ "cid": req.Cid, "status":GardenStatusWait, } waitCount, err := garden.Count(db, where, nil) if err != nil { return nil, errors.DataBaseError } where = map[string]interface{}{ "cid": req.Cid, } gardenApprove.ID = 0 approvedCount, err := gardenApprove.Count(db, where, nil) if err != nil { return nil, errors.DataBaseError } if req.FreeGardenCount <= int32(waitCount+approvedCount) && req.FreeGardenCount >= 0 { return nil, status.Error(10003, "超过数量") } */ inList := []string{} inList = append(inList, req.Appendix...) inList = append(inList, req.GardenPic) inList = append(inList, req.GardenPics...) if err := oss_utils.OssObjAdd(inList, nil); err != nil { db.Rollback() return nil, err } db.Commit() return reply, nil }