package org.eco.als.controller;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.hutool.core.collection.CollUtil;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.eco.common.core.core.domain.CommonResult;
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.core.core.domain.model.LoginUser;
import org.eco.common.web.annotation.RepeatSubmit;
import org.eco.common.excel.utils.ExcelUtil;
import org.eco.common.web.core.BaseController;
import jakarta.annotation.Resource;
import org.eco.als.domain.vo.JudgeFaultLogicImportVo;
import org.eco.als.domain.vo.JudgeFaultLogicVo;
import org.eco.als.domain.bo.JudgeFaultLogicBo;
import org.eco.als.service.IJudgeFaultLogicService;
import org.springframework.web.multipart.MultipartFile;
    import org.eco.common.core.core.page.PageResult;

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

/**
 * 判故逻辑Controller
 *
 * @author wgk
 * @date 2024-12-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/judgeFaultLogic")
public class JudgeFaultLogicController extends BaseController {
    @Resource
    private IJudgeFaultLogicService judgeFaultLogicService;

/**
 * 查询判故逻辑列表
 */
@SaCheckPermission("als:judgeFaultLogic:list")
@GetMapping("/list")
    public CommonResult<PageResult<JudgeFaultLogicVo>> list(JudgeFaultLogicBo judgeFaultLogicBo) {
        return CommonResult.success(judgeFaultLogicService.selectPage(judgeFaultLogicBo));
    }

    /**
     * 导出判故逻辑列表
     */
    @SaCheckPermission("als:judgeFaultLogic:export")
    @Log(title = "判故逻辑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(JudgeFaultLogicBo judgeFaultLogicBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<JudgeFaultLogicVo> list = judgeFaultLogicService.selectList(judgeFaultLogicBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
            judgeFaultLogicService.asyncExport(list, "判故逻辑", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:judgeFaultLogic:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "判故逻辑", JudgeFaultLogicImportVo.class, response);
    }

    /**
     * 导入判故逻辑列表
     */
    @Log(title = "判故逻辑", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:judgeFaultLogic:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
            judgeFaultLogicService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取判故逻辑详细信息
     */
    @SaCheckPermission("als:judgeFaultLogic:query")
    @GetMapping(value = "/{id}")
    public CommonResult<JudgeFaultLogicVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(judgeFaultLogicService.selectById(id));
    }

    /**
     * 新增判故逻辑
     */
    @SaCheckPermission("als:judgeFaultLogic:add")
    @Log(title = "判故逻辑", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody JudgeFaultLogicBo judgeFaultLogicBo) {
        boolean inserted = judgeFaultLogicService.insert(judgeFaultLogicBo);
        if (!inserted) {
            return CommonResult.fail("新增判故逻辑记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改判故逻辑
     */
    @SaCheckPermission("als:judgeFaultLogic:edit")
    @Log(title = "判故逻辑", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody JudgeFaultLogicBo judgeFaultLogicBo) {
        boolean updated = judgeFaultLogicService.update(judgeFaultLogicBo);
        if (!updated) {
            return CommonResult.fail("修改判故逻辑记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除判故逻辑
     */
    @SaCheckPermission("als:judgeFaultLogic:remove")
    @Log(title = "判故逻辑", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = judgeFaultLogicService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除判故逻辑记录失败!");
        }
        return CommonResult.success();
    }
}
