package org.eco.als.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.eco.als.domain.bo.MaintenanceRecordBo;
import org.eco.als.domain.vo.MaintenanceRecordImportVo;
import org.eco.als.domain.vo.MaintenanceRecordVo;
import org.eco.als.service.IMaintenanceRecordService;
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-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/als/maintenanceRecord")
public class MaintenanceRecordController extends BaseController {
    @Resource
    private IMaintenanceRecordService maintenanceRecordService;

    /**
     * 查询维修记录列表
     */
    @SaCheckPermission("als:maintenanceRecord:list")
    @GetMapping("/list")
    public CommonResult<PageResult<MaintenanceRecordVo>> list(MaintenanceRecordBo maintenanceRecordBo) {
        return CommonResult.success(maintenanceRecordService.selectPage(maintenanceRecordBo));
    }

    /**
     * 导出维修记录列表
     */
    @SaCheckPermission("als:maintenanceRecord:export")
    @Log(title = "维修记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public CommonResult<Void> export(HttpServletResponse response, MaintenanceRecordBo maintenanceRecordBo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<MaintenanceRecordVo> list = maintenanceRecordService.selectList(maintenanceRecordBo);
        maintenanceRecordService.asyncExport(list, "维修记录", loginUser);
        return CommonResult.success();
    }

    @SaCheckPermission("als:maintenanceRecord:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维修记录", MaintenanceRecordImportVo.class, response);
    }

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

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

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

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

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