package com.cy.guoyan.admin.module.guoyan.controller.admin.stateownedreformlawcategory;

import com.cy.guoyan.admin.module.guoyan.controller.admin.researchtopiccategory.vo.ResearchTopicCategoryPageReqVO;
import com.cy.guoyan.admin.module.guoyan.controller.admin.researchtopiccategory.vo.ResearchTopicCategoryTreeRespVO;
import com.cy.guoyan.admin.module.guoyan.controller.admin.researchtopicinfo.vo.ResearchTopicInfoSaveReqVO;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.researchtopiccategory.ResearchTopicCategoryDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cy.guoyan.admin.framework.common.pojo.PageParam;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.pojo.CommonResult;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;
import static com.cy.guoyan.admin.framework.common.pojo.CommonResult.success;

import com.cy.guoyan.admin.framework.excel.core.util.ExcelUtils;

import com.cy.guoyan.admin.framework.apilog.core.annotation.ApiAccessLog;
import static com.cy.guoyan.admin.framework.apilog.core.enums.OperateTypeEnum.*;

import com.cy.guoyan.admin.module.guoyan.controller.admin.stateownedreformlawcategory.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.stateownedreformlawcategory.StateOwnedReformLawCategoryDO;
import com.cy.guoyan.admin.module.guoyan.service.stateownedreformlawcategory.StateOwnedReformLawCategoryService;

@Tag(name = "管理后台 - 国企改革法规主题分类")
@RestController
@RequestMapping("/guoyan/state-owned-reform-law-category")
@Validated
public class StateOwnedReformLawCategoryController {

    @Resource
    private StateOwnedReformLawCategoryService stateOwnedReformLawCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建国企改革法规主题分类")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:create')")
    public CommonResult<Long> createStateOwnedReformLawCategory(@Valid @RequestBody StateOwnedReformLawCategorySaveReqVO createReqVO) {
        return success(stateOwnedReformLawCategoryService.createStateOwnedReformLawCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新国企改革法规主题分类")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:update')")
    public CommonResult<Boolean> updateStateOwnedReformLawCategory(@Valid @RequestBody StateOwnedReformLawCategorySaveReqVO updateReqVO) {
        stateOwnedReformLawCategoryService.updateStateOwnedReformLawCategory(updateReqVO);
        return success(true);
    }

    @PutMapping("/void")
    @Operation(summary = "作废国企改革法规主题分类")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:update')")
    public CommonResult<Boolean> voidStateOwnedReformLawCategory(@RequestBody StateOwnedReformLawCategorySaveReqVO updateReqVO) {
        stateOwnedReformLawCategoryService.voidStateOwnedReformLawCategory(updateReqVO.getId(), updateReqVO.getStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除国企改革法规主题分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:delete')")
    public CommonResult<Boolean> deleteStateOwnedReformLawCategory(@RequestParam("id") Long id) {
        stateOwnedReformLawCategoryService.deleteStateOwnedReformLawCategory(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除国企改革法规主题分类")
                @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:delete')")
    public CommonResult<Boolean> deleteStateOwnedReformLawCategoryList(@RequestParam("ids") List<Long> ids) {
        stateOwnedReformLawCategoryService.deleteStateOwnedReformLawCategoryListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得国企改革法规主题分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:query')")
    public CommonResult<StateOwnedReformLawCategoryRespVO> getStateOwnedReformLawCategory(@RequestParam("id") Long id) {
        StateOwnedReformLawCategoryDO stateOwnedReformLawCategory = stateOwnedReformLawCategoryService.getStateOwnedReformLawCategory(id);
        return success(BeanUtils.toBean(stateOwnedReformLawCategory, StateOwnedReformLawCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得国企改革法规主题分类分页")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:query')")
    public CommonResult<PageResult<StateOwnedReformLawCategoryRespVO>> getStateOwnedReformLawCategoryPage(@Valid StateOwnedReformLawCategoryPageReqVO pageReqVO) {
        PageResult<StateOwnedReformLawCategoryDO> pageResult = stateOwnedReformLawCategoryService.getStateOwnedReformLawCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StateOwnedReformLawCategoryRespVO.class));
    }

    @GetMapping("/tree")
    @Operation(summary = "获得国企改革法规主题分类树")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:query')")
    public CommonResult<List<StateOwnedReformLawCategoryTreeVO>> getResearchTopicCategoryTree(@Valid ResearchTopicCategoryPageReqVO pageReqVO) {
        List<StateOwnedReformLawCategoryDO> pageResult = stateOwnedReformLawCategoryService.getStateOwnedReformLawCategoryTree(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StateOwnedReformLawCategoryTreeVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出国企改革法规主题分类 Excel")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStateOwnedReformLawCategoryExcel(@Valid StateOwnedReformLawCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StateOwnedReformLawCategoryDO> list = stateOwnedReformLawCategoryService.getStateOwnedReformLawCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "国企改革法规主题分类.xls", "数据", StateOwnedReformLawCategoryRespVO.class,
                        BeanUtils.toBean(list, StateOwnedReformLawCategoryRespVO.class));
    }

}