// 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" ) func checkChargeBillObjListParam(req *pb_v1.ChargeBillObjListRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") } if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } return nil } type ChargeBillObjItem struct { ObjName string ObjId int64 ObjType int32 BillCount int64 ShouldPayAmount int64 } func GetObjLateFee(objId int64, objType int32, chargeInfoM map[int64]dbmodel.TChargeConf, dbname string) (int64, error) { p := dbmodel.NewChargeBill(dbname) where := map[string]interface{}{ "obj_id": objId, "obj_type": objType, } 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] lfee, _ := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo) ret += lfee } if len(list) < 10 { break } } return ret, nil } func ChargeBillObjList(ctx context.Context, req *pb_v1.ChargeBillObjListRequest) (reply *pb_v1.ChargeBillObjListReply, err error) { reply = &pb_v1.ChargeBillObjListReply{} // 捕获各个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 = checkChargeBillObjListParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) bill := dbmodel.NewChargeBill(dbname) result := []ChargeBillObjItem{} group := "obj_type, obj_id" selectStr := "obj_id, obj_type, obj_name, sum(amount) as should_pay_amount, sum(1) as bill_count" where := map[string]interface{}{} reply.Total, err = bill.CountGroup(database.DB(), where, nil, group) if err != nil { return nil, errors.DataBaseError } reply.Page = req.Page if reply.Total == 0 { return reply, nil } err = bill.SelectGroup(database.DB(), where, nil, selectStr, group, int(req.Page), int(req.PageSize), &result) if err != nil { return nil, errors.DataBaseError } chargeInfoM, err := charge_utils.GetAllChargeInfos(dbname) if err != nil { return nil, err } reply.List = make([]*pb_v1.ChargeBillObjItem, len(result)) for i, v := range result { item := &pb_v1.ChargeBillObjItem{ ObjName: v.ObjName, ObjId: v.ObjId, ObjType: v.ObjType, BillCount: v.BillCount, ShouldPayAmount: v.ShouldPayAmount, } lateFee, err := GetObjLateFee(v.ObjId, v.ObjType, chargeInfoM, dbname) if err != nil { return nil, err } item.ShouldPayAmount += lateFee reply.List[i] = item } return reply, nil }