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));
|
}
|
}
|