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

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.dao.BannerMapper;
import cn.com.countrygarden.bi.main.model.Banner;
import cn.com.countrygarden.bi.main.service.BannerService;
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 BannerServiceImpl extends AbstractService<Banner> implements BannerService {
    @Resource
    private BannerMapper bannerMapper;

    public List<Banner> getMainBanner(int limit) {
        return bannerMapper.getMainBanner(limit);
    }

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

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

        List<Banner> banners = getMainBanner(Integer.parseInt(limit));

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

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

            response.add(item);
        }

        return response;
    }
}
