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.DeviceApplyPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceApplyRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceApplySaveReqVO;
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.DeviceApplyDO;
import com.zt.plat.module.qms.resource.device.service.DeviceApplyService;

@Tag(name = "管理后台 - 设备通用流程,验收、降级、停用、报废、还原、启用")
@RestController
@RequestMapping("/qms/resource/device-apply")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.deviceapply")
public class DeviceApplyController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource private DeviceApplyService deviceApplyService;

    @RequestMapping("/createTempData")
    @Operation(summary = "创建临时数据")
    public CommonResult<DeviceApplyRespVO> createTempData(@RequestBody DeviceApplySaveReqVO createReqVO) {
        return deviceApplyService.createTempData(createReqVO);
    }

    @RequestMapping("/saveDeviceApply")
    @Operation(summary = "保存申请数据")
    public CommonResult<Boolean> saveDeviceApply(@Valid @RequestBody DeviceApplySaveReqVO createReqVO) {
        return deviceApplyService.saveDeviceApply(createReqVO);
    }

    @RequestMapping("/addDetail")
    @Operation(summary = "增加明细")
    public CommonResult<Boolean> addDetail(@RequestBody JSONObject param) {
        return deviceApplyService.addDetail(param);
    }

    @RequestMapping("/submitDeviceApply")
    @Operation(summary = "提交申请")
    public CommonResult<DeviceApplyRespVO> submitDeviceApply(@RequestBody DeviceApplySaveReqVO param) {
        return deviceApplyService.submitDeviceApply(param);
    }

    @RequestMapping("/removeDetail")
    @Operation(summary = "删除明细")
    public CommonResult<Boolean> removeDetail(@RequestBody JSONObject param) {
        return deviceApplyService.removeDetail(param);
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备通用流程,验收、降级、停用、报废、还原、启用")
//    @PreAuthorize("@ss.hasPermission('qms:device-apply:update')")
    public CommonResult<Boolean> updateDeviceApply(@Valid @RequestBody DeviceApplySaveReqVO updateReqVO) {
        deviceApplyService.updateDeviceApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备通用流程,验收、降级、停用、报废、还原、启用")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:device-apply:delete')")
    public CommonResult<Boolean> deleteDeviceApply(@RequestParam("id") Long id) {
        deviceApplyService.deleteDeviceApply(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备通用流程,验收、降级、停用、报废、还原、启用")
    @PreAuthorize("@ss.hasPermission('qms:device-apply:delete')")
    public CommonResult<Boolean> deleteDeviceApplyList(@RequestBody BatchDeleteReqVO req) {
        deviceApplyService.deleteDeviceApplyListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备通用流程,验收、降级、停用、报废、还原、启用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:device-apply:query')")
    public CommonResult<DeviceApplyRespVO> getDeviceApply(@RequestParam("id") Long id) {
        DeviceApplyDO deviceApply = deviceApplyService.getDeviceApply(id);
        return success(BeanUtils.toBean(deviceApply, DeviceApplyRespVO.class));
    }

    @GetMapping("/getWithOpinions")
    @Operation(summary = "获得设备通用流程详情，含意见内容")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<JSONObject> getWithOpinions(@RequestParam("id") Long id) {
        return deviceApplyService.getWithOpinions(id);
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备通用流程,验收、降级、停用、报废、还原、启用分页")
    public CommonResult<PageResult<DeviceApplyRespVO>> getDeviceApplyPage(@Valid DeviceApplyPageReqVO pageReqVO) {
        PageResult<DeviceApplyDO> pageResult = deviceApplyService.getDeviceApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备通用流程,验收、降级、停用、报废、还原、启用 Excel")
    @PreAuthorize("@ss.hasPermission('qms:device-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceApplyExcel(@Valid DeviceApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceApplyDO> list = deviceApplyService.getDeviceApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备通用流程,验收、降级、停用、报废、还原、启用.xls", "数据", DeviceApplyRespVO.class,
                        BeanUtils.toBean(list, DeviceApplyRespVO.class));
    }

}