|
|
package com.yohobuy.platform.mvc.helper;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.netflix.config.DynamicPropertyFactory;
|
|
|
import com.yoho.tools.common.beans.Response;
|
|
|
import com.yohobuy.platform.common.enums.CodeEnum;
|
|
|
import com.yohobuy.platform.common.enums.ErpApiEnum;
|
|
|
import com.yohobuy.platform.model.users.response.UserInfoResponseBO;
|
|
|
import com.yohobuy.platform.mvc.bean.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
|
|
|
RestTemplate restTemplate;
|
|
|
|
|
|
private static String ERP_DOMAIN = DynamicPropertyFactory.getInstance().getStringProperty("erp.domain", "").get();
|
|
|
private static String ERP_STORAGE = DynamicPropertyFactory.getInstance().getStringProperty("erp.storage", "").get();
|
|
|
|
|
|
|
|
|
/**
|
|
|
* /account/profile/query_by_id http://192.168.102.47:9098/erp-gateway-web
|
|
|
/service/account/v1/Profile/getAllBrandProfileDataAuthorityApi http://serve.yohobuy.com
|
|
|
/account/profile/query_page http://192.168.102.47:9098/erp-gateway-web
|
|
|
/account/profileDataEntitlement/dataList http://192.168.102.47:9098/erp-gateway-web
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
* 根据pid,查询用户
|
|
|
* @param pid
|
|
|
* @return
|
|
|
*/
|
|
|
@SuppressWarnings("unchecked")
|
|
|
public UserInfoResponseBO 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;
|
|
|
}
|
|
|
UserInfoResponseBO user = toSysUserResponse(resp.getData());
|
|
|
|
|
|
return user;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 对象转换
|
|
|
*
|
|
|
* @param map
|
|
|
* @return
|
|
|
*/
|
|
|
private UserInfoResponseBO toSysUserResponse(Map<String, Object> map) {
|
|
|
if (MapUtils.isEmpty(map)) {
|
|
|
return null;
|
|
|
}
|
|
|
UserInfoResponseBO bo = new UserInfoResponseBO();
|
|
|
|
|
|
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.getString(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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 北京接口返回:is_all=ture 说明是全品牌,list就是空;is_all = false 说明不是全品牌 品牌id会在list
|
|
|
* 根据pid,获取用户有品牌权限品牌ID。
|
|
|
* 如果为-1, 不存在,所有品牌都不展示
|
|
|
* 如果为空集合,有所有品牌权限
|
|
|
* 如果不为空 ,有品牌权限品牌ID
|
|
|
*/
|
|
|
public List<Integer> getBrandIds(String pid) {
|
|
|
String url = ERP_STORAGE + "/service/account/v1/Profile/getAllBrandProfileDataAuthorityApi";
|
|
|
try {
|
|
|
Response<Map<String, Object>> response = restTemplate.postForObject(url, "["+pid+"]", Response.class);
|
|
|
log.info("call erp method, url:{} and response:{}.", url, response);
|
|
|
if (response == null || response.getCode() != 200) {
|
|
|
log.warn("call erp method fail, response:{}.",response);
|
|
|
// 所有品牌都不展示
|
|
|
return Lists.newArrayList(-1);
|
|
|
}
|
|
|
Map<String,Object> data= (Map<String,Object>) response.getData();
|
|
|
if ((boolean)data.get("is_all")) {
|
|
|
//有所有品牌权限
|
|
|
return Lists.newArrayList();
|
|
|
}
|
|
|
Object brandIdData = data.get("list");
|
|
|
if (brandIdData instanceof List && ((List)brandIdData).isEmpty()) {
|
|
|
//有所有品牌权限
|
|
|
return Lists.newArrayList();
|
|
|
}
|
|
|
List<Integer> brandIds=Lists.newArrayList();
|
|
|
List<String> brandIdStrs=(List<String>)brandIdData;
|
|
|
for (String brandId : brandIdStrs) {
|
|
|
brandIds.add(Integer.parseInt(brandId));
|
|
|
}
|
|
|
return brandIds;
|
|
|
} catch (Exception e) {
|
|
|
log.error("call erp method fail, url:{} and error message:{}.", url, e.getMessage());
|
|
|
// 所有品牌都不展示
|
|
|
return Lists.newArrayList(-1);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//根据pid获取店铺权限
|
|
|
public List<Integer> getShopIds(String pid) {
|
|
|
String url = ERP_DOMAIN + "/account/profileDataEntitlement/dataList";
|
|
|
List<Integer> list = new ArrayList<>();
|
|
|
try{
|
|
|
JSONObject req = new JSONObject();
|
|
|
req.put("pid",Integer.valueOf(pid));
|
|
|
req.put("type",2);
|
|
|
Response<Map<String, Object>> response = restTemplate.postForObject(url, req, Response.class);
|
|
|
log.info("getShopIds erp method, url:{} and response:{}.", url, response);
|
|
|
if(response.getCode() != 200){
|
|
|
log.warn("call erp method fail, response:{}.",response);
|
|
|
return Lists.newArrayList(-1);
|
|
|
}
|
|
|
Map<String,Object> shopsMap = response.getData();
|
|
|
boolean isAll = (boolean) shopsMap.get("isAll");
|
|
|
if(isAll){
|
|
|
//如果是全部权限,这是权限空
|
|
|
return list;
|
|
|
}
|
|
|
List<Map> shopList = (List) shopsMap.get("businessList");
|
|
|
if (CollectionUtils.isEmpty(shopList)) {
|
|
|
return Lists.newArrayList(-1);
|
|
|
}
|
|
|
for (Map m : shopList){
|
|
|
list.add(MapUtils.getInteger(m,"id"));
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
log.info("get shopids from erp-gateway-wab error e={}",e.getMessage());
|
|
|
return Lists.newArrayList(-1);
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
} |