package cn.iocoder.yudao.module.mes.controller.admin.moldrepairtems;

import cn.iocoder.yudao.module.mes.dal.dataobject.criticalcomponent.CriticalComponentDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mes.controller.admin.moldrepairtems.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrepairtems.MoldRepairTemsDO;
import cn.iocoder.yudao.module.mes.service.moldrepairtems.MoldRepairTemsService;

@Tag(name = "管理后台 - 维修项目")
@RestController
@RequestMapping("/mes/mold-repair-tems")
@Validated
public class MoldRepairTemsController {

    @Resource
    private MoldRepairTemsService moldrepairTemsService;

    @PostMapping("/create")
    @Operation(summary = "创建维修项目")
    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:create')")
    public CommonResult<Long> createMoldRepairTems(@Valid @RequestBody MoldRepairTemsSaveReqVO createReqVO) {
        return success(moldrepairTemsService.createMoldRepairTems(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维修项目")
    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:update')")
    public CommonResult<Boolean> updateMoldRepairTems(@Valid @RequestBody MoldRepairTemsSaveReqVO updateReqVO) {
        moldrepairTemsService.updateMoldRepairTems(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维修项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:delete')")
    public CommonResult<Boolean> deleteMoldRepairTems(@RequestParam("ids") String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(String::trim) // 去除可能存在的空格
                .map(Long::valueOf)
                .collect(Collectors.toList());
        moldrepairTemsService.deleteMoldRepairTems(idList);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维修项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:query')")
    public CommonResult<MoldRepairTemsRespVO> getMoldRepairTems(@RequestParam("id") Long id) {
        MoldRepairTemsDO moldrepairTems = moldrepairTemsService.getMoldRepairTems(id);
        return success(BeanUtils.toBean(moldrepairTems, MoldRepairTemsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维修项目分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:query')")
    public CommonResult<PageResult<MoldRepairTemsRespVO>> getMoldRepairTemsPage(@Valid MoldRepairTemsPageReqVO pageReqVO) {
        PageResult<MoldRepairTemsRespVO> pageResult = moldrepairTemsService.getMoldRepairTemsPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维修项目 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldRepairTemsExcel(@Valid MoldRepairTemsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldRepairTemsRespVO> list = moldrepairTemsService.getMoldRepairTemsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维修项目.xls", "数据", MoldRepairTemsRespVO.class,list);
    }



//    @GetMapping("/getMoldOrComponent")
//    @Operation(summary = "获得设备/关键件")
//    @Parameter(name = "moldType", description = "moldType 1-设备 2-关键件", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:query')")
//    public CommonResult<List<MoldRepairTemsRespVO>> getMoldOrComponent(@RequestParam("moldType") Integer moldType) {
//        return success( moldrepairTemsService.getMoldOrComponent(moldType));
//    }
//
//    @GetMapping("/getMoldOrComponentList")
//    @Operation(summary = "获得设备/关键件列表")
////    @Parameter(name = "moldType", description = "moldType 1-设备 2-关键件", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:query')")
//    public CommonResult<List<MoldRepairTemsRespVO>> getMoldOrComponentList(
//            @Parameter(name = "moldId", description = "设备Id", required = true, example = "123")
//            @RequestParam("moldId") Long moldId,
//            @Parameter(name = "componentId", description = "关键件Id", required = false, example = "123")
//            @RequestParam(value = "componentId",required = false) Long componentId) {
//        return success(moldrepairTemsService.getMoldOrComponentList(moldId,componentId));
//    }
//
//
//    @GetMapping("/getComponentList")
//    @Operation(summary = "获得设备关键件")
//    @Parameter(name = "moldId", description = "moldId设备Id", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('mes:mold-repair-tems:query')")
//    public CommonResult<List<CriticalComponentDO>> getComponentList(@RequestParam("moldId") Long moldId) {
//        return success( moldrepairTemsService.getComponentList(moldId));
//    }


}