package main

import (
	"sparrow/pkg/models"
	"sparrow/pkg/rpcs"
)

// GetDeviceCommands 查询设备指令列表（通过设备类型id）
func (r *Registry) GetDeviceCommands(args *rpcs.ArgsDeviceCommandQuery, reply *rpcs.ReplyDeviceCommandList) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	tx := db.Where("device_type_id = ?", args.DeviceTypeId)
	if args.Name != "" {
		tx = tx.Where("name like ?", "%"+args.Name+"%")
	}

	var datas []models.DeviceCommand
	err = tx.Preload("Data").Limit(args.Ps).Offset((args.Pi - 1) * args.Ps).Find(&datas).Error
	if err != nil {
		return err
	}
	var total int
	db.Model(&models.DeviceCommand{}).Where("device_type_id = ?", args.DeviceTypeId).Count(&total)

	reply.List = datas
	reply.Total = total
	return nil
}

// GetDeviceCommandById 根据ID查询设备指令
func (r *Registry) GetDeviceCommandById(recordId string, reply *models.DeviceCommand) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	err = db.Preload("Data").Where("record_id = ?", recordId).First(reply).Error
	return err
}

// GetDeviceStatusList 查询设备状态列表（通过设备类型id）
func (r *Registry) GetDeviceStatusList(args *rpcs.ArgsDeviceStatusQuery, reply *rpcs.ReplyDeviceStatusList) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	tx := db.Where("device_type_id = ?", args.DeviceTypeId)
	if args.Name != "" {
		tx = tx.Where("name like ?", "%"+args.Name+"%")
	}

	var datas []models.DeviceStatus
	err = tx.Preload("Data").Limit(args.Ps).Offset((args.Pi - 1) * args.Ps).Find(&datas).Error
	if err != nil {
		return err
	}
	var total int
	db.Model(&models.DeviceStatus{}).Where("device_type_id = ?", args.DeviceTypeId).Count(&total)

	reply.List = datas
	reply.Total = total
	return nil
}

// GetDeviceStatusById 根据ID查询设备状态
func (r *Registry) GetDeviceStatusById(recordId string, reply *models.DeviceStatus) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	err = db.Preload("Data").Where("record_id = ?", recordId).First(reply).Error
	return err
}

// GetSceneHis 分页查询场景执行历史
func (r *Registry) GetSceneHis(args *rpcs.ArgsSceneHisQuery, reply *rpcs.ReplySceneHisList) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	tx := db.Order("id desc")
	if args.SceneId != "" {
		tx = tx.Where("scene_id = ?", args.SceneId)
	}

	var datas []models.SceneHis
	err = tx.Limit(args.Ps).Offset((args.Pi - 1) * args.Ps).Find(&datas).Error
	if err != nil {
		return err
	}

	countTx := db.Model(&models.SceneHis{})
	if args.SceneId != "" {
		countTx = countTx.Where("scene_id = ?", args.SceneId)
	}
	var total int
	countTx.Count(&total)

	reply.List = datas
	reply.Total = total
	return nil
}

// GetSceneHisBySceneId 按场景ID分页查询执行历史
func (r *Registry) GetSceneHisBySceneId(args *rpcs.ArgsSceneHisQuery, reply *rpcs.ReplySceneHisList) error {
	db, err := getDB()
	if err != nil {
		return err
	}

	var datas []models.SceneHis
	err = db.Where("scene_id = ?", args.SceneId).Order("id desc").
		Limit(args.Ps).Offset((args.Pi - 1) * args.Ps).Find(&datas).Error
	if err != nil {
		return err
	}

	var total int
	db.Model(&models.SceneHis{}).Where("scene_id = ?", args.SceneId).Count(&total)

	reply.List = datas
	reply.Total = total
	return nil
}
