// 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" "property-household/errors" "property-household/impl/v1/house" dbmodel "property-household/model" "property-household/pb" pb_v1 "property-household/pb/v1" "property-household/utils" "strings" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-household/parser" ) // 认证状态 const ( HouseholdRentStatusWait = 1 HouseholdRentStatusSuccess = 2 HouseholdRentStatusFail = 3 ) const ( SystemMsgCodeHouseholdBindHouse = "1" SystemMsgCodeRepairOrder = "2" SystemMsgCodeRepairOrderSended = "3" SystemMsgCodeSuggestionOrder = "4" SystemMsgCodeSuggestionOrderSended = "5" SystemMsgCodeRent = "6" SystemMsgCodeRentAppointment = "7" ) func houseRentSync(data dbmodel.THouseRent, insert bool, gardenId int64, increase int) error { bytes, _ := json.Marshal(data) mreq := pb_v1.GardenHouseRentSyncRequest{ Datas: bytes, Insert: insert, GardenId: gardenId, Increase: int64(increase), } _, err := pb.Garden.GardenHouseRentSync(context.Background(), &mreq) if err != nil { return err } return nil } func isFix(src string) bool { if strings.Contains(src, parser.Conf.Oss.RentObj) || strings.Contains(src, parser.Conf.Oss.EventObj) || strings.Contains(src, parser.Conf.Oss.VoteObj) { return true } return false } func checkHouseRentApplyParam(req *pb_v1.HouseRentApplyRequest) 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.HouseName == "": // return status.Error(10003, "房屋不能为空") case !req.Approve && req.HouseholdUid < 1: return status.Error(10003, "业主不能为空") case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.HouseId < 1: return status.Error(10003, "房屋id不能为空") } housePic := []string{} for _, v := range req.HousePic { if isFix(v) { continue } housePic = append(housePic, v) } req.HousePic = housePic if req.CertPic == nil { req.CertPic = []string{} } return nil } func getGardenInfo(id int64) (*pb_v1.GardenItem, error) { mreq := pb_v1.GardenInfosRequest{ Ids: []int64{id}, } mreply, err := pb.System.GardenInfos(context.Background(), &mreq) if err != nil { return nil, err } if len(mreply.List) == 0 { return nil, status.Error(10003, "未找到小区") } return mreply.List[0], nil } func BaseConfToBool(data int64) pb_v1.HouseRentBaseConf { conf := pb_v1.HouseRentBaseConf{} one := int64(1) if data&one > 0 { conf.Bed = true } if data&(one<<1) > 0 { conf.Gas = true } if data&(one<<2) > 0 { conf.WarmGas = true } if data&(one<<3) > 0 { conf.Broadband = true } if data&(one<<4) > 0 { conf.Refragerator = true } if data&(one<<5) > 0 { conf.Wardobe = true } if data&(one<<6) > 0 { conf.Sofa = true } if data&(one<<7) > 0 { conf.Aircondition = true } if data&(one<<8) > 0 { conf.Tv = true } if data&(one<<9) > 0 { conf.Heater = true } if data&(one<<10) > 0 { conf.Warshing = true } return conf } func SpecialConfToBool(data int64) pb_v1.HouseRentSpecialConf { conf := pb_v1.HouseRentSpecialConf{} one := int64(1) if data&one > 0 { conf.IntelligentLock = true } if data&(one<<1) > 0 { conf.Wifi = true } if data&(one<<2) > 0 { conf.Metro = true } if data&(one<<3) > 0 { conf.ParkSpace = true } if data&(one<<4) > 0 { conf.IndependentWc = true } if data&(one<<5) > 0 { conf.PrivateBalcony = true } if data&(one<<6) > 0 { conf.FirstRent = true } return conf } func BaseConfToBitmap(conf *pb_v1.HouseRentBaseConf) int64 { ret := int64(0) one := int64(1) if conf.Bed { ret = ret | (one) } if conf.Gas { ret = ret | (one << 1) } if conf.WarmGas { ret = ret | (one << 2) } if conf.Broadband { ret = ret | (one << 3) } if conf.Refragerator { ret = ret | (one << 4) } if conf.Wardobe { ret = ret | (one << 5) } if conf.Sofa { ret = ret | (one << 6) } if conf.Aircondition { ret = ret | (one << 7) } if conf.Tv { ret = ret | (one << 8) } if conf.Heater { ret = ret | (one << 9) } if conf.Warshing { ret = ret | (one << 10) } return ret } func SpecialConfToBitmap(conf *pb_v1.HouseRentSpecialConf) int64 { ret := int64(0) one := int64(1) if conf.IntelligentLock { ret = ret | (one) } if conf.Wifi { ret = ret | (one << 1) } if conf.Metro { ret = ret | (one << 2) } if conf.ParkSpace { ret = ret | (one << 3) } if conf.IndependentWc { ret = ret | (one << 4) } if conf.PrivateBalcony { ret = ret | (one << 5) } if conf.FirstRent { ret = ret | (one << 6) } return ret } // func HouseRentApply(ctx context.Context, req *pb_v1.HouseRentApplyRequest) (reply *pb_v1.HouseRentApplyReply, err error) { reply = &pb_v1.HouseRentApplyReply{} // 捕获各个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 = checkHouseRentApplyParam(req) if err != nil { return nil, err } 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, "已存在相同记录") } gardenInfo, err := getGardenInfo(req.GardenId) if err != nil { return nil, err } houseInfo, err := house.GetHouseInfo(req.HouseId, req.GardenId) if err != nil { return nil, err } now := time.Now() hasList := int32(1) if !houseInfo.HasLift { hasList = 2 } houseRent := dbmodel.THouseRent{ GardenId: req.GardenId, GardenName: gardenInfo.GardenName, HouseName: fmt.Sprintf("%v-%v-%v", houseInfo.BuildingNumber, houseInfo.UnitNumber, houseInfo.HouseNumber), Layer: houseInfo.Layer, HouseArea: houseInfo.HouseArea, Diretion: req.Direction, RoomCount: houseInfo.RoomCount, HallCount: houseInfo.HallCount, WcCount: req.WcCount, Decorating: req.Decorating, Contacter: req.Contacter, ContactPhone: req.ContactPhone, PayTimeType: req.PayTimeType, RentType: req.RentType, RoomType: req.RoomType, RoomArea: req.RoomArea, RentPrice: req.RentPrice, Desposit: req.Desposit, InTime: req.InTime, ServicePrice: req.ServicePrice, IntermediaryPrice: req.IntermediaryPrice, BaseConf: req.BaseConf, SpecialConf: req.SpecialConf, HousePic: utils.StringJoin(req.HousePic, ";"), CertPic: utils.StringJoin(req.CertPic, ";"), ProvinceCode: gardenInfo.ProvinceCode, CityCode: gardenInfo.CityCode, AreaCode: gardenInfo.AreaCode, StreetCode: gardenInfo.StreetCode, HouseholdUid: req.HouseholdUid, ApproveStatus: HouseholdRentStatusWait, HasLift: hasList, //Feedback string `gorm:"column:feedback" json:"feedback"` CreatedAt: now, UpdatedAt: now, Desc: req.Desc, Lat: gardenInfo.Lat, Lnt: gardenInfo.Lnt, HouseId: req.HouseId, UnitId: houseInfo.UnitId, } if req.HasLift { houseRent.HasLift = 1 } if req.Approve { houseRent.ApproveStatus = HouseholdRentStatusSuccess houseRent.ApprovedAt = now } db := database.DB().Begin() err = houseRent.Insert(db) if err != nil { db.Rollback() if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, status.Error(10003, "房屋不能重复发布") } return nil, errors.DataBaseError } reply.Id = houseRent.ID increase := 0 if req.Approve { increase = 1 } err = houseRentSync(houseRent, true, req.GardenId, increase) if err != nil { db.Rollback() return nil, err } db.Commit() if !req.Approve { mreq := pb_v1.SystemMsgAddRequest{ GardenId: req.GardenId, Content: "新的房屋租赁申请", Code: SystemMsgCodeRent, } pb.Garden.SystemMsgAdd(ctx, &mreq) } return reply, nil }