package loging

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

type PullAccountLog struct {
	logical LogicalLog
}

func (s *PullAccountLog) SuccessLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 1
	s.logical.Request = request
	err = s.logical.DataAdd()
	if err != nil {
		return
	}
	code := strconv.Itoa(request.Coding)
	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.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.SetComputerPullAccountNumCache(ctx, s.logical.CurrentDate(), s.logical.Request.PcCode, s.logical.Request.GameId)
	return
}

func (s *PullAccountLog) FailLog(ctx context.Context, request request.AddLogRequest) (err error) {
	s.logical.Status = 0
	s.logical.Request = request
	err = s.logical.DataAdd()
	if err != nil {
		return
	}
	code := strconv.Itoa(request.Coding)
	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.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.SetComputerPullAccountNumCache(ctx, s.logical.CurrentDate(), s.logical.Request.PcCode, s.logical.Request.GameId)
	return
}

func (s *PullAccountLog) 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
	}
	err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), s.logical.Request.GameId, strconv.Itoa(request.Coding), NoLogStatus, s.logical.Request.TaskType)
	return
}
