李林
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
161
162
163
164
165
166
167
/*
 *    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.quality.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.quality.dto.TemplateDTO;
import com.chinaztt.mes.quality.entity.Items;
import com.chinaztt.mes.quality.entity.Parts;
import com.chinaztt.mes.quality.entity.Requirements;
import com.chinaztt.mes.quality.entity.Template;
import com.chinaztt.mes.quality.mapper.ItemsMapper;
import com.chinaztt.mes.quality.mapper.PartsMapper;
import com.chinaztt.mes.quality.mapper.RequirementsMapper;
import com.chinaztt.mes.quality.mapper.TemplateMapper;
import com.chinaztt.mes.quality.service.TemplateService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
/**
 * 检测模板
 *
 * @author liuth
 * @date 2020-10-09 13:42:42
 */
@Service
@AllArgsConstructor
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TemplateService {
    private ItemsMapper itemsMapper;
    private RequirementsMapper requirementsMapper;
    private PartsMapper partsMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullSave(TemplateDTO templateDTO) {
        try {
//            1.将检测模板的数据插入到主表中
            templateDTO.setCreateDate(LocalDateTime.now());
            baseMapper.insert(templateDTO);
//            2.1判断关联表检测项的数据是否为空
            if (CollectionUtil.isNotEmpty(templateDTO.getItems())) {
                templateDTO.getItems().forEach(a -> {
                    a.setTemplateId(templateDTO.getId());
                    itemsMapper.insert(a);
                });
            }
        } catch (Exception e) {
            log.error("创建检测模板失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullUpdate(TemplateDTO templateDTO) {
        try {
            Set<Long> itemsIds = new HashSet();
            Set<Long> partsIds = new HashSet();
            //更新主表
            baseMapper.updateById(templateDTO);
            //更新检验项关系表
            if (CollectionUtil.isNotEmpty(templateDTO.getItems())) {
                templateDTO.getItems().forEach(a -> {
                    if (a.getId() == null || a.getId() == 0) {
                        a.setTemplateId(templateDTO.getId());
                        itemsMapper.insert(a);
                    } else {
                        itemsMapper.updateById(a);
                    }
                    itemsIds.add(a.getId());
                });
            }
 
            LambdaQueryWrapper<Items> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Items::getTemplateId, templateDTO.getId());
            if (CollectionUtil.isNotEmpty(itemsIds)) {
                wrapper.notIn(Items::getId, itemsIds);
            }
            itemsMapper.delete(wrapper);
            //修改检测模板时删除检测项同时要把检测要求中对应的检测项目删除
            LambdaQueryWrapper<Requirements> wrapperRequirements = new LambdaQueryWrapper<>();
            wrapperRequirements.eq(Requirements::getTemplateId,templateDTO.getId());
            if (CollectionUtil.isNotEmpty(itemsIds)) {
                wrapperRequirements.notIn(Requirements::getItemsId, itemsIds);
            }
            requirementsMapper.delete(wrapperRequirements);
        } catch (Exception e) {
            log.error("更新检测模板失败:" + templateDTO.getQtplNo(), e);
            throw new RuntimeException("更新检测模板失败");
        }
        return true;
    }
 
    /**
     * 关联删除
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean fullDelete(Long id) {
        baseMapper.deleteById(id);
        itemsMapper.delete(Wrappers.<Items>lambdaQuery().eq(Items::getTemplateId, id));
        //删除检测模板对应的检测要求
        requirementsMapper.delete(Wrappers.<Requirements>lambdaQuery().eq(Requirements::getTemplateId, id));
 
        return true;
 
    }
 
    @Override
    public TemplateDTO getTemplateById(Long id) {
        TemplateDTO templateDTO = new TemplateDTO();
        if (id != 0L) {
            //1.获取检测模板
            templateDTO = baseMapper.selectDtoById(id);
            //2.获取关联检验项
            List<Items> itemsList = itemsMapper.selectList(Wrappers.<Items>lambdaQuery().eq(Items::getTemplateId, id));
            if (CollectionUtil.isNotEmpty(itemsList)) {
                templateDTO.setItems(itemsList);
            }
        }
        return templateDTO;
    }
 
    @Override
    public int deleteItemsById(Long id) {
        return itemsMapper.deleteById(id);
    }
 
    @Override
    public Template selectTemplateByPartId(Long id) {
        return baseMapper.selectTemplateByPartId(id);
    }
 
    @Override
    public IPage<List<Template>> getTemplatePageByStatus(Page page, QueryWrapper<Template> ew) {
        return baseMapper.getTemplatePageByStatus(page,ew);
    }
 
}