package dashboard

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

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

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

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

func querySelf(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	_type, ok := data.(uint8)
	if !ok {
		_ = conn.WriteJSON(utils.WsEvent("adminDashboardQuerySelfRes", utils.Fail("参数错误")))
		return
	}
	list, err := tables.TradeQuerySpecificOne("manager", manager.Id, _type)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminDashboardQuerySelfRes", utils.Fail("查询管理员历史记录失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("adminDashboardQuerySelfRes", utils.Success(list)))
}

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

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

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