package top.continew.admin.controller.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.business.model.resp.ModelCategoryResp;
import top.continew.admin.business.service.ModelCategoryService;
import top.continew.admin.system.model.query.DictQuery;
import top.continew.admin.system.model.req.DictReq;
import top.continew.admin.system.model.resp.DictResp;
import top.continew.admin.system.service.DictService;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * 字典管理 API
 *
 * @author Charles7c
 * @since 2023/9/11 21:29
 */
@Tag(name = "字典管理 API")
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/system/dict", api = {Api.LIST, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE})
public class DictController extends BaseController<DictService, DictResp, DictResp, DictQuery, DictReq> {

    private final ModelCategoryService modelCategoryService;

    @Operation(summary = "获取模型分类列表", description = "获取模型分类列表")
    @GetMapping(value = "/modelCategory/list")
    public R getModelCategoryList() {
        return R.ok(modelCategoryService.list());
    }

    @Operation(summary = "获取模型分类树", description = "获取模型分类树")
    @GetMapping(value = "/modelCategory/tree")
    public R getModelCategoryTree() {
        List<ModelCategoryResp> categoryRespList = BeanUtil.copyToList(modelCategoryService
            .list(), ModelCategoryResp.class);

        List<Tree<Long>> treeList = TreeUtil.build(categoryRespList, 0L, (data, tree) -> {
            tree.setId(data.getId());
            tree.setName(data.getName());
            tree.setParentId(data.getParentId());
            tree.putExtra("type", data.getType());
        });
        return R.ok(treeList);
    }
}