# `Sp_saveReturn_sAcc` (procedure)

> 客户投诉登记(发现问题)反写


- **Type:** PROCEDURE
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sGuid` | `longtext` |
| 2 | IN | `sBrId` | `varchar(100)` |
| 3 | IN | `sSuId` | `varchar(100)` |
| 4 | IN | `iTag` | `int` |
| 5 | OUT | `sCode` | `int` |
| 6 | OUT | `sReturn` | `longtext` |

## Body

_Body is not pre-cached. To inspect: `mysql --defaults-file=~/.my.cnf -e 'SHOW CREATE PROCEDURE `Sp_saveReturn_sAcc`'`._

## Narrative

**Business context:** 质量管理 → 质量管理流程 → 01/01 新增质量事故报告 / 01/02 新增客户投诉单 — 反写 ("back-write") helper for the 质量事故报告 / 客户投诉登记 family. When a 质量事故 (sAcc) row is saved or invalidated, this proc propagates the line's `dQualityreturnQty` back to the source slave (`mftqualityaccidentslave.dReturnQty / bReturn`) and refreshes the originating production-apply row's `sType`, so upstream 投诉/事故 statuses match the latest aggregate.

**What it does:** Parses `sGuid` (CSV of 事故 master ids) into `p_t_sId` via `Fn_spit_length` / `Fn_split_string`. Resolves the source slave ids into `p_sSrcSlaveId_tmp` by joining `mftqualityaccidentMaster.sId`. Aggregates `SUM(dQualityreturnQty)` per `sSrcSlaveId` into `p_data_tmp` — when `iTag=1` (保存前 / pre-save) it excludes the rows being saved (`INSTR(sGuid, S.sId)>0 => 0`), when `iTag=2` (保存后 / post-save) it includes them. Updates `mftqualityaccidentslave.dReturnQty / bReturn` and, depending on `iTag`, updates `mitproductionapplyslave.sType` to either the override coming from this save or the most recent valid 事故 record's `sType`.

**Invocation:** Called by `Sp_beforeSave_sAcc` (with `iTag=1`) and `Sp_afterSave_sAcc` (with `iTag=2`) — those are wired as `gdsmodule.sSaveProNameBefore` / `sSaveProName` on the 01/01 新增质量事故报告 (`sId 11811781131121915261093794470`), 质量事故报告 (`sId 101251240115015934849804300`), 设备异常单 (`sId 101251240115016045682733390`) and 01/02 新增客户投诉单 (`sId 101251240115016180439686010`) modules. Standard save-back-write pattern (`Sp_invalid_backwriting` / `Sp_delete_backwriting` dispatch on invalidate/delete).
