/* * Copyright (c) 2018-2025, ztt All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the pig4cloud.com developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: ztt */ package com.chinaztt.mes.aps.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.chinaztt.mes.aps.core.domain.ResourceBo; import com.chinaztt.mes.aps.dto.ResourceDTO; import com.chinaztt.mes.aps.entity.ResourceCapabilities; import com.chinaztt.mes.aps.mapper.CapabilityMapper; import com.chinaztt.mes.aps.mapper.ResourceCapabilitiesMapper; import com.chinaztt.mes.aps.service.ResourceService; import com.chinaztt.mes.aps.entity.Resource; import com.chinaztt.mes.aps.mapper.ResourceMapper; import com.chinaztt.ztt.common.data.datascope.DataScope; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.util.StringUtils; import java.util.List; import java.util.stream.Collectors; /** * 资源 * * @author fenglang * @date 2020-08-26 09:29:34 */ @AllArgsConstructor @Service public class ResourceServiceImpl extends ServiceImpl implements ResourceService { private ResourceMapper resourceMapper; private CapabilityMapper capabilityMapper; private ResourceCapabilitiesMapper resourceCapabilitiesMapper; /** * Description: 分页 * * @param page * @param resource * @return IPage * @author: * @date: 2020/8/26 14:06 */ @Override public IPage> fetch(Page page, Resource resource, Long id) { return resourceMapper.fetch(page, resource, id); } @Override public List query(String keyword) { if (StringUtils.isEmpty(keyword)) { return null; } return baseMapper.query(keyword); } /** * 关联保存资源以及和能力关联信息 * * @param resource * @return */ @Override public boolean fullSave(ResourceDTO resource) { try { resourceMapper.insert(resource); if (CollectionUtil.isNotEmpty(resource.getResourceCapabilities())) { resource.getResourceCapabilities().forEach(a -> { a.setResourceId(resource.getId()); resourceCapabilitiesMapper.insert(a); }); } } catch (Exception e) { log.error("创建资源能力关联失败", e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return false; } return true; } @Override public boolean fullUpdate(ResourceDTO resource) { try { //更新主表 resourceMapper.updateById(resource); //删除所有关联信息 resourceCapabilitiesMapper.delete(Wrappers.lambdaQuery().eq(ResourceCapabilities::getResourceId, resource.getId())); //插入关联信息 if (CollectionUtil.isNotEmpty(resource.getResourceCapabilities())) { resource.getResourceCapabilities().forEach(a -> { resourceCapabilitiesMapper.insert(a); }); } } catch (Exception e) { log.error("更新资源失败id:" + resource.getId(), e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return false; } return true; } @Override @Transactional(rollbackFor = Exception.class) public boolean fullDelete(Long id) { resourceMapper.deleteById(id); resourceCapabilitiesMapper.delete(Wrappers.lambdaQuery().eq(ResourceCapabilities::getResourceId, id)); return true; } @Override public ResourceDTO getResourceById(Long id) { ResourceDTO resourceDTO = new ResourceDTO(); //当不为空时获取关联能力,为空时只获取未关联能力 if (id != 0L) { // 1.获取资源 resourceDTO = baseMapper.selectDtoById(id); // 2.获取关联的能力 List resourceCapabilities = resourceCapabilitiesMapper.selectList(Wrappers.lambdaQuery().eq(ResourceCapabilities::getResourceId, resourceDTO.getId())); if (CollectionUtil.isNotEmpty(resourceCapabilities)) { resourceDTO.setCapabilityIds(resourceCapabilities.stream().map(e -> e.getCapabilityId()).collect(Collectors.toList())); } } // 3.获取所有的能力 resourceDTO.setCapabilities(capabilityMapper.getCapabilities(id)); return resourceDTO; } @Override public List getAllWithMaxTime(Long sceneId) { return null; } }