package com.bizzan.bitrade.controller.activity;

import com.bizzan.bitrade.annotation.AccessLog;
import com.bizzan.bitrade.constant.AdminModule;
import com.bizzan.bitrade.controller.common.BaseAdminController;
import com.bizzan.bitrade.entity.Admin;
import com.bizzan.bitrade.service.ExperienceGiftRiskAlertService;
import com.bizzan.bitrade.util.MessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

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

/**
 * 体验金防刷风控接口
 */
@RestController
@RequestMapping("activity/experience-gift/risk-alert")
public class ExperienceGiftRiskAlertController extends BaseAdminController {

    @Autowired
    private ExperienceGiftRiskAlertService riskAlertService;

    /**
     * 分页查询预警列表
     */
    @PostMapping("page-query")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "体验金防刷预警列表")
    public MessageResult pageQuery(
            @RequestParam(required = false) Long memberId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "15") int pageSize) {

        Page<Map> page = riskAlertService.pageQuery(memberId, startTime, endTime, status, pageNo, pageSize);
        return success(page);
    }

    /**
     * 取消单条告警
     */
    @PostMapping("cancel/{id}")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "取消体验金预警")
    public MessageResult cancel(@PathVariable Long id, HttpServletRequest request) {
        Admin admin = getAdmin(request);
        if (admin == null) return error("请先登录");
        boolean ok = riskAlertService.cancel(id, admin.getId());
        return ok ? success("已取消告警") : error("取消失败，记录不存在或已处理");
    }

    /**
     * 一键取消所有告警
     */
    @PostMapping("cancel-all")
    @AccessLog(module = AdminModule.ACTIVITY, operation = "一键取消所有体验金预警")
    public MessageResult cancelAll(HttpServletRequest request) {
        Admin admin = getAdmin(request);
        if (admin == null) return error("请先登录");
        int count = riskAlertService.cancelAll(admin.getId());
        return success("已取消 " + count + " 条告警");
    }
}
