package controller

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

type WeianController struct {
	api.Api
}

// GetOrderInfo 获取维安智配送订单详情
// @Summary 获取维安智配送订单详情
// @Description 获取维安智配送订单详情
// @Tags 维安智配送
// @Param no query string false "订单号"
// @Success 200 {object} response.Response{data=response.Page{list=[]model.Waybill}} "{"code": 200, "data": [...]}"
// @Router /api/weian/orderInfo [get]
// @Security Bearer
func (e WeianController) GetOrderInfo(c *gin.Context) {
	s := service.Waybill{}
	req := dto.WeianGetOrderInfoReq{}
	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
	}

	client := resty.New()
	resp, err := client.R().
		SetQueryParams(map[string]string{
			"orderId": req.OrderId,
		}).
		Get(conf.ExtConfig.Weian.OrderInfoUrl)

	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, "获取维安智配送订单信息失败")
		return
	}
	var info model.WeianOrderResponse

	if err = json.Unmarshal(resp.Body(), &info); err != nil {
		e.Error(500, err, "获取药品扫码信息失败")
		return
	}
	if info.Code != 200 {
		e.Error(500, err, info.Msg)
		return
	}

	e.OK(info.Data, "查询成功")
}

// 测试opeapi鉴权
func (e WeianController) GetWaybillNo(c *gin.Context) {
	s := service.Waybill{}
	req := dto.WeianGetWaybillNoReq{}
	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
	}

	err = s.InsertByOrderId(&req)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}
	e.OK(req.WaybillNo, "查询成功")
}
func (e WeianController) GetWaybillData(c *gin.Context) {
	s := service.WaybillTask{}
	req := dto.WeianGetWaybillDataReq{}
	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
	}

	var task model.WaybillTask

	err = s.Get(&req, &task)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}

	var ids []int
	for _, ds := range task.DeviceSensorList {
		ids = append(ids, ds.T_id)
	}
	startTime := task.StartTime.String()
	endTime := task.EndTime.String()
	if len(req.StartTime) > 0 {
		_, err = time.Parse("2006-01-02 15:04:05", req.StartTime)
		if err != nil {
			e.Error(500, err, "开始时间格式不正确")
			return
		}
		startTime = req.StartTime
	}
	if len(req.EndTime) > 0 {
		_, err = time.Parse("2006-01-02 15:04:05", req.EndTime)
		if err != nil {
			e.Error(500, err, "结束时间格式不正确")
			return
		}
		endTime = req.EndTime
	}
	dataReq := dto.WaybillTaskGetDataPageReq{
		Pagination: cDto.Pagination{
			Page:     req.GetPageIndex(),
			PageSize: req.GetPageSize(),
		},
		WaybillNo: task.WaybillNo,
		TaskId:    task.Id,
		T_ids:     ids,
		StartTime: startTime,
		EndTime:   endTime,
	}
	list, count, err := s.GetDataPage(&dataReq)
	if err != nil {
		e.Error(500, err, err.Error())
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// 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 WeianController) 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, "获取轨迹信息成功")
}
