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

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.kqxx.vo.*;
import com.unis.module.coding.dal.dataobject.kqxx.KqxxDO;
import com.unis.module.coding.service.kqxx.KqxxService;

@Tag(name = "管理后台 - 库区信息;")
@RestController
@RequestMapping("/coding/kqxx")
@Validated
public class KqxxController {

    @Resource
    private KqxxService kqxxService;

    @PostMapping("/create")
    @Operation(summary = "创建库区信息;")
    @PreAuthorize("@ss.hasPermission('coding:kqxx:create')")
    public CommonResult<Long> createKqxx(@Valid @RequestBody KqxxSaveReqVO createReqVO) {
        return success(kqxxService.createKqxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库区信息;")
    @PreAuthorize("@ss.hasPermission('coding:kqxx:update')")
    public CommonResult<Boolean> updateKqxx(@Valid @RequestBody KqxxSaveReqVO updateReqVO) {
        kqxxService.updateKqxx(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得库区信息;")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:kqxx:query')")
    public CommonResult<KqxxRespVO> getKqxx(@RequestParam("id") Long id) {
        KqxxDO kqxx = kqxxService.getKqxx(id);
        return success(BeanUtils.toBean(kqxx, KqxxRespVO.class));
    }
    @GetMapping("/getByKqBm")
    @Operation(summary = "根据库区编码获得库区信息;")
    @PreAuthorize("@ss.hasPermission('coding:kqxx:queryByKqBm')")
    public CommonResult<KqxxRespVO> getKqxxByKqBm() {
        KqxxDO kqxx = kqxxService.getKqxxByKqBm();
        return success(BeanUtils.toBean(kqxx, KqxxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库区信息;分页")
    @PreAuthorize("@ss.hasPermission('coding:kqxx:query')")
    public CommonResult<PageResult<KqxxRespVO>> getKqxxPage(@Valid KqxxPageReqVO pageReqVO) {
        PageResult<KqxxDO> pageResult = kqxxService.getKqxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KqxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库区信息; Excel")
    @PreAuthorize("@ss.hasPermission('coding:kqxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKqxxExcel(@Valid KqxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KqxxDO> list = kqxxService.getKqxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库区信息;.xls", "数据", KqxxRespVO.class,
                        BeanUtils.toBean(list, KqxxRespVO.class));
    }

}