package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.FalseAlarmResultBo;
import org.eco.als.domain.vo.FalseAlarmResultVo;
import org.eco.als.service.IFalseAlarmResultService;
import org.eco.common.core.core.domain.CommonResult;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.log.annotation.Log;
import org.eco.common.log.enums.BusinessType;
import org.eco.common.security.utils.LoginHelper;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 虚警抑制结果Controller
 *
 * @author wgk
 * @date 2024-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/falseAlarmResult")
public class FalseAlarmResultController extends BaseController {
    @Resource
    private IFalseAlarmResultService falseAlarmResultService;

    /**
     * 查询虚警抑制结果列表
     */
    @SaCheckPermission("als:falseAlarmResult:list")
    @GetMapping("/list")
    public CommonResult<PageResult<FalseAlarmResultVo>> list(FalseAlarmResultBo falseAlarmResultBo) {
        return CommonResult.success(falseAlarmResultService.selectPage(falseAlarmResultBo));
    }

    /**
     * 导出虚警抑制结果列表
     */
    @SaCheckPermission("als:falseAlarmResult:export")
    @Log(title = "虚警抑制结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(FalseAlarmResultBo falseAlarmResultBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<FalseAlarmResultVo> list = falseAlarmResultService.selectList(falseAlarmResultBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        falseAlarmResultService.asyncExport(list, "虚警抑制结果", loginUser);
        return CommonResult.success();
    }

    /**
     * 获取虚警抑制结果详细信息
     */
    @SaCheckPermission("als:falseAlarmResult:query")
    @GetMapping(value = "/{id}")
    public CommonResult<FalseAlarmResultVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(falseAlarmResultService.selectById(id));
    }

    /**
     * 新增虚警抑制结果
     */
    @SaCheckPermission("als:falseAlarmResult:add")
    @Log(title = "虚警抑制结果", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody FalseAlarmResultBo falseAlarmResultBo) {
        boolean inserted = falseAlarmResultService.insert(falseAlarmResultBo);
        if (!inserted) {
            return CommonResult.fail("新增虚警抑制结果记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改虚警抑制结果
     */
    @SaCheckPermission("als:falseAlarmResult:edit")
    @Log(title = "虚警抑制结果", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody FalseAlarmResultBo falseAlarmResultBo) {
        boolean updated = falseAlarmResultService.update(falseAlarmResultBo);
        if (!updated) {
            return CommonResult.fail("修改虚警抑制结果记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除虚警抑制结果
     */
    @SaCheckPermission("als:falseAlarmResult:remove")
    @Log(title = "虚警抑制结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = falseAlarmResultService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除虚警抑制结果记录失败!");
        }
        return CommonResult.success();
    }
}
