package controllers

import (
	"ai_developer_admin/libs/mysql"
	"ai_developer_admin/models"
	"encoding/json"
	"github.com/beego/beego/v2/core/logs"
	"net/http"
	"time"
)

type VersionController struct {
	MainController
}

func (server *VersionController) Add() {
	_, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}

	body := server.Ctx.Input.RequestBody
	appRequest := models.TaskTypeVersion{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}
	if appRequest.TaskTypeId == 0 {
		server.respond(models.MissingParameter, "Missing task_type_id parameter")
		return
	}

	timestamp := time.Now()
	appRequest.CreatedTime = timestamp
	appRequest.UpdatedTime = timestamp
	appRequest.Deleted = 0

	_, err = mysql.GetMysqlInstace().Ormer.Insert(&appRequest)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}
	server.respond(http.StatusOK, "success")
}

func (server *VersionController) Update() {
	_, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}

	body := server.Ctx.Input.RequestBody
	appRequest := models.TaskTypeVersion{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}
	if appRequest.Id == 0 {
		server.respond(models.MissingParameter, "Missing id parameter")
		return
	}
	//if appRequest.TaskTypeId == 0 {
	//	server.respond(models.MissingParameter, "Missing task_type_id parameter")
	//	return
	//}

	checkVersion := &models.TaskTypeVersion{Id: appRequest.Id}
	err = mysql.GetMysqlInstace().Ormer.Read(checkVersion)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}

	timestamp := time.Now()
	appRequest.CreatedTime = checkVersion.CreatedTime
	appRequest.UpdatedTime = timestamp

	_, err = mysql.GetMysqlInstace().Ormer.Update(&appRequest)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}
	server.respond(http.StatusOK, "success")
}

func (server *VersionController) Delete() {
	_, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}

	body := server.Ctx.Input.RequestBody
	appRequest := models.TaskTypeVersion{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}
	if appRequest.Id == 0 {
		server.respond(models.MissingParameter, "Missing id parameter")
		return
	}
	//if appRequest.TaskTypeId == 0 {
	//	server.respond(models.MissingParameter, "Missing task_type_id parameter")
	//	return
	//}

	checkVersion := &models.TaskTypeVersion{Id: appRequest.Id}
	err = mysql.GetMysqlInstace().Ormer.Read(checkVersion)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}

	timestamp := time.Now()
	checkVersion.UpdatedTime = timestamp
	checkVersion.Deleted = 1

	_, err = mysql.GetMysqlInstace().Ormer.Update(&appRequest)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}
	server.respond(http.StatusOK, "success")
}

func (server *VersionController) Lists() {
	body := server.Ctx.Input.RequestBody
	appRequest := models.TaskTypeVersion{}
	err := json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}

	if appRequest.TaskTypeId == 0 {
		server.respond(models.MissingParameter, "Missing task_type_id parameter")
		return
	}

	qs := mysql.GetMysqlInstace().Ormer.QueryTable("task_type_version").
		Filter("deleted", 0).Filter("task_type_id", appRequest.TaskTypeId).GroupBy("version")
	count, err := qs.Count()
	var lists []*models.TaskTypeVersion
	if count > 0 {
		qs.All(&lists)
	}
	server.respond(http.StatusOK, "", lists)
}
