package loging

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

type FeeLog struct {
	logical LogicalLog
}

func (s *FeeLog) 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)
	s.logical.StatisticsPcFeeRateData(ctx, s.logical.Request.PcCode, s.logical.Request.LogUuid, s.logical.Request.GameId)
	s.logical.StatisticsFeeRateData(ctx, s.logical.Request.GameId, s.logical.Request.LogUuid)
	return
}

func (s *FeeLog) 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
	}
	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)
	}
	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)
		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)
	return
}

func (s *FeeLog) NoLogStatusData(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 2
	s.logical.Request = request
	err = s.logical.NoLogStatusDataAdd(ctx)
	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)
	}
	err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, strconv.Itoa(request.Coding), NoLogStatus, s.logical.Request.TaskType)
	return
}
