// 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" "property-garden/errors" "property-garden/impl/v1/charge_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" ) func checkChargeObjBillListParam(req *pb_v1.ChargeObjBillListRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case !req.All && req.ObjId == 0: //return status.Error(10003, "对象id不能为空") case !req.All && req.ObjType == 0: //return status.Error(10003, "对象类型不能为空") } if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } return nil } func ChargeObjBillList(ctx context.Context, req *pb_v1.ChargeObjBillListRequest) (reply *pb_v1.ChargeObjBillListReply, err error) { reply = &pb_v1.ChargeObjBillListReply{} // 捕获各个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 = checkChargeObjBillListParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) chargeInfoM, err := charge_utils.GetAllChargeInfos(dbname) if err != nil { return nil, err } bill := dbmodel.NewChargeBill(dbname) where := map[string]interface{}{} if req.ObjType > 0 { where["obj_type"] = req.ObjType } if req.ObjId > 0 { where["obj_id"] = req.ObjId } if req.ChargeType > 0 { where["charge_type"] = req.ChargeType } if req.ObjName != "" { where["obj_name"] = req.ObjName } if req.ChargeName != "" { where["charge_name"] = req.ChargeName } // 账单数量 reply.Total, err = bill.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } if reply.Total == 0 { return reply, nil } reply.Page = req.Page // 账单列表 list, err := bill.List(database.DB(), where, nil, int(req.Page), int(req.PageSize), "") if err != nil { return nil, errors.DataBaseError } reply.List = make([]*pb_v1.ChargeBillItem, len(list)) for i, v := range list { chargeInfo := chargeInfoM[v.ChargeId] chargeDesc := dbmodel.ChargeDesc{} json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc) lfee, ldays := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo) item := &pb_v1.ChargeBillItem{ 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, LastUsed: chargeDesc.PowerLastUsed, CurrentUsed: chargeDesc.PowerCurrentUsed, LateFee: lfee, LateDays: ldays, BillId: v.ID, ChargeDesc: charge_utils.GetChargeDesc(chargeDesc), ObjId: v.ObjId, ObjType: v.ObjType, ObjName: v.ObjName, } reply.ShouldPayAmount += item.Amount reply.ShouldPayAmount += item.LateFee reply.List[i] = item } return reply, nil }