// 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 checkChargeBillListParam(req *pb_v1.ChargeBillListRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") case req.BindId == 0: return status.Error(10003, "关系id不能为空") } if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } return nil } func GetBindChargeBillTotalFee(bindId int64, chargeInfoM map[int64]dbmodel.TChargeConf, dbname string) (int64, error) { p := dbmodel.NewChargeBill(dbname) where := map[string]interface{}{ "charge_bind_id": bindId, } page := 1 ret := int64(0) for { list, err := p.List(database.DB(), where, nil, page, 10, "") if err != nil { return 0, errors.DataBaseError } if len(list) == 0 { return ret, nil } for _, v := range list { chargeInfo := chargeInfoM[v.ChargeId] ret += v.Amount lfee, _ := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo) ret += lfee } if len(list) < 10 { break } } return ret, nil } func ChargeBillList(ctx context.Context, req *pb_v1.ChargeBillListRequest) (reply *pb_v1.ChargeBillListReply, err error) { reply = &pb_v1.ChargeBillListReply{} // 捕获各个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 = checkChargeBillListParam(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{}{ "charge_bind_id": req.BindId, } if req.Status > 0 { where["status"] = req.Status } // 账单数量 reply.Total, err = bill.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } if reply.Total == 0 { return reply, nil } // 总的代缴费用包含滞纳金 reply.ShouldPayAmount, err = GetBindChargeBillTotalFee(req.BindId, chargeInfoM, dbname) if err != nil { return nil, err } 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)) chargeIds := []int64{} for _, v := range list { chargeIds = append(chargeIds, v.ChargeId) } for i, v := range list { chargeInfo := chargeInfoM[v.ChargeId] chargeDesc := dbmodel.ChargeDesc{} json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc) latefee, lateDays := 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: latefee, LateDays: lateDays, BillId: v.ID, ChargeDesc: charge_utils.GetChargeDesc(chargeDesc), } if i == 0 { reply.ChargeName = v.ChargeName reply.ChargeType = v.ChargeType } reply.List[i] = item } return reply, nil }