// 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 checkUnitUpdateParam(req *pb_v1.UnitUpdateRequest) error { switch { case req.UnitNumber < 0: return status.Error(10003, "单元编号不能为空") case req.UnitName == "": return status.Error(10003, "单元名称名称不能为空") case req.UnitLayers < 1: return status.Error(10003, "层数不能为空") case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.BuildingId < 1: return status.Error(10003, "楼栋id不能为空") case req.Id < 1: return status.Error(10003, "id 不能为空") } return nil } // func UnitUpdate(ctx context.Context, req *pb_v1.UnitUpdateRequest) (reply *pb_v1.UnitUpdateReply, err error) { reply = &pb_v1.UnitUpdateReply{} // 捕获各个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 = checkUnitUpdateParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) oldUnit := dbmodel.NewUnit(dbname) where := map[string]interface{}{ "id":req.Id, //"garden_id":req.GardenId, } err = oldUnit.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if oldUnit.ID == 0 { return nil, errors.ErrRecordNotFound } reply.Origin = &pb_v1.UnitUpdateRequest{ Id:req.Id, GardenId:req.GardenId, UnitNumber:oldUnit.UnitNumber, UnitLayers:oldUnit.UnitLayers, UnitName:oldUnit.UnitName, BuildingId:oldUnit.BuildingId, } if oldUnit.HasLift == 1 { reply.Origin.HasLift = true } newBuilding := dbmodel.NewBuilding(dbname) where = map[string]interface{}{ "id":req.BuildingId, //"garden_id":req.GardenId, } err = newBuilding.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if newBuilding.ID == 0 { return nil, errors.ErrRecordNotFound } if req.BuildingId != oldUnit.BuildingId || req.UnitNumber != oldUnit.UnitNumber { house := dbmodel.NewHouse(dbname) where := map[string]interface{}{ "unit_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, "已入住房屋, 不能修改房屋编号,单元号,楼栋号") } } hasLift := int64(2) if req.HasLift { hasLift = 1 } now := time.Now() unit := dbmodel.NewUnit(dbname) values := map[string]interface{}{ "unit_name":req.UnitName, "unit_number":req.UnitNumber, "unit_layers":req.UnitLayers, "building_id":req.BuildingId, "has_lift":hasLift, "updated_at": now, } db := database.DB().Begin() where = map[string]interface{}{ "id":req.Id, //"garden_id":req.GardenId, } err = unit.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 } // 更新房屋 house := dbmodel.NewHouse(dbname) where = map[string]interface{}{ "unit_id":req.Id, } values = map[string]interface{}{} if oldUnit.HasLift != hasLift { values["has_lift"] = hasLift } if req.BuildingId != oldUnit.BuildingId { values["building_id"] = req.BuildingId } if len(values) > 0 { err = house.Update(database.DB(), where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } } if req.BuildingId != oldUnit.BuildingId || req.UnitNumber != oldUnit.UnitNumber { if err = RefuseHouseApply(req.GardenId, oldUnit.BuildingId, oldUnit.ID, 0); err != nil { db.Rollback() return nil, err } } if hasLift != oldUnit.HasLift { mreq := pb_v1.HouseRentChangeFieldRequest{ GardenId:req.GardenId, UnitId:req.Id, HasLift:req.HasLift, } err = houseRentChangeField(&mreq, db, dbname) if err != nil { db.Rollback() return nil, err } } db.Commit() return reply, nil }