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

import com.cy.guoyan.admin.module.guoyan.controller.admin.stateownedreformlawcategory.vo.StateOwnedReformLawCategorySaveReqVO;
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.stateownedreformlawlevel.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.stateownedreformlawlevel.StateOwnedReformLawLevelDO;
import com.cy.guoyan.admin.module.guoyan.service.stateownedreformlawlevel.StateOwnedReformLawLevelService;

@Tag(name = "管理后台 - 国企改革法规层级")
@RestController
@RequestMapping("/guoyan/state-owned-reform-law-level")
@Validated
public class StateOwnedReformLawLevelController {

    @Resource
    private StateOwnedReformLawLevelService stateOwnedReformLawLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建国企改革法规层级")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:create')")
    public CommonResult<Long> createStateOwnedReformLawLevel(@Valid @RequestBody StateOwnedReformLawLevelSaveReqVO createReqVO) {
        return success(stateOwnedReformLawLevelService.createStateOwnedReformLawLevel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新国企改革法规层级")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:update')")
    public CommonResult<Boolean> updateStateOwnedReformLawLevel(@Valid @RequestBody StateOwnedReformLawLevelSaveReqVO updateReqVO) {
        stateOwnedReformLawLevelService.updateStateOwnedReformLawLevel(updateReqVO);
        return success(true);
    }

    @PutMapping("/void")
    @Operation(summary = "作废国企改革法规层级")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:update')")
    public CommonResult<Boolean> voidStateOwnedReformLawLevel(@RequestBody StateOwnedReformLawLevelSaveReqVO updateReqVO) {
        stateOwnedReformLawLevelService.voidStateOwnedReformLawLevel(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-level:delete')")
    public CommonResult<Boolean> deleteStateOwnedReformLawLevel(@RequestParam("id") Long id) {
        stateOwnedReformLawLevelService.deleteStateOwnedReformLawLevel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除国企改革法规层级")
                @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:delete')")
    public CommonResult<Boolean> deleteStateOwnedReformLawLevelList(@RequestParam("ids") List<Long> ids) {
        stateOwnedReformLawLevelService.deleteStateOwnedReformLawLevelListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得国企改革法规层级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:query')")
    public CommonResult<StateOwnedReformLawLevelRespVO> getStateOwnedReformLawLevel(@RequestParam("id") Long id) {
        StateOwnedReformLawLevelDO stateOwnedReformLawLevel = stateOwnedReformLawLevelService.getStateOwnedReformLawLevel(id);
        return success(BeanUtils.toBean(stateOwnedReformLawLevel, StateOwnedReformLawLevelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得国企改革法规层级分页")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:query')")
    public CommonResult<PageResult<StateOwnedReformLawLevelRespVO>> getStateOwnedReformLawLevelPage(@Valid StateOwnedReformLawLevelPageReqVO pageReqVO) {
        PageResult<StateOwnedReformLawLevelDO> pageResult = stateOwnedReformLawLevelService.getStateOwnedReformLawLevelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StateOwnedReformLawLevelRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得国企改革法规层级列表")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:query')")
    public CommonResult<List<StateOwnedReformLawLevelRespVO>> getStateOwnedReformLawLevelList(@Valid StateOwnedReformLawLevelPageReqVO pageReqVO) {
        List<StateOwnedReformLawLevelDO> pageResult = stateOwnedReformLawLevelService.getStateOwnedReformLawLevelList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StateOwnedReformLawLevelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出国企改革法规层级 Excel")
    @PreAuthorize("@ss.hasPermission('guoyan:state-owned-reform-law-level:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStateOwnedReformLawLevelExcel(@Valid StateOwnedReformLawLevelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StateOwnedReformLawLevelDO> list = stateOwnedReformLawLevelService.getStateOwnedReformLawLevelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "国企改革法规层级.xls", "数据", StateOwnedReformLawLevelRespVO.class,
                        BeanUtils.toBean(list, StateOwnedReformLawLevelRespVO.class));
    }

}