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.ErpMaintainProjectImportListener;
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.ErpMaintainProjectBo;
import com.maintain.business.service.IErpMaintainProjectService;
import com.maintain.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 维修项目
 *
 * @author liushuai
 * @date 2025-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/maintainProject")
public class ErpMaintainProjectController extends BaseController {

    private final IErpMaintainProjectService iErpMaintainProjectService;

    /**
     * 查询维修项目列表
     */
    @SaCheckPermission("business:maintainProject:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMaintainProjectVo> list(ErpMaintainProjectBo bo, PageQuery pageQuery) {
        return iErpMaintainProjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 维修项目导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpMaintainProjectBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维修项目模板", ErpMaintainProjectVo.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<ErpMaintainProjectVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpMaintainProjectVo.class, new ErpMaintainProjectImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 导出维修项目列表
     */
    @SaCheckPermission("business:maintainProject:export")
    @Log(title = "维修项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMaintainProjectBo bo, HttpServletResponse response) {
        List<ErpMaintainProjectVo> list = iErpMaintainProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修项目", ErpMaintainProjectVo.class, response);
    }

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

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

    /**
     * 修改维修项目
     */
    @SaCheckPermission("business:maintainProject:edit")
    @Log(title = "维修项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpMaintainProjectBo bo) {
        return toAjax(iErpMaintainProjectService.updateByBo(bo));
    }

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