package com.zt.plat.module.erp.controller.admin.erp.internalwarehouse;

import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehouseEnableDisableReqVO;
import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehousePageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehouseRespVO;
import com.zt.plat.module.erp.controller.admin.erp.internalwarehouse.vo.InternalWarehouseSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.internalwarehouse.InternalWarehouseDO;
import com.zt.plat.module.erp.service.erp.internalwarehouse.InternalWarehouseService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - ERP内部库位")
@RestController
@RequestMapping("/base/internal-warehouse")
@Validated
public class InternalWarehouseController implements  BusinessControllerMarker {


    @Resource
    private InternalWarehouseService internalWarehouseService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP库位")
    @PreAuthorize("@ss.hasPermission('basic:warehouse-config:create')")
    public CommonResult<InternalWarehouseRespVO> createInternalWarehouse(@Valid @RequestBody InternalWarehouseSaveReqVO createReqVO) {
        return success(internalWarehouseService.createInternalWarehouse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP库位")
    @PreAuthorize("@ss.hasPermission('basic:warehouse-config:update')")
    public CommonResult<Boolean> updateInternalWarehouse(@Valid @RequestBody InternalWarehouseSaveReqVO updateReqVO) {
        internalWarehouseService.updateInternalWarehouse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP库位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('basic:warehouse-config:delete')")
    public CommonResult<Boolean> deleteInternalWarehouse(@RequestParam("id") Long id) {
        internalWarehouseService.deleteInternalWarehouse(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP库位")
                @PreAuthorize("@ss.hasPermission('basic:warehouse-config:delete')")
    public CommonResult<Boolean> deleteInternalWarehouseList(@RequestBody BatchDeleteReqVO req) {
        internalWarehouseService.deleteInternalWarehouseListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP库位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('basic:warehouse-config:query')")
    public CommonResult<InternalWarehouseRespVO> getInternalWarehouse(@RequestParam("id") Long id) {
        InternalWarehouseDO internalWarehouse = internalWarehouseService.getInternalWarehouse(id);
        return success(BeanUtils.toBean(internalWarehouse, InternalWarehouseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP库位分页")
    @PreAuthorize("@ss.hasPermission('basic:warehouse-config:query')")
    public CommonResult<PageResult<InternalWarehouseRespVO>> getInternalWarehousePage(@Valid InternalWarehousePageReqVO pageReqVO) {
        PageResult<InternalWarehouseDO> pageResult = internalWarehouseService.getInternalWarehousePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InternalWarehouseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP库位 Excel")
    @PreAuthorize("@ss.hasPermission('basic:warehouse-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInternalWarehouseExcel(@Valid InternalWarehousePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InternalWarehouseDO> list = internalWarehouseService.getInternalWarehousePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP库位.xls", "数据", InternalWarehouseRespVO.class,
                        BeanUtils.toBean(list, InternalWarehouseRespVO.class));
    }

    //启用和停用
    @PutMapping("/enable-list")
    @Operation(summary = "启用和停用ERP库位")
    @PreAuthorize("@ss.hasPermission('basic:warehouse-config:update')")
    public CommonResult<Boolean> enableDisable(@Valid @RequestBody List<InternalWarehouseEnableDisableReqVO> reqVOS) {
        internalWarehouseService.enableDisable(reqVOS);
        return success(true);
    }

}