package cn.com.qmth.stmms.biz.config.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.qmth.stmms.biz.common.BaseQueryService;
import cn.com.qmth.stmms.biz.config.dao.SystemConfigDao;
import cn.com.qmth.stmms.biz.config.model.SystemConfig;
import cn.com.qmth.stmms.biz.config.service.SystemConfigService;
import cn.com.qmth.stmms.common.enums.ConfigType;

@Service("systemConfigService")
public class SystemConfigServiceImpl extends BaseQueryService<SystemConfig> implements SystemConfigService {

    @Autowired
    private SystemConfigDao configDao;

    @Autowired
    private SystemCache systemCache;

    @Override
    public SystemConfig update(Integer id, String value) {
        SystemConfig s = configDao.findOne(id);
        s.setDescription(value);
        s.setUpdateTime(new Date());
        configDao.save(s);
        if (ConfigType.FILE_SERVER.equals(s.getType())) {
            systemCache.setFileServer(value);
        }
        if (ConfigType.MARK_TIME.equals(s.getType())) {
            systemCache.setMarkTime(Long.parseLong(value));
        }
        return s;
    }

    @Override
    public List<SystemConfig> findAll() {
        return (List<SystemConfig>) configDao.findAll();
    }

    @Override
    public SystemConfig findById(Integer id) {
        return configDao.findOne(id);
    }

    @Override
    public String findByType(ConfigType type) {
        SystemConfig s = configDao.findByType(type);
        if (s != null) {
            return s.getDescription();
        }
        return null;
    }

}
