package controllers

import (
	"github.com/kataras/iris/v12"
	"sparrow/services/knowoapi/services"
)

// SubDeviceController api
type SubDeviceController struct {
	Ctx     iris.Context
	Service services.SubDeviceService
	Token
}

// Get 获取子设备列表
// GET /SubDevice?pi=&ps=&device_code=
func (a *SubDeviceController) Get() {
	pi, err := a.Ctx.URLParamInt("pi")
	if err != nil {
		badRequest(a.Ctx, err)
		return
	}
	ps, err := a.Ctx.URLParamInt("ps")
	if err != nil {
		badRequest(a.Ctx, err)
		return
	}
	deviceCode := a.Ctx.URLParam("device_code")

	data, total, err := a.Service.GetSubDevices(deviceCode, pi, ps)
	if err != nil {
		responseError(a.Ctx, ErrDatabase, err.Error())
		return
	}
	done(a.Ctx, map[string]interface{}{
		"list":  data,
		"total": total,
	})
}
