package loging

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

type TaskEndLog struct {
	logical LogicalLog
}

func (s *TaskEndLog) 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)
	err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), request.GameId, code, NoLogStatus, request.TaskType)
	if err != nil {
		return
	}
	err = s.logical.DelAccountGameIdCache(ctx, s.logical.CurrentDate(), request.LogUuid)
	return
}

func (s *TaskEndLog) 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)
	if code[5:] == "00" {
		return
	}
	err = s.logical.DelAccountGameIdCache(ctx, s.logical.CurrentDate(), request.LogUuid)
	if err != nil {
		return
	}
	err = s.logical.CodeLogSetNum(ctx, s.logical.CurrentDate(), request.GameId, strconv.Itoa(request.Coding), NoLogStatus, request.TaskType)
	return
}

func (s *TaskEndLog) 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(), request.GameId, strconv.Itoa(request.Coding), NoLogStatus, request.TaskType)
	return
}
