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

import com.unis.framework.common.util.kqbm.KQBMUtils;
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.crkxx.vo.*;
import com.unis.module.coding.dal.dataobject.crkxx.CrkxxDO;
import com.unis.module.coding.service.crkxx.CrkxxService;

@Tag(name = "管理后台 - 出入库信息")
@RestController
@RequestMapping("/delivery/crkxx")
@Validated
public class CrkxxController {

    @Resource
    private CrkxxService crkxxService;

    @PostMapping("/create")
    @Operation(summary = "创建出入库信息")
    @PreAuthorize("@ss.hasPermission('coding:crkxx:create')")
    public CommonResult<Long> createCrkxx(@Valid @RequestBody CrkxxSaveReqVO createReqVO) {
        return success(crkxxService.createCrkxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新出入库信息")
    @PreAuthorize("@ss.hasPermission('coding:crkxx:update')")
    public CommonResult<Boolean> updateCrkxx(@Valid @RequestBody CrkxxSaveReqVO updateReqVO) {
        crkxxService.updateCrkxx(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得出入库信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:crkxx:query')")
    public CommonResult<CrkxxRespVO> getCrkxx(@RequestParam("id") Long id) {
        CrkxxDO crkxx = crkxxService.getCrkxx(id);
        CrkxxRespVO respVO = BeanUtils.toBean(crkxx, CrkxxRespVO.class);
        return success(respVO);
    }

    @PostMapping("/page")
    @Operation(summary = "获得出入库信息分页")
    @PreAuthorize("@ss.hasPermission('coding:crkxx:query')")
    public CommonResult<PageResult<CrkxxRespVO>> getCrkxxPage(@Valid  @RequestBody CrkxxPageReqVO pageReqVO) {
        PageResult<CrkxxDO> pageResult = crkxxService.getCrkxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CrkxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出入库信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:crkxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCrkxxExcel(@Valid CrkxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CrkxxDO> list = crkxxService.getCrkxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "出入库信息.xls", "数据", CrkxxRespVO.class,
                        BeanUtils.toBean(list, CrkxxRespVO.class));
    }

}