package com.ytpm.controller;

import com.ytpm.advertise.param.AppCategoryParam;
import com.ytpm.advertise.param.AppSubCategoryParam;
import com.ytpm.advertise.param.RelativeChannelParam;
import com.ytpm.agent.model.YtPlatformUserApp;
import com.ytpm.agent.param.AppListParam;
import com.ytpm.agent.param.AppParam;
import com.ytpm.agent.view.AgentAppView;
import com.ytpm.agent.view.AgentEnableAppView;
import com.ytpm.agent.view.AgentUserInfo;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.service.AgentAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

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

    @Resource
    private AgentAppService agentAppService;

    /**
     * 查询加盟商上架应用列表
     */
    @ApiOperation("查询加盟商上架应用列表")
    @GetMapping("/getIssuedAppList")
    private ResultTable<YtPlatformUserApp> getIssuedAppList(@ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        return agentAppService.getIssuedAppList(userInfo.getUserId());
    }


    /**
     * 根据当前登录的加盟商查询启用的应用列表
     */
    @ApiOperation("根据当前登录的加盟商查询启用的应用列表")
    @GetMapping("/getEnabledList")
    public ResultTable<AgentEnableAppView> getEnabledList(@ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        return agentAppService.getEnabledList(userInfo.getUserId());
    }

    /**
     * 查询应用列表
     */
    @ApiOperation("查询应用列表")
    @PostMapping("/searchApps")
    public ResultTable<AgentAppView> searchAll(@RequestBody AppListParam appListParam, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        appListParam.setUserId(userInfo.getUserId());
        return agentAppService.searchAppList(appListParam);
    }

    /**
     * 保存应用
     */
    @ApiOperation("保存应用，新增修改共用")
    @PostMapping("/saveApp")
    public Result<?> saveApp(@RequestBody AppParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        return agentAppService.saveApp(param,userInfo.getUserId());
    }

    /**
     * 删除应用
     */
    @ApiOperation("删除应用")
    @GetMapping("/delApp")
    public Result<?> delApp(@ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo, @RequestParam("appId")String appId) {
        return agentAppService.delApp(appId,userInfo.getUserId());
    }

    /**
     * 应用关联广告平台 & 广告平台应用
     */
    @ApiOperation("应用关联广告平台 & 广告平台应用")
    @PostMapping("/relativeChannel")
    public Result<?> relativeChannel(@RequestBody RelativeChannelParam param){
        return agentAppService.relativeChannel(param);
    }

    /**
     * 获取指定平台下的一级分类
     */
    @ApiOperation("获取指定平台下的一级分类")
    @PostMapping("/getPrimaryCategories")
    public Result<?> getPrimaryCategories(@RequestBody AppCategoryParam param){
        return agentAppService.getPrimaryCategories(param.getPlatform());
    }

    /**
     * 获取指定平台下的一级分类下的二级分类
     */
    @ApiOperation("获取指定平台下的一级分类下的二级分类")
    @PostMapping("/getSecondaryCategories")
    public Result<?> getPrimaryCategories(@RequestBody AppSubCategoryParam param){
        return agentAppService.getSecondaryCategories(param.getPlatform(), param.getPrimaryCategory());
    }


}
