package controllers

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

type WhitelistController struct {
	MainController
}

func (server *WhitelistController) Lists() {
	info, err := server.Check()
	if err != nil {
		server.respond(http.StatusUnauthorized, err.Error())
		return
	}
	if !(info.Role == 1 || info.Role == 2) {
		server.respond(models.BusinessFailed, "只有系统管理员或超级管理员才能执行此操作")
		return
	}
	body := server.Ctx.Input.RequestBody
	appRequest := models.AppRequest{}
	err = json.Unmarshal(body, &appRequest) //解析body中数据
	logs.Debug("appRequest", appRequest, string(body))

	if appRequest.Page == 0 {
		appRequest.Page = 1
	}
	if appRequest.Size == 0 {
		appRequest.Size = 10
	}
	offset := (appRequest.Page - 1) * appRequest.Size

	qs := mysql.GetMysqlInstace().Ormer.QueryTable("whitelist").Filter("deleted", 0)
	infoQs := qs.Offset(offset).Limit(appRequest.Size)
	count, err := infoQs.Count()
	logs.Debug("lists = ", count)
	var lists []*models.Whitelist
	if count > 0 {
		infoQs.All(&lists)
	}
	total, err := qs.Count()
	responseData := struct {
		Total int64       `json:"total"`
		Data  interface{} `json:"data,omitempty"`
	}{
		Total: total,
		Data:  lists,
	}
	server.respond(http.StatusOK, "", responseData)
}

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

	if !(info.Role == 1 || info.Role == 2) {
		server.respond(models.BusinessFailed, "只有系统管理员或超级管理员才能执行此操作")
		return
	}
	body := server.Ctx.Input.RequestBody
	logs.Debug("AddLevel body", string(body))
	request := models.Whitelist{}
	err = json.Unmarshal(body, &request) //解析body中数据
	logs.Debug("request", request)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}

	if request.ProfitAddress == "" {
		server.respond(models.MissingParameter, "收益地址不能为空")
		return
	}
	if request.NodeNum == 0 {
		server.respond(models.MissingParameter, "节点个数最少为1")
		return
	}

	timestamp := time.Now()

	request.CreatedTime = timestamp
	request.UpdatedTime = timestamp
	request.Deleted = 0

	_, err = mysql.GetMysqlInstace().Ormer.Insert(&request)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}

	server.respond(http.StatusOK, "添加成功")
}

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

	if !(info.Role == 1 || info.Role == 2) {
		server.respond(models.BusinessFailed, "只有系统管理员或超级管理员才能执行此操作")
		return
	}
	body := server.Ctx.Input.RequestBody
	logs.Debug("AddLevel body", string(body))
	request := models.Whitelist{}
	err = json.Unmarshal(body, &request) //解析body中数据
	logs.Debug("request", request)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}

	if request.Id == 0 {
		server.respond(models.MissingParameter, "id 不能为空")
		return
	}

	if request.ProfitAddress == "" {
		server.respond(models.MissingParameter, "收益地址不能为空")
		return
	}
	if request.NodeNum == 0 {
		server.respond(models.MissingParameter, "节点个数最少为1")
		return
	}
	check := models.Whitelist{Id: request.Id}
	err = mysql.GetMysqlInstace().Ormer.Read(&check)
	if err != nil {
		server.respond(models.BusinessFailed, "该白名单数据不存在")
		return
	}

	timestamp := time.Now()

	request.CreatedTime = check.CreatedTime
	request.UpdatedTime = timestamp

	_, err = mysql.GetMysqlInstace().Ormer.Update(&request)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}

	whitelist := model.Whitelist{
		ID:            int64(request.Id),
		ProfitAddress: request.ProfitAddress,
		NodeNum:       int64(request.NodeNum),
		CreatedTime:   request.CreatedTime,
		UpdatedTime:   request.UpdatedTime,
		Deleted:       int64(request.Deleted),
	}

	odysseus.SetWhiteListToRedis(&whitelist)

	server.respond(http.StatusOK, "更新成功")
}

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

	if !(info.Role == 1 || info.Role == 2) {
		server.respond(models.BusinessFailed, "只有系统管理员或超级管理员才能执行此操作")
		return
	}
	body := server.Ctx.Input.RequestBody
	logs.Debug("AddLevel body", string(body))
	request := models.Whitelist{}
	err = json.Unmarshal(body, &request) //解析body中数据
	logs.Debug("request", request)
	if err != nil {
		server.respond(models.NoRequestBody, err.Error())
		return
	}

	if request.Id == 0 {
		server.respond(models.MissingParameter, "id 不能为空")
		return
	}
	check := models.Whitelist{Id: request.Id}
	err = mysql.GetMysqlInstace().Ormer.Read(&check)
	if err != nil {
		server.respond(models.BusinessFailed, "该白名单数据不存在")
		return
	}

	timestamp := time.Now()

	request.CreatedTime = check.CreatedTime
	request.UpdatedTime = timestamp
	request.Deleted = 1

	_, err = mysql.GetMysqlInstace().Ormer.Update(&request)
	if err != nil {
		server.respond(models.BusinessFailed, err.Error())
		return
	}

	whitelist := model.Whitelist{
		ID:            int64(request.Id),
		ProfitAddress: request.ProfitAddress,
		NodeNum:       int64(request.NodeNum),
		CreatedTime:   request.CreatedTime,
		UpdatedTime:   request.UpdatedTime,
		Deleted:       int64(request.Deleted),
	}

	odysseus.DeleteWhiteList(&whitelist)

	server.respond(http.StatusOK, "删除成功")
}
