123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- // 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 checkChargeUnpayListParam(req *pb_v1.ChargeUnpayListRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.ObjId == 0:
- if req.HouseId == 0 && req.Status == 0 {
- return status.Error(10003, "对象不能为空")
- }
- case req.ObjType == 0:
- if req.HouseId == 0 && req.Status == 0 {
- return status.Error(10003, "对象类型不能为空")
- }
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- return nil
- }
- func GetObjBindChargeLateFee(bindId int64, chargeInfoM map[int64]dbmodel.TChargeConf, dbname string) (int64, error) {
- p := dbmodel.NewChargeBill(dbname)
- where := map[string]interface{}{
- //"obj_id":objId,
- //"obj_type":objType,
- "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]
- lfee, _ := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo)
- ret += lfee
- }
- if len(list) < 10 {
- break
- }
- }
- return ret, nil
- }
- type ChargeUnpayItem struct {
- BindId int64
- ChargeType int32
- ChargeName string
- ChargeTimeType int32
- BillCount int64
- UnpayAmount int64
- }
- func ChargeUnpayList(ctx context.Context, req *pb_v1.ChargeUnpayListRequest) (reply *pb_v1.ChargeUnpayListReply, err error) {
- reply = &pb_v1.ChargeUnpayListReply{}
- // 捕获各个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 = checkChargeUnpayListParam(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)
- result := []ChargeUnpayItem{}
- group := "charge_bind_id"
- selectStr := "charge_bind_id as bind_id, charge_type, charge_name, charge_time_type, sum(amount) as unpay_amount, sum(1) as bill_count"
- where := map[string]interface{}{}
- if req.ObjId > 0 {
- where["obj_id"] = req.ObjId
- }
- if req.ObjType > 0 {
- where["obj_type"] = req.ObjType
- }
- if req.ChargeType > 0 {
- where["charge_type"] = req.ChargeType
- }
- if req.HouseId > 0 {
- where["house_id"] = req.HouseId
- }
- if req.Status > 0 {
- where["status"] = req.Status
- }
- 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
- }
- reply.List = make([]*pb_v1.ChargeUnpayItem, len(result))
- for i, v := range result {
- lateFee, err := GetObjBindChargeLateFee(v.BindId, chargeInfoM, dbname)
- if err != nil {
- return nil, err
- }
- reply.List[i] = &pb_v1.ChargeUnpayItem{
- BindId: v.BindId,
- ChargeType: v.ChargeType,
- ChargeName: v.ChargeName,
- ChargeTimeType: v.ChargeTimeType,
- BillCount: v.BillCount,
- UnpayAmount: v.UnpayAmount + lateFee,
- }
- }
- return reply, nil
- }
|