17 小时以前 9683f8f2b526f67bff039a1cd46818808b53ab3e
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
package com.ruoyi.mock.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.approve.pojo.ApprovalTemplate;
import com.ruoyi.approve.mapper.ApprovalTemplateMapper;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.mapper.ProductMapper;
import com.ruoyi.basic.mapper.SupplierManageMapper;
import com.ruoyi.common.enums.TypeEnums;
import com.ruoyi.mock.dto.DataCheckRequest;
import com.ruoyi.mock.service.DataCheckService;
import com.ruoyi.mock.vo.DataCheckResult;
import com.ruoyi.mock.vo.DataCheckResult.CheckItem;
import com.ruoyi.quality.mapper.QualityTestStandardBindingMapper;
import com.ruoyi.quality.mapper.QualityTestStandardMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service
@RequiredArgsConstructor
public class DataCheckServiceImpl implements DataCheckService {
 
    private final ProductMapper productMapper;
    private final CustomerMapper customerMapper;
    private final SupplierManageMapper supplierManageMapper;
    private final ApprovalTemplateMapper approvalTemplateMapper;
    private final QualityTestStandardMapper qualityTestStandardMapper;
    private final QualityTestStandardBindingMapper qualityTestStandardBindingMapper;
 
    @Override
    public DataCheckResult check(DataCheckRequest request) {
        List<String> modules = request.getModules();
        if (modules == null) {
            modules = List.of();
        }
 
        Map<String, CheckItem> itemMap = new LinkedHashMap<>();
 
        if (!modules.isEmpty()) {
            addProductCheck(itemMap);
        }
 
        for (String module : modules) {
            switch (module) {
                case "sales" -> addSalesChecks(itemMap);
                case "purchase" -> addPurchaseChecks(itemMap);
                case "quality" -> addQualityChecks(itemMap);
            }
        }
 
        List<CheckItem> items = new ArrayList<>(itemMap.values());
 
        DataCheckResult result = new DataCheckResult();
        result.setItems(items);
        result.setTotalItems(items.size());
        result.setPassedItems((int) items.stream().filter(CheckItem::isPassed).count());
        return result;
    }
 
    private void addProductCheck(Map<String, CheckItem> itemMap) {
        String key = "common:产品数据";
        if (itemMap.containsKey(key)) {
            return;
        }
        long count = productMapper.selectCount(null);
        itemMap.put(key, buildItem("common", "产品数据", 1, (int) count,
                "缺少产品数据,请先在【基础数据-产品管理】中添加至少1条产品"));
    }
 
    private void addSalesChecks(Map<String, CheckItem> itemMap) {
        String customerKey = "sales:客户数据";
        if (!itemMap.containsKey(customerKey)) {
            long count = customerMapper.selectCount(null);
            itemMap.put(customerKey, buildItem("sales", "客户数据", 1, (int) count,
                    "缺少客户数据,请先在【基础数据-客户管理】中添加至少1条客户"));
        }
 
        String quotationKey = "sales:报价审批模板";
        if (!itemMap.containsKey(quotationKey)) {
            long count = approvalTemplateMapper.selectCount(
                    new LambdaQueryWrapper<ApprovalTemplate>()
                            .eq(ApprovalTemplate::getBusinessType, TypeEnums.QUOTATION_APPROVAL.getCode()));
            itemMap.put(quotationKey, buildItem("sales", "报价审批模板", 1, (int) count,
                    "缺少报价审批模板,请先在【系统管理-审批模板】中创建报价审批模板"));
        }
 
        String shippingKey = "sales:发货审批模板";
        if (!itemMap.containsKey(shippingKey)) {
            long count = approvalTemplateMapper.selectCount(
                    new LambdaQueryWrapper<ApprovalTemplate>()
                            .eq(ApprovalTemplate::getBusinessType, TypeEnums.SHIPPING_APPROVAL.getCode()));
            itemMap.put(shippingKey, buildItem("sales", "发货审批模板", 1, (int) count,
                    "缺少发货审批模板,请先在【系统管理-审批模板】中创建发货审批模板"));
        }
    }
 
    private void addPurchaseChecks(Map<String, CheckItem> itemMap) {
        String supplierKey = "purchase:供应商数据";
        if (!itemMap.containsKey(supplierKey)) {
            long count = supplierManageMapper.selectCount(null);
            itemMap.put(supplierKey, buildItem("purchase", "供应商数据", 1, (int) count,
                    "缺少供应商数据,请先在【基础数据-供应商管理】中添加至少1条供应商"));
        }
 
        String tplKey = "purchase:采购审批模板";
        if (!itemMap.containsKey(tplKey)) {
            long count = approvalTemplateMapper.selectCount(
                    new LambdaQueryWrapper<ApprovalTemplate>()
                            .eq(ApprovalTemplate::getBusinessType, TypeEnums.PURCHASE_APPROVAL.getCode()));
            itemMap.put(tplKey, buildItem("purchase", "采购审批模板", 1, (int) count,
                    "缺少采购审批模板,请先在【系统管理-审批模板】中创建采购审批模板"));
        }
    }
 
    private void addQualityChecks(Map<String, CheckItem> itemMap) {
        String standardKey = "quality:检测标准";
        if (!itemMap.containsKey(standardKey)) {
            long count = qualityTestStandardMapper.selectCount(null);
            itemMap.put(standardKey, buildItem("quality", "检测标准", 1, (int) count,
                    "缺少检测标准,请先在【质量管理-检测标准】中创建检测标准"));
        }
 
        String bindingKey = "quality:指标绑定";
        if (!itemMap.containsKey(bindingKey)) {
            long count = qualityTestStandardBindingMapper.selectCount(null);
            itemMap.put(bindingKey, buildItem("quality", "指标绑定", 1, (int) count,
                    "缺少指标绑定,请先在【质量管理-检测标准绑定】中将检测标准与产品绑定"));
        }
    }
 
    private CheckItem buildItem(String module, String itemName, int minRequired, int currentCount, String failMessage) {
        CheckItem item = new CheckItem();
        item.setModule(module);
        item.setItemName(itemName);
        item.setMinRequired(minRequired);
        item.setCurrentCount(currentCount);
        item.setPassed(currentCount >= minRequired);
        item.setMessage(item.isPassed() ? "通过" : failMessage);
        return item;
    }
}