package cn.pluss.platform.controller.common;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.common.RiskBlacklistService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.dto.RiskBlacklistDTO;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.vo.RiskBlacklistVO;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 风控黑名单表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2021-09-07
 */
@Controller
@RequestMapping("/riskBlacklist")
public class RiskBlacklistController extends BaseNoModelController {

    @Autowired
    private RiskBlacklistService riskBlacklistService;

    @GetMapping("/index")
    public String index() {
        return "/riskBlacklist/list";
    }

    @GetMapping("/simpleForm")
    public String blacklistForm(HttpServletRequest request, String userId) {
        request.setAttribute("userId", userId);
        return "/riskBlacklist/simple_form";
    }

    @GetMapping("/batchForm")
    public String blacklistBatchForm(HttpServletRequest request, String[] userId) {
        request.setAttribute("userIds", userId);
        return "/riskBlacklist/batch_form";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<Object> pageVO(Page<RiskBlacklistVO> page, @RequestParam Map<String, Object> map) {
        page.getOrders().clear();
        page.getOrders().add(OrderItem.desc("rb.createTime"));
        page = riskBlacklistService.page(page, map);
        return ResultGenerator.genSuccessResult("查询成功", page);
    }

    @PostMapping("/toBlackList")
    @ResponseBody
    public Result<Object> toBlackList(@RequestBody RiskBlacklistDTO riskBlacklistDTO) {
        riskBlacklistService.addData(riskBlacklistDTO);
        return ResultGenerator.genSuccessResult("操作成功, 关闭" + riskBlacklistDTO.getCount() + "个商户", null);
    }

    @PostMapping("/batchToBlackList")
    @ResponseBody
    public Result<Object> batchToBlackList(@RequestBody RiskBlacklistDTO riskBlacklistDTO) {
        riskBlacklistService.batchAddData(riskBlacklistDTO);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }

    @PostMapping("/relieve")
    @ResponseBody
    public Result<Object> relieve(Long userId) {
        final ShiroUser shiroUser = queryCurrentShiroUser();
        riskBlacklistService.relieve(shiroUser.getId(), userId);
        return ResultGenerator.genSuccessResult("操作成功", null);
    }
}
