ErpApiServiceHelper.java 3.97 KB
package com.yoho.unions.common.utils;

import com.netflix.config.DynamicPropertyFactory;
import com.yoho.service.model.union.request.UserInfoBO;
import com.yoho.tools.common.beans.Response;
import com.yoho.unions.common.enums.CodeEnum;
import com.yoho.unions.common.enums.ErpApiEnum;
import com.yoho.unions.common.model.ERPProfileRequestBO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class ErpApiServiceHelper {

	static Logger log = LoggerFactory.getLogger(ErpApiServiceHelper.class);
	@Resource
    private RestTemplate restTemplate;

	private static String ERP_DOMAIN = DynamicPropertyFactory.getInstance().getStringProperty("erp.domain", "").get();


	/**
	 * 根据pid,查询用户
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public UserInfoBO getUserByPid(String pid) {
		log.info("getUserByPid with pid={}", pid);
		ERPProfileRequestBO profile = new ERPProfileRequestBO();
		profile.setPid(Integer.parseInt(pid));
		Response<Map<String, Object>> resp = restTemplate.postForObject(ERP_DOMAIN + ErpApiEnum.GET_USER_BY_PID.getUrl(), profile, Response.class);
		if (resp == null || resp.getCode() != CodeEnum.SUCCESS.getCode()) {
			log.warn("getUserByPid error with pid={}", pid);
			return null;
		}
		UserInfoBO user = toSysUserResponse(resp.getData());

		return user;
	}
	
	/**
	 * 查询用户列表
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<UserInfoBO> getUsersByPids(List<Integer> pids) {
		log.info("getUsers with pids={}", pids == null ? 0 : pids.size());
		
		if (CollectionUtils.isEmpty(pids)) {
			log.warn("getUsersByPids error. pids is empty");
			return null;
		}
		
		ERPProfileRequestBO profile = new ERPProfileRequestBO();
		profile.setpIds(pids);
		profile.setPageNo(1);
		profile.setPageSize(pids.size());
		Response<Map<String, Object>> resp = restTemplate.postForObject(ERP_DOMAIN + ErpApiEnum.GET_USER_LIST.getUrl(), profile, Response.class);
		if (resp == null || resp.getCode() != CodeEnum.SUCCESS.getCode()) {
			log.warn("getUsers error with pids={}", pids.size());
			return null;
		}
		Map<String, Object> map = resp.getData();
		if (MapUtils.isEmpty(map)) {
			log.warn("getUsersList error. result is empty. with pids={}", pids.size());
			return null;
		}
		
		List<Map<String, Object>> records = (List<Map<String, Object>>) map.get("records");
		if (CollectionUtils.isEmpty(records)) {
			return null;
		}
		List<UserInfoBO> list = new ArrayList<>();
		for (Map<String, Object> m : records) {
			list.add(toSysUserResponse(m));
		}
		return list;
	}
	
	/**
	 * 对象转换
	 * 
	 * @param map
	 * @return
	 */
	private UserInfoBO toSysUserResponse(Map<String, Object> map) {
		if (MapUtils.isEmpty(map)) {
			return null;
		}
		UserInfoBO bo = new UserInfoBO();

		bo.setCreate_time(MapUtils.getString(map, "create_time"));
		bo.setEmail(MapUtils.getString(map, "email"));
		bo.setAccount(MapUtils.getString(map, "account"));
		bo.setPhone(MapUtils.getString(map, "phone"));
		bo.setPid(MapUtils.getInteger(map, "pid"));
		bo.setRole_id(MapUtils.getString(map, "role_id"));
		bo.setStatus(MapUtils.getString(map, "status"));
		bo.setTruename(MapUtils.getString(map, "truename"));

		bo.setAuth_site(MapUtils.getString(map, "auth_site"));
		bo.setCreate_date(MapUtils.getString(map, "create_date"));
		bo.setCreate_id(MapUtils.getString(map, "create_id"));
		bo.setDept_id(MapUtils.getString(map, "dept_id"));
		bo.setExpires(MapUtils.getString(map, "expires"));
		bo.setIdentity(MapUtils.getString(map, "identity"));
		bo.setLogin_time(MapUtils.getString(map, "login_time"));
		bo.setLogout_time(MapUtils.getString(map, "logout_time"));
		bo.setStaff_code(MapUtils.getString(map, "staff_code"));

		return bo;
	}


}