package com.ytpm.controller;

import com.ytpm.agent.view.AgentUserInfo;
import com.ytpm.feign.RiskFeign;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.risk.param.RiskBannedListParam;
import com.ytpm.risk.param.RiskBannedParam;
import com.ytpm.risk.param.RiskConfigParam;
import com.ytpm.risk.param.RiskDeblockingListParam;
import com.ytpm.risk.param.RiskRelativeAppParam;
import com.ytpm.risk.param.RiskUserStatusParam;
import com.ytpm.risk.view.RiskBannedListView;
import com.ytpm.risk.view.RiskConfigListView;
import com.ytpm.risk.view.RiskConfigView;
import com.ytpm.risk.view.RiskDeblockingListView;
import com.ytpm.risk.view.RiskTemplateView;
import com.ytpm.service.RiskService;
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.GetMapping;
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("/risk")
public class RiskController {

    @Resource
    private RiskService riskService;


    /**
     * 锁定用户禁止用户登录
     */
    @GetMapping("/lockUser")
    @ApiOperation(value = "锁定用户禁止用户登录")
    public Result<?> lockUser(@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser, @RequestParam("userId")String userId, @RequestParam("appId")String appId){
        return riskService.lockUser(curUser,userId,appId);
    }

    /**
     * 平台或渠道封禁用户
     */
    @PostMapping("/bannedUser")
    @ApiOperation(value = "平台或渠道封禁用户")
    public Result<?> banned(@RequestBody RiskBannedParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        return riskService.banned(param,curUser);
    }

    /**
     * 批量封禁用户
     */
    @PostMapping("/batchBanned")
    @ApiOperation(value = "平台或渠道封禁用户")
    public Result<?> batchBanned(@RequestBody RiskBannedParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        return riskService.batchBanned(param,curUser);
    }

    @PostMapping("/batchBannedIps")
    @ApiOperation(value = "平台或渠道封禁用户IP")
    public Result<?> batchBannedIps(@RequestBody RiskBannedParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        return riskService.batchBannedByIps(param,curUser);
    }



    /**
     * 批量解禁用户
     */
    @PostMapping("/batchDeblock")
    @ApiOperation(value = "平台或渠道封禁用户")
    public Result<?> batchDeblock(@RequestBody RiskDeblockingListParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        return riskService.batchDeblock(param,curUser);
    }

    /**
     * 查询风控配置列表
     */
    @PostMapping("/configList")
    @ApiOperation("查询风控配置列表")
    public ResultTable<RiskConfigListView> configList(@RequestBody RiskConfigParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        param.setChannelId(curUser.getUserId());
        return riskService.getConfigList(param);
    }

    /**
     * 查询风控记录
     */
    @ApiOperation("查询风控用户记录")
    @PostMapping("/bannedList")
    public ResultTable<RiskBannedListView> bannedList(@RequestBody RiskBannedListParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        param.setAgentId(curUser.getUserId());
        return riskService.bannedList(param);
    }

    /**
     * 查询解封记录
     */
    @ApiOperation("查询解封记录")
    @PostMapping("/deblockingList")
    public ResultTable<RiskDeblockingListView> deblockingList(@RequestBody RiskDeblockingListParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        param.setAgentId(curUser.getUserId());
        return riskService.deblockingList(param);
    }

    /**
     * 修改用户风控状态
     */
    @ApiOperation("修改用户风控状态")
    @PostMapping("/changeUserStatus")
    public Result<?> changeUserStatus(@RequestBody RiskUserStatusParam param,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
       return riskService.changeUserStatus(param,curUser);
    }

    /**
     * 风控配置关联应用
     */
    @ApiOperation("风控配置关联应用")
    @PostMapping("/relativeApp")
    public Result<?> relativeApp(@RequestBody RiskRelativeAppParam param){
        return riskService.relativeApp(param);
    }

    /**
     * 保存风控配置
     */
    @ApiOperation("保存风控配置")
    @PostMapping("/saveConfig")
    public Result<?> saveConfig(@RequestBody RiskConfigParam param){
        return riskService.saveConfig(param);
    }

    /**
     * 修改风控配置
     */
    @ApiOperation("修改风控配置")
    @PostMapping("/updateConfig")
    public Result<?> updateConfig(@RequestBody RiskConfigParam param){
        return riskService.updateConfig(param);
    }

    /**
     * 查询配置字段选项
     */
    @ApiOperation("查询配置字段选项")
    @GetMapping("/options")
    public ResultTable<RiskConfigView> getRiskConfig(String appIds) {
        return riskService.getRiskConfig(appIds);
    }

    /**
     * 查询配置字段选项
     */
    @ApiOperation("查询配置字段选项")
    @GetMapping("/getByNode")
    public ResultTable<RiskConfigView> getRiskConfig(@RequestParam(name = "effectNode", required = false) Integer effectNode) {
        return riskService.getFieldConfigByNode(effectNode);
    }

    /**
     * 启用或关闭风控配置
     */
    @ApiOperation("启用或关闭风控配置")
    @ApiImplicitParam(name = "templateId", value = "配置模板ID", required = true)
    @GetMapping("/enabledConfig")
    public Result<?> isEnabledConfig(@RequestParam("templateId")String templateId,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        return riskService.isEnabledConfig(templateId,curUser.getUserId());
    }

    /**
     * 风控配置模板详情
     */
    @ApiOperation("风控配置模板详情")
    @ApiImplicitParam(name = "templateId", value = "配置模板ID", required = true)
    @GetMapping("/templateView")
    public Result<RiskTemplateView> templateView(@RequestParam("templateId")String templateId,@ApiIgnore @AuthenticationPrincipal AgentUserInfo curUser){
        return riskService.templateView(templateId,curUser.getUserId());
    }
}
