package com.cm.common.plugin.controller.apis.tree;

import com.alibaba.fastjson.JSONArray;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.ParamsException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.service.tree.ITreeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: TreeController
 * @Description: 树结构
 * @Author: WangGeng
 * @Date: 2019-07-02 22:09
 * @Version: 1.0
 **/
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/tree")
public class TreeController extends AbstractController {

    @Autowired
    private ITreeService treeService;

    @PostMapping("listtree/{resultType}")
    public JSONArray listTree(@PathVariable("resultType") String resultType) throws ParamsException, SearchException {
        if (!ITreeService.RESULT_TYPE_JUST_LIST.equals(resultType)
                && !ITreeService.RESULT_TYPE_CODE_SUCCESS_RESULT_DATA.equals(resultType)
                && !ITreeService.RESULT_TYPE_STATE_SUCCESS_RESULT_DATA.equals(resultType)) {
            throw new ParamsException("结果类型错误，只能是justList，stateSuccessResultData，codeSuccessResultData");
        }
        Map<String, Object> params = requestParams();
        if (params.get("apiUri") == null || StringUtils.isEmpty(params.get("apiUri").toString())) {
            throw new ParamsException("apiUri参数错误");
        }
        if (ITreeService.RESULT_TYPE_CODE_SUCCESS_RESULT_DATA.equals(resultType)) {
            return treeService.listTreeCodeSuccessResultData(params);
        }
        if (ITreeService.RESULT_TYPE_STATE_SUCCESS_RESULT_DATA.equals(resultType)) {
            return treeService.listTreeStateSuccessResultData(params);
        }
        return treeService.listTreeJustList(params);
    }

}
