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

/**
 * 故障项目
 *
 * @author liushuai
 * @date 2025-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/malfunctionProject")
public class ErpMalfunctionProjectController extends BaseController {

    private final IErpMalfunctionProjectService iErpMalfunctionProjectService;

    /**
     * 查询故障项目列表
     */
    @SaCheckPermission("business:malfunctionProject:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMalfunctionProjectVo> list(ErpMalfunctionProjectBo bo, PageQuery pageQuery) {
        return iErpMalfunctionProjectService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询故障项目列表
     */
    @GetMapping("/listAll")
    public R<List<ErpMalfunctionProjectVo>> listAll(ErpMalfunctionProjectBo bo) {
        return R.ok(iErpMalfunctionProjectService.queryList(bo));
    }

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

    /**
     * 导出故障项目列表
     */
    @SaCheckPermission("business:malfunctionProject:export")
    @Log(title = "故障项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMalfunctionProjectBo bo, HttpServletResponse response) {
        List<ErpMalfunctionProjectVo> list = iErpMalfunctionProjectService.queryList(bo);
        ExcelUtil.exportExcel(list, "故障项目", ErpMalfunctionProjectVo.class, response);
    }

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

    /**
     * 获取故障项目编号
     *
     */
    @SaCheckPermission("business:malfunctionProject:add")
    @GetMapping("/getMalfunctionCode")
    public R getMalfunctionCode() {
        int randomNum = (int)(Math.random() * 90000) + 10000;
        return R.ok("操作成功","GZ" + randomNum);
    }

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

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

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