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

import com.reyun.saas.common.framework.ApiRestController;
import com.reyun.saas.common.restResult.RestResult;
import com.reyun.saas.mob.user.service.ChannelService;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Api(value = "/channel", tags = "渠道模块")
@ApiRestController("channel")
public class ChannelController {

    @Resource
    private ChannelService channelService;



    @Log(value = 4, description = "渠道列表", type = 3)
    @ApiOperation(value = "渠道列表", produces = "application/json",
            notes = "当前页数:page 每页条数:size 排序字段:sortField 排序格则:sortOrder(ASC正序 DESC倒序)" +
                    "除去接口文档中的字段 还需要根据用户点击的筛选条件传参数" +
                    " 渠道别名:channelAlias 渠道id:cid app名称:appName 应用包名:bid " +
                    "是否自定义过别名:isCustomAlias 搜索字段:query")
    @PostMapping("list")
    public RestResult list(@RequestBody Map<String,Object> map){
        return channelService.list(map,SecurityUtils.getLoginUserId());
    }

    @Log(value = 4, description = "查询筛选条件", type = 3)
    @ApiOperation(value = "筛选条件下拉列表", produces = "application/json",
    notes = "下拉列表名称: key   渠道id:cid   渠道别名:channelAlias")
    @GetMapping("screeningInfo")
    public RestResult screening(String key,String cid,String channelAlias,String bid){
        if("bid".equals(key)){
            return channelService.appAndBidScreeningList(SecurityUtils.getLoginUserId());
        }else{
            return channelService.screeningList(key,bid,cid,channelAlias,SecurityUtils.getLoginUserId());
        }
    }

    @Log(value = 3, description = "启用渠道", type = 3,insertDB = true)
    @ApiOperation(value = "修改启用状态", produces = "application/json")
    @PutMapping("updateActive/{channelId}/{status}")
    public RestResult enableChannel(@PathVariable("channelId")Long channelId,@PathVariable("status")Boolean status){
        return channelService.updateActiveStatus(channelId,SecurityUtils.getLoginUserId(),status);
    }


    @Log(value = 3, description = "修改渠道别名", type = 3,insertDB = true)
    @ApiOperation(value = "修改渠道别名", produces = "application/json",
            notes = "除去文档上的字段还需要传 渠道id:ids(数组格式  如果只修改一个渠道数组中传入一个id即可) 修改的渠道名称:newChannelName ")
    @PutMapping("updateChannelName")
    public RestResult updateChannelName(@RequestBody Map<String,Object> map){

        return channelService.updateChannelName(map,SecurityUtils.getLoginUserId());
    }


    @Log(value = 4, description = "根据appkey查询渠道", type = 3,insertDB = true)
    @ApiOperation(value = "根据appkey查询渠道", produces = "application/json",
            notes = "传appkey即可")
    @GetMapping("findChannel")
    public RestResult findChannel(String appkey){
       return channelService.findChannel(appkey,SecurityUtils.getLoginUserId());
    }


}
