# `Sp_GetMachineByApsRule` (procedure)

> 主计划：自定义排程规则(根据APS规则选择最合适机台)


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `sWorkOrderGuid` | `varchar(5000)` |
| 2 | IN | `sWorkOrderProcessid` | `varchar(100)` |
| 3 | IN | `sProcessid` | `varchar(100)` |
| 4 | IN | `p_sMachineid` | `varchar(100)` |
| 5 | IN | `sBrId` | `varchar(100)` |
| 6 | IN | `sSuId` | `varchar(100)` |
| 7 | OUT | `sReturnMachineId` | `varchar(250)` |
| 8 | OUT | `sReturnInfo` | `text` |

## Body

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

## Narrative

**Business context:** 主计划 → 自动排程 → APS规则 — picks the 最合适 (best-fit) machine for a single work-order process row by scoring `eleprocessmachineapsrule` rules against the work-order's printing-color count, weight, length/width, 工艺/材料/产品分类/客户备注, and bill-type (一类单/二类单/.../打样单 from `mftworkordermaster.sManualNo`). Sets the chosen machine into `mftproductionplanslave.sMachineId` (the call site loops over the plan-slave rows).

**What it does:** When `p_sMachineid` is blank picks `eleprocessmachine.bDefault=1`; otherwise excludes that machine. Falls through to a `Machine_Tmp` rule-table search keyed by the seven dimensions above plus three APS-quantity caps (`dAPSProcQtyMax`/`dAPSQtyMax`/`dAPSZQtyMax`). Returns the choice in `sReturnMachineId` and the human-readable trace ("rule matched / fallback to default / no match") in `sReturnInfo`.

**Invocation:** Called per-row by Java `BtnCalculationServiceImpl.doSetTimeCalcMachineHour()` — invoked over `mftproductionplanslave` rows in a fixed-thread-pool batch of 100, after which the controller writes a `mftproductionplancalc` row. Also `CALL`-chained from `Sp_unifiedPlanning_BtnCalculation` and the `Sp_Manufacture_InsertMftPlan*` insertion family. Reachable only via the dispatch, not via any form or gdsmodule hook.
