package com.zt.plat.module.qms.resource.record.controller.admin.recordcategory;

import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.resource.record.controller.admin.recordcategory.vo.RecordCategoryPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordcategory.vo.RecordCategoryRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordcategory.vo.RecordCategorySaveReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordrecord.vo.RecordRecordRespVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
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 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.record.dal.dataobject.recordcategory.RecordCategoryDO;
import com.zt.plat.module.qms.resource.record.service.recordcategory.RecordCategoryService;

@Tag(name = "管理后台 - 文件记录分类")
@RestController
@RequestMapping("/qms/resource/record-category")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.recordcategory")
public class RecordCategoryController extends AbstractFileUploadController implements BusinessControllerMarker {

    private static final Logger log = LoggerFactory.getLogger(RecordCategoryController.class);

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

    @Resource
    private RecordCategoryService recordCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建文件记录分类")
    //@PreAuthorize("@ss.hasPermission('qms:record-category:create')")
    public CommonResult<RecordCategoryRespVO> createRecordCategory(@Valid @RequestBody RecordCategorySaveReqVO createReqVO) {
        if (ObjectUtils.isEmpty(createReqVO.getParentId())) createReqVO.setParentId(0L);
        if (ObjectUtils.isEmpty(createReqVO.getSortNo())) createReqVO.setSortNo(1);
        return success(recordCategoryService.createRecordCategory(createReqVO));
    }

    // 返回实体对象本身
    @PostMapping("/create-temp-data")
    @Operation(summary = "创建文件记录分类临时数据")
    public CommonResult<RecordCategoryRespVO> createRecordTempCategory(@RequestBody RecordCategorySaveReqVO createReqVO) {
        return success(recordCategoryService.createRecordCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件记录分类")
    //@PreAuthorize("@ss.hasPermission('qms:record-category:update')")
    public CommonResult<Boolean> updateRecordCategory(@Valid @RequestBody RecordCategorySaveReqVO updateReqVO) {
        recordCategoryService.updateRecordCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件记录分类")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:record-category:delete')")
    public CommonResult<Boolean> deleteRecordCategory(@RequestParam("id") Long id) {
        recordCategoryService.deleteRecordCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文件记录分类")
    //@PreAuthorize("@ss.hasPermission('qms:record-category:delete')")
    public CommonResult<Boolean> deleteRecordCategoryList(@RequestBody BatchDeleteReqVO req) {
        recordCategoryService.deleteRecordCategoryListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件记录分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:record-category:query')")
    public CommonResult<RecordCategoryRespVO> getRecordCategory(@RequestParam("id") Long id) {
        RecordCategoryDO recordCategory = recordCategoryService.getRecordCategory(id);
        return success(BeanUtils.toBean(recordCategory, RecordCategoryRespVO.class));
    }


    @GetMapping("/getRecordCategoryTree")
    @Operation(summary = "获得文件记录分类")
    //@PreAuthorize("@ss.hasPermission('qms:record-category:query')")
    public CommonResult<List<RecordCategoryRespVO>> getRecordCategoryTree(@Valid RecordCategoryPageReqVO pageReqVO) {
        List<RecordCategoryDO> list = recordCategoryService.getRecordCategoryTree(pageReqVO);
        return success(BeanUtils.toBean(list, RecordCategoryRespVO.class));
    }

    /*
     *  获取指定的分类树
     *      例：党建文件
     */
    @GetMapping("/getAppointCategoryTree")
    @Operation(summary = "获得文件记录分类")
    //@PreAuthorize("@ss.hasPermission('qms:record-category:query')")
    public CommonResult<List<RecordCategoryRespVO>> getAppointCategoryTree(@Valid RecordCategoryPageReqVO pageReqVO) {
        List<RecordCategoryDO> list = recordCategoryService.getRecordCategoryTree(pageReqVO);
        return success(BeanUtils.toBean(list, RecordCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件记录分类 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:record-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordCategoryExcel(@Valid RecordCategoryPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordCategoryDO> list = recordCategoryService.getRecordCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件记录分类.xls", "数据", RecordCategoryRespVO.class,
                BeanUtils.toBean(list, RecordCategoryRespVO.class));
    }

}