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.AirConfigurationImportVo;
import org.eco.als.domain.vo.AirConfigurationVo;
import org.eco.als.domain.bo.AirConfigurationBo;
import org.eco.als.service.IAirConfigurationService;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 飞机构型Controller
 *
 * @author wgk
 * @date 2024-11-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/airConfiguration")
public class AirConfigurationController extends BaseController {
    @Resource
    private IAirConfigurationService airConfigurationService;

    /**
     * 查询飞机构型列表
     */
    @SaCheckPermission("als:airConfiguration:list")
    @GetMapping("/list")
    public CommonResult<List<AirConfigurationVo>> list(AirConfigurationBo airConfigurationBo) {
        List<AirConfigurationVo> list = airConfigurationService.selectList(airConfigurationBo);
        return CommonResult.success(list);
    }

    /**
     * 导出飞机构型列表
     */
    @SaCheckPermission("als:airConfiguration:export")
    @Log(title = "飞机构型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(AirConfigurationBo airConfigurationBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<AirConfigurationVo> list = airConfigurationService.selectList(airConfigurationBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        airConfigurationService.asyncExport(list, "飞机构型", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:airConfiguration:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "飞机构型", AirConfigurationImportVo.class, response);
    }

    /**
     * 导入飞机构型列表
     */
    @Log(title = "飞机构型", businessType = BusinessType.IMPORT)
    @SaCheckPermission("als:airConfiguration:import")
    @PostMapping("/importData")
    public CommonResult<Void> importData(MultipartFile file, boolean updateSupport) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        airConfigurationService.asyncImportData(file, updateSupport, loginUser);
        return CommonResult.success();
    }

    /**
     * 获取飞机构型详细信息
     */
    @SaCheckPermission("als:airConfiguration:query")
    @GetMapping(value = "/{id}")
    public CommonResult<AirConfigurationVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(airConfigurationService.selectById(id));
    }

    /**
     * 新增飞机构型
     */
    @SaCheckPermission("als:airConfiguration:add")
    @Log(title = "飞机构型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody AirConfigurationBo airConfigurationBo) {
        boolean inserted = airConfigurationService.insert(airConfigurationBo);
        if (!inserted) {
            return CommonResult.fail("新增飞机构型记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改飞机构型
     */
    @SaCheckPermission("als:airConfiguration:edit")
    @Log(title = "飞机构型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody AirConfigurationBo airConfigurationBo) {
        boolean updated = airConfigurationService.update(airConfigurationBo);
        if (!updated) {
            return CommonResult.fail("修改飞机构型记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除飞机构型
     */
    @SaCheckPermission("als:airConfiguration:remove")
    @Log(title = "飞机构型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult<Void> remove(@PathVariable Long[] ids) {
        boolean deleted = airConfigurationService.deleteByIds(ids);
        if (!deleted) {
            return CommonResult.fail("删除飞机构型记录失败!");
        }
        return CommonResult.success();
    }
}
