...
|
...
|
@@ -8,6 +8,8 @@ import java.util.Set; |
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
...
|
...
|
@@ -20,80 +22,97 @@ import com.yoho.search.dal.model.ProductAttributePropertyValues; |
|
|
|
|
|
/**
|
|
|
* 从product_attribute_property_values和product_attribute获取skn的AttributeNames
|
|
|
*
|
|
|
* @author gemingdan
|
|
|
*
|
|
|
*/
|
|
|
@Service
|
|
|
public class ProductAttributeService {
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductAttributeService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private ProductAttributeMapper productAttributeMapper;
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private ProductAttributePropertyValuesMapper productAttributePropertyValuesMapper;
|
|
|
|
|
|
public Map<Integer,String> selectBySkns(List<Integer> skns){
|
|
|
Map<Integer,String> result= new HashMap<Integer,String>();
|
|
|
List<ProductAttributePropertyValues> attributeList =productAttributePropertyValuesMapper.selectBySkns(skns);
|
|
|
if(attributeList==null){
|
|
|
return result;
|
|
|
|
|
|
public Map<Integer, String> selectBySkns(List<Integer> skns) {
|
|
|
if (skns == null || skns.isEmpty()) {
|
|
|
return new HashMap<Integer, String>();
|
|
|
}
|
|
|
Set<Integer> attributeIds=new HashSet<Integer>();
|
|
|
attributeList.forEach(a->{
|
|
|
attributeIds.add(a.getAttributeId());
|
|
|
});
|
|
|
if(attributeIds.size()==0){
|
|
|
return result;
|
|
|
List<ProductAttributePropertyValues> attributePropertyValues = productAttributePropertyValuesMapper.selectBySkns(skns);
|
|
|
if (attributePropertyValues == null || attributePropertyValues.isEmpty()) {
|
|
|
return new HashMap<Integer, String>();
|
|
|
}
|
|
|
List<ProductAttribute> valueList=productAttributeMapper.selectByAttributeIds(attributeIds);
|
|
|
if(valueList==null||valueList.size()==0){
|
|
|
return result;
|
|
|
Set<Integer> attributeIds = new HashSet<Integer>();
|
|
|
for (ProductAttributePropertyValues attributePropertyValue : attributePropertyValues) {
|
|
|
attributeIds.add(attributePropertyValue.getAttributeId());
|
|
|
}
|
|
|
Map<Integer, ProductAttribute> ProductAttributeMap = valueList.stream().parallel().collect(Collectors.toMap(ProductAttribute::getAttributeId, (p) -> p));
|
|
|
Map<Integer,Set<String>> resultSet= new HashMap<Integer,Set<String>>();
|
|
|
attributeList.forEach(a->{
|
|
|
if(ProductAttributeMap.get(a.getAttributeId())==null||a.getAttributeValueId()==null){
|
|
|
return;
|
|
|
}
|
|
|
ProductAttribute pa= ProductAttributeMap.get(a.getAttributeId());
|
|
|
String[] attributeValueIds = a.getAttributeValueId().split(",");
|
|
|
if(pa.getAttributeValues()==null||pa.getAttributeValues().indexOf("[")<0){
|
|
|
return;
|
|
|
}
|
|
|
JSONArray values = JSONArray.parseArray(pa.getAttributeValues());
|
|
|
Set<String> attributeNameSet=new HashSet<String>();
|
|
|
values.forEach(v->{
|
|
|
JSONObject vobj=(JSONObject)v;
|
|
|
for(int i=0;i<attributeValueIds.length;i++){
|
|
|
Integer id=(Integer)vobj.get("id");
|
|
|
if(id!=null&&attributeValueIds[i].endsWith(id.toString())){
|
|
|
attributeNameSet.add((String)vobj.get("name"));
|
|
|
List<ProductAttribute> productAttributes = productAttributeMapper.selectByAttributeIds(attributeIds);
|
|
|
if (productAttributes == null || productAttributes.isEmpty()) {
|
|
|
return new HashMap<Integer, String>();
|
|
|
}
|
|
|
Map<Integer, ProductAttribute> productAttributeMap = productAttributes.stream().parallel().collect(Collectors.toMap(ProductAttribute::getAttributeId, (p) -> p));
|
|
|
|
|
|
Map<Integer, Set<String>> sknToNameSetMap = new HashMap<Integer, Set<String>>();
|
|
|
for (ProductAttributePropertyValues a : attributePropertyValues) {
|
|
|
try {
|
|
|
// 1、判断ProductAttribute是否存在,以及对应数据的合法性
|
|
|
if (productAttributeMap.get(a.getAttributeId()) == null || a.getAttributeValueId() == null) {
|
|
|
continue;
|
|
|
}
|
|
|
ProductAttribute productAttribute = productAttributeMap.get(a.getAttributeId());
|
|
|
if (productAttribute.getAttributeValues() == null || productAttribute.getAttributeValues().indexOf("[") < 0) {
|
|
|
continue;
|
|
|
}
|
|
|
JSONArray attributeValues = JSONArray.parseArray(productAttribute.getAttributeValues());
|
|
|
|
|
|
// 2、判断attributeValueIds的合法性
|
|
|
String[] attributeValueIds = a.getAttributeValueId().split(",");
|
|
|
|
|
|
// 3、循环对比attribute_value_id和attribute_values
|
|
|
Set<String> attributeNameSet = new HashSet<String>();
|
|
|
for (int i = 0; i < attributeValues.size(); i++) {
|
|
|
JSONObject attributeValue = attributeValues.getJSONObject(i);
|
|
|
String idString = attributeValue.getString("id");
|
|
|
if (StringUtils.isBlank(idString)) {
|
|
|
continue;
|
|
|
}
|
|
|
for (String attributeValueId : attributeValueIds) {
|
|
|
if (attributeValueId.equals(idString)) {
|
|
|
attributeNameSet.add(attributeValue.getString("name"));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
if(attributeNameSet.size()==0){
|
|
|
return;
|
|
|
}
|
|
|
if(resultSet.get(a.getProductSkn())!=null){
|
|
|
attributeNameSet.addAll(resultSet.get(a.getProductSkn()));
|
|
|
if (attributeNameSet.size() == 0) {
|
|
|
continue;
|
|
|
}
|
|
|
if (sknToNameSetMap.get(a.getProductSkn()) != null) {
|
|
|
attributeNameSet.addAll(sknToNameSetMap.get(a.getProductSkn()));
|
|
|
}
|
|
|
sknToNameSetMap.put(a.getProductSkn(), attributeNameSet);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
}
|
|
|
resultSet.put(a.getProductSkn(),attributeNameSet);
|
|
|
});
|
|
|
resultSet.forEach((skn,set)->{
|
|
|
result.put(skn,StringUtils.join(set.toArray(), ",") );
|
|
|
});
|
|
|
}
|
|
|
// 构造结果
|
|
|
Map<Integer, String> result = new HashMap<Integer, String>();
|
|
|
for (Map.Entry<Integer, Set<String>> entry : sknToNameSetMap.entrySet()) {
|
|
|
result.put(entry.getKey(), StringUtils.join(entry.getValue().toArray(), ","));
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public void saveOrUpdate(ProductAttributePropertyValues productAttributePropertyValues) {
|
|
|
if(productAttributePropertyValues==null||productAttributePropertyValues.getId()==null){
|
|
|
if (productAttributePropertyValues == null || productAttributePropertyValues.getId() == null) {
|
|
|
return;
|
|
|
}
|
|
|
if(productAttributePropertyValuesMapper.selectByPrimaryKey(productAttributePropertyValues.getId())==null){
|
|
|
//插入
|
|
|
productAttributePropertyValuesMapper.insert(productAttributePropertyValues);
|
|
|
}else{
|
|
|
//更新
|
|
|
if (productAttributePropertyValuesMapper.selectByPrimaryKey(productAttributePropertyValues.getId()) == null) {
|
|
|
// 插入
|
|
|
productAttributePropertyValuesMapper.insertSelective(productAttributePropertyValues);
|
|
|
} else {
|
|
|
// 更新
|
|
|
productAttributePropertyValuesMapper.updateByPrimaryKey(productAttributePropertyValues);
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -107,22 +126,21 @@ public class ProductAttributeService { |
|
|
}
|
|
|
|
|
|
public void saveOrUpdateProductAttribute(ProductAttribute productAttribute) {
|
|
|
if(productAttribute==null||productAttribute.getAttributeId()==null){
|
|
|
if (productAttribute == null || productAttribute.getAttributeId() == null) {
|
|
|
return;
|
|
|
}
|
|
|
if(productAttributeMapper.selectByPrimaryKey(productAttribute.getAttributeId())==null){
|
|
|
//插入
|
|
|
if (productAttributeMapper.selectByPrimaryKey(productAttribute.getAttributeId()) == null) {
|
|
|
// 插入
|
|
|
productAttributeMapper.insert(productAttribute);
|
|
|
}else{
|
|
|
//更新
|
|
|
} else {
|
|
|
// 更新
|
|
|
productAttributeMapper.updateByPrimaryKey(productAttribute);
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public void deleteProductAttributeById(Integer id) {
|
|
|
productAttributeMapper.deleteByPrimaryKey(id);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|