package com.sptg.device.biz.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.MqConf;
import com.sptg.device.biz.service.DeviceRegisterServiceInterface;
import com.sptg.device.biz.service.DeviceServiceInterface;
import com.sptg.device.biz.service.MqConfServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
/**
 * title: DeviceRegisterServiceImpl
 * package: com.sptg.device.biz.security.impl
 * description: 设备注册业务实现类
 *
 *
 *
 */
@Service
@RequiredArgsConstructor
public class DeviceRegisterServiceImpl implements DeviceRegisterServiceInterface {

    private final DeviceServiceInterface deviceServiceInterface;
    private final MqConfServiceInterface mqConfServiceInterface;

    @Override
    public ResultBean<Device> getConnection(Device device) {
        //  判断该序列号是否已在后台录入
        Device dbDevice = this.deviceServiceInterface.selectBySerialNumber(device.getSerialNumber());
        MqConf conf;
        if (null == dbDevice){
            dbDevice = new Device();
            //  默认不启用时间策列
            dbDevice.setTimeStrategyIsEnable(OperationConstant.TWO);
            //  默认开门密码
            dbDevice.setOpenDoorPwd("000000");
            //  初始化  orgPositon (超管position)
            dbDevice.setOrgPosition("0/1/");
            //  默认人脸识别度
            dbDevice.setFaceResolution(75);
            dbDevice.setSerialNumber(device.getSerialNumber());
            dbDevice.setDeviceFactory(device.getDeviceFactory());
            dbDevice.setDeviceModelNumber(device.getDeviceModelNumber());
            dbDevice.setVersionNumber(device.getVersionNumber());
            dbDevice.setCreateDate(new Date());
            //  获取当前可用的队列服务器
            conf = this.mqConfServiceInterface.selectUsableServer();
            Assert.notNull(conf,"没有可用的队列服务器");
            dbDevice.setMqId(conf.getId());
            //  录入设备信息
            this.deviceServiceInterface.insert(dbDevice);
            //  保存队列连接数
            conf.setThreads(conf.getThreads() + 1);
            this.mqConfServiceInterface.updateById(conf);
        }else {
            //  获取队列信息
            conf = this.mqConfServiceInterface.selectById(dbDevice.getMqId());
            boolean isModify = false;
            //  更新设备厂商
            if (null != device.getDeviceFactory() && !device.getDeviceFactory().equals(dbDevice.getDeviceFactory())){
                dbDevice.setDeviceFactory(device.getDeviceFactory());
                isModify = true;
            }
            //  更新设备型号
            if (null != device.getDeviceModelNumber() && !device.getDeviceModelNumber().equals(dbDevice.getDeviceModelNumber())){
                dbDevice.setDeviceModelNumber(device.getDeviceModelNumber());
                isModify = true;
            }
            //  更新设备版本号
            if (null != device.getVersionNumber() && !device.getVersionNumber().equals(dbDevice.getVersionNumber())){
                dbDevice.setVersionNumber(device.getVersionNumber());
                isModify = true;
            }
            if (isModify){
                this.deviceServiceInterface.updateById(dbDevice);
            }
        }
        dbDevice.setMqConf(conf);
        return new ResultBean<>(ResultEnum.SUCCESS,dbDevice);
    }

    @Override
    public ResultBean<MqConf> getDHConnection() {
        MqConf conf;
        //  获取当前可用的队列服务器
        conf = this.mqConfServiceInterface.selectUsableServer();
        Assert.notNull(conf,"没有可用的队列服务器");
        //  保存队列连接数
//        conf.setThreads(conf.getThreads() + 1);
//        this.mqConfServiceInterface.updateById(conf);
        return new ResultBean<>(ResultEnum.SUCCESS, conf);
    }
}
