package com.reyun.saas.web.controller.user;

import com.reyun.saas.common.framework.ApiRestController;
import com.reyun.saas.common.restResult.PageParam;
import com.reyun.saas.common.restResult.RestResult;
import com.reyun.saas.mob.user.domain.App;
import com.reyun.saas.mob.user.service.AppService;
import com.reyun.saas.security.utils.SecurityUtils;
import com.reyun.saas.web.aop.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Api(value = "/app", tags = "App应用管理")
@ApiRestController("app")
public class AppController {

    @Resource
    private AppService appServiceImpl;

    @Log(value = 1, description = "App创建", type = 2,insertDB = true)
    @ApiOperation(value = "App创建", notes = "只需要传这三个字段：appName,bid,os" , produces = "application/json")
    @PostMapping("create")
    public RestResult create(@RequestBody@ApiParam(name = "App信息", required = true) App app){
        return appServiceImpl.create(app,SecurityUtils.getLoginUserId());
    }

    @Log(value = 4, description = "App列表", type = 2)
    @ApiOperation(value = "App列表", notes = "query:关键字搜索，size:每页多少个，page:第几页(从1开始)，sortField:排序字段，sortOrder:desc-倒序/asc-正序" , produces = "application/json")
    @GetMapping("list")
    public RestResult list(PageParam pageParam, String query,String os,Long createUser){
        return appServiceImpl.list(pageParam, query,os,createUser,SecurityUtils.getLoginUserId());
    }

    @Log(value = 3, description = "修改App状态", type = 2 ,insertDB = true)
    @ApiOperation(value = "修改状态" , produces = "application/json")
    @GetMapping("disable/{appId}/{flag}")
    public RestResult disable(@PathVariable("flag")@ApiParam(name = "flag", required = true) Boolean flag,
                              @PathVariable("appId")@ApiParam(name = "appId", required = true) Long appId){
        return appServiceImpl.disable(appId,flag,SecurityUtils.getLoginUserId());
    }

    @Log(value = 2, description = "删除", type = 2,insertDB = true)
    @ApiOperation(value = "删除App", produces = "application/json")
    @DeleteMapping("delete/{appId}")
    public RestResult delete(@PathVariable("appId")@ApiParam(name = "appId", required = true) Long appId){
        return appServiceImpl.delete(appId,SecurityUtils.getLoginUserId());
    }

    @Log(value = 3, description = "设置编辑", type = 2, insertDB = true)
    @ApiOperation(value = "设置编辑" , produces = "application/json")
    @PostMapping("/update")
    public RestResult update(@RequestBody@ApiParam(name = "App信息", required = true) App app){
        return appServiceImpl.update(app,SecurityUtils.getLoginUserId());
    }

}
