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

import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenanceDetailsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenanceDetailsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintenance.vo.IotMaintenanceDetailsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.maintenance.IotMaintenanceDetailsDO;
import cn.iocoder.yudao.module.pms.service.maintenance.IotMaintenanceDetailsService;
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 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.*;


@Tag(name = "管理后台 - 保养计划明细")
@RestController
@RequestMapping("/rq/iot-maintenance-details")
@Validated
public class IotMaintenanceDetailsController {

    @Resource
    private IotMaintenanceDetailsService iotMaintenanceDetailsService;

    @PostMapping("/create")
    @Operation(summary = "创建保养计划明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintenance-details:create')")
    public CommonResult<Long> createIotMaintenanceDetails(@Valid @RequestBody IotMaintenanceDetailsSaveReqVO createReqVO) {
        return success(iotMaintenanceDetailsService.createIotMaintenanceDetails(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新保养计划明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintenance-details:update')")
    public CommonResult<Boolean> updateIotMaintenanceDetails(@Valid @RequestBody IotMaintenanceDetailsSaveReqVO updateReqVO) {
        iotMaintenanceDetailsService.updateIotMaintenanceDetails(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除保养计划明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-maintenance-details:delete')")
    public CommonResult<Boolean> deleteIotMaintenanceDetails(@RequestParam("id") Long id) {
        iotMaintenanceDetailsService.deleteIotMaintenanceDetails(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得保养计划明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintenance-details:query')")
    public CommonResult<IotMaintenanceDetailsRespVO> getIotMaintenanceDetails(@RequestParam("id") Long id) {
        IotMaintenanceDetailsDO iotMaintenanceDetails = iotMaintenanceDetailsService.getIotMaintenanceDetails(id);
        return success(BeanUtils.toBean(iotMaintenanceDetails, IotMaintenanceDetailsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得保养计划明细分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintenance-details:query')")
    public CommonResult<PageResult<IotMaintenanceDetailsRespVO>> getIotMaintenanceDetailsPage(@Valid IotMaintenanceDetailsPageReqVO pageReqVO) {
        PageResult<IotMaintenanceDetailsDO> pageResult = iotMaintenanceDetailsService.getIotMaintenanceDetailsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMaintenanceDetailsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出保养计划明细 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-maintenance-details:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMaintenanceDetailsExcel(@Valid IotMaintenanceDetailsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMaintenanceDetailsDO> list = iotMaintenanceDetailsService.getIotMaintenanceDetailsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "保养计划明细.xls", "数据", IotMaintenanceDetailsRespVO.class,
                        BeanUtils.toBean(list, IotMaintenanceDetailsRespVO.class));
    }

}
