// 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" 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" ) func checkBuildingUpdateParam(req *pb_v1.BuildingUpdateRequest) error { switch { case req.BuildingNumber == "": return status.Error(10003, "楼栋编号不能为空") case req.BuildingName == "": return status.Error(10003, "楼栋名称不能为空") case req.Id < 1: return status.Error(10003, "id 不能为空") case req.GardenId < 1: return status.Error(10003, "小区id不能为空") case strings.Contains(req.BuildingNumber, "-"): return status.Error(10003, "楼栋号不能包含-") } return nil } // func BuildingUpdate(ctx context.Context, req *pb_v1.BuildingUpdateRequest) (reply *pb_v1.BuildingUpdateReply, err error) { reply = &pb_v1.BuildingUpdateReply{} // 捕获各个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 = checkBuildingUpdateParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) old := dbmodel.NewBuilding(dbname) where := map[string]interface{}{ "id": req.Id, //"garden_id":req.GardenId, } err = old.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if old.ID == 0 { return nil, errors.ErrRecordNotFound } if req.BuildingNumber != old.BuildingNumber { house := dbmodel.NewHouse(dbname) where = map[string]interface{}{ "building_id": req.Id, "status >": HouseStatusWait, } count, err := house.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } if count > 0 { return nil, status.Error(10003, "已入住房屋, 不能修改房屋编号,单元号,楼栋号") } } reply.Origin = &pb_v1.BuildingUpdateRequest{ Id: req.Id, GardenId: req.GardenId, BuildingName: old.BuildingName, BuildingNumber: old.BuildingNumber, BuildingArea: old.BuildingArea, BuildingUsedArea: old.BuildingUsedArea, } now := time.Now() building := dbmodel.NewBuilding(dbname) values := map[string]interface{}{ "building_name": req.BuildingName, "building_number": req.BuildingNumber, "building_area": req.BuildingArea, "building_used_area": req.BuildingUsedArea, "comment": req.Comment, "updated_at": now, } db := database.DB().Begin() where = map[string]interface{}{ "id": req.Id, //"garden_id":req.GardenId, } err = building.Update(db, where, values) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { db.Rollback() return nil, errors.ErrRecordExist } db.Rollback() return nil, errors.DataBaseError } if req.BuildingNumber != old.BuildingNumber { // 删除未审核的房屋申请 if err = RefuseHouseApply(req.GardenId, old.ID, 0, 0); err != nil { db.Rollback() return nil, err } } db.Commit() return reply, nil }