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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
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.materialcategory.MaterialCategorySaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MaterialCategoryDO;
import cn.iocoder.yudao.module.system.service.biz.MaterialCategoryService;
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.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("/materialCategory")
@Validated
public class MaterialCategoryController {

    @Resource
    private MaterialCategoryService materialCategoryService;

    // ========== CRUD ==========

    @PostMapping("/create")
    @Operation(summary = "创建物资类别")
    public CommonResult<Long> create(@Valid @RequestBody MaterialCategorySaveReqVO reqVO) {
        // tenantId 可由前端传；不传则默认当前租户
        if (reqVO.getTenantId() == null) {
            reqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        return success(materialCategoryService.create(reqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物资类别")
    @TenantIgnore
    public CommonResult<Boolean> update(@Valid @RequestBody MaterialCategorySaveReqVO reqVO) {
        if (reqVO.getTenantId() == null) {
            reqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        materialCategoryService.update(reqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得物资类别")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<MaterialCategoryRespVO> get(@RequestParam("id") Long id) {
        MaterialCategoryDO data = materialCategoryService.get(id);
        return success(BeanUtils.toBean(data, MaterialCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得物资类别列表")
    @TenantIgnore
    public CommonResult<List<MaterialCategoryRespVO>> list(@Valid TenantIdsReqVO reqVO) {
        Long[] tenantIds = reqVO.getTenantIds();
        if (tenantIds == null || tenantIds.length == 0) {
            tenantIds = Arrays.asList(TenantContextHolder.getTenantId()).toArray(new Long[0]);
        }
        List<MaterialCategoryDO> list = materialCategoryService.getList(tenantIds);
        return success(BeanUtils.toBean(list, MaterialCategoryRespVO.class));
    }

    // ========== Tree ==========

    @GetMapping("/getTree")
    @Operation(summary = "获取物资类别树形结构")
    @TenantIgnore
    public CommonResult<List<MaterialCategoryDO>> getTree(@Valid TenantIdsReqVO reqVO) {
        Long[] tenantIds = reqVO.getTenantIds();
        tenantIds = (tenantIds == null || tenantIds.length == 0)
                ? Arrays.asList(TenantContextHolder.getTenantId()).toArray(new Long[0])
                : tenantIds;
        return success(materialCategoryService.getTree(tenantIds));
    }
}
