package controller

import (
	"cold-delivery/app/admin/model"
	"cold-delivery/app/admin/service"
	"cold-delivery/app/admin/service/dto"
	"cold-delivery/common/nats/nats_server"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"gogs.baozhida.cn/zoie/OAuth-core/api"
	_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
)

type WaybillTaskController struct {
	api.Api
}

// GetPage 获取运单任务列表
// @Summary 获取运单任务列表
// @Description 获取运单任务列表
// @Tags 运单任务
// @Param waybillNo query string true "运单号"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.WaybillTask}} "{"code": 200, "data": [...]}"
// @Router /api/waybill-task [get]
// @Security Bearer
func (e WaybillTaskController) GetPage(c *gin.Context) {
	s := service.WaybillTask{}
	req := dto.WaybillTaskGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Query).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	list := make([]model.WaybillTask, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	//e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
	e.PageOK(list, int(count), 0, 0, "查询成功")
}

// GetData 获取温湿度记录
// @Summary 获取温湿度记录
// @Description 获取温湿度记录
// @Tags 运单任务
// @Accept  application/json
// @Product application/json
// @Param data body dto.WaybillTaskGetDataPageReq true "body"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.WaybillTask}} "{"code": 200, "data": [...]}"
// @Router /api/waybill-task/data [post]
// @Security Bearer
func (e WaybillTaskController) GetData(c *gin.Context) {
	s := service.WaybillTask{}
	req := dto.WaybillTaskGetDataPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	list := make([]nats_server.DeviceData_R, 0)
	var count int64

	list, count, err = s.GetDataPage(&req)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// GetNewestData 获取最新温湿度记录
// @Summary 获取最新温湿度记录
// @Description 获取最新温湿度记录
// @Tags 运单任务
// @Accept  application/json
// @Product application/json
// @Param data body dto.WaybillTaskGetNewestDataPageReq true "body"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.WaybillTask}} "{"code": 200, "data": [...]}"
// @Router /api/waybill-task/newest-locus [post]
// @Security Bearer
func (e WaybillTaskController) GetNewestData(c *gin.Context) {
	s := service.WaybillTask{}
	req := dto.WaybillTaskGetNewestDataPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	data := nats_server.DeviceData_R{}

	data, err = s.GetNewestDataPage(&req)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(data, "查询成功")
}

// GetLocus 获取轨迹信息
// @Summary 获取轨迹信息
// @Description 获取轨迹信息
// @Tags 运单任务
// @Accept  application/json
// @Product application/json
// @Param data body dto.WaybillTaskGetNewestDataPageReq true "body"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.WaybillTask}} "{"code": 200, "data": [...]}"
// @Router /api/waybill-task/locus [post]
// @Security Bearer
func (e WaybillTaskController) GetLocus(c *gin.Context) {
	s := service.WaybillTask{}
	req := dto.WaybillGetLocusReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Query).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	list := make([]nats_server.DeviceData_R2, 0)

	list, err = s.GetLocus(&req)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(list, "获取轨迹信息成功")
}
