# `Sp_mes_Timeanalyse_Machine` (procedure)

> @author:zhucx
                @date:20210621 封存
                @describe: 机器时间分析


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `tBetweenDate` | `varchar(100)` |
| 2 | IN | `sPMachineID` | `varchar(100)` |
| 3 | IN | `sBrId` | `varchar(100)` |
| 4 | IN | `sSuId` | `varchar(100)` |
| 5 | IN | `sLoginId` | `varchar(100)` |
| 6 | IN | `bFilter` | `varchar(5000)` |
| 7 | IN | `pageNum` | `int` |
| 8 | IN | `pageSize` | `int` |
| 9 | OUT | `totalCount` | `int` |
| 10 | IN | `countCloumn` | `varchar(5000)` |
| 11 | OUT | `countMapJson` | `longtext` |
| 12 | IN | `sFilterOrderBy` | `varchar(5000)` |
| 13 | IN | `sGroupby_select_sql` | `varchar(5000)` |
| 14 | IN | `sGroupby_group_sql` | `varchar(5000)` |
| 15 | OUT | `sColumnNameConfig` | `longtext` |

## Body

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

## Narrative

**Business context:** 生产管理 → 生产执行 → 数据采集与设备控制 → 设备用时分析 — paged 机器时间分析 report with *dynamic columns*: one column per distinct PLC abnormality status (`sStatus`), so the user sees "停机原因 A 用时", "停机原因 B 用时" etc. as separate columns rather than a single status code.

**What it does:** Defaults date range to `NOW() - 1 MONTH`; builds `p_bd_Tm`/`p_machinedata`/`p_bd_Tm_erro_detial`/`p_bd_Tm_return` from `plc_machinedata` and machine masters. The base columns hold调试/生产/异常/保养 totals per `sMachineId`. Then iterates over distinct `sStatus` values, dynamically `ALTER`s `p_bd_Tm_return` to ADD a DECIMAL column named after the status, `UPDATE`s it from `p_bd_Tm_erro_detial`, and accumulates `p_sColumnNameConfig` via `Fun_get_show_config_ys40` so the front-end grid renders the new column. Returns `p_bd_Tm_return` plus `sColumnNameConfig` as a JSON array.

**Invocation:** Bound as the data-source of the 设备用时分析 form (`sParentId 19211681019715708720228980`) via `gdsconfigformmaster.sSqlStr` — under 数据采集与设备控制 inside 生产执行. xly-src ships `script/标版/30100101/schedule/Sp_mes_Timeanalyse_Machine.sql` (install) and a `客户/亚明威` custom variant. Body marked 封存.
