package com.usky.sas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.sas.domain.SasSystemConfig;
import com.usky.sas.mapper.SasSystemConfigMapper;
import com.usky.sas.mqtt.MqttService;
import com.usky.sas.service.SasSystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 系统配置 服务实现
 */
@Service
public class SasSystemConfigServiceImpl extends AbstractCrudService<SasSystemConfigMapper, SasSystemConfig>
        implements SasSystemConfigService {

    @Override
    public SasSystemConfig getConfig() {
        LambdaQueryWrapper<SasSystemConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.last("limit 1");
        return this.getOne(wrapper, false);
    }

    @Override
    public void updateConfig(SasSystemConfig config) {
        LocalDateTime now = LocalDateTime.now();
        SasSystemConfig exist = null;
        if (config.getId() != null) {
            exist = this.getById(config.getId());
        } else {
            exist = getConfig();
        }
        if (exist == null) {
            config.setCreateTime(now);
            config.setUpdateTime(now);
            this.save(config);
        } else {
            config.setId(exist.getId());
            config.setCreateTime(exist.getCreateTime());
            config.setUpdateTime(now);
            this.updateById(config);
        }
    }
}
