package loging

import (
	"context"
	"log-server/model/log/request"
	"strconv"
)

// 进入主线日志
type EnterMainLog struct {
	logical LogicalLog
}

func (s *EnterMainLog) SuccessLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 1
	s.logical.ScriptType = request.ScriptType
	s.logical.Request = request
	err = s.logical.DataAdd()
	if err != nil {
		return
	}
	if s.logical.ScriptType == 1 {
		return
	}
	b, _ := s.logical.ExistsUuidCodeCache(ctx, s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	}

	code := strconv.Itoa(request.Coding)
	err = s.logical.PartTypeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, OkStatus, s.logical.Request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.NodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, OkStatus, s.logical.Request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.SetComputerTaskSuccessNumCache(ctx, s.logical.CurrentDate(), s.logical.Request.PcCode, s.logical.Request.GameId)
	return
}

func (s *EnterMainLog) FailLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 0
	s.logical.ScriptType = request.ScriptType
	s.logical.Request = request
	code := strconv.Itoa(request.Coding)
	if code[5:] == "00" {
		s.logical.Status = 3
	}
	err = s.logical.DataAdd()
	if err != nil {
		return
	}
	b, _ := s.logical.ExistsUuidCodeCache(ctx, s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	}
	// 中控数据不统计
	if s.logical.ScriptType == 1 {
		return
	}
	if code[5:] == "00" {
		err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, strconv.Itoa(request.Coding), NoLogStatus, s.logical.Request.TaskType)
		if code[3:] == "1000" {
			err = s.logical.SetComputerEnterMainNumCache(ctx, s.logical.CurrentDate(), s.logical.Request.PcCode, s.logical.Request.GameId)
		}
		return
	}
	err = s.logical.PartTypeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, FailStatus, s.logical.Request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.NodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, code, FailStatus, s.logical.Request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.DelAccountGameIdCache(ctx, s.logical.CurrentDate(), request.LogUuid)
	return
}

func (s *EnterMainLog) NoLogStatusData(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 2
	s.logical.Request = request
	err = s.logical.NoLogStatusDataAdd(ctx)
	b, _ := s.logical.ExistsUuidCodeCache(ctx, s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	if b {
		return
	} else {
		_ = s.logical.SetUuidCodeCache(context.Background(), s.logical.CurrentDate(), s.logical.Request.LogUuid, s.logical.Request.Coding, s.logical.Request.GameId)
	}
	if err != nil {
		return
	}
	err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, strconv.Itoa(request.Coding), NoLogStatus, s.logical.Request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.DelAccountGameIdCache(ctx, s.logical.CurrentDate(), request.LogUuid)
	return
}
