package com.kmall.api.service; import com.google.common.collect.Maps; import com.kmall.api.dao.ApiGoodsSpecificationMapper; import com.kmall.api.entity.GoodsSpecificationVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class ApiGoodsSpecificationService { @Autowired private ApiGoodsSpecificationMapper apiGoodsSpecificationMapper; public GoodsSpecificationVo queryObject(Integer id) { return apiGoodsSpecificationMapper.queryObject(id); } public List queryList(Map map) { return apiGoodsSpecificationMapper.queryList(map); } public int queryTotal(Map map) { return apiGoodsSpecificationMapper.queryTotal(map); } public void save(GoodsSpecificationVo goods) { apiGoodsSpecificationMapper.save(goods); } public void update(GoodsSpecificationVo goods) { apiGoodsSpecificationMapper.update(goods); } public void delete(Integer id) { apiGoodsSpecificationMapper.delete(id); } public void deleteBatch(Integer[] ids) { apiGoodsSpecificationMapper.deleteBatch(ids); } public String[] queryNamesByIds(String[] ids) { String[] goodsSepcifitionValue = null; Map specificationParam = Maps.newHashMap(); specificationParam.put("ids", ids); List specificationEntities = queryList(specificationParam); goodsSepcifitionValue = new String[specificationEntities.size()]; for (int i = 0; i < specificationEntities.size(); i++) { goodsSepcifitionValue[i] = specificationEntities.get(i).getValue(); } return goodsSepcifitionValue; } /** * 按规格名称分组 */ public List queryByGoodsIdGroupByNames(Long goodsId) { Map specificationParam = Maps.newHashMap(); specificationParam.put("fields", "gs.*, s.name"); specificationParam.put("goods_id", goodsId); specificationParam.put("specification", true); specificationParam.put("sidx", "s.sort_order"); specificationParam.put("order", "asc"); List goodsSpecificationEntityList = apiGoodsSpecificationMapper.queryList(specificationParam); List specificationList = new ArrayList(); //按规格名称分组 for (int i = 0; i < goodsSpecificationEntityList.size(); i++) { GoodsSpecificationVo specItem = goodsSpecificationEntityList.get(i); // List tempList = null; for (int j = 0; j < specificationList.size(); j++) { if (specificationList.get(j).get("specification_id").equals(specItem.getSpecification_id())) { tempList = (List) specificationList.get(j).get("valueList"); break; } } // if (null == tempList) { Map temp = Maps.newHashMap(); temp.put("specification_id", specItem.getSpecification_id()); temp.put("name", specItem.getName()); tempList = new ArrayList(); tempList.add(specItem); temp.put("valueList", tempList); specificationList.add(temp); } else { for (int j = 0; j < specificationList.size(); j++) { if (specificationList.get(j).get("specification_id").equals(specItem.getSpecification_id())) { tempList = (List) specificationList.get(j).get("valueList"); tempList.add(specItem); break; } } } } return specificationList; } }