package com.blueland.bpm.identity.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

import com.blueland.bpm.identity.OrgService;
import com.blueland.bpm.identity.model.Org;
import com.blueland.bpm.identity.util.OrgServiceRestImplCondition;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@Service("orgServiceImpl")
@Conditional(OrgServiceRestImplCondition.class)
public class OrgServiceRestImpl implements OrgService {
	private static final Log logger = LogFactory.getLog(OrgServiceRestImpl.class);
	
    private static final String GET_ORG_BY_ID_URL = "/org/getOrgById";
    
    private static final String FIND_SUB_ORG_URL = "/org/findSubOrg";
    
    private static final String LIST_ROOT_ORG_NODES_URL = "/org/listRootOrgNodes";
    
    private static final String FIND_PARENT_ORG_TREE_URL = "/org/findParentOrgTree";
    
    @Value("${rzdata.bpmcRestUrl}")
    String bpmcRestUrl;
    
	private String orgBizCode="";
	
	@Override
	public Org getOrgById(String orgId) {
		Org org =null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("orgBizCode", orgBizCode);
			paramsJson.put("orgId", orgId);
			String url = bpmcRestUrl + GET_ORG_BY_ID_URL;
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			org = JSONUtil.parseObj(result).toBean(Org.class);
		}catch(Exception e) {
			return new Org();
		}
		return org;
	}

	@Override
	public List<Org> findSubOrg(String orgId) {
		List<Org> orgList = null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("orgBizCode", orgBizCode);
			paramsJson.put("orgId", orgId);
			String url = bpmcRestUrl + FIND_SUB_ORG_URL;
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			orgList = JSONUtil.parseArray(result).toList(Org.class);
		}catch(Exception e) {
			return null;
		}
		return orgList;
	}

	@Override
	public List<Org> listRootOrgNodes() {
		List<Org> orgList = null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("orgBizCode", orgBizCode);
			String url = bpmcRestUrl + LIST_ROOT_ORG_NODES_URL;
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			orgList = JSONUtil.parseArray(result).toList(Org.class);
		}catch(Exception e) {
			return null;
		}
		return orgList;
	}

	@Override
	public List<Org> findParentOrgTree(String orgId) {
		List<Org> orgList = null;
		try {
			JSONObject paramsJson = new JSONObject();
			paramsJson.put("orgBizCode", orgBizCode);
			paramsJson.put("orgId", orgId);
			String url = bpmcRestUrl + FIND_PARENT_ORG_TREE_URL;
			String result = HttpUtil.post(url, JSONUtil.toJsonStr(paramsJson));
			orgList = JSONUtil.parseArray(result).toList(Org.class);
		}catch(Exception e) {
			return null;
		}
		return orgList;
	}

	@Override
	public List<Org> findSubOrgTree(String orgId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Org> findParentOrgTree(Org org) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Org> findParentOrgTree(String orgId, boolean isIncludeSelf) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Org> findParentOrgTree(String orgId, Integer orgLevel) {
		return findParentOrgTree(orgId);
	}

	@Override
	public List<Org> findOrgByEntityCriteria(Org org) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Org> findOrgByOrgIds(List<String> orgIds) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean hasSubOrgs(String orgId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isInOrg(String parentOrgId, String subOrgId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<Org> findAllCompany() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Org> findAllDept() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getFullPathName(String orgId, String pjStr) {
		String result = "";
		List<Org> parentOrgList = findParentOrgTree(orgId);
		for(Org parentOrg : parentOrgList) {
			result = result + parentOrg.getOrgName() + pjStr;
		}
		Org org = getOrgById(orgId);
		result = result + org.getOrgName();
		return result;
	}
	
	@Override
	public Org queryProvincialOrgNode(String orgBizCode, String orgUnitId) {
		// TODO Auto-generated method stub
		return null;
	}

}
