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.FlightTaskBo;
import org.eco.als.domain.vo.FlightTaskImportVo;
import org.eco.als.domain.vo.FlightTaskVo;
import org.eco.als.service.IFlightTaskService;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 飞行任务Controller
 *
 * @author wgk
 * @date 2024-07-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/flightTask")
public class FlightTaskController extends BaseController {
    @Resource
    private IFlightTaskService flightTaskService;

    /**
     * 查询飞行任务列表
     */
    @SaCheckPermission("als:flightTask:list")
    @GetMapping("/list")
    public CommonResult<PageResult<FlightTaskVo>> list(FlightTaskBo flightTaskBo) {
        return CommonResult.success(flightTaskService.selectPage(flightTaskBo));
    }

    /**
     * 导出飞行任务列表
     */
    @SaCheckPermission("als:flightTask:export")
    @Log(title = "飞行任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(FlightTaskBo flightTaskBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<FlightTaskVo> list = flightTaskService.selectList(flightTaskBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        flightTaskService.asyncExport(list, "飞行任务", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:flightTask:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "飞行任务", FlightTaskImportVo.class, response);
    }

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

    /**
     * 获取飞行任务详细信息
     */
    @SaCheckPermission("als:flightTask:query")
    @GetMapping(value = "/{id}")
    public CommonResult<FlightTaskVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(flightTaskService.selectById(id));
    }

    /**
     * 新增飞行任务
     */
    @SaCheckPermission("als:flightTask:add")
    @Log(title = "飞行任务", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody FlightTaskBo flightTaskBo) {
        boolean inserted = flightTaskService.insert(flightTaskBo);
        if (!inserted) {
            return CommonResult.fail("新增飞行任务记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改飞行任务
     */
    @SaCheckPermission("als:flightTask:edit")
    @Log(title = "飞行任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody FlightTaskBo flightTaskBo) {
        boolean updated = flightTaskService.update(flightTaskBo);
        if (!updated) {
            return CommonResult.fail("修改飞行任务记录失败!");
        }
        return CommonResult.success();
    }

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