统计当前系统中的报表
This commit is contained in:
parent
9ad3415cdb
commit
5866882c2d
|
@ -2,6 +2,7 @@ package controller
|
|||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
|
@ -14,6 +15,7 @@ var StatisticsWithdrawLog = logger.Named("Handler", "StatisticsWithdraw")
|
|||
|
||||
func InitializeStatisticsController(router *fiber.App) {
|
||||
router.Get("/audits", security.OPSAuthorize, currentAuditAmount)
|
||||
router.Get("/stat/reports", statReports)
|
||||
|
||||
}
|
||||
|
||||
|
@ -31,4 +33,53 @@ func currentAuditAmount(c *fiber.Ctx) error {
|
|||
fiber.Map{"withdraw": amount})
|
||||
}
|
||||
|
||||
func statReports(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
var (
|
||||
enterprises int64 = 0
|
||||
parks int64 = 0
|
||||
reports []model.ParkPeriodStatistics
|
||||
)
|
||||
if session.Type != 0 {
|
||||
enterprises, err = service.StatisticsService.EnabledEnterprises()
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
parks, err = service.StatisticsService.EnabledParks()
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
//TODO: 2023.07.26 报表数据库结构改变,此处逻辑复杂放在最后处理
|
||||
reports, err = service.StatisticsService.ParkNewestState()
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
} else {
|
||||
parks, err = service.StatisticsService.EnabledParks(session.Uid)
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
//TODO: 2023.07.26 报表数据库结构改变,此处逻辑复杂放在最后处理
|
||||
reports, err = service.StatisticsService.ParkNewestState(session.Uid)
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return result.Success("已经完成园区报告的统计。", fiber.Map{
|
||||
"statistics": fiber.Map{
|
||||
"enterprises": enterprises,
|
||||
"parks": parks,
|
||||
"reports": reports,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
|
@ -35,4 +36,10 @@ type Park struct {
|
|||
type Parks struct {
|
||||
Park
|
||||
NormAuthorizedLossRate float64 `json:"norm_authorized_loss_rate"`
|
||||
}
|
||||
}
|
||||
|
||||
type ParkPeriodStatistics struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Period *types.DateRange
|
||||
}
|
||||
|
|
74
service/statistics.go
Normal file
74
service/statistics.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type _StatisticsService struct {
|
||||
l *zap.Logger
|
||||
ss goqu.DialectWrapper
|
||||
}
|
||||
|
||||
var StatisticsService = _StatisticsService{
|
||||
logger.Named("Service", "Stat"),
|
||||
goqu.Dialect("postgres"),
|
||||
}
|
||||
|
||||
//用于统计企业用户数量
|
||||
func (ss _StatisticsService) EnabledEnterprises() (int64, error) {
|
||||
ss.l.Info("开始统计企业数量。")
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
UserCountQuery, UserCountQueryArgs, _ := ss.ss.
|
||||
From(goqu.T("user")).
|
||||
Where(goqu.I("type").Eq(model.USER_TYPE_ENT)).
|
||||
Where(goqu.I("enabled").Eq(true)).
|
||||
Select(goqu.COUNT("*")).ToSQL()
|
||||
|
||||
var c int64
|
||||
err := pgxscan.Get(ctx, global.DB, &c, UserCountQuery, UserCountQueryArgs...)
|
||||
if err != nil {
|
||||
ss.l.Error("统计企业数量出错", zap.Error(err))
|
||||
return 0, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
//用于统计园区数量
|
||||
func (ss _StatisticsService) EnabledParks(userIds ...string) (int64, error) {
|
||||
ss.l.Info("开始统计园区数量", zap.Strings("userId", userIds))
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
ParkCountQuery := ss.ss.
|
||||
From(goqu.T("park")).
|
||||
Where(goqu.I("enabled").Eq(true))
|
||||
|
||||
if len(userIds) > 0 {
|
||||
ParkCountQuery = ParkCountQuery.Where(goqu.I("user_id").In(userIds))
|
||||
}
|
||||
|
||||
ParkCountQuerySql, ParkCountQueryArgs, _ := ParkCountQuery.Select(goqu.COUNT("*")).ToSQL()
|
||||
|
||||
var c int64
|
||||
err := pgxscan.Get(ctx, global.DB, &c, ParkCountQuerySql, ParkCountQueryArgs...)
|
||||
if err != nil {
|
||||
ss.l.Error("园区数量统计错误", zap.Error(err))
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
//用户统计报表
|
||||
func (ss _StatisticsService) ParkNewestState(userIds ...string) ([]model.ParkPeriodStatistics, error) {
|
||||
//TODO: 2023.07.26 报表数据库结构改变,此处逻辑复杂放在最后处理
|
||||
//return nil,errors.New("还未处理逻辑")
|
||||
return []model.ParkPeriodStatistics{}, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user