package service

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

// 中间件管理服务
var LampPoleService = new(lampPoleService)

type lampPoleService struct{}

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

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

	return &model.LampPoleDetail{
		LampPole:          *device,
		AlarmTerminalList: AlarmTerminalService.GetByLampPole(device.ID),
		CameraList:        CameraService.GetByLampPole(device.ID),
		CaptureUnitList:   CaptureUintService.GetByLampPole(device.ID),
		GatewayList:       GatewayService.GetByLampPole(device.ID),
		InfoBoardList:     InfoBoardService.GetByLampPole(device.ID),
		IpBroadcastList:   IpBroadcastService.GetByLampPole(device.ID),
		LightControlList:  LightControlService.GetByLampPole(device.ID),
		SensorList:        OptoSensorService.GetByLampPole(device.ID),
		ZigbeeList:        ZigbeeService.GetByLampPole(device.ID),
	}, nil
}

func (s *lampPoleService) CreateOrUpdate(userId int, tenantId string, req *dao.LampPole) *common.Errors {
	device := req
	device.TenantId = tenantId
	device.CoordType = 1
	device.UpdateUser = userId
	device.UpdateTime = time.Now()

	if device.ID == 0 {
		device.CreateTime = time.Now()
		device.CreateUser = userId

		if device.IsExistedBySN() {
			logger.Logger.Errorf("Create IsExistedBySN \n")
			return common.ParamsInvalidResponse(model.RepeatedPrompts, nil)
		}
		logger.Logger.Errorf("device = %+v \n", device)
		if err := device.Create(); err != nil {
			logger.Logger.Errorf("Create err = %s \n", err.Error())
			return common.FailResponse(err.Error(), nil)
		}
		service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
			common.DeviceTypeLampPole, common.GetDeviceObject(device.ID, device.PoleName), common.OperationSuccess)
		return common.SuccessResponse(common.Succeeded, nil)
	}

	if err := device.Update(); err != nil {
		logger.Logger.Errorf("Update err = %s \n", err.Error())
		return common.FailResponse(err.Error(), nil)
	}

	service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
		common.DeviceTypeLampPole, common.GetDeviceObject(device.ID, device.PoleName), common.OperationSuccess)
	return common.SuccessResponse(common.Succeeded, nil)
}

func (s *lampPoleService) List(searchValue, groupId, boxId string, current, size int) ([]dao.LampPole, int64, *common.Errors) {
	device := dao.LampPole{}
	if searchValue != "" {
		device.PoleSN = searchValue
	}

	device.GroupId = common.StringToInt(groupId)
	device.BoxId = common.StringToInt(boxId)

	offset := (current - 1) * size
	limit := size
	devices, total, err := device.GetDevices(offset, limit)
	if err != nil {
		return nil, 0, common.FailResponse(err.Error(), nil)
	}
	return devices, total, nil
}

func (s *lampPoleService) Remove(userId int, tenantId string, id int) *common.Errors {
	// 创建查询实例
	device := &dao.LampPole{
		ID:         id,
		IsDeleted:  1,
		UpdateUser: userId,
		UpdateTime: time.Now(),
	}
	err := device.Delete()
	if err != nil {
		return common.FailResponse(err.Error(), nil)
	}
	service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
		common.DeviceTypeLampPole, common.GetDeviceObject(device.ID, device.PoleName), common.OperationSuccess)
	return nil
}

func (s *lampPoleService) GetList(tenantId string) ([]*dao.LampPole, *common.Errors) {
	device := &dao.LampPole{
		TenantId:  tenantId,
		IsDeleted: 0,
	}
	devices, err := device.GetAllDevices()
	for _, device := range devices {
		device.PoleName = device.PoleName + "(" + device.PoleSN + ")"
	}
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}

	return devices, nil
}

func (s *lampPoleService) GetOne(id int) (*dao.LampPole, *common.Errors) {
	device := &dao.LampPole{
		ID: id,
	}
	err := device.GetDevice()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return device, nil
}

func (s *lampPoleService) CountLampPole(groupId int) int64 {
	device := &dao.LampPole{
		GroupId: groupId,
	}
	return device.Count()
}

func (s *lampPoleService) DeviceCount() dao.DeviceStatus {
	var rsp dao.DeviceStatus
	d := dao.LampPole{}
	rsp.Set(d.DeviceCount1())
	return rsp
}
func (s *lampPoleService) ListDevices() []dao.Device {
	pole := dao.LampPole{}
	return pole.ListDevices1()
}