# `Sp_BtnCalculation_Machine_time_byMachineType` (procedure)

> 根据机台类型循环计算时间


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

## Parameters

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

## Body

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

## Narrative

**Business context:** 排程 / 统筹排程 — machine-loop time-calculator for the unified-planning button (统筹排程 / 计算时间). Iterates printing machines (`p_sMachineType=1`) or all non-printing machines (`p_sMachineType<>1`) and recomputes per-`mftproductionplanslave` start/end times based on the last 报工 (`mftproductionreportmaster.tCreateDate`) plus shift definition on `elemachine.sWorkType/sShiftsWork/sOffWork`.

**What it does:** Cursors `elemachine` rows filtered by `p_sMachineType`. Per machine, reads the latest checked production-report `tCreateDate` and seeds it through `FUN_GET_DATE_BC_STARTDATE_NEWTYPE` to derive `p_dbtimestart`. When `p_sMachineType=2`, picks the earliest in-progress (`sState IN ('1','2')`) `mftproductionplanslave` ordered by `tReportCompletDate/tHeoryCompletDate`; otherwise picks earliest by `tStartDate`. Then back-rolls scheduled `tHeoryCompletDate` for the chained next-process planslaves so downstream processes shift in lockstep. Locked rows (`bCalcTimeLock=1`) are respected by tracking `p_tMaxEndDate`.

**Invocation:** Called by sibling procedure `Sp_unifiedPlanning_BtnCalculation_Machine` — twice, with `NULL` (印刷机) and `2` (其它机台). That sibling is in turn dispatched dynamically via `GenericProcedureCallServiceImpl.doSetTimeCalcMachineHour()` (special-cased on `sProName='sp_unifiedplanning_btncalculation'`) when the 统筹排程 → 计算时间 button fires from the schedule UI.
