package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.AtlasFileBo;
import org.eco.als.domain.vo.AtlasFileImportVo;
import org.eco.als.domain.vo.AtlasFileVo;
import org.eco.als.service.IAtlasFileService;
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.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 文件管理Controller
 *
 * @author wgk
 * @date 2024-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/atlasFile")
public class AtlasFileController extends BaseController {
    @Resource
    private IAtlasFileService atlasFileService;

    /**
     * 查询文件管理列表
     */
    @SaCheckPermission("als:atlasFile:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AtlasFileVo>> list(AtlasFileBo atlasFileBo) {
        return CommonResult.success(atlasFileService.selectPage(atlasFileBo));
    }

    /**
     * 导出文件管理列表
     */
    @SaCheckPermission("als:atlasFile:export")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(AtlasFileBo atlasFileBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<AtlasFileVo> list = atlasFileService.selectList(atlasFileBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        atlasFileService.asyncExport(list, "文件管理", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:atlasFile:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "文件管理", AtlasFileImportVo.class, response);
    }

    /**
     * 导入文件管理列表
     */
    @Log(title = "文件管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:atlasFile:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        atlasFileService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取文件管理详细信息
     */
    @SaCheckPermission("als:atlasFile:query")
    @GetMapping(value = "/{id}")
    public CommonResult<AtlasFileVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(atlasFileService.selectById(id));
    }

    /**
     * 新增文件管理
     */
    @SaCheckPermission("als:atlasFile:add")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody AtlasFileBo atlasFileBo) {
        boolean inserted = atlasFileService.insert(atlasFileBo);
        if (!inserted) {
            return CommonResult.fail("新增文件管理记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改文件管理
     */
    @SaCheckPermission("als:atlasFile:edit")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody AtlasFileBo atlasFileBo) {
        boolean updated = atlasFileService.update(atlasFileBo);
        if (!updated) {
            return CommonResult.fail("修改文件管理记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除文件管理
     */
    @SaCheckPermission("als:atlasFile:remove")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = atlasFileService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除文件管理记录失败!");
        }
        return CommonResult.success();
    }
}
