package com.ytpm.middle.controller;

import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.middle.param.AppForm;
import com.ytpm.middle.param.AppListParam;
import com.ytpm.middle.param.GrantAppParam;
import com.ytpm.middle.service.AppService;
import com.ytpm.middle.view.AppListVO;
import com.ytpm.middle.view.MiddleUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@Api(tags = "应用管理")
@RestController
@RequestMapping("/app")
public class AppController {

    @Resource
    private AppService appService;

    /**
     * 查询已上架应用列表
     */
    @ApiOperation("查询已上架应用列表")
    @PostMapping("/appList")
    public ResultTable<AppListVO> appList(@RequestBody AppListParam param){
        return appService.appList(param);
    }

    /**
     * 新增上架应用
     */
    @ApiOperation("新增上架应用")
    @PostMapping("/addApp")
    public Result<String> addApp(@RequestBody AppForm form, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        form.setCreateUserId(userInfo.getUserId());
        return appService.addApp(form);
    }
    /**
     * 修改上架应用信息
     */
    @ApiOperation("修改上架应用")
    @PostMapping("/updateApp")
    public Result<String> updateApp(@RequestBody AppForm form, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        form.setUpdateParam(userInfo.getUserId());
        return appService.updateApp(form);
    }
    /**
     * 删除应用
     */
    @ApiImplicitParam(name = "appId", value = "应用ID", required = true)
    @ApiOperation("删除应用")
    @PostMapping("/delApp")
    public Result<String> delApp(@RequestParam("appId")String appId, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        return appService.delApp(appId,userInfo.getUserId());
    }

    /**
     * 应用授权企业
     */
    @ApiOperation("应用授权企业")
    @PostMapping("/grantApp")
    public Result<String> grantApp(@RequestBody GrantAppParam param, @ApiIgnore @AuthenticationPrincipal MiddleUserInfo userInfo){
        param.setUserId(userInfo.getUserId());
        return appService.grantApp(param);
    }
}
