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.MaintenanceImportVo;
import org.eco.als.domain.vo.MaintenanceVo;
import org.eco.als.domain.bo.MaintenanceBo;
import org.eco.als.service.IMaintenanceService;
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-10-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/maintenance")
public class MaintenanceController extends BaseController {
    @Resource
    private IMaintenanceService maintenanceService;

    /**
     * 查询维修信息列表
     */
    @SaCheckPermission("als:maintenance:list")
    @GetMapping("/list")
    public CommonResult<PageResult<MaintenanceVo>> list(MaintenanceBo maintenanceBo) {
        return CommonResult.success(maintenanceService.selectPage(maintenanceBo));
    }

    /**
     * 导出维修信息列表
     */
    @SaCheckPermission("als:maintenance:export")
    @Log(title = "维修信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(MaintenanceBo maintenanceBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<MaintenanceVo> list = maintenanceService.selectList(maintenanceBo);
        if (CollUtil.isEmpty(list)) {
            return CommonResult.fail("导出列表为空");
        }
        maintenanceService.asyncExport(list, "维修信息", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:maintenance:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维修信息", MaintenanceImportVo.class, response);
    }

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

    /**
     * 获取维修信息详细信息
     */
    @SaCheckPermission("als:maintenance:query")
    @GetMapping(value = "/{id}")
    public CommonResult<MaintenanceVo> getInfo(@PathVariable Long id) {
        return CommonResult.success(maintenanceService.selectById(id));
    }

    /**
     * 新增维修信息
     */
    @SaCheckPermission("als:maintenance:add")
    @Log(title = "维修信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody MaintenanceBo maintenanceBo) {
        boolean inserted = maintenanceService.insert(maintenanceBo);
        if (!inserted) {
            return CommonResult.fail("新增维修信息记录失败！");
        }
        return CommonResult.success();
    }

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

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