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

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.*;
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.dwxx.vo.*;
import com.unis.module.coding.dal.dataobject.dwxx.DwxxDO;
import com.unis.module.coding.service.dwxx.DwxxService;

@Tag(name = "单位信息")
@RestController
@RequestMapping("/coding/dwxx")
@Validated
public class DwxxController {

    @Resource
    private DwxxService dwxxService;

    @PostMapping("/create")
    @Operation(summary = "创建单位信息")
    @PreAuthorize("@ss.hasPermission('coding:dwxx:create')")
    public CommonResult<Long> createDwxx(@Valid @RequestBody DwxxSaveReqVO createReqVO) {
        return success(dwxxService.createDwxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单位信息")
    @PreAuthorize("@ss.hasPermission('coding:dwxx:update')")
    public CommonResult<Boolean> updateDwxx(@Valid @RequestBody DwxxSaveReqVO updateReqVO) {
        dwxxService.updateDwxx(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/getByKqBm")
    @Operation(summary = "根据库区获得单位信息")
    @Parameter( required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:dwxx:kqbm')")
    public CommonResult<DwxxRespVO> getDwxxByKqBm() {
        DwxxDO dwxx = dwxxService.getDwxxByKqBm();
        return success(BeanUtils.toBean(dwxx, DwxxRespVO.class));
    }
    @GetMapping("/get")
    @Operation(summary = "获得单位信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:dwxx:query')")
    public CommonResult<DwxxRespVO> getDwxx(@RequestParam("id") Long id) {
        DwxxDO dwxx = dwxxService.getDwxx(id);
        return success(BeanUtils.toBean(dwxx, DwxxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单位信息分页")
    @PreAuthorize("@ss.hasPermission('coding:dwxx:query')")
    public CommonResult<PageResult<DwxxRespVO>> getDwxxPage(@Valid DwxxPageReqVO pageReqVO) {
        PageResult<DwxxDO> pageResult = dwxxService.getDwxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DwxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单位信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:dwxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDwxxExcel(@Valid DwxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DwxxDO> list = dwxxService.getDwxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单位信息.xls", "数据", DwxxRespVO.class,
                        BeanUtils.toBean(list, DwxxRespVO.class));
    }

}