package net.northking.cctp.scriptcase.api.app;

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.scriptcase.api.app.service.AppApiService;
import net.northking.cctp.scriptcase.api.app.service.FileService;
import net.northking.cctp.scriptcase.dto.app.AppDetailDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/pub")
public class AppPubCtrl {
    @Autowired
    private AppApiService appApiService;
    @Autowired
    private FileService fileService;

    @ApiOperation(value = "获取：应用表")
    @GetMapping(
            value = "/apps/{uuid}",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<AppDetailDto> findByPK(
            @PathVariable("uuid") String uuid) {
        ResultWrapper<AppDetailDto> wrapper = new ResultWrapper<>();
        AppDetailDto dto = appApiService.findByPK(uuid);
        return wrapper.success(dto);
    }

    @ApiOperation(value = "获取应用详情")
    @GetMapping(
            value = "/apps/getAppDetail",
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultWrapper<AppDetailDto> getAppDetail(
            @RequestParam("appId") String appId) {
        ResultWrapper<AppDetailDto> wrapper = new ResultWrapper<>();
        AppDetailDto dto = appApiService.getAppDetail(appId);
        return wrapper.success(dto);
    }

    @ApiOperation("新版下载应用")
    @RequiredPermissions("ui-downloadApp")
    @GetMapping("/file/downloadApp/{appId}")
    public void downloadApp(HttpServletResponse response, @PathVariable("appId")String appId){
        fileService.downloadApp(response,appId);
    }

    @ApiOperation("根据id集合查询app信息")
    @PostMapping("/apps/queryByIds")
    public ResultWrapper<List<AppDetailDto>> queryByIds(@RequestBody List<String> ids){
        ResultWrapper<List<AppDetailDto>> wrapper = new ResultWrapper<>();
        List<AppDetailDto> resultList = appApiService.queryByIds(ids);
        return wrapper.success(resultList);
    }
}
