package com.zy.asrs.common.wms.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zy.asrs.common.wms.entity.Tag; import com.zy.asrs.common.wms.mapper.TagMapper; import com.zy.asrs.common.wms.service.TagService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zy.asrs.framework.common.Cools; import com.zy.asrs.framework.exception.CoolException; import org.springframework.stereotype.Service; import java.util.Date; @Service("manTagService") public class TagServiceImpl extends ServiceImpl implements TagService { @Override public synchronized Tag getTop(Long hostId) { Tag top = this.getOne(new LambdaQueryWrapper().eq(Tag::getLevel, 1).eq(Tag::getHostId, hostId)); if (top == null) { top = new Tag(); top.setName("全部"); top.setType(0); top.setLevel(1); top.setStatus(1); top.setSort(0); top.setHostId(hostId); Integer insert = this.baseMapper.insert(top); if (insert == 0) { throw new CoolException("服务器异常"); } } return top; } @Override public Tag getTag(String firstTagName, String secondTagName, Long hostId) { Date now = new Date(); Tag tag = this.getOne(new LambdaQueryWrapper() .eq(Tag::getName, "全部") .eq(Tag::getHostId, hostId) .isNull(Tag::getParentId)); if (tag == null) { throw new CoolException("分类系统异常"); } if (Cools.isEmpty(firstTagName)) { //一级分类,直接返回根分类(全部) return tag; } if (!Cools.isEmpty(firstTagName)) { //存在一级分类,查询一级分类 Tag firstTag = this.getOne(new LambdaQueryWrapper() .eq(Tag::getName, firstTagName) .eq(Tag::getHostId, hostId)); if (firstTag == null) { //一级分类为空,创建一级分类 firstTag = new Tag(); firstTag.setName(firstTagName); firstTag.setParentId(tag.getId()); firstTag.setStatus(1); firstTag.setHostId(hostId); firstTag.setCreateTime(now); firstTag.setUpdateTime(now); this.save(firstTag); } if (!Cools.isEmpty(secondTagName)) { //存在二级分类,查询二级分类 Tag secondTag = this.getOne(new LambdaQueryWrapper() .eq(Tag::getName, secondTagName) .eq(Tag::getHostId, hostId)); if (secondTag == null) { //二级分类为空,创建二级分类 secondTag = new Tag(); secondTag.setName(secondTagName); secondTag.setParentId(firstTag.getId()); secondTag.setStatus(1); secondTag.setHostId(hostId); secondTag.setCreateTime(now); secondTag.setUpdateTime(now); this.save(secondTag); return secondTag; } return secondTag; }else { return firstTag;//不存在二级分类,直接返回一级分类 } } return tag; } }