package jnpf.permission.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; import jnpf.base.service.SuperServiceImpl; import jnpf.constant.CodeConst; import jnpf.permission.entity.GroupEntity; import jnpf.permission.mapper.GroupMapper; import jnpf.permission.model.usergroup.GroupPagination; import jnpf.permission.service.CodeNumService; import jnpf.permission.service.GroupService; import jnpf.util.DateUtil; import jnpf.util.RandomUtil; import jnpf.util.StringUtil; import jnpf.util.UserProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; /** * 分组管理业务类实现类 * * @author :JNPF开发平台组 * @version: V3.1.0 * @copyright 引迈信息技术有限公司 * @date :2022/3/10 18:00 */ @Service public class GroupServiceImpl extends SuperServiceImpl implements GroupService { @Autowired private CodeNumService codeNumService; @Override public List getList(GroupPagination pagination) { QueryWrapper queryWrapper = new QueryWrapper<>(); // 定义变量判断是否需要使用修改时间倒序 boolean flag = false; // 判断关键字 String keyword = pagination.getKeyword(); if (StringUtil.isNotEmpty(keyword)) { flag = true; queryWrapper.lambda().and( t -> t.like(GroupEntity::getFullName, keyword) .or().like(GroupEntity::getEnCode, keyword) .or().like(GroupEntity::getDescription, keyword) ); } if (pagination.getEnabledMark() != null) { queryWrapper.lambda().eq(GroupEntity::getEnabledMark, pagination.getEnabledMark()); } // 获取列表 queryWrapper.lambda().orderByAsc(GroupEntity::getSortCode).orderByAsc(GroupEntity::getCreatorTime); if (flag) { queryWrapper.lambda().orderByDesc(GroupEntity::getLastModifyTime); } //不分页 if (Objects.equals(pagination.getDataType(), 1)) { return list(queryWrapper); } //有分页 Page page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize()); IPage iPage = this.page(page, queryWrapper); return pagination.setData(iPage.getRecords(), iPage.getTotal()); } @Override public List list() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(GroupEntity::getEnabledMark, 1); queryWrapper.lambda().orderByAsc(GroupEntity::getSortCode).orderByAsc(GroupEntity::getCreatorTime); return this.list(queryWrapper); } @Override public Map getGroupMap() { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().select(GroupEntity::getId, GroupEntity::getFullName); return this.list(queryWrapper).stream().collect(Collectors.toMap(GroupEntity::getId, GroupEntity::getFullName)); } @Override public Map getGroupEncodeMap() { return getGroupEncodeMap(false); } @Override public Map getGroupEncodeMap(boolean enabledMark) { QueryWrapper queryWrapper = new QueryWrapper<>(); if (enabledMark) { queryWrapper.lambda().eq(GroupEntity::getEnabledMark, 1); } queryWrapper.lambda().select(GroupEntity::getId, GroupEntity::getFullName, GroupEntity::getEnCode); return this.list(queryWrapper).stream().collect(Collectors.toMap(group -> group.getFullName() + "/" + group.getEnCode(), GroupEntity::getId)); } @Override public GroupEntity getInfo(String id) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(GroupEntity::getId, id); return this.getOne(queryWrapper); } @Override public GroupEntity getInfo(String fullName, String enCode) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(GroupEntity::getFullName, fullName); queryWrapper.lambda().eq(GroupEntity::getEnCode, enCode); return this.getOne(queryWrapper); } @Override public void crete(GroupEntity entity) { entity.setId(RandomUtil.uuId()); if (StringUtil.isEmpty(entity.getEnCode())) { entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.YHZ), code -> this.isExistByEnCode(code, null))); } entity.setCreatorUserId(UserProvider.getUser().getUserId()); entity.setCreatorTime(DateUtil.getNowDate()); entity.setEnabledMark(1); this.save(entity); } @Override public Boolean update(String id, GroupEntity entity) { entity.setId(id); if (StringUtil.isEmpty(entity.getEnCode())) { entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.YHZ), code -> this.isExistByEnCode(code, id))); } entity.setLastModifyUserId(UserProvider.getUser().getUserId()); entity.setLastModifyTime(DateUtil.getNowDate()); entity.setEnabledMark(1); return this.updateById(entity); } @Override public void delete(GroupEntity entity) { this.removeById(entity.getId()); } @Override public Boolean isExistByFullName(String fullName, String id) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(GroupEntity::getFullName, fullName); if (!StringUtil.isEmpty(id)) { queryWrapper.lambda().ne(GroupEntity::getId, id); } return this.count(queryWrapper) > 0; } @Override public Boolean isExistByEnCode(String enCode, String id) { if (StringUtil.isEmpty(enCode)) return false; QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(GroupEntity::getEnCode, enCode); if (!StringUtil.isEmpty(id)) { queryWrapper.lambda().ne(GroupEntity::getId, id); } return this.count(queryWrapper) > 0; } @Override public List getListByIds(List idList) { return getListByIds(idList, true); } @Override public List getListByIds(List idList, Boolean filterEnabledMark) { if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST; QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().in(GroupEntity::getId, idList); if (idList.size() > 1000) { List> lists = Lists.partition(idList, 1000); queryWrapper.lambda().and(t -> { for (List list : lists) { t.in(GroupEntity::getId, list).or(); } }); } else { queryWrapper.lambda().in(GroupEntity::getId, idList); } if (filterEnabledMark) { queryWrapper.lambda().eq(GroupEntity::getEnabledMark, 1); } return this.list(queryWrapper); } }