package com.crm.ib.task.service.impl;

import com.crm.ib.task.dao.mapper.DailyConfigDao;
import com.crm.ib.task.service.DailyConfigService;
import com.crm.rely.backend.core.pojo.table.DailyConfigTable;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by max on 2020/6/23.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class DailyConfigServiceImpl implements DailyConfigService{

    @Autowired
    private DailyConfigDao dailyConfigDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateValByKey(String key, Date stamp) throws Exception {

        DailyConfigTable configTable = new DailyConfigTable();
        configTable.setKey(key);
        configTable.setVal(stamp);
        dailyConfigDao.updateValByKey(configTable);


    }

    @Override
    public Map<String, Date> getConfigMap() throws Exception {

        /*
            获取所有统计起始时间
         */
        List<DailyConfigTable> configTables = dailyConfigDao.findAll();
        if (CollectionUtils.isEmpty(configTables)) {
            return null;
        }
        Map<String, Date> configs = Maps.newHashMap();
        for (DailyConfigTable configTable : configTables) {
            configs.put(configTable.getKey(), configTable.getVal());
        }

        return configs;
    }
}
