package cn.com.countrygarden.bi.main.service.impl;

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.dao.ModuleMapper;
import cn.com.countrygarden.bi.main.model.Module;
import cn.com.countrygarden.bi.main.service.ModuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ModuleServiceImpl extends AbstractService<Module> implements ModuleService {
    @Resource
    private ModuleMapper moduleMapper;

    public List<Module> getMainModule(int limit) {
        return moduleMapper.getMainModule(limit);
    }

    public List<Map<String, String>> getMainModule(Map<String, String> param) {
        String limit = param.getOrDefault("limit", "8");

        List<Map<String, String>> response = new ArrayList<>();

        List<Module> modules = getMainModule(Integer.parseInt(limit));

        if (modules.isEmpty()) {
            return response;
        }

        for (Module module : modules) {
            Map<String, String> item = new HashMap<>();
            item.put("id", String.valueOf(module.getId()));
            item.put("title", module.getTitle());
            item.put("content", module.getContent());
            item.put("type", module.getType());
            item.put("url", module.getUrl());
            item.put("image", module.getImage());
            item.put("sort", module.getSort());
            item.put("param", module.getParam());
            item.put("iosController", module.getAndroidActivity());
            item.put("androidActivity", module.getIosController());

            response.add(item);
        }


        return response;
    }
}
