package com.zt.plat.module.qms.common.data.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.common.data.dal.dataobject.DataCollectionDO;
import com.zt.plat.module.qms.common.data.controller.vo.DataCollectionPageReqVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataCollectionRespVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataCollectionSaveReqVO;
import com.zt.plat.module.qms.common.data.service.DataCollectionService;
import com.zt.plat.module.qms.core.constant.DataTypeConstant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 数据集")
@RestController
@RequestMapping("/qms/common/data/data-collection")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "common.data.datacollection")
public class DataCollectionController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private DataCollectionService dataCollectionService;

    @PostMapping("/create-temp-data")
    @Operation(summary = "创建临时数据集")
    public CommonResult<DataCollectionRespVO> createTempData(@Valid @RequestBody DataCollectionSaveReqVO vo) {
        vo.setCancelFlag("-1");
        vo.setNodeType(DataTypeConstant.DATA_TYPE_DATA);
        vo.setVersion(1);
        return success(dataCollectionService.createDataCollection(vo));
    }

    @PostMapping("/saveDataCollection")
    @Operation(summary = "保存数据集")
    @PreAuthorize("@ss.hasPermission('qms:data-collection:create')")
    public CommonResult<String> saveDataCollection(@Valid @RequestBody DataCollectionSaveReqVO createReqVO) {
        return dataCollectionService.saveDataCollection(createReqVO);
    }

    @PostMapping("/saveClassify")
    @Operation(summary = "保存数据集分类")
    @PreAuthorize("@ss.hasPermission('qms:data-collection:create')")
    public CommonResult<DataCollectionRespVO> saveClassify(@Valid @RequestBody DataCollectionSaveReqVO createReqVO) {
        return dataCollectionService.saveClassify(createReqVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据集")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:data-collection:delete')")
    public CommonResult<Boolean> deleteDataCollection(@RequestParam("id") Long id) {
        dataCollectionService.deleteDataCollection(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据集")
    @PreAuthorize("@ss.hasPermission('qms:data-collection:delete')")
    public CommonResult<Boolean> deleteDataCollectionList(@RequestBody BatchDeleteReqVO req) {
        dataCollectionService.deleteDataCollectionListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:data-collection:query')")
    public CommonResult<DataCollectionRespVO> getDataCollection(@RequestParam("id") Long id) {
        DataCollectionDO dataCollection = dataCollectionService.getDataCollection(id);
        return success(BeanUtils.toBean(dataCollection, DataCollectionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据集分页")
    public CommonResult<PageResult<DataCollectionRespVO>> getDataCollectionPage(@Valid DataCollectionPageReqVO pageReqVO) {
        PageResult<DataCollectionDO> pageResult = dataCollectionService.getDataCollectionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataCollectionRespVO.class));
    }

    @GetMapping("/get-tree-data")
    @Operation(summary = "查询分类树")
    public CommonResult<List<DataCollectionRespVO>> getTreeData() {
        List<DataCollectionDO> list = dataCollectionService.getTreeData();
        return success(BeanUtils.toBean(list, DataCollectionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据集 Excel")
    @PreAuthorize("@ss.hasPermission('qms:data-collection:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataCollectionExcel(@Valid DataCollectionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataCollectionDO> list = dataCollectionService.getDataCollectionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据集.xls", "数据", DataCollectionRespVO.class,
                        BeanUtils.toBean(list, DataCollectionRespVO.class));
    }

}