package bills import ( "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/userconfigs" "github.com/TeaOSLab/EdgeUser/internal/utils/numberutils" "github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils" "github.com/iwind/TeaGo/maps" ) type IndexAction struct { actionutils.ParentAction } func (this *IndexAction) Init() { this.Nav("", "", "index") } func (this *IndexAction) RunGet(params struct { PaidFlag int32 `default:"-1"` Month string }) { this.Data["paidFlag"] = params.PaidFlag // 账号余额 accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{ UserId: this.UserId(), }) if err != nil { this.ErrorPage(err) return } var account = accountResp.UserAccount if account == nil { this.Fail("当前用户还没有账号") } // 需要支付的总额 unpaidAmountResp, err := this.RPC().UserBillRPC().SumUserUnpaidBills(this.UserContext(), &pb.SumUserUnpaidBillsRequest{UserId: this.UserId()}) if err != nil { this.ErrorPage(err) return } this.Data["accountTotal"] = account.Total this.Data["unpaidAmount"] = unpaidAmountResp.Amount countResp, err := this.RPC().UserBillRPC().CountAllUserBills(this.UserContext(), &pb.CountAllUserBillsRequest{ PaidFlag: params.PaidFlag, UserId: this.UserId(), Month: params.Month, }) if err != nil { this.ErrorPage(err) return } page := this.NewPage(countResp.Count) this.Data["page"] = page.AsHTML() billsResp, err := this.RPC().UserBillRPC().ListUserBills(this.UserContext(), &pb.ListUserBillsRequest{ PaidFlag: params.PaidFlag, UserId: this.UserId(), Month: params.Month, Offset: page.Offset, Size: page.Size, }) if err != nil { this.ErrorPage(err) return } var billMaps = []maps.Map{} for _, bill := range billsResp.UserBills { var userMap maps.Map = nil if bill.User != nil { userMap = maps.Map{ "id": bill.User.Id, "fullname": bill.User.Fullname, } } var month = bill.Month var dayFrom = bill.DayFrom var dayTo = bill.DayTo if len(month) == 6 { month = month[:4] + "-" + month[4:] } if len(dayFrom) == 8 { dayFrom = dayFrom[:4] + "-" + dayFrom[4:6] + "-" + dayFrom[6:] } if len(dayTo) == 8 { dayTo = dayTo[:4] + "-" + dayTo[4:6] + "-" + dayTo[6:] } billMaps = append(billMaps, maps.Map{ "id": bill.Id, "code": bill.Code, "isPaid": bill.IsPaid, "month": month, "dayFrom": dayFrom, "dayTo": dayTo, "amount": numberutils.FormatFloat(bill.Amount, 2), "typeName": bill.TypeName, "user": userMap, "description": bill.Description, "canPay": bill.CanPay, "pricePeriodName": userconfigs.PricePeriodName(bill.PricePeriod), "pricePeriod": bill.PricePeriod, "isOverdue": bill.IsOverdue, }) } this.Data["bills"] = billMaps this.Show() }