|  |  |  | 
|---|
|  |  |  | package com.doumee.service.business.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.core.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.core.model.PageData; | 
|---|
|  |  |  | import com.doumee.core.model.PageWrap; | 
|---|
|  |  |  | import com.doumee.dao.business.JkLineMapper; | 
|---|
|  |  |  | import com.doumee.dao.business.model.JkLine; | 
|---|
|  |  |  | import com.doumee.service.business.third.model.LoginUserInfo; | 
|---|
|  |  |  | import com.doumee.service.business.third.model.PageData; | 
|---|
|  |  |  | import com.doumee.service.business.third.model.PageWrap; | 
|---|
|  |  |  | import com.doumee.core.utils.Constants; | 
|---|
|  |  |  | import com.doumee.core.utils.Utils; | 
|---|
|  |  |  | import com.doumee.dao.business.CategoryMapper; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.github.yulichang.query.MPJQueryWrapper; | 
|---|
|  |  |  | import com.github.yulichang.wrapper.MPJLambdaWrapper; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.shiro.SecurityUtils; | 
|---|
|  |  |  | import org.checkerframework.checker.units.qual.C; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.CollectionUtils; | 
|---|
|  |  |  | import org.springframework.validation.BindException; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Objects; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 分类信息表Service实现 | 
|---|
|  |  |  | 
|---|
|  |  |  | public class CategoryServiceImpl implements CategoryService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CategoryMapper categoryMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private JkLineMapper jkLineMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Integer create(Category category) { | 
|---|
|  |  |  | @Transactional(rollbackFor = {Exception.class, BindException.class}) | 
|---|
|  |  |  | public Integer create(Category category){ | 
|---|
|  |  |  | checkUnique(category); | 
|---|
|  |  |  | LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); | 
|---|
|  |  |  | if(loginUserInfo ==null){ | 
|---|
|  |  |  | 
|---|
|  |  |  | insert.setRemark(category.getRemark()); | 
|---|
|  |  |  | insert.setStatus(Constants.ZERO); | 
|---|
|  |  |  | insert.setSortnum(category.getSortnum()); | 
|---|
|  |  |  | insert.setBizType(category.getBizType()); | 
|---|
|  |  |  | insert.setImgurl(category.getImgurl()); | 
|---|
|  |  |  | insert.setType(Constants.ZERO); | 
|---|
|  |  |  | insert.setType(category.getType()); | 
|---|
|  |  |  | insert.setParentId(category.getParentId()); | 
|---|
|  |  |  | categoryMapper.insert(insert); | 
|---|
|  |  |  | if(insert.getBizType()!=null){ | 
|---|
|  |  |  | //清空其他bizType,只保留当前新增记录唯一有效 | 
|---|
|  |  |  | categoryMapper.update(null,new UpdateWrapper<Category>().lambda() | 
|---|
|  |  |  | .set(Category::getBizType,null) | 
|---|
|  |  |  | .eq(Category::getBizType,insert.getType()) | 
|---|
|  |  |  | .ne(Category::getId,insert.getId()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return insert.getId(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void deleteById(Integer id) { | 
|---|
|  |  |  | categoryMapper.deleteById(id); | 
|---|
|  |  |  | Category c = categoryMapper.selectById(id); | 
|---|
|  |  |  | if(c == null||Constants.equalsInteger(c.getIsdeleted(),Constants.ONE)){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.DATA_EMPTY); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(Constants.equalsInteger(c.getType(),Constants.FOUR)){ | 
|---|
|  |  |  | //如果是新线路,检查是否有子线路,有则不允许删除 | 
|---|
|  |  |  | if(jkLineMapper.selectCount(new MPJLambdaWrapper<JkLine>().eq(JkLine::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | .eq(JkLine::getCategoryId,id)) >0){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"主线路【"+c.getName()+"】下已存在子线路,不允许删除!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | categoryMapper.update(null,new UpdateWrapper<Category>().lambda().set(Category::getIsdeleted,Constants.ONE) | 
|---|
|  |  |  | .eq(Category::getId,id) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | if (CollectionUtils.isEmpty(ids)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | categoryMapper.deleteBatchIds(ids); | 
|---|
|  |  |  | for(Integer id :ids){ | 
|---|
|  |  |  | Category c = categoryMapper.selectById(id); | 
|---|
|  |  |  | if(c == null ||Constants.equalsInteger(c.getIsdeleted(),Constants.ONE)){ | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(Constants.equalsInteger(c.getType(),Constants.FOUR)){ | 
|---|
|  |  |  | //如果是新线路,检查是否有子线路,有则不允许删除 | 
|---|
|  |  |  | if(jkLineMapper.selectCount(new MPJLambdaWrapper<JkLine>().eq(JkLine::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | .eq(JkLine::getCategoryId,id)) >0){ | 
|---|
|  |  |  | throw  new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"主线路【"+c.getName()+"】下已存在子线路,不允许删除!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | categoryMapper.update(null,new UpdateWrapper<Category>().lambda().set(Category::getIsdeleted,Constants.ONE) | 
|---|
|  |  |  | .in(Category::getId,ids) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional(rollbackFor = {Exception.class, BindException.class}) | 
|---|
|  |  |  | public void updateById(Category category) { | 
|---|
|  |  |  | checkUnique(category); | 
|---|
|  |  |  | categoryMapper.updateById(category); | 
|---|
|  |  |  | if(category.getBizType()!=null){ | 
|---|
|  |  |  | //清空其他该bizType属性,只保留当前记录唯一有效 | 
|---|
|  |  |  | categoryMapper.update(null,new UpdateWrapper<Category>().lambda() | 
|---|
|  |  |  | .set(Category::getBizType,null) | 
|---|
|  |  |  | .eq(Category::getBizType,category.getBizType()) | 
|---|
|  |  |  | .ne(Category::getId,category.getId()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Category> queryList(Category category) { | 
|---|
|  |  |  | List<Category> categoryList = categoryMapper.selectList(new QueryWrapper<Category>().lambda() | 
|---|
|  |  |  | .eq(Objects.nonNull(category)&&Objects.nonNull(category.getType()),Category::getType,category.getType()) | 
|---|
|  |  |  | .isNull(Objects.nonNull(category)&&Objects.isNull(category.getParentId()),Category::getParentId) | 
|---|
|  |  |  | .eq(Objects.nonNull(category)&&Objects.nonNull(category.getParentId()),Category::getParentId,category.getParentId()) | 
|---|
|  |  |  | .eq(Category::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | return categoryList; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public PageData<Category> findPage(PageWrap<Category> pageWrap) { | 
|---|
|  |  |  | IPage<Category> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|---|
|  |  |  | QueryWrapper<Category> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | MPJLambdaWrapper<Category> queryWrapper = new MPJLambdaWrapper<>(); | 
|---|
|  |  |  | Utils.MP.blankToNull(pageWrap.getModel()); | 
|---|
|  |  |  | if (pageWrap.getModel().getId() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getId, pageWrap.getModel().getId()); | 
|---|
|  |  |  | String prefixUrl = systemDictDataBiz.queryByCode(Constants.FTP,Constants.FTP_RESOURCE_PATH).getCode() + | 
|---|
|  |  |  | systemDictDataBiz.queryByCode(Constants.FTP,Constants.BANNER_IMG).getCode(); | 
|---|
|  |  |  | queryWrapper.selectAll(Category.class ); | 
|---|
|  |  |  | if(Constants.equalsInteger(pageWrap.getModel().getType(),Constants.FOUR)){ | 
|---|
|  |  |  | //如果是主线路,查询线路数和客户数 | 
|---|
|  |  |  | queryWrapper.select( "(select count(1) from jk_line b where b.isdeleted=0 and b.CATEGORY_ID = t.id)",Category::getLineNum); | 
|---|
|  |  |  | queryWrapper.select( "(select count(1) from jk_customer b left join jk_line c on b.line_id=c.id where c.isdeleted=0 and b.isdeleted=0 and c.CATEGORY_ID = t.id)",Category::getCustomerNum); | 
|---|
|  |  |  | queryWrapper.select( "(select count(1) from jk_customer b left join jk_line c on b.line_id=c.id where c.isdeleted=0 and b.isdeleted=0 and c.CATEGORY_ID = t.id and b.DISTANCE_STATUS = 1)",Category::getCustomerDoneNum); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getCreator() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getCreator, pageWrap.getModel().getCreator()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getCreateDate() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().ge(Category::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate())); | 
|---|
|  |  |  | queryWrapper.lambda().le(Category::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getEditor() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getEditor, pageWrap.getModel().getEditor()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getEditDate() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().ge(Category::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate())); | 
|---|
|  |  |  | queryWrapper.lambda().le(Category::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getIsdeleted() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getIsdeleted, pageWrap.getModel().getIsdeleted()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getName() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getName, pageWrap.getModel().getName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getRemark() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getRemark, pageWrap.getModel().getRemark()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getStatus() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getStatus, pageWrap.getModel().getStatus()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getSortnum() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getSortnum, pageWrap.getModel().getSortnum()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getImgurl() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getImgurl, pageWrap.getModel().getImgurl()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getType() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getType, pageWrap.getModel().getType()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (pageWrap.getModel().getParentId() != null) { | 
|---|
|  |  |  | queryWrapper.lambda().eq(Category::getParentId, pageWrap.getModel().getParentId()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for(PageWrap.SortData sortData: pageWrap.getSorts()) { | 
|---|
|  |  |  | if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { | 
|---|
|  |  |  | queryWrapper.orderByDesc(sortData.getProperty()); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | queryWrapper.orderByAsc(sortData.getProperty()); | 
|---|
|  |  |  | queryWrapper.eq(Category::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | .eq(Objects.nonNull(pageWrap.getModel().getType()),Category::getType,pageWrap.getModel().getType()) | 
|---|
|  |  |  | .like(StringUtils.isNotBlank(pageWrap.getModel().getName()),Category::getName,pageWrap.getModel().getName()) | 
|---|
|  |  |  | .isNull(Category::getParentId) | 
|---|
|  |  |  | .orderByDesc(Category::getSortnum); | 
|---|
|  |  |  | PageData<Category> categoryPageData = PageData.from(categoryMapper.selectPage(page, queryWrapper)); | 
|---|
|  |  |  | //查询所有二级数据 | 
|---|
|  |  |  | List<Category> categoryList = categoryMapper.selectList( | 
|---|
|  |  |  | new QueryWrapper<Category>().lambda() | 
|---|
|  |  |  | .eq(Objects.nonNull(pageWrap.getModel().getType()),Category::getType,pageWrap.getModel().getType()) | 
|---|
|  |  |  | .eq(Category::getIsdeleted,Constants.ZERO).isNotNull(Category::getParentId).orderByDesc(Category::getSortnum)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (Category category:categoryPageData.getRecords()) { | 
|---|
|  |  |  | category.setChildCategoryList( | 
|---|
|  |  |  | categoryList.stream().filter(i->Constants.equalsInteger(i.getParentId(),category.getId())).collect(Collectors.toList()) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | if(StringUtils.isNotBlank(category.getImgurl())){ | 
|---|
|  |  |  | category.setImgurlFull(prefixUrl + category.getImgurl()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return PageData.from(categoryMapper.selectPage(page, queryWrapper)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return categoryPageData; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | QueryWrapper<Category> wrapper = new QueryWrapper<>(category); | 
|---|
|  |  |  | return categoryMapper.selectCount(wrapper); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public  List<Category> findChileList(Category model) { | 
|---|
|  |  |  | if(Objects.isNull(model)){ | 
|---|
|  |  |  | model = new Category(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | model.setIsdeleted(Constants.ZERO); | 
|---|
|  |  |  | List<Category> list =findList(model); | 
|---|
|  |  |  | List<Category> data = new ArrayList<>(); | 
|---|
|  |  |  | if(list!=null){ | 
|---|
|  |  |  | for(Category category : list){ | 
|---|
|  |  |  | if(category.getParentId()!=null){ | 
|---|
|  |  |  | Category pcate = getParentById(category.getParentId(),list); | 
|---|
|  |  |  | if(pcate!=null){ | 
|---|
|  |  |  | category.setParentName(StringUtils.defaultString(pcate.getName(),"")); | 
|---|
|  |  |  | category.setName(StringUtils.defaultString(category.getName(),"")); | 
|---|
|  |  |  | category.setGroupName( category.getParentName()+"/"+category.getName()); | 
|---|
|  |  |  | data.add(category); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | data.add(category); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  data; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Category getParentById(Integer parentId, List<Category> list) { | 
|---|
|  |  |  | if(list!=null){ | 
|---|
|  |  |  | for(Category category : list){ | 
|---|
|  |  |  | if(Constants.equalsInteger(parentId,category.getId())){ | 
|---|
|  |  |  | return  category; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void checkUnique(Category category){ | 
|---|
|  |  |  | QueryWrapper<Category> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.lambda() | 
|---|
|  |  |  | .eq(Category::getIsdeleted,Constants.ZERO) | 
|---|
|  |  |  | .eq(Category::getType,category.getType()) | 
|---|
|  |  |  | .eq(Category::getName,category.getName()); | 
|---|
|  |  |  | .eq(Category::getName,category.getName()) | 
|---|
|  |  |  | .ne(category.getId()!=null, Category::getId,category.getId()) | 
|---|
|  |  |  | ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Category> categories = categoryMapper.selectList(wrapper); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (org.apache.commons.collections.CollectionUtils.isNotEmpty(categories)){ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"分类信息已存在"); | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"信息已存在"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|