package org.dromara.payment.earlyWarn.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.payment.earlyWarn.domain.bo.LgEarlyWarnBo;
import org.dromara.payment.earlyWarn.domain.vo.LgEarlyWarnVo;
import org.dromara.payment.earlyWarn.service.ILgEarlyWarnService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 风控预警设置
 * 前端访问路由地址为:/system/earlyWarn
 *
 * @author LionLi
 * @date 2025-06-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/earlyWarn")
public class LgEarlyWarnController extends BaseController {

    private final ILgEarlyWarnService lgEarlyWarnService;

    /**
     * 查询风控预警设置列表
     */
    @SaCheckPermission("system:earlyWarn:list")
    @GetMapping("/list")
    public TableDataInfo<LgEarlyWarnVo> list(LgEarlyWarnBo bo, PageQuery pageQuery) {
        return lgEarlyWarnService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出风控预警设置列表
     */
    @SaCheckPermission("system:earlyWarn:export")
    @Log(title = "风控预警设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LgEarlyWarnBo bo, HttpServletResponse response) {
        List<LgEarlyWarnVo> list = lgEarlyWarnService.queryList(bo);
        ExcelUtil.exportExcel(list, "风控预警设置", LgEarlyWarnVo.class, response);
    }

    /**
     * 获取风控预警设置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:earlyWarn:query")
    @GetMapping("/{id}")
    public R<LgEarlyWarnVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(lgEarlyWarnService.queryById(id));
    }

    /**
     * 新增风控预警设置
     */
    @SaCheckPermission("system:earlyWarn:add")
    @Log(title = "风控预警设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LgEarlyWarnBo bo) {
        return toAjax(lgEarlyWarnService.insertByBo(bo));
    }

    /**
     * 修改风控预警设置
     */
    @SaCheckPermission("system:earlyWarn:edit")
    @Log(title = "风控预警设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LgEarlyWarnBo bo) {
        return toAjax(lgEarlyWarnService.updateByBo(bo));
    }

    /**
     * 删除风控预警设置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:earlyWarn:remove")
    @Log(title = "风控预警设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(lgEarlyWarnService.deleteWithValidByIds(List.of(ids), true));
    }
}
