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.AirInstallImportVo;
import org.eco.als.domain.vo.AirInstallVo;
import org.eco.als.domain.bo.AirInstallBo;
import org.eco.als.service.IAirInstallService;
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-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/airInstall")
public class AirInstallController extends BaseController {
    @Resource
    private IAirInstallService airInstallService;

    /**
     * 查询装机信息列表
     */
    @SaCheckPermission("als:airInstall:list")
    @GetMapping("/list")
    public CommonResult<PageResult<AirInstallVo>> list(AirInstallBo airInstallBo) {
        return CommonResult.success(airInstallService.selectPage(airInstallBo));
    }
    /**
     * 查询装机信息列表不分页select
     */
    @SaCheckPermission("als:airInstall:list")
    @GetMapping("/select/list")
    public CommonResult<List<AirInstallVo>> selectList(AirInstallBo airInstallBo) {
        return CommonResult.success(airInstallService.selectList(airInstallBo));
    }

    /**
     * 导出装机信息列表
     */
    @SaCheckPermission("als:airInstall:export")
    @Log(title = "装机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(AirInstallBo airInstallBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<AirInstallVo> list = airInstallService.selectList(airInstallBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        airInstallService.asyncExport(list, "装机信息", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:airInstall:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "装机信息", AirInstallImportVo.class, response);
    }

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

    /**
     * 获取装机信息详细信息
     */
    @SaCheckPermission("als:airInstall:query")
    @GetMapping(value = "/{id}")
    public CommonResult<AirInstallVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(airInstallService.selectById(id));
    }

    /**
     * 新增装机信息
     */
    @SaCheckPermission("als:airInstall:add")
    @Log(title = "装机信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody AirInstallBo airInstallBo) {
        boolean inserted = airInstallService.insert(airInstallBo);
        if (!inserted) {
            return CommonResult.fail("新增装机信息记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改装机信息
     */
    @SaCheckPermission("als:airInstall:edit")
    @Log(title = "装机信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody AirInstallBo airInstallBo) {
        boolean updated = airInstallService.update(airInstallBo);
        if (!updated) {
            return CommonResult.fail("修改装机信息记录失败!");
        }
        return CommonResult.success();
    }

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