package com.zt.plat.module.qms.resource.device.controller.admin;

import com.zt.plat.module.qms.resource.device.controller.vo.DeviceUseRecordDetailPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceUseRecordDetailRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceUseRecordDetailSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceUseRecordDetailDO;
import com.zt.plat.module.qms.resource.device.service.DeviceUseRecordDetailService;

@Tag(name = "管理后台 - 设备-使用记录明细")
@RestController
@RequestMapping("/qms/resource/device-use-record-detail")
@Validated
@FileUploadController(source = "qms.deviceuserecorddetail")
public class DeviceUseRecordDetailController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = DeviceUseRecordDetailController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private DeviceUseRecordDetailService deviceUseRecordDetailService;

    @GetMapping("/getListByParentId")
    @Operation(summary = "获得设备-使用记录明细分页")
    public CommonResult<List<DeviceUseRecordDetailRespVO>> getListByParentId(Long parentId) {
        List<DeviceUseRecordDetailDO> list = deviceUseRecordDetailService.getListByParentId(parentId);
        return success(BeanUtils.toBean(list, DeviceUseRecordDetailRespVO.class));
    }

    @PostMapping("/create")
    @Operation(summary = "创建设备-使用记录明细")
    @PreAuthorize("@ss.hasPermission('qms:device-use-record-detail:create')")
    public CommonResult<DeviceUseRecordDetailRespVO> createDeviceUseRecordDetail(@Valid @RequestBody DeviceUseRecordDetailSaveReqVO createReqVO) {
        return success(deviceUseRecordDetailService.createDeviceUseRecordDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备-使用记录明细")
    @PreAuthorize("@ss.hasPermission('qms:device-use-record-detail:update')")
    public CommonResult<Boolean> updateDeviceUseRecordDetail(@Valid @RequestBody DeviceUseRecordDetailSaveReqVO updateReqVO) {
        deviceUseRecordDetailService.updateDeviceUseRecordDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备-使用记录明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:device-use-record-detail:delete')")
    public CommonResult<Boolean> deleteDeviceUseRecordDetail(@RequestParam("id") Long id) {
        deviceUseRecordDetailService.deleteDeviceUseRecordDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备-使用记录明细")
    @PreAuthorize("@ss.hasPermission('qms:device-use-record-detail:delete')")
    public CommonResult<Boolean> deleteDeviceUseRecordDetailList(@RequestBody BatchDeleteReqVO req) {
        deviceUseRecordDetailService.deleteDeviceUseRecordDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备-使用记录明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DeviceUseRecordDetailRespVO> getDeviceUseRecordDetail(@RequestParam("id") Long id) {
        DeviceUseRecordDetailDO deviceUseRecordDetail = deviceUseRecordDetailService.getDeviceUseRecordDetail(id);
        return success(BeanUtils.toBean(deviceUseRecordDetail, DeviceUseRecordDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备-使用记录明细分页")
    public CommonResult<PageResult<DeviceUseRecordDetailRespVO>> getDeviceUseRecordDetailPage(@Valid DeviceUseRecordDetailPageReqVO pageReqVO) {
        PageResult<DeviceUseRecordDetailDO> pageResult = deviceUseRecordDetailService.getDeviceUseRecordDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceUseRecordDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备-使用记录明细 Excel")
    @PreAuthorize("@ss.hasPermission('qms:device-use-record-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceUseRecordDetailExcel(@Valid DeviceUseRecordDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceUseRecordDetailDO> list = deviceUseRecordDetailService.getDeviceUseRecordDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备-使用记录明细.xls", "数据", DeviceUseRecordDetailRespVO.class,
                        BeanUtils.toBean(list, DeviceUseRecordDetailRespVO.class));
    }

}