package service

import (
	"fmt"
	"iot_manager_service/app/record/dao"
	"iot_manager_service/app/record/edge_service"
)

var AKeyAlarmRecordService = new(aKeyAlarmRecordService)

type aKeyAlarmRecordService struct{}

func (s aKeyAlarmRecordService) List(searchValue string, start string, end string, id int, current int, size int) ([]dao.AkeyAlarmRecord, error) {
	var record dao.AkeyAlarmRecord
	offset := (current - 1) * size
	limit := size
	records, err := record.GetRecords(offset, limit, start, end, searchValue, id)
	if err != nil {
		return nil, err
	}
	return records, nil
}

func (s aKeyAlarmRecordService) Detail(id int) (*dao.AkeyAlarmRecord, error) {
	record := &dao.AkeyAlarmRecord{
		ID: id,
	}
	return record.Get()
}

func (s aKeyAlarmRecordService) Refresh() {
	// TODO:边缘数据
	up := edge_service.RecordAlarmSos{}
	maxId, maxUpDateTime := dao.AkeyAlarmRecord{}.GetMaxIdAndUpDateTime()
	recordLightUpDatas, err := up.SyncRecord(maxId, maxUpDateTime)
	//fmt.Printf("recordLightUpDatas = %v", recordLightUpDatas)
	dao.AkeyAlarmRecord{}.BatchCreate(recordLightUpDatas)
	if err != nil {
		fmt.Printf("Refresh err.Error() = %v", err)
		return
	}
}

func (s aKeyAlarmRecordService) UpdateRemark(id int, remark string) error {
	record := dao.AkeyAlarmRecord{ID: id}
	record.Remark = remark
	err := record.Update()
	if err != nil {
		return err
	}
	return nil
}
