瀏覽代碼

电脑统计

wangbin 3 年之前
父節點
當前提交
cff5a64868

+ 15 - 12
api/v1/log/loging.go

@@ -6,7 +6,6 @@ import (
 	"go.uber.org/zap"
 	"log-server/global"
 	"log-server/model/common/response"
-	"log-server/model/example"
 	"log-server/model/log/request"
 	"log-server/service/log"
 	loging2 "log-server/service/log/loging"
@@ -342,7 +341,7 @@ func (s *ApiLoging) ComputerTest(c *gin.Context) {
 	var paramsInfo request.OnlineComputerRequest
 	_ = c.ShouldBindJSON(&paramsInfo)
 	ServiceStatisticsLog.RegularCheckPc()
-	response.OkWithDetailed(0, "上报成功", c)
+	response.OkWithDetailed("b", "上报成功", c)
 }
 
 // @Tags excel
@@ -350,19 +349,23 @@ func (s *ApiLoging) ComputerTest(c *gin.Context) {
 // @Security ApiKeyAuth
 // @accept application/json
 // @Produce  application/octet-stream
-// @Param data body example.ExcelInfo true "导出Excel文件信息"
+// @Param data body request.GetStatisticsComputerRequest true "导出Excel文件信息"
 // @Success 200
-// @Router /excel/exportExcel [post]
+// @Router /loging/exportExcel [post]
 func (e *ApiLoging) ExportExcel(c *gin.Context) {
-	var createDate request.CreateDateReply
-	_ = c.ShouldBindJSON(&createDate)
-	if createDate.CreateDate == "" {
-		global.GVA_LOG.Error("参数错误!")
-		response.FailWithMessage("参数错误", c)
+	var excelInfo request.ExcelInfo
+	_ = c.ShouldBindJSON(&excelInfo)
+	paramsInfo := excelInfo.InfoList
+	paramsInfo.PageSize = 300
+	paramsInfo.Page = 1
+	list, _, err := ServiceStatisticsLog.OnlineComputerStatistics(c, paramsInfo.LogComputer, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc)
+	if err != nil {
+		global.GVA_LOG.Error("获取电脑数据失败!", zap.Error(err))
+		response.FailWithMessage("获取电脑数据失败", c)
+		return
 	}
-	fileName := createDate.CreateDate + "-pc.xlsx"
-	filePath := global.GVA_CONFIG.Excel.Dir + fileName
-	err := excelService.ParseInfoList2Excel(excelInfo.InfoList, filePath)
+	filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
+	err = ServiceStatisticsLog.ParseInfoList2Excel(list, filePath)
 	if err != nil {
 		global.GVA_LOG.Error("转换Excel失败!", zap.Error(err))
 		response.FailWithMessage("转换Excel失败", c)

+ 2 - 0
config.yaml

@@ -157,3 +157,5 @@ zap:
   max-age: 30
   show-line: true
   log-in-console: true
+send-url:
+  computer-send-url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b111"

+ 2 - 1
config/config.go

@@ -25,5 +25,6 @@ type Server struct {
 	Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"`
 
 	// 跨域配置
-	Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"`
+	Cors    CORS    `mapstructure:"cors" json:"cors" yaml:"cors"`
+	SendUrl SendUrl `mapstructure:"send-url" json:"send-url" yaml:"send-url"`
 }

+ 5 - 0
config/send_url.go

@@ -0,0 +1,5 @@
+package config
+
+type SendUrl struct {
+	ComputerSendUrl string `mapstructure:"computer-send-url" json:"computer-send-url" yaml:"computer-send-url"`
+}

+ 18 - 14
initialize/timer.go

@@ -43,18 +43,22 @@ func Timer() {
 		fmt.Println("add CreateComputerStatisticsData timer error:", err)
 	}
 
-	//_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 0/8 6-23 * * * ", serviceStatisticsLog.TodayCreateStatisticsGameInfoLog)
-	//if err != nil {
-	//	fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
-	//}
-	//
-	//_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 0/3 6-23 * * * ", serviceStatisticsLog.TaskStatisticsDataCache)
-	//if err != nil {
-	//	fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
-	//}
-	//
-	//_, err = global.GVA_Timer.AddTaskByFunc("createFailLog", "2 0/5 6-23 * * * ", ServiceLogList.CreateFailLog)
-	//if err != nil {
-	//	fmt.Println("everyDayResetStatisticsCache timer error:", err)
-	//}
+	_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 0/8 6-23 * * * ", serviceStatisticsLog.TodayCreateStatisticsGameInfoLog)
+	if err != nil {
+		fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
+	}
+
+	_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 0/3 6-23 * * * ", serviceStatisticsLog.TaskStatisticsDataCache)
+	if err != nil {
+		fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
+	}
+
+	_, err = global.GVA_Timer.AddTaskByFunc("createFailLog", "2 0/5 6-23 * * * ", ServiceLogList.CreateFailLog)
+	if err != nil {
+		fmt.Println("everyDayResetStatisticsCache timer error:", err)
+	}
+	_, err = global.GVA_Timer.AddTaskByFunc("RegularCheckPc", "2 0/57 8-23 * * * ", serviceStatisticsLog.RegularCheckPc)
+	if err != nil {
+		fmt.Println("RegularCheckPc timer error:", err)
+	}
 }

+ 13 - 1
model/log/request/log_statistics.go

@@ -1,6 +1,8 @@
 package request
 
-import "log-server/model/log"
+import (
+	"log-server/model/log"
+)
 
 type GetStatisticsLogRequest struct {
 	log.StatisticsLog
@@ -55,3 +57,13 @@ type TaskData struct {
 	RetainedAccountNum   int `json:"retained_account_num"`
 	FeeAccountNum        int `json:"fee_account_num"`
 }
+
+type ExcelInfo struct {
+	FileName string `json:"fileName"` // 文件名
+	InfoList struct {
+		log.LogComputer
+		PageInfo
+		OrderKey string `json:"orderKey"` // 排序
+		Desc     bool   `json:"desc"`     // 排序方式:升序false(默认)|降序true
+	} `json:"infoList"`
+}

+ 3 - 1
model/log/request/loging.go

@@ -1,6 +1,8 @@
 package request
 
-import "log-server/model/log"
+import (
+	"log-server/model/log"
+)
 
 type AddLogRequest struct {
 	SimulatorIp      string `json:"simulator_ip"`

+ 1 - 1
model/log/response/loging.go

@@ -75,6 +75,6 @@ type ComputerUseLogReply struct {
 	PcCode           string `json:"pc_code"`
 	Operator         string `json:"operator"`
 	Status           int    `json:"status"`
-	TaskSuccessTotal int    `json:"task_success_total"`
+	PullAccountTotal int    `json:"pull_account_total"`
 	EnterMainTotal   int    `json:"enter_main_total"`
 }

+ 1 - 0
router/log/loging.go

@@ -23,5 +23,6 @@ func (e *LogingRouter) InitLogingRouter(Router *gin.RouterGroup) {
 		excelRouter.POST("getComputerNum", logApi.GetComputerNum)
 		excelRouter.POST("computerHeartbeat", logApi.ComputerHeartbeat)
 		excelRouter.POST("test", logApi.ComputerTest)
+		excelRouter.POST("exportExcel", logApi.ExportExcel)
 	}
 }

+ 0 - 39
service/log/computer.go

@@ -1,11 +1,9 @@
 package log
 
 import (
-	"context"
 	"errors"
 	"fmt"
 	"github.com/xuri/excelize/v2"
-	"go.uber.org/zap"
 	"gorm.io/gorm"
 	"log-server/global"
 	"log-server/model/log"
@@ -242,40 +240,3 @@ func (a *ServiceComputer) UpdatePcStatus(c log.Computer) (err error) {
 	err = global.GVA_DB.Table("computer").Where("id", c.Id).Update("status", c.Status).Error
 	return err
 }
-
-// 定时检查电脑情况
-func (s *ServiceComputer) RegularCheckPc() {
-	ctx := context.Background()
-	date := s.Logical.CurrentDate()
-	// 获取当天已上报的电脑数据
-	onlineComputerMpa, _ := s.Logical.GetOnlineComputerNumCache(ctx, date)
-	// 获取数据库的数据
-	var computer log.Computer
-	computers, err := computer.OnlinePcCodeCache()
-	if err != nil {
-		global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
-	}
-	// 查询两小时内上报的数据
-	var noReportingPc []string
-	for pc, _ := range onlineComputerMpa {
-		num := s.Logical.GetPcReportingLog(ctx, pc)
-		delete(computers, pc)
-		if num == 0 {
-			noReportingPc = append(noReportingPc, pc)
-		}
-	}
-	// 未上报逻辑
-	var errReportingPc []string
-	for pc, _ := range computers {
-		errReportingPc = append(errReportingPc, pc)
-	}
-	if len(errReportingPc) != 0 {
-		// 发信息数据群里
-		fmt.Println("异常上报数据: ", errReportingPc, len(errReportingPc))
-	}
-
-	if len(noReportingPc) != 0 {
-		// 发信息数据群里
-		fmt.Println("两小时内未检测到上报数据: ", noReportingPc, len(noReportingPc))
-	}
-}

+ 111 - 26
service/log/log_statistics.go

@@ -13,8 +13,8 @@ import (
 	"log-server/model/log"
 	"log-server/model/log/request"
 	"log-server/model/log/response"
-	"log-server/model/system"
 	loging2 "log-server/service/log/loging"
+	"log-server/utils"
 	"math"
 	"strconv"
 	"time"
@@ -591,6 +591,12 @@ func (s *ServiceStatisticsLog) CreateComputerStatisticsData() {
 			csReply.CreateDate = time.Now().Format("2006-01-02")
 			csReply.Status = 1
 			if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
+				if !errors.Is(global.GVA_DB.Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Where("status = ?", -1).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
+					err = global.GVA_DB.Table("log_computer").Where("create_date = ?", csReply.CreateDate).Where("pc_code = ?", csReply.PcCode).Update("status", 1).Error
+					if err != nil {
+						global.GVA_LOG.Error("更新数据失败onlineComputerMpa", zap.Error(err))
+					}
+				}
 				continue
 			}
 			onlineComputer = append(onlineComputer, csReply)
@@ -719,13 +725,14 @@ func (s *ServiceStatisticsLog) ComputerStatistics(ctx context.Context, api log.L
 }
 
 // 在线电脑
-func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
+func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api log.LogComputer, info request.PageInfo, order string, desc bool) ([]*response.ComputerUseLogReply, int64, error) {
 	date := api.CreateDate
 	if date == "" {
 		date = s.CurrentDate()
 	}
 	db := global.GVA_DB.Model(&log.LogComputer{})
 	var total int64
+	db = db.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date")
 	db = db.Where("create_date = ?", date)
 	if api.Operator != "" {
 		db = db.Where("operator = ?", api.Operator)
@@ -743,7 +750,7 @@ func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api
 	}
 	limit := info.PageSize
 	offset := info.PageSize * (info.Page - 1)
-	var statisticsLogs []*log.LogComputer
+	var statisticsLogs []*response.ComputerUseLogReply
 	db = db.Limit(limit).Offset(offset)
 	if order != "" {
 		var OrderStr string
@@ -769,17 +776,12 @@ func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api
 	if err != nil {
 		return nil, 0, err
 	}
-	var statisticsLogsComputer []*response.ComputerStatisticsReply1
 
 	for _, statisticsLog := range statisticsLogs {
-		statisticsLogComputer := new(response.ComputerStatisticsReply1)
-		statisticsLogComputer.PcCode = statisticsLog.PcCode
-		statisticsLogComputer.Operator = statisticsLog.Operator
-		statisticsLogComputer.CreateDate = statisticsLog.CreateDate[:10]
-		statisticsLogComputer.Status = statisticsLog.Status
-		statisticsLogsComputer = append(statisticsLogsComputer, statisticsLogComputer)
+		statisticsLog.CreateDate = statisticsLog.CreateDate[:10]
+
 	}
-	return statisticsLogsComputer, total, err
+	return statisticsLogs, total, err
 }
 
 //根据游戏id查询数据
@@ -965,7 +967,7 @@ func (s *ServiceStatisticsLog) ComputerTest(c context.Context) (interface{}, err
 }
 
 // 定时检查电脑情况
-func (s *ServiceStatisticsLog) RegularCheckPc() {
+func (s *ServiceStatisticsLog) RegularCheckPc1() {
 	ctx := context.Background()
 	date := s.LogicalLog.CurrentDate()
 	// 获取当天已上报的电脑数据
@@ -987,33 +989,116 @@ func (s *ServiceStatisticsLog) RegularCheckPc() {
 	}
 	// 未上报逻辑
 	var errReportingPc []string
-	for pc, _ := range computers {
+	for pc, name := range computers {
+		if name == "备用" {
+			continue
+		}
 		errReportingPc = append(errReportingPc, pc)
 	}
+	var content string
 	if len(errReportingPc) != 0 {
 		// 发信息数据群里
-		fmt.Println("异常上报数据: ", errReportingPc, len(errReportingPc))
+		content += fmt.Sprintf("异常租机数据<font color=\"warning\">%d台</font>: %s", len(errReportingPc), errReportingPc)
 	}
-
 	if len(noReportingPc) != 0 {
 		// 发信息数据群里
-		fmt.Println("两小时内未检测到上报数据: ", noReportingPc, len(noReportingPc))
+		content += "\n"
+		content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>: %s", len(noReportingPc), noReportingPc)
+	}
+	if content != "" {
+		var sendMsg SendMsg
+		sendMsg.MsgType = "markdown"
+		sendMsg.Markdown.Content = content
+		//c ,_ := json.Marshal(sendMsg)
+		url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
+		//url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
+		_, err = s.SendMsgData(url, sendMsg)
 	}
 }
 
-func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []system.SysBaseMenu, filePath string) error {
+// 定时检查电脑情况
+func (s *ServiceStatisticsLog) RegularCheckPc() {
+	ctx := context.Background()
+	var computer log.Computer
+	computers, err := computer.OnlinePcCodeCache()
+	//computersF, err := computer.OnlinePcCodeCache()
+	if err != nil {
+		global.GVA_LOG.Error("获取租机表数据失败", zap.Error(err))
+	}
+	// 查询两小时内上报的数据
+	var computersF = map[string]string{}
+	var noReportingPc []string
+	for pc, name := range computers {
+		computersF[pc] = name
+		if name == "备用" {
+			delete(computers, pc)
+			continue
+		}
+		num := s.LogicalLog.GetPcReportingLog(ctx, pc)
+		delete(computers, pc)
+		if num == 0 {
+			noReportingPc = append(noReportingPc, pc)
+		}
+	}
+
+	var content string
+
+	if len(noReportingPc) != 0 {
+		content += fmt.Sprintf("两小时内未检测到中控数据<font color=\"warning\">%d台</font>:", len(noReportingPc))
+		for _, pc := range noReportingPc {
+			content += "\n"
+			name := computersF[pc]
+			content += name + " : " + pc
+		}
+	}
+	if content != "" {
+		var sendMsg SendMsg
+		sendMsg.MsgType = "markdown"
+		sendMsg.Markdown.Content = content
+		//c ,_ := json.Marshal(sendMsg)
+		url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
+		//url := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
+		_, err = s.SendMsgData(url, sendMsg)
+	}
+}
+
+type SendMsg struct {
+	MsgType  string `json:"msgtype"`
+	Markdown struct {
+		Content string `json:"content"`
+	} `json:"markdown"`
+}
+
+func (s *ServiceStatisticsLog) SendMsgData(url string, params interface{}) (result []byte, err error) {
+	result, err = utils.HttpPost(url, params)
+	return
+}
+
+var statusMps = map[int]string{
+	-1: "未上报",
+	1:  "中控上报",
+	2:  "任务上报",
+}
+
+func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
 	excel := excelize.NewFile()
-	excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "路由Name", "路由Path", "是否隐藏", "父节点", "排序", "文件名称"})
-	for i, menu := range infoList {
+	excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态"})
+	for i, statisticsLog := range infoList {
 		axis := fmt.Sprintf("A%d", i+2)
+		var r interface{}
+		if statisticsLog.EnterMainTotal != 0 && statisticsLog.PullAccountTotal != 0 {
+			r = fmt.Sprintf("%.2f", float64(statisticsLog.EnterMainTotal)/float64(statisticsLog.PullAccountTotal)*100)
+		} else {
+			r = "0.00"
+		}
 		excel.SetSheetRow("Sheet1", axis, &[]interface{}{
-			menu.ID,
-			menu.Name,
-			menu.Path,
-			menu.Hidden,
-			menu.ParentId,
-			menu.Sort,
-			menu.Component,
+			statisticsLog.PcCode,
+			statisticsLog.Operator,
+			statisticsLog.CreateDate[:10],
+			statisticsLog.PullAccountTotal,
+			statisticsLog.EnterMainTotal,
+			r,
+			statusMps[statisticsLog.Status],
 		})
 	}
 	err := excel.SaveAs(filePath)

+ 8 - 5
service/log/loging/logical_log.go

@@ -64,17 +64,20 @@ func (s *LogicalLog) DataAdd() (err error) {
 	/*if s.Status == 0 {
 		_ = s.errLogAdd(logInfo)
 	}*/
-	err = s.SetGameCache(context.Background(), s.CurrentDate(), logInfo.GameId)
+	ctx := context.Background()
+	err = s.SetGameCache(ctx, s.CurrentDate(), logInfo.GameId)
 	if err != nil {
 		return
 	}
-	err = s.SetAccountGameIdCache(context.Background(), s.CurrentDate(), logInfo)
+	err = s.SetAccountGameIdCache(ctx, s.CurrentDate(), logInfo)
 	if err != nil {
 		return
 	}
 	if logInfo.PcCode != "" {
-		err = s.SetComputerCache(context.Background(), s.CurrentDate(), logInfo.PcCode, logInfo.Operator)
-		_ = s.StatisticsComputerRateData(context.Background(), logInfo.PcCode, logInfo.GameId)
+		err = s.SetComputerCache(ctx, s.CurrentDate(), logInfo.PcCode, logInfo.Operator)
+		_ = s.StatisticsComputerRateData(ctx, logInfo.PcCode, logInfo.GameId)
+		_ = s.SetOnlineComputerNumCache(ctx, s.CurrentDate(), logInfo.PcCode, logInfo.Operator)
+		s.SetPcReportingLog(ctx, logInfo.PcCode, logInfo.Operator)
 	}
 	return
 }
@@ -725,7 +728,7 @@ func (s *LogicalLog) GetPcReportingLog(ctx context.Context, pcCode string) (num
 	if len(z) == 0 {
 		return
 	}
-	end := time.Now().UnixNano() / 1e6
+	end := time.Now().UnixNano()/1e6 + 60*1000*2
 	start := end - (120*60*1000 + 60*1000)
 	op := redis.ZRangeBy{
 		Min: strconv.Itoa(int(start)),

+ 24 - 0
utils/requst_http.go

@@ -1,9 +1,11 @@
 package utils
 
 import (
+	"encoding/json"
 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"strings"
 )
 
 func HttpGet(url string, params map[string]string) (result []byte, err error) {
@@ -36,3 +38,25 @@ func HttpGet(url string, params map[string]string) (result []byte, err error) {
 	}()
 	return
 }
+
+func HttpPost(url string, params interface{}) (result []byte, err error) {
+	data, _ := json.Marshal(params)
+	client := &http.Client{}
+	req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
+	if err != nil {
+		return
+	}
+	fmt.Println(url)
+	req.Header.Set("Content-Type", "application/json;charset=UTF-8")
+	resp, err := client.Do(req)
+	if err != nil {
+		return
+	}
+	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return
+	}
+	result = body
+	return
+}