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.FaultCaseImportVo;
import org.eco.als.domain.vo.FaultCaseVo;
import org.eco.als.domain.bo.FaultCaseBo;
import org.eco.als.service.IFaultCaseService;
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-11-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/faultCase")
public class FaultCaseController extends BaseController {
    @Resource
    private IFaultCaseService faultCaseService;

    /**
     * 查询故障案例列表
     */
    @SaCheckPermission("als:faultCase:list")
    @GetMapping("/list")
    public CommonResult<PageResult<FaultCaseVo>> list(FaultCaseBo faultCaseBo) {
        return CommonResult.success(faultCaseService.selectPage(faultCaseBo));
    }

    /**
     * 导出故障案例列表
     */
    @SaCheckPermission("als:faultCase:export")
    @Log(title = "故障案例", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(FaultCaseBo faultCaseBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<FaultCaseVo> list = faultCaseService.selectList(faultCaseBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        faultCaseService.asyncExport(list, "故障案例", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:faultCase:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "故障案例", FaultCaseImportVo.class, response);
    }

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

    /**
     * 获取故障案例详细信息
     */
    @SaCheckPermission("als:faultCase:query")
    @GetMapping(value = "/{id}")
    public CommonResult<FaultCaseVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(faultCaseService.selectById(id));
    }

    /**
     * 新增故障案例
     */
    @SaCheckPermission("als:faultCase:add")
    @Log(title = "故障案例", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody FaultCaseBo faultCaseBo) {
        boolean inserted = faultCaseService.insert(faultCaseBo);
        if (!inserted) {
            return CommonResult.fail("新增故障案例记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改故障案例
     */
    @SaCheckPermission("als:faultCase:edit")
    @Log(title = "故障案例", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody FaultCaseBo faultCaseBo) {
        boolean updated = faultCaseService.update(faultCaseBo);
        if (!updated) {
            return CommonResult.fail("修改故障案例记录失败!");
        }
        return CommonResult.success();
    }

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