Kaynağa Gözat

电脑备注\自动删缓存

wangbin 3 yıl önce
ebeveyn
işleme
e16326d5d5

+ 29 - 1
api/v1/log/loging.go

@@ -6,6 +6,7 @@ import (
 	"go.uber.org/zap"
 	"log-server/global"
 	"log-server/model/common/response"
+	log2 "log-server/model/log"
 	"log-server/model/log/request"
 	"log-server/service/log"
 	loging2 "log-server/service/log/loging"
@@ -344,7 +345,7 @@ func (s *ApiLoging) ComputerTest(c *gin.Context) {
 	response.OkWithDetailed("b", "上报成功", c)
 }
 
-// @Tags excel
+// @Tags loging
 // @Summary 导出Excel
 // @Security ApiKeyAuth
 // @accept application/json
@@ -374,3 +375,30 @@ func (e *ApiLoging) ExportExcel(c *gin.Context) {
 	c.Writer.Header().Add("success", "true")
 	c.File(filePath)
 }
+
+// @Tags loging
+// @Summary 修改租机使用备注信息
+// @Security ApiKeyAuth
+// @accept application/json
+// @Produce application/json
+// @Param data body log2.ComputerUseRemarks true "id, 租机编号, 使用者, 租机供应商"
+// @Success 200 {object} response.Response{msg=string} "修改租机使用备注信息"
+// @Router /computer/updatePcRemarks [post]
+func (s *ApiLoging) UpdatePcRemarks(c *gin.Context) {
+	var paramsInfo log2.ComputerUseRemarks
+	_ = c.ShouldBindJSON(&paramsInfo)
+	if err := utils.Verify(paramsInfo, utils.PcVerify); err != nil {
+		response.FailWithMessage(err.Error(), c)
+		return
+	}
+	if paramsInfo.PcCode == "" || paramsInfo.CreateDate == "" {
+		response.FailWithMessage("有必要的参数未添加", c)
+		return
+	}
+	if err := ServiceStatisticsLog.UpdateComputerUseLog(paramsInfo); err != nil {
+		global.GVA_LOG.Error("修改失败!", zap.Error(err))
+		response.FailWithMessage(err.Error(), c)
+	} else {
+		response.OkWithMessage("修改成功", c)
+	}
+}

+ 1 - 1
config.yaml

@@ -118,7 +118,7 @@ qiniu:
   secret-key: ""
   use-cdn-domains: false
 redis:
-  db: 8
+  db: 7
   addr: 120.92.140.137:6379
   password: "rootredis!fu>6Wy2"
 system:

+ 36 - 24
initialize/timer.go

@@ -33,32 +33,44 @@ func Timer() {
 	//if err != nil {
 	//	fmt.Println("everyDayResetStatisticsCache timer error:", err)
 	//}
+	// 定时更新前一天的统计数据
 	_, err := global.GVA_Timer.AddTaskByFunc("StatisticsLog", "2 8 5 * * * ", serviceStatisticsLog.CreateStatisticsLog)
 	if err != nil {
 		fmt.Println("add taskCode timer error:", err)
 	}
-
-	_, err = global.GVA_Timer.AddTaskByFunc("CreateComputerStatisticsData", "40 0/9 7-23 * * * ", serviceStatisticsLog.CreateComputerStatisticsData)
-	if err != nil {
-		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("RegularCheckPc", "2 0/57 8-23 * * * ", serviceStatisticsLog.RegularCheckPc)
-	if err != nil {
-		fmt.Println("RegularCheckPc timer error:", err)
-	}
+	//// 定时同步电脑相关的统计数据
+	//_, err = global.GVA_Timer.AddTaskByFunc("CreateComputerStatisticsData", "40 0/9 7-23 * * * ", serviceStatisticsLog.CreateComputerStatisticsData)
+	//if err != nil {
+	//	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("TaskStatisticsDataCache", "2 0/3 6-23 * * * ", serviceStatisticsLog.TaskStatisticsDataCache)
+	//if err != nil {
+	//	fmt.Println("add TaskStatisticsDataCache 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)
+	//}
+	//// 新建日志表
+	//_, err = global.GVA_Timer.AddTaskByFunc("RegularCreateLogingTable", "2 1 19 20,29 * * ", ServiceLogList.RegularCreateLogingTable)
+	//if err != nil {
+	//	fmt.Println("add RegularCreateLogingTable timer error:", err)
+	//}
+	//// 定时删缓存
+	//_, err = global.GVA_Timer.AddTaskByFunc("RegularDelCheckData", "2 33 11 * * *", serviceStatisticsLog.RegularDelCheckData)
+	//if err != nil {
+	//	fmt.Println("add RegularDelCheckData timer error:", err)
+	//}
 }

+ 7 - 0
model/log/computer_use_remacks.go

@@ -0,0 +1,7 @@
+package log
+
+type ComputerUseRemarks struct {
+	CreateDate string `json:"create_date"`
+	Remarks    string `json:"remarks"`
+	PcCode     string `json:"pc_code"`
+}

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

@@ -77,4 +77,5 @@ type ComputerUseLogReply struct {
 	Status           int    `json:"status"`
 	PullAccountTotal int    `json:"pull_account_total"`
 	EnterMainTotal   int    `json:"enter_main_total"`
+	Remarks          string `json:"remarks"`
 }

+ 1 - 0
router/log/loging.go

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

+ 22 - 6
service/log/log_list.go

@@ -11,6 +11,7 @@ import (
 	"log-server/model/log/response"
 	loging2 "log-server/service/log/loging"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -22,8 +23,10 @@ type ServiceLogList struct {
 func (s *ServiceLogList) GetCodeLogList(ctx context.Context, api log.Loging, info request.PageInfo, order string, desc bool, code int) (list interface{}, total int64, err error) {
 	limit := info.PageSize
 	offset := info.PageSize * (info.Page - 1)
-	db := global.GVA_DB.Model(&log.Loging{})
 
+	if api.GameId == 0 {
+		return
+	}
 	var apiList []log.Loging
 	var createDate string
 	if api.CreateDate == "" {
@@ -31,15 +34,19 @@ func (s *ServiceLogList) GetCodeLogList(ctx context.Context, api log.Loging, inf
 	} else {
 		createDate = api.CreateDate
 	}
+	splitTime := strings.Split(createDate, "-")
+	date := splitTime[0] + "-" + splitTime[1]
+	db := global.GVA_DB.Model(&log.Loging{})
+	if date != "2022-11" {
+		table := "loging_" + splitTime[0] + "-" + splitTime[1]
+		fmt.Println(table)
+		db = global.GVA_DB.Table(table)
+	}
 	db = db.Where("create_date = ?", createDate)
+	db = db.Where("game_id = ?", api.GameId)
 	if api.Coding != 0 {
 		db = db.Where("coding = ?", api.Coding)
 	}
-	if api.GameId != 0 {
-		db = db.Where("game_id = ?", api.GameId)
-	} else {
-		return
-	}
 	if api.PcCode != "" {
 		db = db.Where("pc_code = ?", api.PcCode)
 	}
@@ -221,3 +228,12 @@ func (s *ServiceLogList) CreateFailLog() {
 		global.GVA_LOG.Info("重试!: " + data.LogUuid + " code: " + strconv.Itoa(data.Coding))
 	}
 }
+
+func (s *ServiceLogList) RegularCreateLogingTable() {
+	var loging log.Loging
+	err := loging.CreateLogingTable()
+	if err != nil {
+		global.GVA_LOG.Error("RegularCreateLogingTable 建表失败!", zap.Error(err))
+	}
+	global.GVA_LOG.Info("建表成功")
+}

+ 27 - 2
service/log/log_statistics.go

@@ -732,7 +732,7 @@ func (s *ServiceStatisticsLog) OnlineComputerStatistics(ctx context.Context, api
 	}
 	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.Select("SUM(pull_account_num) pull_account_total,SUM(enter_main) enter_main_total,operator,pc_code,status,create_date,remarks")
 	db = db.Where("create_date = ?", date)
 	if api.Operator != "" {
 		db = db.Where("operator = ?", api.Operator)
@@ -1082,7 +1082,7 @@ var statusMps = map[int]string{
 
 func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.ComputerUseLogReply, filePath string) error {
 	excel := excelize.NewFile()
-	excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态"})
+	excel.SetSheetRow("Sheet1", "A1", &[]string{"电脑编号", "使用者", "日期", "拉取账号", "进入主线", "主线成功率", "使用状态", "备注"})
 	for i, statisticsLog := range infoList {
 		axis := fmt.Sprintf("A%d", i+2)
 		var r interface{}
@@ -1099,8 +1099,33 @@ func (exa *ServiceStatisticsLog) ParseInfoList2Excel(infoList []*response.Comput
 			statisticsLog.EnterMainTotal,
 			r,
 			statusMps[statisticsLog.Status],
+			statisticsLog.Remarks,
 		})
 	}
 	err := excel.SaveAs(filePath)
 	return err
 }
+
+// 删除两天前的缓存数据
+func (s *ServiceStatisticsLog) RegularDelCheckData() {
+	ctx := context.Background()
+	date := time.Now().Add(-time.Hour * 48).Format("2006-01-02")
+	s.LogicalLog.DelHashUuidKey(ctx, date)
+	s.LogicalLog.DelZSetKey(ctx, date)
+	s.LogicalLog.DelHashKey(ctx, date)
+}
+
+//@author: [piexlmax](https://github.com/piexlmax)
+//@function: UpdatePc
+//@description: 根据id更新pc
+//@param: Computer log.Computer
+//@return: err error
+func (a *ServiceStatisticsLog) UpdateComputerUseLog(c log.ComputerUseRemarks) (err error) {
+	if !errors.Is(global.GVA_DB.Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).First(&log.LogComputer{}).Error, gorm.ErrRecordNotFound) {
+		var updateInfo = make(map[string]interface{})
+		updateInfo["remarks"] = c.Remarks
+		updateInfo["remarks_update_time"] = time.Now().Format("2006-01-02 15:04:05")
+		err = global.GVA_DB.Table("log_computer").Where("create_date = ?", c.CreateDate).Where("pc_code = ?", c.PcCode).Updates(updateInfo).Error
+	}
+	return
+}

+ 62 - 2
service/log/loging/logical_log.go

@@ -92,7 +92,8 @@ func (s *LogicalLog) NoLogStatusDataAdd(ctx context.Context) (err error) {
 }
 
 func (s *LogicalLog) getLogByUuid(uuid string) (log log.Loging, err error) {
-	db := global.GVA_DB.Table("loging")
+	createDate := time.Now().Format("2006-01")
+	db := global.GVA_DB.Table("loging_" + createDate)
 	db = db.Where("log_uuid = ?", uuid)
 	db = db.Where("coding = 4101099 or coding = 4103099")
 	db.First(&log)
@@ -100,7 +101,8 @@ func (s *LogicalLog) getLogByUuid(uuid string) (log log.Loging, err error) {
 }
 
 func (s *LogicalLog) GetLogByUuidCoding(uuid string, coding int, scriptType int) (log log.Loging, err error) {
-	db := global.GVA_DB.Table("loging")
+	createDate := time.Now().Format("2006-01")
+	db := global.GVA_DB.Table("loging_" + createDate)
 	db = db.Where("log_uuid = ?", uuid)
 	db = db.Where("coding = ?", coding)
 	db = db.Where("script_type = ?", scriptType)
@@ -173,6 +175,13 @@ func (s *LogicalLog) codeData() (nodeCode, typeCode int, err error) {
 }
 
 func (s *LogicalLog) LogAdd(l *log.Loging) error {
+	createDate := l.CreateDate
+	splitTime := strings.Split(createDate, "-")
+	date := splitTime[0] + "-" + splitTime[1]
+	if date != "2022-11" {
+		table := "loging_" + splitTime[0] + "-" + splitTime[1]
+		return global.GVA_DB.Table(table).Create(&l).Error
+	}
 	return global.GVA_DB.Create(&l).Error
 }
 
@@ -751,3 +760,54 @@ func (s *LogicalLog) SetPcReportingLog(ctx context.Context, pcCode string, opera
 	}
 	global.GVA_REDIS.ZAdd(ctx, key, z)
 }
+
+// 删除hash缓存数据
+func (s *LogicalLog) DelHashKey(ctx context.Context, date string) {
+	gameKeys := fmt.Sprintf(GameCacheKey, date)
+	data, _ := global.GVA_REDIS.HKeys(ctx, gameKeys).Result()
+	global.GVA_REDIS.HDel(ctx, gameKeys, data...)
+	logUuidAccountGameId := fmt.Sprintf(logUuidAccountGameId, date)
+	logUuid, _ := global.GVA_REDIS.HKeys(ctx, logUuidAccountGameId).Result()
+	global.GVA_REDIS.HDel(ctx, logUuidAccountGameId, logUuid...)
+	taskStatisticsKey := fmt.Sprintf("%s:taskStatistics", date)
+	taskStatistics, _ := global.GVA_REDIS.HKeys(ctx, taskStatisticsKey).Result()
+	global.GVA_REDIS.HDel(ctx, taskStatisticsKey, taskStatistics...)
+	computerListKey := fmt.Sprintf(ComputerCacheKey, date)
+	computerListKeys, _ := global.GVA_REDIS.HKeys(ctx, computerListKey).Result()
+	global.GVA_REDIS.HDel(ctx, computerListKey, computerListKeys...)
+}
+
+// 删除ZSet缓存数据
+func (s *LogicalLog) DelZSetKey(ctx context.Context, date string) {
+	gamePcKey := fmt.Sprintf("%s:gamePc:gameFeeRate*", date)
+	gameKey := fmt.Sprintf("%s:game:gameFeeRate*", date)
+	pcReportingKey := fmt.Sprintf("%s:pc:Reporting:*", date)
+	beginTime, _ := time.ParseInLocation("2006-01-02", date, time.Local)
+	beginTimeI := strconv.Itoa(int(beginTime.Unix() * 1000))
+	endTime := strconv.Itoa(int(beginTime.Unix()*1000 + 24*60*60*1000))
+	gamePcKeys, _ := global.GVA_REDIS.Keys(ctx, gamePcKey).Result()
+	gameKeys, _ := global.GVA_REDIS.Keys(ctx, gameKey).Result()
+	pcReportingKeys, _ := global.GVA_REDIS.Keys(ctx, pcReportingKey).Result()
+	for _, k := range gamePcKeys {
+		global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime)
+	}
+	for _, k := range gameKeys {
+		global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime)
+	}
+	for _, k := range pcReportingKeys {
+		global.GVA_REDIS.ZRemRangeByScore(ctx, k, beginTimeI, endTime)
+	}
+}
+
+// 删除hashUuid缓存数据
+func (s *LogicalLog) DelHashUuidKey(ctx context.Context, date string) {
+	gameIds, _ := s.GetGameCache(ctx, date)
+	for id, _ := range gameIds {
+		gamekeys := fmt.Sprintf("%s:logUuid:%s:*", date, id)
+		data, _ := global.GVA_REDIS.Keys(ctx, gamekeys).Result()
+		for _, hkey := range data {
+			value, _ := global.GVA_REDIS.HKeys(ctx, hkey).Result()
+			global.GVA_REDIS.HDel(ctx, hkey, value...)
+		}
+	}
+}