// 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" "gorm.io/gorm" "property-household/errors" "property-household/impl/v1/house" dbmodel "property-household/model" pb_v1 "property-household/pb/v1" "property-household/utils" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkHouseRentUpdateParam(req *pb_v1.HouseRentUpdateRequest) error { switch { case req.RentPrice == 0: return status.Error(10003, "租金不能为空") case req.RentType < 1: return status.Error(10003, "出租方式不能为空") case req.ContactPhone == "": return status.Error(10003, "联系人电话不能为空") case req.Contacter == "": return status.Error(10003, "联系人不能为空") case req.HouseId == 0: return status.Error(10003, "房屋不能为空") case req.Id < 1: return status.Error(10003, "id不能为空") case req.GardenId < 1: return status.Error(10003, "小区不能为空") } housePic := []string{} for _, v := range req.HousePic { if isFix(v) { continue } if v == "" { continue } housePic = append(housePic, v) } req.HousePic = housePic if req.CertPic == nil { req.CertPic = []string{} } return nil } // func HouseRentUpdate(ctx context.Context, req *pb_v1.HouseRentUpdateRequest) (reply *pb_v1.HouseRentUpdateReply, err error) { reply = &pb_v1.HouseRentUpdateReply{} // 捕获各个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 = checkHouseRentUpdateParam(req) if err != nil { return nil, err } houseInfo, err := house.GetHouseInfo(req.HouseId, req.GardenId) if err != nil { return nil, err } p := dbmodel.THouseRent{} where := map[string]interface{}{ "id": req.Id, } err = p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if p.ID == 0 { return nil, errors.ErrRecordNotFound } if p.ApproveStatus == 2 { return nil, status.Error(10003, "当前状态不能修改") } if p.HouseId != req.HouseId || p.GardenId != req.GardenId { old := dbmodel.THouseRent{} oldWhere := map[string]interface{}{ "garden_id": req.GardenId, "house_id": req.HouseId, "approve_status <": 4, } oldCount, err := old.Count(database.DB(), oldWhere, nil) if err != nil { return nil, errors.DataBaseError } if oldCount > 0 { return nil, status.Error(10003, "已存在相同记录") } } now := time.Now() haseLift := int32(1) if !houseInfo.HasLift { haseLift = 2 } values := map[string]interface{}{ "house_name": fmt.Sprintf("%v-%v-%v", houseInfo.BuildingNumber, houseInfo.UnitNumber, houseInfo.HouseNumber), "layer": houseInfo.Layer, "house_area": houseInfo.HouseArea, "diretion": req.Direction, "room_count": houseInfo.RoomCount, "hall_count": houseInfo.HallCount, "wc_count": req.WcCount, "decorating": req.Decorating, "contacter": req.Contacter, "contact_phone": req.ContactPhone, "pay_time_type": req.PayTimeType, "rent_type": req.RentType, "room_type": req.RoomType, "room_area": req.RoomArea, "rent_price": req.RentPrice, "desposit": req.Desposit, "in_time": req.InTime, "service_price": req.ServicePrice, "intermediary_price": req.IntermediaryPrice, "base_conf": req.BaseConf, "special_conf": req.SpecialConf, "house_pic": utils.StringJoin(req.HousePic, ";"), "cert_pic": utils.StringJoin(req.CertPic, ";"), "has_lift": haseLift, "updated_at": now, "desc": req.Desc, "house_id": req.HouseId, "unit_id": houseInfo.UnitId, "approve_status": 1, } if req.HasLift { values["has_lift"] = 1 } db := database.DB().Begin() err = p.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } err = p.Find(db, where) if err != nil { db.Rollback() return nil, errors.DataBaseError } err = houseRentSync(p, false, req.GardenId, 0) if err != nil { db.Rollback() return nil, err } db.Commit() return reply, nil }