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

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.cwxx.vo.*;
import com.unis.module.coding.dal.dataobject.cwxx.CwxxDO;
import com.unis.module.coding.service.cwxx.CwxxService;

@Tag(name = "管理后台 - 财务信息")
@RestController
@RequestMapping("/coding/cwxx")
@Validated
public class CwxxController {

    @Resource
    private CwxxService cwxxService;

    @PostMapping("/create")
    @Operation(summary = "创建财务信息")
    @PreAuthorize("@ss.hasPermission('coding:cwxx:create')")
    public CommonResult<Long> createCwxx(@Valid @RequestBody CwxxSaveReqVO createReqVO) {
        return success(cwxxService.createCwxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新财务信息")
    @PreAuthorize("@ss.hasPermission('coding:cwxx:update')")
    public CommonResult<Boolean> updateCwxx(@Valid @RequestBody CwxxSaveReqVO updateReqVO) {
        cwxxService.updateCwxx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除财务信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('coding:cwxx:delete')")
    public CommonResult<Boolean> deleteCwxx(@RequestParam("id") Long id) {
        cwxxService.deleteCwxx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得财务信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:cwxx:query')")
    public CommonResult<CwxxRespVO> getCwxx(@RequestParam("id") Long id) {
        return success(cwxxService.getCwxx(id));
    }

    @GetMapping("/getByKqBm")
    @Operation(summary = "根据库区编码获得财务信息")
    @PreAuthorize("@ss.hasPermission('coding:cwxx:queryByKqBm')")
    public CommonResult<CwxxRespVO> getCwxxByKqBm() {
        return success(cwxxService.getCwxxByKqBm());
    }

    @GetMapping("/page")
    @Operation(summary = "获得财务信息分页")
    @PreAuthorize("@ss.hasPermission('coding:cwxx:query')")
    public CommonResult<PageResult<CwxxRespVO>> getCwxxPage(@Valid CwxxPageReqVO pageReqVO) {
        PageResult<CwxxDO> pageResult = cwxxService.getCwxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CwxxRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出财务信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:cwxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCwxxExcel(@Valid CwxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CwxxDO> list = cwxxService.getCwxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "财务信息.xls", "数据", CwxxRespVO.class,
                        BeanUtils.toBean(list, CwxxRespVO.class));
    }

}