package com.ard.inspection.service.impl;
|
|
import java.util.ArrayList;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.stream.Collectors;
|
|
import com.ard.common.constant.UserConstants;
|
import com.ard.common.core.domain.entity.SysUser;
|
import com.ard.common.exception.ServiceException;
|
import com.ard.common.utils.DateUtils;
|
import com.ard.common.utils.SecurityUtils;
|
import com.ard.common.utils.StringUtils;
|
import com.ard.common.utils.spring.SpringUtils;
|
import com.ard.inspection.domain.ArdInspectionGroup;
|
import com.ard.inspection.domain.GroupTreeSelect;
|
import com.ard.inspection.mapper.ArdInspectionGroupMapper;
|
import com.ard.inspection.service.IArdInspectionGroupService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
/**
|
* 精细化巡检分组Service业务层处理
|
*
|
* @author xia
|
* @date 2026-01-15
|
*/
|
@Service
|
public class ArdInspectionGroupServiceImpl implements IArdInspectionGroupService {
|
|
@Autowired
|
private ArdInspectionGroupMapper ardInspectionGroupMapper;
|
|
/**
|
* 查询精细化巡检分组
|
*
|
* @param groupId 精细化巡检分组主键
|
* @return 精细化巡检分组
|
*/
|
@Override
|
public ArdInspectionGroup selectArdInspectionGroupByGroupId(Long groupId) {
|
return ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(groupId);
|
}
|
|
/**
|
* 查询精细化巡检分组列表
|
*
|
* @param ardInspectionGroup 精细化巡检分组
|
* @return 精细化巡检分组
|
*/
|
@Override
|
public List<ArdInspectionGroup> selectArdInspectionGroupList(ArdInspectionGroup ardInspectionGroup) {
|
return ardInspectionGroupMapper.selectArdInspectionGroupList(ardInspectionGroup);
|
}
|
|
/**
|
* 新增精细化巡检分组
|
*
|
* @param ardInspectionGroup 精细化巡检分组
|
* @return 结果
|
*/
|
@Override
|
public int insertArdInspectionGroup(ArdInspectionGroup ardInspectionGroup) {
|
if (ardInspectionGroup.getGroupId() == null){
|
ardInspectionGroup.setGroupId((long) (ardInspectionGroupMapper.selectMaxGroupId() + 100));
|
ardInspectionGroup.setGroupParentId(0L);
|
ardInspectionGroup.setAncestors("0");
|
}else {
|
ArdInspectionGroup inspectionGroup = ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(ardInspectionGroup.getGroupParentId());
|
ardInspectionGroup.setAncestors(inspectionGroup.getAncestors() + "," + ardInspectionGroup.getGroupParentId());
|
}
|
ardInspectionGroup.setCreateTime(DateUtils.getNowDate());
|
ardInspectionGroup.setDelFlag("0");
|
return ardInspectionGroupMapper.insertArdInspectionGroup(ardInspectionGroup);
|
}
|
|
/**
|
* 修改精细化巡检分组
|
*
|
* @param ardInspectionGroup 精细化巡检分组
|
* @return 结果
|
*/
|
@Override
|
public int updateArdInspectionGroup(ArdInspectionGroup ardInspectionGroup) {
|
ArdInspectionGroup newGroup = ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(ardInspectionGroup.getGroupParentId());
|
ArdInspectionGroup oldGroup = ardInspectionGroupMapper.selectArdInspectionGroupByGroupId(ardInspectionGroup.getGroupId());
|
if(StringUtils.isNotNull(newGroup) && StringUtils.isNotNull(oldGroup)){
|
String newAncestors = newGroup.getAncestors() + "," + newGroup.getGroupId();
|
String oldAncestors = oldGroup.getAncestors();
|
ardInspectionGroup.setAncestors(newAncestors);
|
updateInspectionGroupChildren(ardInspectionGroup.getGroupId(), newAncestors, oldAncestors);
|
}
|
ardInspectionGroup.setUpdateTime(DateUtils.getNowDate());
|
return ardInspectionGroupMapper.updateArdInspectionGroup(ardInspectionGroup);
|
}
|
|
/**
|
* 修改子元素关系
|
*
|
* @param groupId 被修改的分组ID
|
* @param newAncestors 新的父ID集合
|
* @param oldAncestors 旧的父ID集合
|
*/
|
public void updateInspectionGroupChildren(Long groupId, String newAncestors, String oldAncestors){
|
List<ArdInspectionGroup> children = ardInspectionGroupMapper.selectChildrenInspectionGroupById(groupId);
|
for (ArdInspectionGroup child : children) {
|
child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
|
}
|
if (!children.isEmpty()){
|
ardInspectionGroupMapper.updateInspectionGroupChildren(children);
|
}
|
}
|
|
/**
|
* 删除精细化巡检分组信息
|
*
|
* @param groupId 精细化巡检分组主键
|
* @return 结果
|
*/
|
@Override
|
public int deleteArdInspectionGroupByGroupId(Long groupId) {
|
return ardInspectionGroupMapper.deleteArdInspectionGroupByGroupId(groupId);
|
}
|
|
/**
|
* 校验分组名称是否唯一
|
*
|
* @param ardInspectionGroup 精细化巡检分组
|
* @return 结果
|
*/
|
@Override
|
public boolean checkInspectionGroupNameUnique(ArdInspectionGroup ardInspectionGroup) {
|
Long groupId = StringUtils.isNull(ardInspectionGroup.getGroupId()) ? -1L : ardInspectionGroup.getGroupId();
|
ArdInspectionGroup infoA = ardInspectionGroupMapper.checkInspectionGroupNameUniqueA(ardInspectionGroup.getGroupName(),ardInspectionGroup.getGroupParentId());
|
ArdInspectionGroup infoB = ardInspectionGroupMapper.checkInspectionGroupNameUniqueB(ardInspectionGroup.getGroupName());
|
if (StringUtils.isNotNull(infoB) && infoB.getGroupId().longValue() != groupId.longValue())
|
{
|
return UserConstants.NOT_UNIQUE;
|
}
|
return UserConstants.UNIQUE;
|
}
|
|
/**
|
* 校验分组是否有数据权限
|
*
|
* @param groupId 精细化巡检分组主键
|
*/
|
@Override
|
public void checkInspectionGroupDataScope(Long groupId) {
|
if (!SysUser.isAdmin(SecurityUtils.getUserId()) && StringUtils.isNotNull(groupId)){
|
ArdInspectionGroup group = new ArdInspectionGroup();
|
group.setGroupId(groupId);
|
List<ArdInspectionGroup> groups = SpringUtils.getAopProxy(this).selectArdInspectionGroupList(group);
|
if (StringUtils.isEmpty(groups)){
|
throw new ServiceException("没有权限访问分组数据!");
|
}
|
}
|
}
|
|
/**
|
* 是否存在分组子节点
|
*
|
* @param groupId 分组ID
|
* @return 结果
|
*/
|
@Override
|
public boolean hasChildByInspectionGroupId(Long groupId) {
|
int result = ardInspectionGroupMapper.hasChildByInspectionGroupId(groupId);
|
return result > 0;
|
}
|
|
/**
|
* 查询分组是否存在用户
|
*
|
* @param groupId 分组ID
|
* @return 结果 true 存在 false 不存在
|
*/
|
@Override
|
public boolean checkInspectionGroupExistUser(Long groupId) {
|
int result = ardInspectionGroupMapper.checkInspectionGroupExistUser(groupId);
|
return result > 0;
|
}
|
|
/**
|
* 查询分组树结构信息
|
*
|
* @param ardInspectionGroup 精细化巡检分组
|
* @return 分组树信息集合
|
*/
|
@Override
|
public List<GroupTreeSelect> selectGroupTreeList(ArdInspectionGroup ardInspectionGroup)
|
{
|
List<ArdInspectionGroup> groups = selectArdInspectionGroupList(ardInspectionGroup);
|
return buildGroupTreeSelect(groups);
|
}
|
|
/**
|
* 构建前端所需要下拉树结构
|
*
|
* @param groups 分组列表
|
* @return 下拉树结构列表
|
*/
|
@Override
|
public List<GroupTreeSelect> buildGroupTreeSelect(List<ArdInspectionGroup> groups){
|
List<ArdInspectionGroup> groupTrees = buildGroupTree(groups);
|
return groupTrees.stream().map(GroupTreeSelect::new).collect(Collectors.toList());
|
}
|
|
/**
|
* 构建前端所需要树结构
|
*
|
* @param groups 分组列表
|
* @return 树结构列表
|
*/
|
@Override
|
public List<ArdInspectionGroup> buildGroupTree(List<ArdInspectionGroup> groups){
|
List<ArdInspectionGroup> returnList = new ArrayList<ArdInspectionGroup>();
|
List<Long> tempList = groups.stream().map(ArdInspectionGroup::getGroupId).collect(Collectors.toList());
|
for (ArdInspectionGroup group : groups)
|
{
|
// 如果是顶级节点, 遍历该父节点的所有子节点
|
if (!tempList.contains(group.getGroupParentId()))
|
{
|
recursionFn(groups, group);
|
returnList.add(group);
|
}
|
}
|
if (returnList.isEmpty())
|
{
|
returnList = groups;
|
}
|
return returnList;
|
}
|
|
/**
|
* 递归列表
|
*/
|
private void recursionFn(List<ArdInspectionGroup> list, ArdInspectionGroup t)
|
{
|
// 得到子节点列表
|
List<ArdInspectionGroup> childList = getChildList(list, t);
|
t.setChildren(childList);
|
for (ArdInspectionGroup tChild : childList)
|
{
|
if (hasChild(list, tChild))
|
{
|
recursionFn(list, tChild);
|
}
|
}
|
}
|
|
/**
|
* 得到子节点列表
|
*/
|
private List<ArdInspectionGroup> getChildList(List<ArdInspectionGroup> list, ArdInspectionGroup t)
|
{
|
List<ArdInspectionGroup> tlist = new ArrayList<ArdInspectionGroup>();
|
Iterator<ArdInspectionGroup> it = list.iterator();
|
while (it.hasNext())
|
{
|
ArdInspectionGroup n = (ArdInspectionGroup) it.next();
|
if (StringUtils.isNotNull(n.getGroupParentId()) && n.getGroupParentId().longValue() == t.getGroupId().longValue())
|
{
|
tlist.add(n);
|
}
|
}
|
return tlist;
|
}
|
|
/**
|
* 判断是否有子节点
|
*/
|
private boolean hasChild(List<ArdInspectionGroup> list, ArdInspectionGroup t)
|
{
|
return getChildList(list, t).size() > 0;
|
}
|
|
}
|