package statistic import ( "context" "server/internal/dao" "server/internal/model" "server/internal/model/do" "server/internal/service" "server/utility/ecode" ) type sStatistic struct { } func New() service.IStatistic { return &sStatistic{} } func init() { service.RegisterStatistic(New()) } func (s *sStatistic) GetOnlineDevice(ctx context.Context, in *model.GetOnlineDeviceIn) (out *model.GetOnlineDeviceOut, err error) { storeId, err := dao.Stores.Ctx(ctx).Fields(dao.Stores.Columns().Id).Where(do.Stores{NetbarAccount: in.NetbarAccount}).Value() if storeId.Int() == 0 { return nil, ecode.Fail.Sub("门店不存在") } if err != nil { return nil, ecode.Fail.Sub("查询门店 id失败") } var data model.GetOnlineDeviceOut if err = dao.StoreClients.Ctx(ctx).Where(do.StoreClients{StoreId: storeId}).Fields("count(*) as terminal_total,count(case when status = 3 then 1 end) as online_terminal_total").Scan(&data); err != nil { return nil, ecode.Fail.Sub("终端数量统计失败") } if err = dao.StoreClientSessions.Ctx(ctx).Where(do.StoreClientSessions{StoreId: storeId}).Fields("count(*) as start_times").Scan(&data); err != nil { return nil, ecode.Fail.Sub("终端启动次数统计失败") } if err = dao.UserTasks.Ctx(ctx).Where(do.UserTasks{StoreId: storeId}).Fields("count(*) as task_times,count(case when status = 2 then 1 end) as task_completed_total").Scan(&data); err != nil { return nil, ecode.Fail.Sub("任务相关统计失败") } return &data, nil }