package service

import (
	"fmt"
	"iot_manager_service/app/device/dao"
	"iot_manager_service/app/device/model"
	"iot_manager_service/app/system/service"
	"iot_manager_service/util/cache"
	"iot_manager_service/util/common"
	"iot_manager_service/util/logger"
	"time"
)

var SwitchBoxService = new(switchBoxService)

type switchBoxService struct{}

func (s *switchBoxService) Get(id int) (*dao.SwitchBox, *common.Errors) {
	// 创建查询实例
	device := &dao.SwitchBox{
		ID: id,
	}
	err := device.GetDevice()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return device, nil

}

func (s *switchBoxService) CreateOrUpdate(userId int64, tenantId string, req dao.SwitchBox) *common.Errors {
	// 创建查询实例
	device := req
	device.TenantId = tenantId
	device.UpdateUser = userId
	device.UpdateTime = time.Now()
	if req.ID == 0 {
		device.CreateTime = time.Now()
		device.CreateUser = userId
		if device.IsExistedBySN() {
			logger.Logger.Errorf("Create GetDeviceID err \n")
			return common.ParamsInvalidResponse("编码不能重复，请重新填写！", nil)
		}
		if err := device.Create(); err != nil {
			logger.Logger.Errorf("Create err = %s \n", err.Error())
			return common.FailResponse(err.Error(), nil)
		}
		cache.Redis.Del(getSwitchBoxListRedisKey(tenantId))
		service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
			common.DeviceTypeSwitchBox, common.GetDeviceObject(device.ID, device.BoxName), common.OperationSuccess)
		return common.SuccessResponse(common.Succeeded, nil)
	}
	//更新
	if device.IsExistedByNameAndCode() {
		return common.ParamsInvalidResponse("编码不能重复，请重新填写！", nil)
	}

	if err := device.Update(); err != nil {
		logger.Logger.Errorf("Update err = %s \n", err.Error())
		return common.FailResponse(err.Error(), nil)
	}
	cache.Redis.Del(getSwitchBoxListRedisKey(tenantId))
	service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
		common.DeviceTypeSwitchBox, common.GetDeviceObject(device.ID, device.BoxName), common.OperationSuccess)
	return common.SuccessResponse(common.Succeeded, nil)
}

func (s *switchBoxService) List(searchValue string, current, size int) ([]dao.SwitchBox, int64, *common.Errors) {
	device := dao.SwitchBox{}
	if searchValue != "" {
		device.BoxSN = searchValue
		device.BoxName = searchValue
	}

	offset := (current - 1) * size
	limit := size
	devices, total, err := device.GetDevices(offset, limit)
	if err != nil {
		return nil, 0, common.FailResponse(err.Error(), nil)
	}
	//关联灯杆
	for i, box := range devices {
		pole := dao.LampPole{}
		pole.BoxId = box.ID
		devices[i].CountLampPole, _ = pole.GetSwitchBoxCount()
	}
	return devices, total, nil
}

func (s *switchBoxService) Remove(userId int64, tenantId string, id int) *common.Errors {
	// 创建查询实例
	device := &dao.SwitchBox{
		ID:         id,
		IsDeleted:  1,
		UpdateUser: userId,
		UpdateTime: time.Now(),
	}
	err := device.Delete()
	if err != nil {
		return common.FailResponse(err.Error(), nil)
	}
	cache.Redis.Del(getSwitchBoxListRedisKey(tenantId))
	service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
		common.DeviceTypeSwitchBox, common.GetDeviceObject(device.ID, device.BoxName), common.OperationSuccess)
	return nil
}

func (s *switchBoxService) GetList(tenantId string, ) ([]*dao.SwitchBox, *common.Errors) {
	var devices []*dao.SwitchBox
	err := cache.Redis.Get(getSwitchBoxListRedisKey(tenantId)).Scan(&devices)
	if err == nil {
		return devices, nil
	}

	device := &dao.SwitchBox{
		TenantId:  tenantId,
		IsDeleted: 0,
	}
	devices, err = device.GetAllDevices()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	_ = cache.Redis.Set(getSwitchBoxListRedisKey(tenantId), devices, 0).Err()
	return devices, nil
}

func getSwitchBoxListRedisKey(tenantId string, ) string {
	return fmt.Sprintf(model.SwitchBoxList, tenantId)
}
