package com.cm.common.plugin.oauth.service.position.impl;

import com.alibaba.fastjson.JSONArray;
import com.cm.common.base.AbstractService;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.exception.AccessTokenException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.IApiConsts;
import com.cm.common.plugin.oauth.service.position.IPositionService;
import com.cm.common.plugin.oauth.token.ClientTokenManager;
import com.cm.common.plugin.utils.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: PositionServiceImpl
 * @Description: 职位
 * @Author: WangGeng
 * @Date: 2019/8/30 5:34 下午
 * @Version: 1.0
 **/
@Service
public class PositionServiceImpl extends AbstractService implements IPositionService {

    @Autowired
    private RestTemplateUtil restTemplateUtil;
    @Autowired
    private ApiPathProperties apiPathProperties;

    @Override
    public JSONArray listUserPosition(Map<String, Object> params) throws AccessTokenException, SearchException {
        String result = restTemplateUtil.doPostForm(String.format(IApiConsts.LIST_USER_POSITION, apiPathProperties.getUserCenter(), params.get("userId").toString()), params);
        if (result == null) {
            throw new AccessTokenException("认证失败");
        }
        if (result.isEmpty()) {
            throw new SearchException("获取职位列表失败");
        }
        return JSONArray.parseArray(result);
    }

    @Override
    public JSONArray listZTreePosition(String positionParentId) throws AccessTokenException, SearchException {
        Map<String, Object> params = new HashMap<>(1);
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        params.put("id", positionParentId);
        String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_ZTREE_POSITION, apiPathProperties.getUserCenter()), params);
        if (result == null) {
            throw new AccessTokenException("认证失败");
        }
        if (result.isEmpty()) {
            throw new SearchException("获取列表失败");
        }
        return JSONArray.parseArray(result);
    }

}
