// 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" dbmodel "property-household/model" "property-household/pb" pb_v1 "property-household/pb/v1" "strings" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkHouseRentAppointmentAddParam(req *pb_v1.HouseRentAppointmentAddRequest) error { switch { case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.Uid < 1: return status.Error(10003, "用户不能为空") case req.Name == "": return status.Error(10003, "名字不能为空") case req.RentId < 1: return status.Error(10003, "租房信息不能为空") case req.Phone == "": return status.Error(10003, "手机号不能为空") case req.Gender == 0: return status.Error(10003, "性别不能为空") } return nil } const ( _ = iota HouseRentAppointmentWait HouseRentAppointmentAccess HouseRentAppointmentRefruse HouseRentAppointmentNotVisit HouseRentAppointmentVisited HouseRentAppointmentExpired ) var HouseRentAppointmentStatusM = map[int]string{ HouseRentAppointmentWait: "等待处理", HouseRentAppointmentAccess: "已受理", HouseRentAppointmentRefruse: "无法处理", HouseRentAppointmentNotVisit: "未到访", HouseRentAppointmentVisited: "已到访", } // func HouseRentAppointmentAdd(ctx context.Context, req *pb_v1.HouseRentAppointmentAddRequest) (reply *pb_v1.HouseRentAppointmentAddReply, err error) { reply = &pb_v1.HouseRentAppointmentAddReply{} // 捕获各个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 = checkHouseRentAppointmentAddParam(req) if err != nil { return nil, err } now := time.Now() rent := dbmodel.THouseRent{} where := map[string]interface{}{ "id": req.RentId, } err = rent.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if rent.ID == 0 { return nil, errors.ErrRecordNotFound } p := &dbmodel.THouseRentAppointment{ Uid: req.Uid, Name: req.Name, Phone: req.Phone, AppointmentTime: req.AppointmentTime, UpdatedAt: now, CreatedAt: now, Gender: req.Gender, HouseName: rent.HouseName, RentId: req.RentId, Area: rent.RoomArea, Layer: rent.Layer, Status: HouseRentAppointmentWait, GardenId: req.GardenId, } err = p.Insert(database.DB()) if err != nil { if strings.Contains(strings.ToLower(err.Error()), "duplicate") { return nil, status.Error(10003, "不能重复预约") } return nil, errors.DataBaseError } mreq := pb_v1.SystemMsgAddRequest{ GardenId: req.GardenId, Content: "新的租房看房预约", Code: SystemMsgCodeRentAppointment, } pb.Garden.SystemMsgAdd(ctx, &mreq) reply.Id = p.ID return reply, nil }