package change

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

func Query(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("adminWineChangeQueryRes", utils.Fail("参数错误")))
		return
	}
	total, warns, err := tables.WarnQuery(manager.Id, param.Cond, param.Deal, param.Limit, param.Page)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeQueryRes", utils.Fail("查询信息失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"adminWineChangeQueryRes",
		utils.Success(utils.JsonType{"total": total, "list": warns}),
	))
}

func QueryWine(conn *websocket.Conn, data any) {
	cond, ok := data.(string)
	if !ok {
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeQueryWineRes", utils.Fail("参数错误")))
		return
	}
	wines, err := tables.WinesQueryByIdOrName(cond)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeQueryWineRes", utils.Fail("查询酒品失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"adminWineChangeQueryWineRes",
		utils.Success(wines),
	))
}

func QueryWorker(conn *websocket.Conn, manager *tables.ManagerTable, data any) {
	cond, ok := data.(string)
	if !ok {
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeQueryWorkerRes", utils.Fail("参数错误")))
		return
	}
	workers, err := tables.WorkersQueryByPhoneOrName(manager.Id, cond)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeQueryWorkerRes", utils.Fail("查询酒品失败")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent(
		"adminWineChangeQueryWorkerRes",
		utils.Success(workers),
	))
}

func Assign(conn *websocket.Conn, data any) {
	var param assignParam
	err := utils.AnyTrans(data, &param)
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Fail("参数错误")))
		return
	}
	if param.Worker == "" {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Fail("请选择上酒工")))
		return
	}
	worker := tables.WorkerTable{Id: param.Worker}
	err = worker.Get()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Fail("上酒工不存在")))
		return
	}
	warn := tables.WarnTable{Id: param.Id}
	err = warn.Get()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Fail("报警不存在")))
		return
	}
	change := tables.ChangeTable{
		Wid: warn.Id, Device: warn.Device, Manager: warn.Manager,
		Worker: param.Worker, Code: utils.RandomString(6, utils.NumberPatten),
	}
	change.Old, change.New = param.Old, param.New
	err = change.Insert()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Fail("安排失败 01")))
		return
	}
	warn.Deal = true
	err = warn.UpdateSelf()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Fail("安排失败 02")))
		return
	}
	worker.Todo++
	err = worker.UpdateSelf()
	if err != nil {
		utils.Logger.Println(err)
		_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Fail("安排失败 03")))
		return
	}
	_ = conn.WriteJSON(utils.WsEvent("adminWineChangeAssignRes", utils.Success(nil)))
}
