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

import com.zt.plat.module.qms.resource.device.common.DeviceConstant;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigBusinessItemPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigBusinessItemRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigBusinessItemSaveReqVO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceConfigBusinessRuleDO;
import com.zt.plat.module.qms.resource.device.service.DeviceConfigBusinessRuleService;
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.DeviceConfigBusinessItemDO;
import com.zt.plat.module.qms.resource.device.service.DeviceConfigBusinessItemService;

@Tag(name = "管理后台 - 设备-检查项目配置")
@RestController
@RequestMapping("/qms/resource/device-config-business-item")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.deviceconfigbusinessitem")
public class DeviceConfigBusinessItemController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private DeviceConfigBusinessItemService deviceConfigBusinessItemService;
    @Resource private DeviceConfigBusinessRuleService deviceConfigBusinessRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建设备-检查项目配置")
//    @PreAuthorize("@ss.hasPermission('qms:device-config-business-item:create')")
    public CommonResult<DeviceConfigBusinessItemRespVO> createDeviceConfigBusinessItem(@Valid @RequestBody DeviceConfigBusinessItemSaveReqVO createReqVO) {
        return success(deviceConfigBusinessItemService.createDeviceConfigBusinessItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备-检查项目配置")
//    @PreAuthorize("@ss.hasPermission('qms:device-config-business-item:update')")
    public CommonResult<Boolean> updateDeviceConfigBusinessItem(@Valid @RequestBody DeviceConfigBusinessItemSaveReqVO updateReqVO) {
        deviceConfigBusinessItemService.updateDeviceConfigBusinessItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备-检查项目配置")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:device-config-business-item:delete')")
    public CommonResult<Boolean> deleteDeviceConfigBusinessItem(@RequestParam("id") Long id) {
        deviceConfigBusinessItemService.deleteDeviceConfigBusinessItem(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备-检查项目配置")
//                @PreAuthorize("@ss.hasPermission('qms:device-config-business-item:delete')")
    public CommonResult<Boolean> deleteDeviceConfigBusinessItemList(@RequestBody BatchDeleteReqVO req) {
        deviceConfigBusinessItemService.deleteDeviceConfigBusinessItemListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备-检查项目配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:device-config-business-item:query')")
    public CommonResult<DeviceConfigBusinessItemRespVO> getDeviceConfigBusinessItem(@RequestParam("id") Long id) {
        DeviceConfigBusinessItemDO deviceConfigBusinessItem = deviceConfigBusinessItemService.getDeviceConfigBusinessItem(id);
        return success(BeanUtils.toBean(deviceConfigBusinessItem, DeviceConfigBusinessItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备-检查项目配置分页")
//    @PreAuthorize("@ss.hasPermission('qms:device-config-business-item:query')")
    public CommonResult<PageResult<DeviceConfigBusinessItemRespVO>> getDeviceConfigBusinessItemPage(@Valid DeviceConfigBusinessItemPageReqVO pageReqVO) {

        Long productId = pageReqVO.getProductId();
        if (productId != null) {
            DeviceConfigBusinessRuleDO ruleDO = deviceConfigBusinessRuleService.getByProductId(productId, pageReqVO.getBusinessDomain());
            if(ruleDO != null){
                pageReqVO.setRuleId(ruleDO.getId());
            }
        }

        PageResult<DeviceConfigBusinessItemDO> pageResult = deviceConfigBusinessItemService.getDeviceConfigBusinessItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceConfigBusinessItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备-检查项目配置 Excel")
//    @PreAuthorize("@ss.hasPermission('qms:device-config-business-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceConfigBusinessItemExcel(@Valid DeviceConfigBusinessItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceConfigBusinessItemDO> list = deviceConfigBusinessItemService.getDeviceConfigBusinessItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备-检查项目配置.xls", "数据", DeviceConfigBusinessItemRespVO.class,
                        BeanUtils.toBean(list, DeviceConfigBusinessItemRespVO.class));
    }

}