package ink.wgink.login.app.controller.api.appverion;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.app.pojo.dtos.appversion.AppVersionDTO;
import ink.wgink.login.app.pojo.vos.appversion.AppVersionSaveVO;
import ink.wgink.login.app.pojo.vos.appversion.AppVersionVO;
import ink.wgink.login.app.service.appversion.IAppVersionService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: AppVersionController
 * @Description: app版本
 * @Author: admin
 * @Date: 2019-08-26 14:50:40
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "app版本管理接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/appversion")
public class AppVersionController extends DefaultBaseController {

    @Autowired
    private IAppVersionService appVersionService;

    @ApiOperation(value = "app版本新增", notes = "app版本新增接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    @CheckRequestBodyAnnotation
    public SuccessResult saveAppVersion(@RequestBody AppVersionSaveVO appVersionSaveVO) {
        appVersionService.save(appVersionSaveVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "app版本删除", notes = "通过id列表批量删除app版本接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "app版本ID列表，用下划线分隔", paramType = "path", example = "1_2_3")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult remove(@PathVariable("ids") String ids) throws RemoveException {
        appVersionService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "app版本修改", notes = "app版本修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appVersionId", value = "app版本ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{appVersionId}")
    @CheckRequestBodyAnnotation
    public SuccessResult update(@PathVariable("appVersionId") String appVersionId, @RequestBody AppVersionVO appVersionVO) {
        appVersionService.update(appVersionId, appVersionVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "更新APP接口锁定状态", notes = "更新APP接口锁定状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appVersionId", value = "app版本ID", paramType = "path"),
            @ApiImplicitParam(name = "appApiLock", value = "接口版本锁定", paramType = "path")
    })
    @PutMapping("update-api-lock/{appVersionId}/{appApiLock}")
    public SuccessResult updateAppApiLock(@PathVariable("appVersionId") String appVersionId, @PathVariable("appApiLock") Integer appApiLock) {
        appVersionService.updateApiLock(appVersionId, appApiLock);
        return new SuccessResult();
    }

    @ApiOperation(value = "更新APP为发布状态", notes = "更新APP为发布状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appVersionId", value = "app版本ID", paramType = "path"),
    })
    @PutMapping("update-release/{appVersionId}")
    public SuccessResult updateRelease(@PathVariable("appVersionId") String appVersionId) {
        appVersionService.updateRelease(appVersionId, 1);
        return new SuccessResult();
    }

    @ApiOperation(value = "更新APP为未发布状态", notes = "更新APP为未发布状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appVersionId", value = "app版本ID", paramType = "path"),
    })
    @PutMapping("update-unrelease/{appVersionId}")
    public SuccessResult updateUnRelease(@PathVariable("appVersionId") String appVersionId) {
        appVersionService.updateRelease(appVersionId, 0);
        return new SuccessResult();
    }

    @ApiOperation(value = "app版本列表", notes = "app版本列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list")
    public List<AppVersionDTO> listAppVersion() {
        Map<String, Object> params = getParams();
        return appVersionService.list(params);
    }

    @ApiOperation(value = "app版本详情", notes = "app版本详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appVersionId", value = "app版本ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{appVersionId}")
    public AppVersionDTO get(@PathVariable("appVersionId") String appVersionId) {
        return appVersionService.get(appVersionId);
    }

    @ApiOperation(value = "分页app版本列表", notes = "分页app版本列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<AppVersionDTO>> listPageAppVersion(ListPage page) {
        Map<String, Object> params = requestParams();
        page.setParams(params);
        return appVersionService.listPage(page);
    }

    @ApiOperation(value = "获取app二维码", notes = "获取app二维码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appVersionId", value = "app版本ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("downloadqrcode/{appVersionId}")
    public void downloadQrCode(@PathVariable("appVersionId") String appVersionId, HttpServletResponse response) throws IOException {
        appVersionService.downloadQrCode(appVersionId, response);
    }

}
