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

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.caxx.vo.*;
import com.unis.module.coding.dal.dataobject.caxx.CaxxDO;
import com.unis.module.coding.service.caxx.CaxxService;

@Tag(name = "仓廒信息")
@RestController
@RequestMapping("/coding/caxx")
@Validated
public class CaxxController {

    @Resource
    private CaxxService caxxService;

    @PostMapping("/create")
    @Operation(summary = "创建仓廒信息;")
    @PreAuthorize("@ss.hasPermission('coding:caxx:create')")
    public CommonResult<Long> createCaxx(@Valid @RequestBody CaxxSaveReqVO createReqVO) {
        return success(caxxService.createCaxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓廒信息;")
    @PreAuthorize("@ss.hasPermission('coding:caxx:update')")
    public CommonResult<Boolean> updateCaxx(@Valid @RequestBody CaxxSaveReqVO updateReqVO) {
        caxxService.updateCaxx(updateReqVO);
        return success(true);
    }


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

    @GetMapping("/get")
    @Operation(summary = "获得仓廒信息;")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:caxx:query')")
    public CommonResult<CaxxRespVO> getCaxx(@RequestParam("id") Long id) {
        CaxxDO caxx = caxxService.getCaxx(id);
        return success(BeanUtils.toBean(caxx, CaxxRespVO.class));
    }

    @GetMapping("/getByKqBm")
    @Operation(summary = "库区人员获得仓廒信息;")
    @PreAuthorize("@ss.hasPermission('coding:caxx:queryByKqBm')")
    public CommonResult<List<CaxxRespVO>> getCaxx() {
        List<CaxxDO> pageResult = caxxService.getCaxxByKqBm();
        return success(BeanUtils.toBean(pageResult, CaxxRespVO.class));
    }


    @GetMapping("/page")
    @Operation(summary = "获得仓廒信息;分页")
    @PreAuthorize("@ss.hasPermission('coding:caxx:query')")
    public CommonResult<PageResult<CaxxRespVO>> getCaxxPage(@Valid CaxxPageReqVO pageReqVO) {
        PageResult<CaxxDO> pageResult = caxxService.getCaxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CaxxRespVO.class));
    }
//    @GetMapping("/pageByKqDM")
//    @Operation(summary = "库区人员获得仓廒信息;分页")
//    @PreAuthorize("@ss.hasPermission('coding:caxx:querybydkdm')")
//    public CommonResult<PageResult<CaxxRespVO>> getCaxxPageByKqDm(@Valid CaxxPageReqVO pageReqVO) {
//        PageResult<CaxxDO> pageResult = caxxService.getCaxxPageByKqDm(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, CaxxRespVO.class));
//    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓廒信息; Excel")
    @PreAuthorize("@ss.hasPermission('coding:caxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCaxxExcel(@Valid CaxxPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CaxxDO> list = caxxService.getCaxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓廒信息;.xls", "数据", CaxxRespVO.class,
                BeanUtils.toBean(list, CaxxRespVO.class));
    }

}