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.eco.als.domain.vo.*;
import org.eco.als.listener.AirConfigurationImportListener;
import org.eco.als.listener.FaultStatisticsImportListener;
import org.eco.common.excel.core.ExcelResult;
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.bo.FaultStatisticsBo;
import org.eco.als.service.IFaultStatisticsService;
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-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/faultStatistics")
public class FaultStatisticsController extends BaseController {
    @Resource
    private IFaultStatisticsService faultStatisticsService;

    /**
     * 查询故障统计列表
     */
    @SaCheckPermission("als:faultStatistics:list")
    @GetMapping("/list")
    public CommonResult<PageResult<FaultStatisticsVo>> list(FaultStatisticsBo faultStatisticsBo) {
        return CommonResult.success(faultStatisticsService.selectPage(faultStatisticsBo));
    }

    /**
     * 查询故障统计列表
     */
    @SaCheckPermission("als:faultStatistics:list")
    @GetMapping("/listAll")
    public CommonResult<List<FaultStatisticsVo>> listAll(FaultStatisticsBo faultStatisticsBo) {
        return CommonResult.success(faultStatisticsService.selectList(faultStatisticsBo));
    }

    /**
     * 外部查询故障统计列表
     */
    @GetMapping("/outsideListAll")
    public CommonResult<List<FaultStatisticsWbVo>> outsideListAll(FaultStatisticsBo faultStatisticsBo) {
        return CommonResult.success(faultStatisticsService.outsideListAll(faultStatisticsBo));
    }

    /**
     * 导出故障统计列表
     */
//    @SaCheckPermission("als:faultStatistics:export")
    @Log(title = "故障统计", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, FaultStatisticsBo faultStatisticsBo) {
        List<FaultStatisticsVo> list = faultStatisticsService.selectList(faultStatisticsBo);
        ExcelUtil.exportExcel(list, "故障统计", FaultStatisticsVo.class, response);
    }

    @SaCheckPermission("als:faultStatistics:import")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "故障统计", FaultStatisticsImportVo.class, response);
    }

    /**
     * 导入故障统计列表
     */
    @Log(title = "故障统计", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        LoginUser loginUser = LoginHelper.getLoginUser();
        ExcelResult<FaultStatisticsImportVo> result = ExcelUtil.importExcel(file.getInputStream(), FaultStatisticsImportVo.class, new FaultStatisticsImportListener(updateSupport, loginUser));
        return CommonResult.success(result.getAnalysis().getLogInfo());
    }

    /**
     * 获取故障统计详细信息
     */
    @SaCheckPermission("als:faultStatistics:query")
    @GetMapping(value = "/{id}")
    public CommonResult<FaultStatisticsVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(faultStatisticsService.selectById(id));
    }

    /**
     * 新增故障统计
     */
    @SaCheckPermission("als:faultStatistics:add")
    @Log(title = "故障统计", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody FaultStatisticsBo faultStatisticsBo) {
        boolean inserted = faultStatisticsService.insert(faultStatisticsBo);
        if (!inserted) {
            return CommonResult.fail("新增故障统计记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改故障统计
     */
    @SaCheckPermission("als:faultStatistics:edit")
    @Log(title = "故障统计", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody FaultStatisticsBo faultStatisticsBo) {
        boolean updated = faultStatisticsService.update(faultStatisticsBo);
        if (!updated) {
            return CommonResult.fail("修改故障统计记录失败!");
        }
        return CommonResult.success();
    }

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