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

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.iotmainworkorderdetails.vo.IotMainWorkOrderDetailsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderdetails.vo.IotMainWorkOrderDetailsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderdetails.vo.IotMainWorkOrderDetailsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainworkorderdetails.IotMainWorkOrderDetailsDO;
import cn.iocoder.yudao.module.pms.service.iotmainworkorderdetails.IotMainWorkOrderDetailsService;
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 = "管理后台 - 保养工单明细")
@RestController
@RequestMapping("/pms/iot-main-work-order-details")
@Validated
public class IotMainWorkOrderDetailsController {

    @Resource
    private IotMainWorkOrderDetailsService iotMainWorkOrderDetailsService;

    @PostMapping("/create")
    @Operation(summary = "创建保养工单明细")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-details:create')")
    public CommonResult<Long> createIotMainWorkOrderDetails(@Valid @RequestBody IotMainWorkOrderDetailsSaveReqVO createReqVO) {
        return success(iotMainWorkOrderDetailsService.createIotMainWorkOrderDetails(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新保养工单明细")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-details:update')")
    public CommonResult<Boolean> updateIotMainWorkOrderDetails(@Valid @RequestBody IotMainWorkOrderDetailsSaveReqVO updateReqVO) {
        iotMainWorkOrderDetailsService.updateIotMainWorkOrderDetails(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除保养工单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-details:delete')")
    public CommonResult<Boolean> deleteIotMainWorkOrderDetails(@RequestParam("id") Long id) {
        iotMainWorkOrderDetailsService.deleteIotMainWorkOrderDetails(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得保养工单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-details:query')")
    public CommonResult<IotMainWorkOrderDetailsRespVO> getIotMainWorkOrderDetails(@RequestParam("id") Long id) {
        IotMainWorkOrderDetailsDO iotMainWorkOrderDetails = iotMainWorkOrderDetailsService.getIotMainWorkOrderDetails(id);
        return success(BeanUtils.toBean(iotMainWorkOrderDetails, IotMainWorkOrderDetailsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得保养工单明细分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-details:query')")
    public CommonResult<PageResult<IotMainWorkOrderDetailsRespVO>> getIotMainWorkOrderDetailsPage(@Valid IotMainWorkOrderDetailsPageReqVO pageReqVO) {
        PageResult<IotMainWorkOrderDetailsDO> pageResult = iotMainWorkOrderDetailsService.getIotMainWorkOrderDetailsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMainWorkOrderDetailsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出保养工单明细 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order-details:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMainWorkOrderDetailsExcel(@Valid IotMainWorkOrderDetailsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMainWorkOrderDetailsDO> list = iotMainWorkOrderDetailsService.getIotMainWorkOrderDetailsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "保养工单明细.xls", "数据", IotMainWorkOrderDetailsRespVO.class,
                        BeanUtils.toBean(list, IotMainWorkOrderDetailsRespVO.class));
    }

}