# `Sp_productionMainPlan_BtnEventAutoOrder` (procedure)

> 主计划：智能排序，并保存之前的数据


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sProInParam` | `varchar(10000)` |
| 2 | IN | `sMakePerson` | `varchar(100)` |
| 3 | IN | `sBrId` | `varchar(100)` |
| 4 | IN | `sSuId` | `varchar(100)` |
| 5 | OUT | `sReturn` | `varchar(1000)` |
| 6 | OUT | `sCode` | `int` |

## Body

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

## Narrative

**Business context:** 生产管理 / 主计划 → 智能排序 — planner-grid "auto-sort" button. Re-orders the in-flight production-plan rows by the upstream work-order's 送货日期 (delivery date) and snapshots the previous order to `mftproductionplaniorderbak` so the user can rewind. Sister procs `Sp_productionMainPlan_BtnEventOrder` (撤单), `Sp_productionMainPlan_BtnEventPlanning` (投入规划), `Sp_productionMainPlan_BtnEventComplete` (工单完工), `Sp_productionMainPlan_BackupCurrPlaniOrder` (manual snapshot) share the same page.

**What it does:** Reads `MAX(iInsertBatch)+1` from `mftproductionplaniorderbak`. Builds temp `p_CalcBasicTable` from `mftproductionplan P JOIN viw_mftworkorder M` for rows in `sState IN (2,3,7)` keyed on `M.tDeliverDate` (commented-out alternatives: `M.tCheckDate`, `IFNULL(P.tCompleteDate, M.tDeliverDate)`). Snapshots the pre-sort `iOrder` to `mftproductionplaniorderbak` with the new batch number. Then re-numbers via a `@rownum` window starting at 100000 ordered by `tCreateDate`, and writes the resulting sequence back to `mftproductionplan.iOrder` via the temp `p_CalcTable`. Drops both temp tables before returning.

**Invocation:** Standard `Sp_<Module>_BtnEvent*` slot — dispatched by `GenericProcedureCallServiceImpl.doGenericProcedureCall()` when the 主计划 grid's 智能排序 button names this procedure. xly-src ships `script/标版/30100101/schedule/Sp_productionMainPlan_BtnEventAutoOrder.sql`. No form-master or gdsmodule binding.
