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.SasConfig;
import com.usky.sas.mapper.SasConfigMapper;
import com.usky.sas.service.SasConfigService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * Agbox/Metabus配置 服务实现
 */
@Service
public class SasConfigServiceImpl extends AbstractCrudService<SasConfigMapper, SasConfig>
        implements SasConfigService {

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

    @Override
    public void updateConfig(SasConfig config) {
        LocalDateTime now = LocalDateTime.now();
        SasConfig 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);
        }
    }
}

