123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- // 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"
- "strings"
- "time"
- )
- func checkChargeListParam(req *pb_v1.ChargeListRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.ObjId == 0:
- return status.Error(10003, "对象不能为空")
- case req.ObjType == 0:
- return status.Error(10003, "对象类型不能为空")
- }
- if req.Page == 0 {
- req.Page = 1
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- return nil
- }
- func ChargeList(ctx context.Context, req *pb_v1.ChargeListRequest) (reply *pb_v1.ChargeListReply, err error) {
- reply = &pb_v1.ChargeListReply{}
- // 捕获各个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 = checkChargeListParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- bind := dbmodel.NewChargeBind(dbname)
- where := map[string]interface{}{
- "obj_id": req.ObjId,
- "obj_type": req.ObjType,
- }
- if req.ChargeType > 0 {
- where["charge_type"] = req.ChargeType
- }
- reply.Total, err = bind.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.Page = req.Page
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := bind.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.List = make([]*pb_v1.ChargeItem, len(list))
- chargeIds := make([]int64, len(list))
- for i, v := range list {
- chargeIds[i] = v.ChargeId
- }
- chargeM, err := charge_utils.GetChargeInfos(chargeIds, dbname)
- if err != nil {
- return nil, err
- }
- bill := dbmodel.NewChargeBill(dbname)
- type Bcount struct {
- Count int64 `json:"count"`
- BindId int64 `json:"bind_id"`
- }
- blist := []Bcount{}
- err = bill.SelectGroup(database.DB(), where, nil, "sum(1) as count, charge_bind_id as bind_id", "charge_bind_id", -1, -1, &blist)
- if err != nil {
- return nil, errors.DataBaseError
- }
- bm := map[int64]bool{}
- for _, v := range blist {
- if v.Count > 0 {
- bm[v.BindId] = true
- }
- }
- for i, v := range list {
- chargeInfo := chargeM[v.ChargeId]
- item := &pb_v1.ChargeItem{
- BindId: v.ID,
- ChargeType: v.ChargeType,
- ChargeName: chargeInfo.ChargeName,
- ChargeTimeType: chargeInfo.ChargeTimeType,
- CreatedAt: v.CreatedAt.Format("2006-01-02"),
- Start: "-",
- End: "-",
- Status: "未开始",
- HasBill: bm[v.ID],
- }
- if v.Start > 0 {
- item.Start = time.Unix(v.Start, 0).Format("2006-01-02")
- }
- if v.End > 0 {
- item.End = time.Unix(v.End, 0).Format("2006-01-02")
- }
- if v.Start <= time.Now().Unix() {
- item.Status = "缴费中"
- }
- array := []string{}
- if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSpaceArea ||
- chargeInfo.ChargeBasis == charge_utils.ChargeBasisArea ||
- chargeInfo.ChargeBasis == charge_utils.ChargeBasisUsedArea {
- array = append(array, fmt.Sprintf("面积:%f", v.ObjArea))
- }
- if chargeInfo.ChargeBasis == charge_utils.ChargeBasisFix {
- array = append(array, fmt.Sprintf("固定费用:%.2f", float64(chargeInfo.FixAmount)/100.00))
- } else if chargeInfo.FixAmount > 0 {
- array = append(array, fmt.Sprintf("附加费用(%s):%.2f", chargeInfo.FixAmountName, float64(chargeInfo.FixAmount)/100.00))
- }
- if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
- array = append(array, fmt.Sprintf("自定义费用:%.2f", float64(v.CustomFee)/100.00))
- }
- if chargeInfo.UnitPrice > 0 {
- array = append(array, fmt.Sprintf("单价:%.2f", float64(chargeInfo.UnitPrice)/100.00))
- }
- item.Desc = strings.Join(array, "/")
- reply.List[i] = item
- }
- return reply, nil
- }
|