package dashboard

import (
	"Wine-Server/utils"
	"Wine-Server/utils/tables"
	"github.com/gorilla/websocket"
)

func rankAdmins(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankAdminsRes", utils.Fail("权限不足")))
		return
	}
	var param rankParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankAdminsRes", utils.Fail("参数错误")))
		return
	}
	rank, err := tables.TradeRankAdmins(param.Start, param.End)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankAdminsRes", utils.Fail("查询管理员排行失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardRankAdminsRes", utils.Success(rank)))
}

func rankDevices(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankDevicesRes", utils.Fail("权限不足")))
		return
	}
	var param rankParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankDevicesRes", utils.Fail("参数错误")))
		return
	}
	rank, err := tables.TradeRankDevices(param.Start, param.End)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankDevicesRes", utils.Fail("查询设备排行失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardRankDevicesRes", utils.Success(rank)))
}

func rankWines(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWinesRes", utils.Fail("权限不足")))
		return
	}
	var param rankParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWinesRes", utils.Fail("参数错误")))
		return
	}
	rank, err := tables.TradeRankWines(param.Start, param.End)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWinesRes", utils.Fail("查询酒品排行失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWinesRes", utils.Success(rank)))
}

func rankWorkers(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWorkersRes", utils.Fail("权限不足")))
		return
	}
	var param rankParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWorkersRes", utils.Fail("参数错误")))
		return
	}
	rank, err := tables.ChangeRankWorkers(param.Start, param.End)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWorkersRes", utils.Fail("查询上酒工排行失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardRankWorkersRes", utils.Success(rank)))
}

func queryAdmin(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryAdminRes", utils.Fail("权限不足")))
		return
	}
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryAdminRes", utils.Fail("参数错误")))
		return
	}
	list, err := tables.TradeQuerySpecificOne("manager", param.Id, param.Type)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryAdminRes", utils.Fail("查询管理员历史记录失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryAdminRes", utils.Success(list)))
}

func queryDevice(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryDeviceRes", utils.Fail("权限不足")))
		return
	}
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryDeviceRes", utils.Fail("参数错误")))
		return
	}
	list, err := tables.TradeQuerySpecificOne("device", param.Id, param.Type)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryDeviceRes", utils.Fail("查询设备历史记录失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryDeviceRes", utils.Success(list)))
}

func queryWine(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWineRes", utils.Fail("权限不足")))
		return
	}
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWineRes", utils.Fail("参数错误")))
		return
	}
	list, err := tables.TradeQuerySpecificOne("wine", param.Id, param.Type)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWineRes", utils.Fail("查询酒品历史记录失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWineRes", utils.Success(list)))
}

func queryWorker(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWorkerRes", utils.Fail("权限不足")))
		return
	}
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWorkerRes", utils.Fail("参数错误")))
		return
	}
	list, err := tables.ChangeQueryWorker(param.Id, param.Type)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWorkerRes", utils.Fail("查询上酒工历史记录失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("superDashboardQueryWorkerRes", utils.Success(list)))
}
