// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package house import ( "context" "encoding/json" "fmt" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/statistic" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "strings" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) const ( HouseStatusWait = 1 HouseStatusUsed = 2 HouseStatusRented = 3 ) func checkHouseAddParam(req *pb_v1.HouseAddRequest) error { switch { case req.HouseNumber == "": return status.Error(10003, "门牌号不能为空") case req.Layer < 1: return status.Error(10003, "楼层不能为空") case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.RoomCount < 0 || req.HallCount < 0: return status.Error(10003, "几室几厅不能为空") case req.HouseType < 1: return status.Error(10003, "房屋类型不能为空") case req.UnitId < 1: return status.Error(10003, "单元id不能为空") case req.HouseArea < 1: return status.Error(10003, "房屋面积不能为空") case req.HouseUsedArea < 1: return status.Error(10003, "房屋使用面积不能为空") case strings.Contains(req.HouseNumber, "-"): return status.Error(10003, "门牌号不能包含-") } return nil } // func HouseAdd(ctx context.Context, req *pb_v1.HouseAddRequest) (reply *pb_v1.HouseAddReply, err error) { reply = &pb_v1.HouseAddReply{} // 捕获各个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 = checkHouseAddParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) unit := dbmodel.NewUnit(dbname) where := map[string]interface{}{ "id": req.UnitId, } err = unit.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if unit.ID == 0 { return nil, errors.ErrRecordNotFound } building := dbmodel.NewBuilding(dbname) where = map[string]interface{}{ "id": unit.BuildingId, } err = building.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if building.ID == 0 { return nil, errors.ErrRecordNotFound } if building.ID != req.BuildingId { return nil, status.Error(10003, "单元和楼栋不匹配") } now := time.Now() house := dbmodel.THouse{ //GardenId: req.GardenId, HouseType: req.HouseType, HouseNumber: req.HouseNumber, BuildingId: unit.BuildingId, UnitId: req.UnitId, HasLift: unit.HasLift, HouseArea: req.HouseArea, HouseUsedArea: req.HouseUsedArea, Layer: req.Layer, RoomCount: req.RoomCount, HallCount: req.HallCount, UpdatedAt: now, CreatedAt: now, Status: HouseStatusWait, } house.SetTable(dbname) db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() err = house.Insert(db) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, errors.ErrRecordExist } return nil, errors.DataBaseError } mreq := pb_v1.ObjStatisticSetRequest{ ObjType: statistic.ObjTypeHouse, TotalIncrease: int64(1), GardenId: req.GardenId, } err = statistic.ObjStatisticSetWithDb(&mreq, db) if err != nil { return nil, err } if err = checkHouseCount(req.GardenId, int64(1)); err != nil { return nil, err } if err = houseCountChange(req.GardenId, int64(1)); err != nil { return nil, err } db.Commit() reply.Id = house.ID return reply, nil }