package com.yeshi.fanli.service.impl.lable; import java.util.ArrayList; import java.util.Date; import java.util.HashSet; import java.util.List; import javax.annotation.Resource; import javax.transaction.Transactional; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.yeshi.fanli.dao.mybatis.lable.LabelClassMapper; import com.yeshi.fanli.entity.bus.clazz.GoodsClass; import com.yeshi.fanli.entity.bus.clazz.GoodsSubClass; import com.yeshi.fanli.entity.bus.lable.Label; import com.yeshi.fanli.entity.bus.lable.LabelClass; import com.yeshi.fanli.exception.LabelClassException; import com.yeshi.fanli.service.inter.lable.LabelClassService; import com.yeshi.fanli.service.inter.lable.LabelService; @Service public class LabelClassServiceImpl implements LabelClassService { @Resource private LabelService labelService; @Resource private LabelClassMapper labelClassMapper; @Override public int insert(LabelClass record) throws LabelClassException{ return labelClassMapper.insert(record); } @Override public int updateByPrimaryKey(LabelClass record) throws LabelClassException{ return labelClassMapper.updateByPrimaryKey(record); } @Override public int updateByPrimaryKeySelective(LabelClass record) throws LabelClassException{ return labelClassMapper.updateByPrimaryKeySelective(record); } @Override public int deleteByPrimaryKey(Long id) throws LabelClassException{ return labelClassMapper.deleteByPrimaryKey(id); } @Override public LabelClass selectByPrimaryKey(Long id) throws LabelClassException { return labelClassMapper.selectByPrimaryKey(id); } @Override public List getByClassId(Long classId) throws LabelClassException { return labelClassMapper.getByClassId(classId); } @Override public List getBySubClassId(Long classId) throws LabelClassException { return labelClassMapper.getBySubClassId(classId); } @Override public List queryByClassId(int start, int count, Long classId) throws LabelClassException { return labelClassMapper.queryByClassId(start, count, classId); } @Override public int getCountQueryByClassId(Long classId) throws LabelClassException { return labelClassMapper.getCountQueryByClassId(classId); } @Override public List queryBySubClassId(int start, int count, Long classId) throws LabelClassException { return labelClassMapper.queryBySubClassId(start, count, classId); } @Override @Cacheable(value = "labelCache",key="'queryBySubClassIdCache-'+#start+'-'+#classId") public List queryBySubClassIdCache(int start, int count, Long classId) throws LabelClassException { return queryBySubClassId(start, count, classId); } @Override public List getRelationLabIds(int start, int count, Long classId) throws LabelClassException { /* 1、根据标签查询商品 */ List listLabId = null; List labelClassList = queryBySubClassIdCache(start, count, classId); if (labelClassList != null && labelClassList.size() > 0) { listLabId = new ArrayList(); for (LabelClass labelClass : labelClassList) { Label label = labelClass.getLabel(); listLabId.add(label.getId()); } } return listLabId; } @Override public int getCountQueryBySubClassId(Long subClassId) throws LabelClassException { return labelClassMapper.getCountQueryBySubClassId(subClassId); } @Override @Transactional public void addBatchClass(GoodsClass goodsClass,List labIdList) throws Exception { long recordId = goodsClass.getId(); if (labIdList == null || labIdList.size() == 0) { // 清空关联标签 labelClassMapper.deleteByClassId(recordId); return; } // 删除标签 List subList = new ArrayList(); // 查询现有关联标签 List hasList = labelClassMapper.getByClassId(recordId); // 已存在,则遍历哪些需要删除 if (hasList != null && hasList.size() > 0) { for (LabelClass lClass: hasList) { Long lablClassID = lClass.getId(); Label label = lClass.getLabel(); if (label != null) { Long labid = label.getId(); if (labIdList.contains(labid)) { // 包含,则从集合中删除 labid labIdList.remove(labid); } else { // 当前传递中不包含 那么需要删除 lablClassID subList.add(lablClassID); } } } } if (subList != null && subList.size() > 0) { for (Long id: subList) { labelClassMapper.deleteByPrimaryKey(id); } } if (labIdList != null && labIdList.size() > 0) { // 去重复 HashSet h = new HashSet(labIdList); labIdList.clear(); labIdList.addAll(h); for (Long labId: labIdList) { // 查询数据库 标签是否存在 Label label = labelService.selectByPrimaryKey(labId); if (label == null) { continue; } LabelClass labelClass = new LabelClass(); labelClass.setLabel(label); labelClass.setGoodClass(goodsClass); labelClass.setCreatetime(new Date()); labelClassMapper.insertSelective(labelClass); } } } @Override @Transactional public void addBatchSubClass(GoodsSubClass goodsSubClass,List labIdList) throws Exception { long recordId = goodsSubClass.getId(); // 删除标签 List subList = new ArrayList(); // 查询现有关联标签 List hasList = labelClassMapper.getBySubClassId(recordId); // 已存在,则遍历哪些需要删除 if (hasList != null && hasList.size() > 0) { for (LabelClass lClass: hasList) { Long lablClassID = lClass.getId(); Label label = lClass.getLabel(); if (label != null) { Long id = label.getId(); if (labIdList.contains(id)) { // 包含,则从集合中删除 labid labIdList.remove(id); } else { // 当前传递中不包含 那么需要删除 lablClassID subList.add(lablClassID); } } } } /* 删除标签 */ if (subList != null && subList.size() > 0) { for (Long id: subList) { labelClassMapper.deleteByPrimaryKey(id); } } /* 添加新增标签 */ if (labIdList != null && labIdList.size() > 0) { // 去重复 HashSet h = new HashSet(labIdList); labIdList.clear(); labIdList.addAll(h); for (Long labId: labIdList) { Label label = labelService.selectByPrimaryKey(labId); if (label == null) { continue; } LabelClass labelClass = new LabelClass(); labelClass.setLabel(label); labelClass.setCreatetime(new Date()); labelClass.setGoodsSubClass(goodsSubClass); labelClassMapper.insertSelective(labelClass); } } } @Override public int deleteByClassId(Long recordId) throws Exception { return labelClassMapper.deleteByClassId(recordId); } @Override public int deleteBySubClassId(Long recordId) throws Exception { return labelClassMapper.deleteBySubClassId(recordId); } }