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

import com.alibaba.fastjson.JSONObject;
import com.zt.plat.module.qms.resource.device.controller.vo.*;
import org.springframework.security.core.context.SecurityContextHolder;
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.DevicePeriodCheckPlanDO;
import com.zt.plat.module.qms.resource.device.service.DevicePeriodCheckPlanService;

@Tag(name = "管理后台 - 设备-期间核查计划")
@RestController
@RequestMapping("/qms/resource/device-period-check-plan")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.deviceperiodcheckplan")
public class DevicePeriodCheckPlanController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private DevicePeriodCheckPlanService devicePeriodCheckPlanService;

    @PostMapping("/createPlanDevice")
    @Operation(summary = "创建设备-期间核查计划")
    public CommonResult<DevicePeriodCheckPlanRespVO> createPlanDevice(@RequestBody JSONObject param) {
        String checkYear = param.getString("checkYear");
        return success(devicePeriodCheckPlanService.createPlanDevice(checkYear));
    }

    @PostMapping("/createPlanMaterial")
    @Operation(summary = "创建设备-期间核查计划")
    public CommonResult<DevicePeriodCheckPlanRespVO> createPlanMaterial(@RequestBody JSONObject param) {
        String checkYear = param.getString("checkYear");
        return success(devicePeriodCheckPlanService.createPlanMaterial(checkYear));
    }

    @RequestMapping("/savePlan")
    @Operation(summary = "编辑计划")
    public CommonResult<DevicePeriodCheckPlanRespVO> savePlan(@RequestBody DevicePeriodCheckPlanSaveReqVO param) {
        return devicePeriodCheckPlanService.savePlan(param);
    }

    @RequestMapping("/submitApply")
    @Operation(summary = "提交申请")
    public CommonResult<DevicePeriodCheckPlanRespVO> submitApply(@RequestBody DevicePeriodCheckPlanSaveReqVO param) {
        return devicePeriodCheckPlanService.submitApply(param);
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备-期间核查计划")
    public CommonResult<Boolean> updateDevicePeriodCheckPlan(@Valid @RequestBody DevicePeriodCheckPlanSaveReqVO updateReqVO) {
        devicePeriodCheckPlanService.updateDevicePeriodCheckPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备-期间核查计划")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteDevicePeriodCheckPlan(@RequestParam("id") Long id) {
        devicePeriodCheckPlanService.deleteDevicePeriodCheckPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备-期间核查计划")
    @PreAuthorize("@ss.hasPermission('qms:device-period-check-plan:delete')")
    public CommonResult<Boolean> deleteDevicePeriodCheckPlanList(@RequestBody BatchDeleteReqVO req) {
        devicePeriodCheckPlanService.deleteDevicePeriodCheckPlanListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备-期间核查计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DevicePeriodCheckPlanRespVO> getDevicePeriodCheckPlan(@RequestParam("id") Long id) {
        DevicePeriodCheckPlanDO devicePeriodCheckPlan = devicePeriodCheckPlanService.getDevicePeriodCheckPlan(id);
        return success(BeanUtils.toBean(devicePeriodCheckPlan, DevicePeriodCheckPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备-期间核查计划分页")
    public CommonResult<PageResult<DevicePeriodCheckPlanRespVO>> getDevicePeriodCheckPlanPage(@Valid DevicePeriodCheckPlanPageReqVO pageReqVO) {
        PageResult<DevicePeriodCheckPlanDO> pageResult = devicePeriodCheckPlanService.getDevicePeriodCheckPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DevicePeriodCheckPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备-期间核查计划 Excel")
    @PreAuthorize("@ss.hasPermission('qms:device-period-check-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDevicePeriodCheckPlanExcel(@Valid DevicePeriodCheckPlanPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DevicePeriodCheckPlanDO> list = devicePeriodCheckPlanService.getDevicePeriodCheckPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备-期间核查计划.xls", "数据", DevicePeriodCheckPlanRespVO.class, BeanUtils.toBean(list, DevicePeriodCheckPlanRespVO.class));
    }

}