package cn.iocoder.yudao.module.member.controller.admin.storegoodsinventoryrecord;

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 cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.storegoodsinventoryrecord.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoodsinventoryrecord.StoreGoodsInventoryRecordDO;
import cn.iocoder.yudao.module.member.service.storegoodsinventoryrecord.StoreGoodsInventoryRecordService;

@Tag(name = "管理后台 - 商品出入库记录")
@RestController
@RequestMapping("/member/store-goods-inventory-record")
@Validated
public class StoreGoodsInventoryRecordController {

    @Resource
    private StoreGoodsInventoryRecordService storeGoodsInventoryRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建商品出入库记录")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory-record:create')")
    public CommonResult<Integer> createStoreGoodsInventoryRecord(@Valid @RequestBody StoreGoodsInventoryRecordSaveReqVO createReqVO) {
        return success(storeGoodsInventoryRecordService.createStoreGoodsInventoryRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品出入库记录")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory-record:update')")
    public CommonResult<Boolean> updateStoreGoodsInventoryRecord(@Valid @RequestBody StoreGoodsInventoryRecordSaveReqVO updateReqVO) {
        storeGoodsInventoryRecordService.updateStoreGoodsInventoryRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品出入库记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory-record:delete')")
    public CommonResult<Boolean> deleteStoreGoodsInventoryRecord(@RequestParam("id") Integer id) {
        storeGoodsInventoryRecordService.deleteStoreGoodsInventoryRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品出入库记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory-record:query')")
    public CommonResult<StoreGoodsInventoryRecordRespVO> getStoreGoodsInventoryRecord(@RequestParam("id") Integer id) {
        StoreGoodsInventoryRecordDO storeGoodsInventoryRecord = storeGoodsInventoryRecordService.getStoreGoodsInventoryRecord(id);
        return success(BeanUtils.toBean(storeGoodsInventoryRecord, StoreGoodsInventoryRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品出入库记录分页")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory-record:query')")
    public CommonResult<PageResult<StoreGoodsInventoryRecordRespVO>> getStoreGoodsInventoryRecordPage(@Valid StoreGoodsInventoryRecordPageReqVO pageReqVO) {
        PageResult<StoreGoodsInventoryRecordDO> pageResult = storeGoodsInventoryRecordService.getStoreGoodsInventoryRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreGoodsInventoryRecordRespVO.class));
    }


    @GetMapping("/pageRecord")
    @Operation(summary = "获得商品出入库记录分页")
    public CommonResult<PageResult<InventoryRecordPageVO>> getStoreGoodsInventoryRecordPage(@Valid InventoryRecordPageReqVO pageReqVO) {
        PageResult<InventoryRecordPageVO> pageResult = storeGoodsInventoryRecordService.getRecordPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出商品出入库记录 Excel")
    @PreAuthorize("@ss.hasPermission('member:store-goods-inventory-record:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreGoodsInventoryRecordExcel(@Valid StoreGoodsInventoryRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreGoodsInventoryRecordDO> list = storeGoodsInventoryRecordService.getStoreGoodsInventoryRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品出入库记录.xls", "数据", StoreGoodsInventoryRecordRespVO.class,
                        BeanUtils.toBean(list, StoreGoodsInventoryRecordRespVO.class));
    }

    @GetMapping("/recordCount")
    @Operation(summary = "商品出入库记录统计")
    public CommonResult<InventoryStatisticsVO> getRecordCount(@Valid InventoryRecordPageReqVO pageReqVO) {
        InventoryStatisticsVO recordCount = storeGoodsInventoryRecordService.getRecordCount(pageReqVO);
        return success(recordCount);
    }




}