package com.yonge.cooleshow.admin.controller.open;

import com.microsvc.toolkit.common.response.template.R;
import com.microsvc.toolkit.common.webportal.exception.BizException;
import com.yonge.cooleshow.biz.dal.entity.AppVersionInfo;
import com.yonge.cooleshow.biz.dal.service.AppVersionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/open/appVersionInfo")
@Api(tags = "app版本信息管理")
public class OpenAppVersionInfoController {

    @Autowired
    private AppVersionInfoService appVersionInfoService;


    @ApiOperation("根据app客户端查询最近的app版本")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "platform", value = "平台名称", required = true, dataType = "String", paramType = "path"),
        @ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "String",
            paramType = "newest/history")
    })
    @GetMapping(value = "/queryLatestByPlatform")
    public R<AppVersionInfo> queryLatestByPlatform(String platform, String status) {
        String appStatus = Optional.ofNullable(status).orElse("newest");
        if ("history".equals(appStatus)) {
            appStatus = "newest,history";
        }
        List<AppVersionInfo> list = appVersionInfoService.queryLatestByPlatform(platform, appStatus);
        if (list.size() > 0) {
            return R.from(list.get(0));
        }
        throw BizException.from("未找到数据");
    }
}
