// 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 checkChargePayedBillListParam(req *pb_v1.ChargePayedBillListRequest) 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 } func getMonthStartAndEnd(data int64) (int64, int64) { t := time.Unix(data, 0) start := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local) t = t.AddDate(0, 1, 0) end := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local) return start.Unix(), end.Unix() } func ChargePayedBillList(ctx context.Context, req *pb_v1.ChargePayedBillListRequest) (reply *pb_v1.ChargePayedBillListReply, err error) { reply = &pb_v1.ChargePayedBillListReply{} // 捕获各个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 = checkChargePayedBillListParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) bill := dbmodel.NewChargeBillPayed(dbname) where := map[string]interface{}{} 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 } if req.PayTime > 0 { start, end := getMonthStartAndEnd(req.PayTime) where["pay_time >="] = start where["pay_time <"] = end } if req.HouseId > 0 { where["house_id"] = req.HouseId } if req.ObjType > 0 { where["obj_type"] = req.ObjType } // 账单数量 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), "pay_time desc") if err != nil { return nil, errors.DataBaseError } reply.List = make([]*pb_v1.ChargePayedBillItem, len(list)) for i, v := range list { chargeDesc := dbmodel.ChargeDesc{} json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc) item := &pb_v1.ChargePayedBillItem{ 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: v.LateFee, BillId: v.ID, ChargeDesc: charge_utils.GetChargeDesc(chargeDesc), PayTime: time.Unix(v.PayTime, 0).Format("2006-01-02 15:04:05"), PayType: v.PayType, ObjName: v.ObjName, ObjType: v.ObjType, } reply.List[i] = item } return reply, nil }