李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *    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<ResourceMapper, Resource> implements ResourceService {
    private ResourceMapper resourceMapper;
    private CapabilityMapper capabilityMapper;
    private ResourceCapabilitiesMapper resourceCapabilitiesMapper;
 
    /**
     * Description: 分页
     *
     * @param page
     * @param resource
     * @return IPage<List < Resource>
     * @author:
     * @date: 2020/8/26 14:06
     */
    @Override
    public IPage<List<Resource>> fetch(Page page, Resource resource, Long id) {
        return resourceMapper.fetch(page, resource, id);
    }
 
    @Override
    public List<Resource> 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.<ResourceCapabilities>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.<ResourceCapabilities>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> resourceCapabilities = resourceCapabilitiesMapper.selectList(Wrappers.<ResourceCapabilities>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<ResourceBo> getAllWithMaxTime(Long sceneId) {
        return null;
    }
 
 
}