package worker

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

func Query(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	if !manager.Super {
		_ = conn.WriteJSON(utils.WsEvent("superRoleWorkerQueryRes", utils.Fail("权限不足")))
		return
	}
	var param queryParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleWorkerQueryRes", utils.Fail("参数错误")))
		return
	}
	total, workers, err := tables.WorkerQuery(param.Manager, param.Cond, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("superRoleWorkerQueryRes", utils.Fail("查询上酒工失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"superRoleWorkerQueryRes",
		utils.Success(utils.JsonType{"total": total, "list": workers}),
	))
}
