// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package charge 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" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" ) func checkChargeOrderTicketParam(req *pb_v1.ChargeOrderTicketRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.OrderId == "": return status.Error(10003, "订单不能为空") } return nil } func ChargeOrderTicket(ctx context.Context, req *pb_v1.ChargeOrderTicketRequest) (reply *pb_v1.ChargeOrderTicketReply, err error) { reply = &pb_v1.ChargeOrderTicketReply{} // 捕获各个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 = checkChargeOrderTicketParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) order := dbmodel.NewChargeOrder(dbname) where := map[string]interface{}{ "order_id": req.OrderId, } err = order.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if order.OrderId == "" { return nil, errors.ErrRecordNotFound } if order.Status != charge_utils.PayStatusPayed { return nil, status.Error(10003, "当前订单未支付") } reply.ShouldPayAmount = order.Amount reply.Comment = order.Comment bill := dbmodel.NewChargeBillPayed(dbname) where = map[string]interface{}{ "order_id": req.OrderId, } // 账单列表 list, err := bill.List(database.DB(), where, nil, -1, -1, "") if err != nil { return nil, errors.DataBaseError } reply.List = make([]*pb_v1.ChargeOrderTicketItem, len(list)) for i, v := range list { chargeDesc := dbmodel.ChargeDesc{} json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc) item := &pb_v1.ChargeOrderTicketItem{ Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"), End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"), ChargeType: v.ChargeType, ChargeName: v.ChargeName, Amount: v.Amount, ObjName: v.ObjName, ObjType: v.ObjType, LateFee: v.LateFee, UnitPrice: chargeDesc.UnitPrice, PayTime: time.Unix(v.PayTime, 0).Format("2006-01-02 15:04:05"), } if v.ObjType == charge_utils.ObjTypeHouse { if charge_utils.IsPowerChargeType[v.ChargeType] { item.AreaUsed = chargeDesc.PowerCurrentUsed - chargeDesc.PowerLastUsed } else { item.AreaUsed = chargeDesc.ObjArea } } if v.ObjType == charge_utils.ObjTypeSpace { item.AreaUsed = chargeDesc.ObjArea } if v.ObjType == charge_utils.ObjTypeVehicle { if charge_utils.ChargeBasisSelf == chargeDesc.ChargeBasis { item.CustomFee = chargeDesc.CustomFee } } reply.List[i] = item } return reply, nil }