package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.api.kingdee.KingdeeApi;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.WarehouseConfigPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.WarehouseConfigRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.WarehouseConfigSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.WarehouseConfigDO;
import cn.iocoder.yudao.module.system.service.biz.WarehouseConfigService;
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.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

/**
 * @Author: jinyd
 * @CreateTime: 2025/3/12  16:41
 * @Description:
 * @Version: 4.0
 */


@Tag(name = "管理后台 - 仓库设置")
@RestController
@RequestMapping("/warehouse/config")
@Validated
public class WarehouseConfigController {

    @Resource
    private WarehouseConfigService configService;


    @Resource
    private KingdeeApi kingdeeApi;

    @PostMapping("/create")
    @Operation(summary = "创建仓库设置")
    @TenantIgnore
    public CommonResult<Long> createConfig(@Valid @RequestBody WarehouseConfigSaveReqVO createReqVO) {
        createReqVO.setTenantId(createReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(configService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库设置")
    @TenantIgnore
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody WarehouseConfigSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(updateReqVO.getTenantId() == null ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        configService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库设置")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        configService.deleteConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<WarehouseConfigRespVO> getConfig(@RequestParam("id") Long id) {
        WarehouseConfigDO config = configService.getConfig(id);
        return success(BeanUtils.toBean(config, WarehouseConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库设置分页")
    @TenantIgnore
    public CommonResult<PageResult<WarehouseConfigRespVO>> getConfigPage(@Valid WarehouseConfigPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<WarehouseConfigDO> pageResult = configService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WarehouseConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仓库设置 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigExcel(@Valid WarehouseConfigPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WarehouseConfigDO> list = configService.getConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仓库设置.xls", "数据", WarehouseConfigRespVO.class,
                BeanUtils.toBean(list, WarehouseConfigRespVO.class));
    }

}
