package cn.com.countrygarden.bi.main.controller;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.service.VersionService;
import cn.com.countrygarden.bi.main.util.RedisCacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;


@Controller("versionController")
@RequestMapping("/api/biapp/version")
public class VersionController {

    @Autowired
    private VersionService versionService;


    /**
     * 获取最新版本
     *
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping(value = "/latest", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Object getLatestVersion(@RequestParam(value = "platform", required = true, defaultValue = "biappandroid") String platform)
            throws ServiceException {
        Map<String, String> param = new HashMap<String, String>() {{
            put("platform", platform);
        }};
        Function<Map<String, String>, Map<String, Object>> function = p -> versionService.getLatestVersion(p);

        Map<String, Object> version = RedisCacheUtils.rememberMap(
                "biapp:version:latest",
                900 /* 15 * 60 */,
                param,
                function
        );
        return Response.response(version);
    }
}
