package com.cm.common.plugin.oauth.service.department.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.department.IDepartmentService;
import com.cm.common.plugin.oauth.token.ClientTokenManager;
import com.cm.common.plugin.utils.RestTemplateUtil;
import com.cm.common.pojo.dtos.department.DepartmentSortDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DepartmentServiceImpl
 * @Description: 组织部门
 * @Author: WangGeng
 * @Date: 2019-07-29 21:46
 * @Version: 1.0
 **/
@Service
public class DepartmentServiceImpl extends AbstractService implements IDepartmentService {

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

    @Override
    public JSONArray listDepartments(Map<String, Object> params) throws AccessTokenException, SearchException {
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        String result = restTemplateUtil.doPostFormNormal(String.format(IApiConsts.LIST_DEPARTMENT, apiPathProperties.getUserCenter(), params.get("parentId").toString()), params);
        if (result == null) {
            throw new AccessTokenException("认证失败");
        }
        if (result.isEmpty()) {
            throw new SearchException("获取组织部门列表失败");
        }
        return JSONArray.parseArray(result);
    }

    @Override
    public JSONArray listDepartmentsAllByParentId(String parentId) throws AccessTokenException, SearchException {
        Map<String, Object> params = getHashMap(2);
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        String result = restTemplateUtil.doPostFormNormal(String.format(IApiConsts.LIST_ALL_DEPARTMENT, apiPathProperties.getUserCenter(), parentId), params);
        if (result == null) {
            throw new AccessTokenException("认证失败");
        }
        if (result.isEmpty()) {
            throw new SearchException("获取组织部门列表失败");
        }
        return JSONArray.parseArray(result);
    }

    @Override
    public JSONArray listZTreeDepartmentWithUser(String departmentParentId) throws AccessTokenException, SearchException {
        Map<String, Object> params = new HashMap<>(2);
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        params.put("id", departmentParentId);
        String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_ZTREE_DEPARTMENT_WITH_USER, apiPathProperties.getUserCenter()), params);
        searchResourceResult(result, "获取部门人员列表失败");
        return JSONArray.parseArray(result);
    }

    @Override
    public JSONArray listZTreeDepartment(Map<String, Object> params) throws AccessTokenException, SearchException {
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_ZTREE_DEPARTMENT, apiPathProperties.getUserCenter()), params);
        searchResourceResult(result, "获取部门人员列表失败");
        return JSONArray.parseArray(result);
    }

    @Override
    public List<DepartmentSortDTO> listSortByType(String type) {
        Map<String, Object> params = new HashMap<>(2);
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        params.put("departmentType", type);
        String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_DEPARTMENT_SORT, apiPathProperties.getUserCenter()), params);
        searchResourceResult(result, "获取部门列表失败");
        return JSONArray.parseArray(result, DepartmentSortDTO.class);
    }

    @Override
    public List<DepartmentSortDTO> listSortByParentId(String parentId) {
        Map<String, Object> params = new HashMap<>(2);
        params.put(IApiConsts.ACCESS_TOKEN, ClientTokenManager.getInstance().getClientToken().getAccessToken());
        params.put("departmentParentId", parentId);
        String result = restTemplateUtil.doGetFormNormal(String.format(IApiConsts.LIST_DEPARTMENT_SORT, apiPathProperties.getUserCenter()), params);
        searchResourceResult(result, "获取部门列表失败");
        return JSONArray.parseArray(result, DepartmentSortDTO.class);
    }
}
