package com.unis.module.coding.controller.admin.cwxxyear;

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.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.util.object.BeanUtils;
import static com.unis.framework.common.pojo.CommonResult.success;

import com.unis.framework.excel.core.util.ExcelUtils;

import com.unis.framework.apilog.core.annotation.ApiAccessLog;
import static com.unis.framework.apilog.core.enums.OperateTypeEnum.*;

import com.unis.module.coding.controller.admin.cwxxyear.vo.*;
import com.unis.module.coding.dal.dataobject.cwxxyear.CwxxYearDO;
import com.unis.module.coding.service.cwxxyear.CwxxYearService;

@Tag(name = "管理后台 - 财年务情况")
@RestController
@RequestMapping("/coding/cwxx-year")
@Validated
public class CwxxYearController {

    @Resource
    private CwxxYearService cwxxYearService;

    @PostMapping("/create")
    @Operation(summary = "创建财年务情况")
    @PreAuthorize("@ss.hasPermission('coding:cwxx-year:create')")
    public CommonResult<Long> createCwxxYear(@Valid @RequestBody CwxxYearSaveReqVO createReqVO) {
        return success(cwxxYearService.createCwxxYear(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新财年务情况")
    @PreAuthorize("@ss.hasPermission('coding:cwxx-year:update')")
    public CommonResult<Boolean> updateCwxxYear(@Valid @RequestBody CwxxYearSaveReqVO updateReqVO) {
        cwxxYearService.updateCwxxYear(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除财年务情况")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('coding:cwxx-year:delete')")
    public CommonResult<Boolean> deleteCwxxYear(@RequestParam("id") Long id) {
        cwxxYearService.deleteCwxxYear(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得财年务情况")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:cwxx-year:query')")
    public CommonResult<CwxxYearRespVO> getCwxxYear(@RequestParam("id") Long id) {
        CwxxYearDO cwxxYear = cwxxYearService.getCwxxYear(id);
        return success(BeanUtils.toBean(cwxxYear, CwxxYearRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得财年务情况分页")
    @PreAuthorize("@ss.hasPermission('coding:cwxx-year:query')")
    public CommonResult<PageResult<CwxxYearRespVO>> getCwxxYearPage(@Valid CwxxYearPageReqVO pageReqVO) {
        PageResult<CwxxYearDO> pageResult = cwxxYearService.getCwxxYearPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CwxxYearRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出财年务情况 Excel")
    @PreAuthorize("@ss.hasPermission('coding:cwxx-year:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCwxxYearExcel(@Valid CwxxYearPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CwxxYearDO> list = cwxxYearService.getCwxxYearPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "财年务情况.xls", "数据", CwxxYearRespVO.class,
                        BeanUtils.toBean(list, CwxxYearRespVO.class));
    }

}