liding
6 天以前 3728fa293e1faf4bf4f9bf41105b84ddb43dc82e
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
package com.ruoyi.inspect.service;
 
import com.ruoyi.framework.exception.ErrorException;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
/**
 * 删除委托拥有的业务记录;标准、模板、设备、用户等公共资料不属于委托。
 */
@Service
@RequiredArgsConstructor
public class InsOrderDeletionService {
 
    private final NamedParameterJdbcTemplate jdbc;
 
    @Transactional(rollbackFor = Exception.class)
    public int deleteOrder(Integer orderId) {
        if (orderId == null || orderId <= 0) {
            throw new ErrorException("请选择要删除的委托单");
        }
        List<Map<String, Object>> orders = jdbc.queryForList(
                "SELECT type_source, ifs_inventory_id FROM ins_order WHERE id = :id FOR UPDATE",
                Collections.singletonMap("id", orderId));
        if (orders.isEmpty()) {
            throw new ErrorException("委托单不存在或已被删除");
        }
        Map<String, Object> order = orders.get(0);
        Object source = order.get("type_source");
        Object inventoryId = order.get("ifs_inventory_id");
        if ((source instanceof Number && ((Number) source).intValue() == 1)
                || (inventoryId != null && !inventoryId.toString().trim().isEmpty())) {
            throw new ErrorException("此入口仅允许删除委托下单中的委托单");
        }
 
        List<Long> orderIds = Collections.singletonList(orderId.longValue());
        List<Long> sampleIds = ids("ins_sample", "id", "ins_order_id", orderIds);
        List<Long> productIds = ids("ins_product", "id", "ins_sample_id", sampleIds);
        List<Long> reportIds = ids("ins_report", "id", "ins_order_id", orderIds);
        List<Long> inspectionIds = ids("cnas_inspection_order", "inspection_order_id", "ins_order_id", orderIds);
        List<Long> processSampleIds = ids("cnas_process_sample", "id", "inspection_order_id", inspectionIds);
        List<Long> satisfactionIds = ids("cnas_client_satisfaction", "client_satisfaction_id", "order_id", orderIds);
        List<Long> verifyIds = ids("ins_order_factory_verify", "factory_verify_id", "ins_order_id", orderIds);
        List<Long> warningIds = ids("ins_product_deviation_warning", "deviation_warning_id", "ins_order_id", orderIds);
        List<Long> evaluateIds = ids("cnas_quality_monitor_details_evaluate", "details_evaluate_id", "ins_order_id", orderIds);
        List<Long> retestIds = ids("ins_unqualified_retest_product", "id", "ins_product_id", productIds);
        List<Long> handlerIds = ids("ins_unqualified_handler", "id", "ins_order_id", orderIds);
 
        // 先删除外部业务模块中由本委托生成的记录,再删除检验主从表。
        delete("cnas_client_satisfaction_attachment", "client_satisfaction_id", satisfactionIds);
        delete("cnas_client_satisfaction", "client_satisfaction_id", satisfactionIds);
        delete("cnas_process_complain", "ins_report_id", reportIds);
        delete("cnas_quality_monitor_details_evaluate_file", "details_evaluate_id", evaluateIds);
        delete("cnas_quality_monitor_details_evaluate", "ins_order_id", orderIds);
        delete("cnas_process_deal", "ins_sample_id", sampleIds);
        delete("cnas_process_deal", "process_sample_id", processSampleIds);
        // 报告发送记录的历史字段 inspection_order_id 实际存储 ins_order.id。
        delete("cnas_process_report", "inspection_order_id", orderIds);
        delete("cnas_process_order_device", "inspection_order_id", inspectionIds);
        delete("cnas_process_sample", "inspection_order_id", inspectionIds);
        delete("cnas_inspection_order_detail", "inspection_order_id", inspectionIds);
        delete("cnas_inspection_order", "inspection_order_id", inspectionIds);
 
        delete("auxiliary_output_working_hours", "order_id", orderIds);
        delete("auxiliary_output_working_hours_temporary", "order_id", orderIds);
        delete("auxiliary_output_working_hours", "sample_id", sampleIds);
        delete("auxiliary_output_working_hours_temporary", "sample_id", sampleIds);
        delete("auxiliary_output_working_hours", "ins_product_id", productIds);
        delete("auxiliary_output_working_hours_temporary", "ins_product_id", productIds);
 
        delete("ins_report_edit_session", "report_id", reportIds);
        delete("ins_report_return_record", "report_id", reportIds);
        delete("ins_report_return_record", "ins_order_id", orderIds);
        delete("seal_usage_ledger", "report_id", reportIds);
        delete("seal_usage_ledger", "ins_order_id", orderIds);
        delete("ins_report", "ins_order_id", orderIds);
 
        delete("ins_product_deviation_warning_detail", "deviation_warning_id", warningIds);
        delete("ins_product_deviation_warning_detail", "ins_order_id", orderIds);
        delete("ins_product_deviation_warning_detail", "ins_product_id", productIds);
        delete("ins_product_deviation_warning", "ins_order_id", orderIds);
        delete("ins_product_deviation_warning", "ins_product_id", productIds);
        delete("ins_order_rates", "ins_order_id", orderIds);
        delete("ins_order_rates", "ins_product_id", productIds);
        delete("ins_unqualified_retest_result", "retest_product_id", retestIds);
        delete("ins_unqualified_retest_product", "ins_product_id", productIds);
        delete("ins_unqualified_retest_product", "ins_sample_id", sampleIds);
        delete("ins_unqualified_handler_file", "unqualified_id", handlerIds);
        delete("ins_unqualified_handler", "ins_order_id", orderIds);
        delete("ins_product_result", "ins_product_id", productIds);
        delete("ins_product_user", "ins_product_id", productIds);
        delete("ins_product", "ins_sample_id", sampleIds);
        delete("warehouse_history", "ins_sample_id", sampleIds);
        delete("ins_sample_receipt", "ins_order_id", orderIds);
        // 历史字段名为 ins_sample_id,实际存储 ins_order.id,不能按样品 ID 删除。
        delete("ins_sample_user", "ins_sample_id", orderIds);
        delete("ins_sample", "ins_order_id", orderIds);
 
        delete("ins_order_factory_verify_item", "factory_verify_id", verifyIds);
        delete("ins_order_factory_verify", "ins_order_id", orderIds);
        delete("device_record", "ins_order_id", orderIds);
        delete("ins_order_file", "ins_order_id", orderIds);
        delete("ins_order_template_device", "ins_order_id", orderIds);
        delete("ins_order_standard_template", "ins_order_id", orderIds);
        delete("ins_order_state", "ins_order_id", orderIds);
        return delete("ins_order", "id", orderIds);
    }
 
    // 表名、列名仅来自本类固定的关联清单;所有业务 ID 均使用绑定参数。
    private List<Long> ids(String table, String key, String foreignKey, List<Long> parentIds) {
        if (parentIds.isEmpty()) {
            return Collections.emptyList();
        }
        return jdbc.queryForList("SELECT " + key + " FROM " + table + " WHERE " + foreignKey + " IN (:ids)",
                Collections.singletonMap("ids", parentIds), Long.class);
    }
 
    private int delete(String table, String foreignKey, List<Long> parentIds) {
        if (parentIds.isEmpty()) {
            return 0;
        }
        return jdbc.update("DELETE FROM " + table + " WHERE " + foreignKey + " IN (:ids)",
                Collections.singletonMap("ids", parentIds));
    }
}