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

import cn.iocoder.yudao.module.mes.dal.dataobject.criticalcomponent.CriticalComponentDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.deviceledger.DeviceLedgerDO;
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.repairtems.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.repairtems.RepairTemsDO;
import cn.iocoder.yudao.module.mes.service.repairtems.RepairTemsService;

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

    @Resource
    private RepairTemsService repairTemsService;

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

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

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

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

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

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



    @GetMapping("/getDeviceOrComponent")
    @Operation(summary = "获得设备/关键件")
    @Parameter(name = "deviceType", description = "deviceType 1-设备 2-关键件", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:repair-tems:query')")
    public CommonResult<List<RepairTemsRespVO>> getDeviceOrComponent(@RequestParam("deviceType") Integer deviceType) {
        return success( repairTemsService.getDeviceOrComponent(deviceType));
    }

    @GetMapping("/getDeviceOrComponentList")
    @Operation(summary = "获得设备/关键件列表")
//    @Parameter(name = "deviceType", description = "deviceType 1-设备 2-关键件", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:repair-tems:query')")
    public CommonResult<List<RepairTemsRespVO>> getDeviceOrComponentList(
            @Parameter(name = "deviceId", description = "设备Id", required = true, example = "123")
            @RequestParam("deviceId") Long deviceId,
            @Parameter(name = "componentId", description = "关键件Id", required = false, example = "123")
            @RequestParam(value = "componentId",required = false) Long componentId) {
        return success(repairTemsService.getDeviceOrComponentList(deviceId,componentId));
    }


    @GetMapping("/getComponentList")
    @Operation(summary = "获得设备关键件")
    @Parameter(name = "deviceId", description = "deviceId设备Id", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:repair-tems:query')")
    public CommonResult<List<CriticalComponentDO>> getComponentList(@RequestParam("deviceId") Long deviceId) {
        return success( repairTemsService.getComponentList(deviceId));
    }


}