package com.maintain.business.controller;

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

import com.maintain.business.domain.vo.*;
import com.maintain.business.listener.ErpMaintainCauseImportListener;
import com.maintain.common.excel.ExcelResult;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.maintain.common.annotation.RepeatSubmit;
import com.maintain.common.annotation.Log;
import com.maintain.common.core.controller.BaseController;
import com.maintain.common.core.domain.PageQuery;
import com.maintain.common.core.domain.R;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.utils.poi.ExcelUtil;
import com.maintain.business.domain.bo.ErpMaintainCauseBo;
import com.maintain.business.service.IErpMaintainCauseService;
import com.maintain.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 维修原因
 *
 * @author liushuai
 * @date 2025-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/maintainCause")
public class ErpMaintainCauseController extends BaseController {

    private final IErpMaintainCauseService iErpMaintainCauseService;

    /**
     * 查询维修原因列表
     */
    @SaCheckPermission("business:maintainCause:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMaintainCauseVo> list(ErpMaintainCauseBo bo, PageQuery pageQuery) {
        return iErpMaintainCauseService.queryPageList(bo, pageQuery);
    }

    /**
     * 维修原因导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpMaintainCauseBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维修原因模板", ErpMaintainCauseImportVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "检测项目导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<ErpMaintainCauseImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpMaintainCauseImportVo.class, new ErpMaintainCauseImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 导出维修原因列表
     */
    @SaCheckPermission("business:maintainCause:export")
    @Log(title = "维修原因", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMaintainCauseBo bo, HttpServletResponse response) {
        List<ErpMaintainCauseVo> list = iErpMaintainCauseService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修原因", ErpMaintainCauseVo.class, response);
    }

    /**
     * 获取维修原因详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:maintainCause:query")
    @GetMapping("/{id}")
    public R<ErpMaintainCauseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpMaintainCauseService.queryById(id));
    }

    /**
     * 新增维修原因
     */
    @SaCheckPermission("business:maintainCause:add")
    @Log(title = "维修原因", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpMaintainCauseBo bo) {
        return toAjax(iErpMaintainCauseService.insertByBo(bo));
    }

    /**
     * 修改维修原因
     */
    @SaCheckPermission("business:maintainCause:edit")
    @Log(title = "维修原因", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpMaintainCauseBo bo) {
        return toAjax(iErpMaintainCauseService.updateByBo(bo));
    }

    /**
     * 删除维修原因
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:maintainCause:remove")
    @Log(title = "维修原因", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpMaintainCauseService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
