package cn.autoform.web.client.impl; import cn.autoform.bean.Tenant; import cn.autoform.db.entity.IpAndPortEntity; import cn.autoform.fw.exception.IEMRuntimeException; import cn.autoform.util.CacheUtil; import cn.autoform.util.HttpUtil; import cn.autoform.util.thread.Keys; import cn.autoform.util.thread.ThreadData; import cn.autoform.util.tool.JSONTool; import cn.autoform.util.tool.ObjectUtil; import cn.autoform.web.client.CscApiService; import cn.autoform.web.client.bean.*; import cn.autoform.web.client.conster.BaseConst; import cn.autoform.web.client.util.CscUtils; import cn.autoform.web.client.util.HttpClientTool; import cn.autoform.web.client.util.IpAndPortTool; import cn.autoform.web.client.util.OrgUtil; import com.alibaba.fastjson.TypeReference; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.ui.ModelMap; import java.util.*; import java.util.concurrent.TimeoutException; @Slf4j @Service public class CscApiServiceImpl implements CscApiService,BaseConst{ @Value("${autoForm.cscApi}") private String ProcessClient; @Autowired private IpAndPortTool ipAndPortTool; private static final String USER_NAME = "USER_NAME"; private static final Map ipMap = new HashMap<>(); static { ipMap.put("ByTenantId", "%s/csc-ie-user/rest/json/api/tenant/findOrganization"); ipMap.put("findRelationDetail", "%s/csc-ie-user/rest/json/api/organization/findRelationDetail"); ipMap.put("findTenantCompanys", "%s/csc-ie-user/rest/json/api/organization/findRelationDetail"); ipMap.put("findSystemList", "%s/csc-ie-user/rest/json/api/company/findSystemList"); ipMap.put("findOrganizationByCompanyId", "%s"); ipMap.put("findTenantsAndCompanys", "%s/csc-ie-user/rest/json/api/user/findTenantsAndCompanys"); ipMap.put("findUserOrgs", "%s"); } /** * 根据租户id查询租户下的公司列表 * @param * @return */ @Override public Tenant findTenantCompanys(Map map) { // TODO Auto-generated method stub return null; } /** * 根据租户id查询租户对应的组织机构信息 * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Map findOrganizationByTenantId(Map map){ Map maps = null; IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(ProcessClient); System.out.println("请求路径:"+String.format(ipMap.get("findOrganizationByTenantId"), ipAndPortEntity.getIp())); HttpClientTool hc = new HttpClientTool(String.format(ipMap.get("findOrganizationByTenantId"), ipAndPortEntity.getIp())); hc.setParams(CscUtils.getParam(new ModelMap("tenantId", map.get("tenantID")))); try { maps = hc.sendPost(j->JSONTool.toObj(j, new TypeReference>(){})); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } return maps; } /** * 根据公司id获取所有业务系统 * @return */ @Override public Company findSystemList(Map map) { // TODO Auto-generated method stub return null; } /** * 根据公司id获取该公司对应的组织结构信息 * @return */ @Override public Organizations findOrganizationByCompanyId(Map map) { // TODO Auto-generated method stub return null; } /** * 通过openId查询用户所属的租户和公司信息 * @return */ @Override public TenantsAndCompanys findTenantsAndCompanys(Map map) { // TODO Auto-generated method stub return null; } /** * 通过openId查询用户所属组织机构 */ @Override public Organizations findUserOrgs(Map map) { // TODO Auto-generated method stub return null; } /** * * 根据公司id获取公司下的组织机构用户岗位信息 * */ @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public List findRelationDetail(Map map) { Map maps = null; IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(ProcessClient); HttpClientTool hc = new HttpClientTool(String.format(ipMap.get("findRelationDetail"), ipAndPortEntity.getIp())); //hc.setBodyInfos(CscUtils.getParam(new ModelMap("companyId", map.get("companyId")))); hc.setParams(CscUtils.getParam(new ModelMap("companyId", map.get("companyId")))); try { maps = hc.sendPost(j->JSONTool.toObj(j, new TypeReference>(){})); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } //格式话组织机构数据 List orgs = JSONTool.toObj(JSONTool.toJson(maps),Organizations.class).getOrgs(); OrgUtil.organizationFormat(orgs); return orgs; } public TenantsAndCompanys findTenantsAndCompanys() { TenantsAndCompanys tenantsAndCompanys = null; List tt=new ArrayList<>(); IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(ProcessClient); try { System.out.println("url:"+String.format(ipMap.get("findTenantsAndCompanys"), ipAndPortEntity.getIp())); tenantsAndCompanys = HttpUtil.post( String.format(ipMap.get("findTenantsAndCompanys"), ipAndPortEntity.getIp()) , null, CscUtils.getParam(new ModelMap("openId",ThreadData.get(Keys.OPEN_ID)/*"e1b50368384f4a27"*/)) , null, new TypeReference(){}); System.out.println("返回结果:"+JSONTool.toJson(tenantsAndCompanys)); if(ObjectUtil.empty(tenantsAndCompanys.getTenants())){ System.out.println("当前无租户"); return null; } for (cn.autoform.web.client.bean.Tenant ts : tenantsAndCompanys.getTenants()) { List com=new ArrayList<>(); for (Company ts1 : ts.getCompanys()) { Company sys = findSystemList(ts1.getId()); ts1.setData(sys.getData()); com.add(ts1); } ts.setCompanys(com); tt.add(ts); } tenantsAndCompanys.setTenants(tt); return tenantsAndCompanys; } catch (Exception e) { throw new IEMRuntimeException(N0002)/*.setchildIEMExc(changHongProperties.getPassUrl(PassPortName.FIND_USER_TENANTSANDCOMPANYS, null))*/; } } public Company findSystemList(Integer id) { IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(ProcessClient); Company sys = null; try { sys=HttpUtil.post(String.format(ipMap.get("findSystemList"), ipAndPortEntity.getIp()) , null, CscUtils.getParam(new ModelMap("companyId", id)) //ThreadData.get(COMPANY_ID) 应该传入的参数 , null, new TypeReference(){}); System.out.println("公司ID:"+JSONTool.toJson(sys)); return sys; } catch (Exception e) { throw new IEMRuntimeException(C0002).setchildIEMExc("error"); } } @SuppressWarnings("unchecked") @Override public Organizations findRelationDetail(Integer companyId) { Map maps = null; Organizations orgs = null; IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(ProcessClient); HttpClientTool hc = new HttpClientTool(String.format(ipMap.get("findRelationDetail"), ipAndPortEntity.getIp())); //hc.setBodyInfos(CscUtils.getParam(new ModelMap("companyId", map.get("companyId")))); hc.setParams(CscUtils.getParam(new ModelMap("companyId", companyId))); try { maps = hc.sendPost(j->JSONTool.toObj(j, new TypeReference>(){})); orgs = JSONTool.toObj(JSONTool.toJson(maps),Organizations.class); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } //格式话组织机构数据 return orgs; } @SuppressWarnings("unchecked") @Override public Organization findOrganization(Integer tenantID) { Map maps = null; Organization orgs = null; IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(ProcessClient); HttpClientTool hc = new HttpClientTool(String.format(ipMap.get("ByTenantId"), ipAndPortEntity.getIp())); hc.setParams(CscUtils.getParam(new ModelMap("tenantId", tenantID))); try { maps = hc.sendPost(j->JSONTool.toObj(j, new TypeReference>(){})); orgs = JSONTool.toObj(JSONTool.toJson(maps.get("organization")),Organization.class); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } return orgs; } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override // @Cacheable(value="orgList") public List findRelationDetail() { return CacheUtil.get(String.format("orgList:%s:%s", ThreadData.get(Keys.TOKEN), ThreadData.get(Keys.COMPANY_ID)), List.class, 1000*60*60*24, ()->{ Organizations maps = null; IpAndPortEntity ipAndPortEntity = ipAndPortTool.queryIpAndPortByServiceName(ProcessClient); HttpClientTool hc = new HttpClientTool(String.format(ipMap.get("findRelationDetail"), ipAndPortEntity.getIp())); hc.setParams(CscUtils.getParam(new ModelMap("companyId", ThreadData.get(Keys.COMPANY_ID)))); hc.setCookie(null); try { maps = hc.sendPost(j->JSONTool.toObj(j, new TypeReference(){})); } catch (TimeoutException e) { e.printStackTrace(); log.error("查询组织机构错误!"); } return Optional.ofNullable(maps).map(Organizations::getOrgs).orElse(Collections.EMPTY_LIST); }); } //匹配人员Id @Override public String getUserId(String openId) { if(ObjectUtil.empty(openId)){ return " "; } return getUser(findRelationDetail(), openId).map(User::getId).orElse(""); } public Optional getUser(List children, String openId){ if (ObjectUtil.empty(children)) { return Optional.empty(); } Optional result; for(Organization o :children){ if(!ObjectUtil.empty(o.getUsers())){ for(User u: o.getUsers()){ if(openId.equals(u.getOpenId()) || Objects.equals(openId,u.getPhone())){ return Optional.ofNullable(u); } } } result = getUser(o.getChildren(), openId); if(result.isPresent()){ return result; } } return Optional.empty(); } // @Cacheable(USER_NAME) @Override public String getUserName(String openId) { if(openId==null || "".equals(openId)){ return " "; } return getUser(findRelationDetail(), openId).map(User::getName).orElse(""); } }