// 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/public_msg" dbmodel "property-household/model" pb_v1 "property-household/pb/v1" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkHouseRentApproveParam(req *pb_v1.HouseRentApproveRequest) error { switch { case req.Id < 1: return status.Error(10003, "id不能为空") case req.GardenId < 1: return status.Error(10003, "小区不能为空") } return nil } // func HouseRentApprove(ctx context.Context, req *pb_v1.HouseRentApproveRequest) (reply *pb_v1.HouseRentApproveReply, err error) { reply = &pb_v1.HouseRentApproveReply{} // 捕获各个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 = checkHouseRentApproveParam(req) 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 } now := time.Now() approveStatus := HouseholdRentStatusFail if req.Status { approveStatus = HouseholdRentStatusSuccess } values := map[string]interface{}{ "approve_status": approveStatus, "approved_at": now, "feedback": req.Feedback, } db := database.DB().Begin() err = p.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } p.ApproveStatus = int32(approveStatus) p.ApprovedAt = now p.Feedback = req.Feedback increase := 0 if req.Status { increase = 1 } err = houseRentSync(p, false, req.GardenId, increase) if err != nil { db.Rollback() return nil, err } db.Commit() serviceInfo := public_msg.ServiceInfo{ Name: "委托发布房源", State: "已通过", } if !req.Status { serviceInfo.State = "未通过(" + req.Feedback + ")" } public_msg.SendMsgWx(p.HouseholdUid, p.GardenId, serviceInfo) return reply, nil }