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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.common.TenantIdsReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.materialcategory.MaterialCategoryRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.store.StoreManagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.store.StoreManageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.store.StoreManageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MaterialCategoryDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.StoreManageDO;
import cn.iocoder.yudao.module.system.service.biz.StoreManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Arrays;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 仓库管理")
@RestController
@RequestMapping("/store/manage")
@Validated
public class StoreManageController {

    @Resource
    private StoreManageService storeManageService;

    @PostMapping("/create")
    @Operation(summary = "创建仓库")
    public CommonResult<Long> create(@Valid @RequestBody StoreManageSaveReqVO createReqVO) {
        return success(storeManageService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仓库")
    @TenantIgnore
    public CommonResult<Boolean> update(@Valid @RequestBody StoreManageSaveReqVO updateReqVO) {
        storeManageService.update(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得仓库详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @TenantIgnore
    public CommonResult<StoreManageRespVO> get(@RequestParam("id") Long id) {
        StoreManageDO store = storeManageService.get(id);
        return success(BeanUtils.toBean(store, StoreManageRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得仓库列表")
    @TenantIgnore
    public CommonResult<List<StoreManageDO>> list(@Valid TenantIdsReqVO tenantIdsReqVO) {
        Long[] tenantIds = tenantIdsReqVO.getTenantIds();
        if(tenantIds == null || tenantIds.length == 0){
            tenantIds = new Long[]{TenantContextHolder.getTenantId()};
        }
        List<StoreManageDO> list = storeManageService.list(tenantIds);
        return success(BeanUtils.toBean(list, StoreManageDO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仓库分页")
    @TenantIgnore // 允许上级通过传 tenantIds 查看多个下级数据
    public CommonResult<PageResult<StoreManageRespVO>> page(@Valid StoreManagePageReqVO pageReqVO) {
        // tenantIds 前端可传（查多个下级）；不传则默认当前租户
        if (pageReqVO.getTenantIds() == null || pageReqVO.getTenantIds().length == 0) {
            pageReqVO.setTenantIds(new Long[]{TenantContextHolder.getTenantId()});
        }
        PageResult<StoreManageDO> pageResult = storeManageService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreManageRespVO.class));
    }

}

