SCTMES_V5/mes_in_sct/app/action/GetTrayProcessResult.php

147 lines
4.9 KiB
PHP
Raw Normal View History

2025-06-14 18:55:09 +08:00
<?php
namespace app\action;
use Exception;
use libs\db\Db;
/**
* 根据工序编码获取电池最新批次的工序数据
* @author sheng`
* @date 2024-07-17
* @param device_code 设备编码
* @param process_code 工序编码
* @param battery_ids 电池条码数组
*
* @return array
*/
class GetTrayProcessResult
{
public function execute($post)
{
// 验证数据
$param = check_valid($post['action'], [
['device_code', 'string', '设备编码'],
['process_code', 'string', '工序编码'],
['tray', 'string', '托盘条码']
], $post['param']);
try{
//获取托盘种在使用的电池条码
$sql = sprintf(
"SELECT tray,lot,batch,subbatch,next_process_code
FROM hf_mes_production_tray_map
WHERE tray='%s' AND active=1 LIMIT 1;",
$param['tray']
);
$ret = Db::fetch($sql);
if (empty($ret)) {
throw new Exception("托盘[{$param['tray']}]不是激活状态");
}
$sql = sprintf(
"SELECT battery_id,active,class,classname,process_code
FROM \"%s\"
WHERE tray='%s' AND lot='%s' ORDER BY id ASC;",
config('app.bkv_prefix') . $ret['subbatch'],
$ret['tray'],
$ret['lot']
);
$ret = Db::fetchAll($sql);
if (empty($ret)) {
throw new Exception("托盘[{$param['tray']}]的电池数据不存在[bkv]");
}
$battery_ids = array_map(function ($value) {
// 如果为空,统一返回字符串空
return empty($value) ? '' : $value;
}, array_column($ret, 'battery_id'));
$process_code = $param['process_code'];
$result = [];
// 判断结果参数是否都上传了
$sql = sprintf(
"SELECT id FROM hf_mes_technology_process WHERE code='%s' LIMIT 1;",
$process_code
);
$ret = Db::fetch($sql);
if (is_null($ret)) {
throw new Exception("工序编码[{$process_code}]不存在");
}
// 判断结果参数是否都上传了
$sql = sprintf(
"SELECT a.id,b.code as field_code,b.name as field_name,b.field_type,b.is_upload
FROM hf_mes_technology_process AS a
INNER JOIN hf_mes_technology_process_result_param b ON a.id = b.process_id
WHERE a.code='%s';",
$process_code
);
$result_param_need = Db::fetchAll($sql);
$result_param_need_params = array_column($result_param_need, 'field_code');
if (count($result_param_need_params) <= 0) {
throw new Exception("工序编码[{$process_code}]不存在编码数组");
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
$select_param_sql = "";
foreach ($result_param_need_params as $value) {
$result[$value] = [];
$select_param_sql .= "\"{$process_code}.{$value}\",";
}
// 初始化结果数组,保证返回的数据结构完整
foreach ($result_param_need_params as $value) {
$result[$value] = array_fill(0, count($battery_ids), '0'); // 先填充默认值
}
foreach ($battery_ids as $k => $battery_id) {
if ($battery_id === '') {
continue; // 空电池ID不查询保留默认值
}
$sql = sprintf(
"SELECT tray,lot,batch,subbatch
FROM hf_mes_production_battery_map
WHERE battery_id='%s' ORDER BY id DESC LIMIT 1;",
$battery_id
);
$ret = Db::fetch($sql);
if (is_null($ret)) {
continue; // 查询不到也跳过,默认值仍然有效
}
list($tray, $lot, $batch, $subbatch) = [
$ret['tray'],
$ret['lot'],
$ret['batch'],
$ret['subbatch']
];
$sql = sprintf(
"SELECT $select_param_sql class,classname,next_process_code
FROM \"%s\"
WHERE battery_id='%s' AND tray='%s' AND lot='%s' LIMIT 1;",
config('app.bkv_prefix') . $subbatch,
$battery_id,
$tray,
$lot
);
$ret = Db::fetch($sql);
if (is_null($ret)) {
continue; // 查询不到,默认值仍然有效
}
foreach ($result_param_need_params as $value) {
$result[$value][$k] = $ret["{$process_code}.{$value}"] ?? ''; // 直接覆盖到对应索引
}
}
return $result;
}
}