package com.ytpm.controller;

import com.ytpm.agent.view.AgentUserInfo;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.risk.param.RiskConfigParam;
import com.ytpm.risk.view.RiskConfigListView;
import com.ytpm.risk.view.RiskConfigView;
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("/riskConfig")
public class RiskConfigController {

    @Resource
    private RiskService riskService;

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

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

    /**
     * 保存风控配置
     */
    @ApiOperation("保存风控配置")
    @PostMapping("/saveConfig")
    public Result<?> saveConfig(@RequestBody RiskConfigParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo){
        param.setChannelId(userInfo.getUserId());
        param.setOperator(userInfo.getUserId());
        param.setOperatorName(userInfo.getNickName());
        return riskService.saveConfig(param);
    }

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

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

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

    /**
     * 修改风控配置模版
     */
    @ApiOperation("修改风控配置模版")
    @PostMapping("/updateConfig")
    public Result<?> updateConfig(@RequestBody RiskConfigParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo){
        param.setChannelId(userInfo.getUserId());
        return riskService.updateConfig(param);
    }
}
