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

import com.zt.plat.module.qms.resource.device.controller.vo.DeviceAffiliationRelativityPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceAffiliationRelativityRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceAffiliationRelativitySaveReqVO;
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 com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;

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.DeviceAffiliationRelativityDO;
import com.zt.plat.module.qms.resource.device.service.DeviceAffiliationRelativityService;

@Tag(name = "管理后台 - 附属设备关系")
@RestController
@RequestMapping("/qms/resource/device-affiliation-relativity")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.deviceaffiliationrelativity")
public class DeviceAffiliationRelativityController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private DeviceAffiliationRelativityService deviceAffiliationRelativityService;

    @PostMapping("/create")
    @Operation(summary = "创建附属设备关系")
    @PreAuthorize("@ss.hasPermission('qms:device-affiliation-relativity:create')")
    public CommonResult<DeviceAffiliationRelativityRespVO> createDeviceAffiliationRelativity(@Valid @RequestBody DeviceAffiliationRelativitySaveReqVO createReqVO) {
        return success(deviceAffiliationRelativityService.createDeviceAffiliationRelativity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新附属设备关系")
    @PreAuthorize("@ss.hasPermission('qms:device-affiliation-relativity:update')")
    public CommonResult<Boolean> updateDeviceAffiliationRelativity(@Valid @RequestBody DeviceAffiliationRelativitySaveReqVO updateReqVO) {
        deviceAffiliationRelativityService.updateDeviceAffiliationRelativity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除附属设备关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:device-affiliation-relativity:delete')")
    public CommonResult<Boolean> deleteDeviceAffiliationRelativity(@RequestParam("id") Long id) {
        deviceAffiliationRelativityService.deleteDeviceAffiliationRelativity(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除附属设备关系")
                @PreAuthorize("@ss.hasPermission('qms:device-affiliation-relativity:delete')")
    public CommonResult<Boolean> deleteDeviceAffiliationRelativityList(@RequestBody BatchDeleteReqVO req) {
        deviceAffiliationRelativityService.deleteDeviceAffiliationRelativityListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得附属设备关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:device-affiliation-relativity:query')")
    public CommonResult<DeviceAffiliationRelativityRespVO> getDeviceAffiliationRelativity(@RequestParam("id") Long id) {
        DeviceAffiliationRelativityDO deviceAffiliationRelativity = deviceAffiliationRelativityService.getDeviceAffiliationRelativity(id);
        return success(BeanUtils.toBean(deviceAffiliationRelativity, DeviceAffiliationRelativityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得附属设备关系分页")
    @PreAuthorize("@ss.hasPermission('qms:device-affiliation-relativity:query')")
    public CommonResult<PageResult<DeviceAffiliationRelativityRespVO>> getDeviceAffiliationRelativityPage(@Valid DeviceAffiliationRelativityPageReqVO pageReqVO) {
        PageResult<DeviceAffiliationRelativityDO> pageResult = deviceAffiliationRelativityService.getDeviceAffiliationRelativityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceAffiliationRelativityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出附属设备关系 Excel")
    @PreAuthorize("@ss.hasPermission('qms:device-affiliation-relativity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceAffiliationRelativityExcel(@Valid DeviceAffiliationRelativityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceAffiliationRelativityDO> list = deviceAffiliationRelativityService.getDeviceAffiliationRelativityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "附属设备关系.xls", "数据", DeviceAffiliationRelativityRespVO.class,
                        BeanUtils.toBean(list, DeviceAffiliationRelativityRespVO.class));
    }

}