晏有为
2024-05-23 10de56ca16261e6a4eec49dfd32f0a6b232f7baf
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.yuanchu.mom.service.impl;
 
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.mapper.StandardMethodMapper;
import com.yuanchu.mom.pojo.StandardMethod;
import com.yuanchu.mom.service.StandardMethodService;
import com.yuanchu.mom.utils.QueryWrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.*;
 
/**
* @author Administrator
* @description 针对表【standard_method(标准方法)】的数据库操作Service实现
* @createDate 2024-03-03 19:21:41
*/
@Service
@AllArgsConstructor
public class StandardMethodServiceImpl extends ServiceImpl<StandardMethodMapper, StandardMethod>
    implements StandardMethodService{
 
    private GetLook getLook;
 
    private StandardMethodMapper standardMethodMapper;
 
    @Override
    public Map<String, Object> selectStandardMethodList(Page page, StandardMethod standardMethod) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(StandardMethod.class));
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectStandardMethodList");
        if(map1.get("look")==1) standardMethod.setCreateUser(map1.get("userId"));
        map.put("body", standardMethodMapper.selectStandardMethodList(page, QueryWrappers.queryWrappers(standardMethod)));
        return map;
    }
 
    @Override
    public List<StandardMethod> selectStandardMethods() {
        return standardMethodMapper.selectList(Wrappers.<StandardMethod>lambdaQuery().select(StandardMethod::getId,StandardMethod::getCode,StandardMethod::getName).ne(StandardMethod::getId, 0));
    }
 
    @Override
    public int addStandardMethod(StandardMethod standardMethod) {
        int insert = standardMethodMapper.insert(standardMethod);
        return insert;
    }
 
    @Override
    public int delStandardMethod(Integer id) {
        int i = standardMethodMapper.deleteById(id);
        return i;
    }
 
    @Override
    public int upStandardMethod(StandardMethod standardMethod) {
        int i = standardMethodMapper.updateById(standardMethod);
        return i;
    }
 
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void inputExcel(MultipartFile file) throws IOException {
        // 存储检测对象List
        List<Object> structureTestObjectIdList = new ArrayList<>();
        List<StandardMethod> result = new ArrayList<>();
        ExcelUtil.readBySax(file.getInputStream(), 0, (i, l, list) -> {
            // 去除第一行表头
            if (l == 0) {
                return;
            }
            // 存储唯一检测对象
            if (!structureTestObjectIdList.contains(list.get(2))) {
                structureTestObjectIdList.add(list.get(2));
            }
            StandardMethod standardMethod = formatData(list);
            result.add(standardMethod);
        });
        addStructureTest(structureTestObjectIdList, result);
    }
 
    // 格式化数据
    public StandardMethod formatData(List<Object> list) {
        StandardMethod standardMethod = new StandardMethod();
        standardMethod.setField(list.get(1).toString());
        // 造格式
        List<List<Object>> structureTestObjectId = new ArrayList<>();
        if (ObjectUtils.isEmpty(list.get(3))){
            structureTestObjectId.add(Arrays.asList(list.get(2)));
        } else {
            structureTestObjectId.add(Arrays.asList(list.get(2), list.get(3)));
        }
        standardMethod.setStructureTestObjectId(JSONUtil.toJsonStr(structureTestObjectId));
        standardMethod.setCode(list.get(4).toString());
        standardMethod.setName(list.get(5).toString());
        standardMethod.setNameEn(list.get(6).toString());
        if (!Objects.equals(list.get(7), null)) {
            standardMethod.setRemark(list.get(7).toString());
        }
        standardMethod.setQualificationId(list.get(8).toString());
        if (ObjectUtils.isNotEmpty(list.get(9))) {
            if (list.get(9).equals("是")) {
                standardMethod.setIsProduct(1);
            } else if (list.get(9).equals("否")) {
                standardMethod.setIsProduct(0);
            }
        }
        if (ObjectUtils.isNotEmpty(list.get(10))) {
            if (list.get(10).equals("是")) {
                standardMethod.setIsUse(1);
            } else if (list.get(9).equals("否")) {
                standardMethod.setIsUse(0);
            }
        }
        return standardMethod;
    }
 
    // 新增数据
    public void addStructureTest(List<Object> structureTestObjectIdList, List<StandardMethod> standardMethodList) {
        List<StandardMethod> updateList = new ArrayList<>();
        List<Integer> deleteListId = new ArrayList<>();
        List<StandardMethod> addList = new ArrayList<>();
        if (!structureTestObjectIdList.isEmpty()) {
            // 循环excel里面的分组
            structureTestObjectIdList.forEach(j -> {
                // 以excel中的组名查询数据库中的分组
                List<StandardMethod> standardMethods = baseMapper.selectList(Wrappers.<StandardMethod>lambdaQuery()
                        .like(StandardMethod::getStructureTestObjectId, "\"" + j + "\""));
                // 将结果循环匹配
                for (int i = 0; i < standardMethods.size(); i++) {
                    boolean isExistence = false;
                    for (int i1 = 0; i1 < standardMethodList.size(); i1++) {
                        // 更新
                        if (standardMethods.get(i).getStructureTestObjectId().equals(standardMethodList.get(i1).getStructureTestObjectId())
                                && standardMethods.get(i).getCode().equals(standardMethodList.get(i1).getCode())
                                && standardMethods.get(i).getField().equals(standardMethodList.get(i1).getField())) {
                            // 给excel数据赋值id做更新
                            standardMethodList.get(i1).setId(standardMethods.get(i).getId());
                            // 更新
                            updateList.add(standardMethodList.get(i1));
                            isExistence = true;
                            break;
                        }
                    }
                    // 删除
                    if (!isExistence) {
                        deleteListId.add(standardMethods.get(i).getId());
                    }
                }
                for (int i = 0; i < standardMethodList.size(); i++) {
                    if (standardMethodList.get(i).getStructureTestObjectId().contains("\"" + j + "\"")) {
                        boolean isExistence = false;
                        for (int i1 = 0; i1 < standardMethods.size(); i1++) {
                            if (standardMethods.get(i1).getStructureTestObjectId().equals(standardMethodList.get(i).getStructureTestObjectId())
                                    && standardMethods.get(i1).getCode().equals(standardMethodList.get(i).getCode())
                                    && standardMethods.get(i1).getField().equals(standardMethodList.get(i).getField())) {
                                isExistence = true;
                                break;
                            }
                        }
                        // 新增
                        if (!isExistence) {
                            addList.add(standardMethodList.get(i));
                        }
                    }
                }
            });
        }
        if (!addList.isEmpty()) {
            // 新增
            baseMapper.insertBatchSomeColumn(addList);
        }
 
        if (!deleteListId.isEmpty()) {
            // 删除
            baseMapper.deleteBatchIds(deleteListId);
        }
 
        if (!updateList.isEmpty()) {
            // 更新
            updateList.forEach(i -> {
                baseMapper.updateById(i);
            });
        }
    }
}