package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.WarningBo;
import org.eco.als.domain.vo.WarningImportVo;
import org.eco.als.domain.vo.WarningVo;
import org.eco.als.service.IWarningService;
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.excel.utils.ExcelUtil;
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 org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 警告信息Controller
 *
 * @author wgk
 * @date 2024-07-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/warning")
public class WarningController extends BaseController {
    @Resource
    private IWarningService warningService;

    /**
     * 查询警告信息列表
     */
    @SaCheckPermission("als:warning:list")
    @GetMapping("/list")
    public CommonResult<PageResult<WarningVo>> list(WarningBo warningBo) {
        return CommonResult.success(warningService.selectPage(warningBo));
    }

    /**
     * 导出警告信息列表
     */
    @SaCheckPermission("als:warning:export")
    @Log(title = "警告信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(HttpServletResponse response, WarningBo warningBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<WarningVo> list = warningService.selectList(warningBo);
        warningService.asyncExport(list, "警告信息", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:warning:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "警告信息", WarningImportVo.class, response);
    }

    /**
     * 导入警告信息列表
     */
    @Log(title = "警告信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:warning:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        warningService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取警告信息详细信息
     */
    @SaCheckPermission("als:warning:query")
    @GetMapping(value = "/{id}")
    public CommonResult<WarningVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(warningService.selectById(id));
    }

    /**
     * 新增警告信息
     */
    @SaCheckPermission("als:warning:add")
    @Log(title = "警告信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody WarningBo warningBo) {
        boolean inserted = warningService.insert(warningBo);
        if (!inserted) {
            return CommonResult.fail("新增警告信息记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改警告信息
     */
    @SaCheckPermission("als:warning:edit")
    @Log(title = "警告信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody WarningBo warningBo) {
        boolean updated = warningService.update(warningBo);
        if (!updated) {
            return CommonResult.fail("修改警告信息记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除警告信息
     */
    @SaCheckPermission("als:warning:remove")
    @Log(title = "警告信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = warningService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除警告信息记录失败!");
        }
        return CommonResult.success();
    }
}
