package cn.iocoder.yudao.module.pms.controller.admin.iotmainplanbommaterial;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainplanbommaterial.vo.IotMainPlanBomMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainplanbommaterial.vo.IotMainPlanBomMaterialRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainplanbommaterial.vo.IotMainPlanBomMaterialSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainplanbommaterial.IotMainPlanBomMaterialDO;
import cn.iocoder.yudao.module.pms.service.iotmainplanbommaterial.IotMainPlanBomMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - PMS 保养计划明细设备BOM挂载物料关联")
@RestController
@RequestMapping("/pms/iot-main-plan-bom-material")
@Validated
public class IotMainPlanBomMaterialController {

    @Resource
    private IotMainPlanBomMaterialService iotMainPlanBomMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS 保养计划明细设备BOM挂载物料关联")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-plan-bom-material:create')")
    public CommonResult<Long> createIotMainPlanBomMaterial(@Valid @RequestBody IotMainPlanBomMaterialSaveReqVO createReqVO) {
        return success(iotMainPlanBomMaterialService.createIotMainPlanBomMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS 保养计划明细设备BOM挂载物料关联")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-plan-bom-material:update')")
    public CommonResult<Boolean> updateIotMainPlanBomMaterial(@Valid @RequestBody IotMainPlanBomMaterialSaveReqVO updateReqVO) {
        iotMainPlanBomMaterialService.updateIotMainPlanBomMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS 保养计划明细设备BOM挂载物料关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-main-plan-bom-material:delete')")
    public CommonResult<Boolean> deleteIotMainPlanBomMaterial(@RequestParam("id") Long id) {
        iotMainPlanBomMaterialService.deleteIotMainPlanBomMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS 保养计划明细设备BOM挂载物料关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-plan-bom-material:query')")
    public CommonResult<IotMainPlanBomMaterialRespVO> getIotMainPlanBomMaterial(@RequestParam("id") Long id) {
        IotMainPlanBomMaterialDO iotMainPlanBomMaterial = iotMainPlanBomMaterialService.getIotMainPlanBomMaterial(id);
        return success(BeanUtils.toBean(iotMainPlanBomMaterial, IotMainPlanBomMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS 保养计划明细设备BOM挂载物料关联分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-plan-bom-material:query')")
    public CommonResult<PageResult<IotMainPlanBomMaterialRespVO>> getIotMainPlanBomMaterialPage(@Valid IotMainPlanBomMaterialPageReqVO pageReqVO) {
        PageResult<IotMainPlanBomMaterialDO> pageResult = iotMainPlanBomMaterialService.getIotMainPlanBomMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMainPlanBomMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS 保养计划明细设备BOM挂载物料关联 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-plan-bom-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMainPlanBomMaterialExcel(@Valid IotMainPlanBomMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMainPlanBomMaterialDO> list = iotMainPlanBomMaterialService.getIotMainPlanBomMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS 保养计划明细设备BOM挂载物料关联.xls", "数据", IotMainPlanBomMaterialRespVO.class,
                        BeanUtils.toBean(list, IotMainPlanBomMaterialRespVO.class));
    }

}