// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package house_rent 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" "property-garden/errors" dbmodel "property-garden/model" "property-garden/pb" pb_v1 "property-garden/pb/v1" "property-garden/utils" ) func checkGardenHouseRentChangeFieldParam(req *pb_v1.GardenHouseRentChangeFieldRequest) error { switch { case req.GardenId < 1: return status.Error(10003, "小区不能为空") } return nil } // func GardenHouseRentChangeField(ctx context.Context, req *pb_v1.GardenHouseRentChangeFieldRequest) (reply *pb_v1.GardenHouseRentChangeFieldReply, err error) { reply = &pb_v1.GardenHouseRentChangeFieldReply{} // 捕获各个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 = checkGardenHouseRentChangeFieldParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) p := dbmodel.NewHouseRent(dbname) where := map[string]interface{}{} values := map[string]interface{}{} switch { case req.HouseId == 0 && req.UnitId == 0: if req.Lnt > 0 { values["lnt"] = req.Lnt } if req.Lat > 0 { values["lat"] = req.Lat } if req.GardenName != "" { values["garden_name"] = req.GardenName } case req.UnitId > 0: where["unit_id"] = req.UnitId hasLift := int32(1) if !req.HasLift { hasLift = 2 } values["has_lift"] = hasLift case req.HouseId > 0: where["house_id"] = req.HouseId if req.Layer > 0 { values["layer"] = req.Layer } if req.HouseArea > 0 { values["house_area"] = req.HouseArea } if req.RoomCount > 0 { values["room_count"] = req.RoomCount } if req.HallCount > 0 { values["hall_count"] = req.HallCount } } if len(values) == 0 { return reply, nil } db := database.DB().Begin() where["garden_id"] = req.GardenId err = p.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } mreq := pb_v1.HouseRentChangeFieldRequest{ GardenId:req.GardenId, HouseId:req.HouseId, UnitId: req.UnitId, RoomCount:req.RoomCount, HallCount:req.HallCount, HasLift:req.HasLift, HouseArea:req.HouseArea, Layer:req.Layer, GardenName:req.GardenName, Lnt:req.Lnt, Lat:req.Lat, } _, err = pb.Household.HouseRentChangeField(ctx, &mreq) if err != nil { db.Rollback() return nil, err } db.Commit() return reply, nil }