SCTMES_V5/mes_in_sct/app/action/SetBatteryRebatch.php

245 lines
13 KiB
PHP
Raw Permalink Normal View History

2025-06-14 18:55:09 +08:00
<?php
namespace app\action;
use Exception;
use libs\db\Db;
use libs\listener\Event;
class SetBatteryRebatch
{
/*
{
"action":"set_battery_rebatch",
"param":{
"battery_ids":["MP01436ES2A0190"]
}
}
1、循环电池数组判断电池是否在hf_mes_production_battery_map表,如果不存在返回异常如果存在则获取该电池信息id最大的一条数据中的lot号和subbatch
2、通过返回lotsubbatch去hf_mes_tmp_bkv_{subbatch号}表中判断电池的激活状态 如果active=1则返回异常对应电池是激活的然后再判断电池是否有NG class != 'NG'如果没有则返回异常对应电池没有NG信息
3、分别在hf_mes_production_battery_map hf_mes_tmp_bkv_{subbatch号}表还有 hf_mes_bkv_batch_process_log表中新插入一条数据 之前查询条件返回的所有数据一致 就是lot号需要修改为当前时间的时间戳
*/
public function execute($post)
{
// 验证数据
$param = check_valid($post['action'], [
['battery_ids', 'array', '电池条码数组']
], $post['param']);
$battery_ids = $param['battery_ids'];
// 判断是否存在不是字符串'0'的情况,约束托盘空位值只能传字符串'0'而不是其他值
$filteredArray = array_filter($battery_ids, function ($value) {
return $value !== '0' && empty($value);
}, ARRAY_FILTER_USE_BOTH);
if (count($filteredArray) > 0) {
$keys = implode(',', array_keys($filteredArray));
throw new Exception("电池条码数组[battery_ids]以下位置[{$keys}]中存在不合法的值,如[0, '', null, NAN]等,电池条码值只能是[字符串'0'或者真实条码]");
}
// 判断电池是否存在重复值
$filteredArray = array_filter($battery_ids, function ($value) {
return !empty($value);
});
$repeat_batterys = array_keys(array_count_values($filteredArray), 2);
if (count($repeat_batterys) > 0) {
$repeat_keys = implode(',', array_keys($repeat_batterys));
throw new Exception("电池条码数组[battery_ids]存在重复的电池条码[{$repeat_keys}],请检查!");
}
Db::beginTrans();
try {
// 循环电池数组
foreach ($battery_ids as $battery_id) {
// 1. 判断电池是否在 hf_mes_production_battery_map 表,并获取 id 最大的一条记录
$sql = sprintf(
"SELECT * FROM hf_mes_production_battery_map WHERE battery_id='%s' ORDER BY id DESC LIMIT 1;",
$battery_id
);
$battery_info = Db::query($sql);
if (empty($battery_info)) {
throw new Exception("电池 {$battery_id} 在表 hf_mes_production_battery_map 中不存在!");
}
$lot = $battery_info['lot'];
$subbatch = $battery_info['subbatch'];
$batch = $battery_info['batch'];
// 2. 动态查询 hf_mes_tmp_bkv_{subbatch} 表,表名加双引号
$subbatch_table = sprintf('hf_mes_tmp_bkv_%s', $subbatch);
$sql = sprintf(
'SELECT * FROM "%s" WHERE battery_id=\'%s\' AND lot=\'%s\';',
$subbatch_table,
$battery_id,
$lot // 添加 lot 作为查询条件
);
$battery_status = Db::query($sql);
if (empty($battery_status)) {
throw new Exception("电池 【{$battery_id}】在批次 【{$batch} 】中不存在!");
}
// 判断激活状态
if ($battery_status[0]['active'] == 1) {
throw new Exception("电池 【{$battery_id}】在批次【 {$batch}】中是激活的,不需要重新激活!");
}
// 判断是否有 NG 信息
if ($battery_status[0]['class'] != 'NG') {
throw new Exception("电池 【{$battery_id}】在批次【 {$batch}】中没有NG信息不是NG电池不需要重新激活");
}
// 获取当前时间戳
$current_timestamp = get_subtraction();
// ===============================复制hf_mes_production_battery_map表数据===============================================
// 查询hf_mes_production_battery_map表结构获取字段列表
$table_info_sql = "SELECT column_name FROM information_schema.columns WHERE table_name = 'hf_mes_production_battery_map' AND table_schema = 'public';";
$columns = Db::query($table_info_sql);
// 获取字段名,去除自增主键'id',因为插入时不需要提供它
$fields = array_column($columns, 'column_name');
$fields = array_filter($fields, function ($field) {
return $field != 'id'; // 假设 'id' 是自增主键
});
$battery_info['lot'] = $current_timestamp;
// 构造插入语句中的字段和对应的值
$insert_fields = [];
$insert_values = [];
foreach ($fields as $field => $data_type) {
$value = $battery_info[$fields[$field]];
$insert_fields[] = $fields[$field];
// 处理数据类型,确保整数类型使用 NULL
if ($data_type === 'integer' || $data_type === 'bigint') {
$insert_values[] = ($value === null || $value === '') ? 'NULL' : intval($value);
} else {
// 处理其他类型,使用 addslashes 转义字符串
if ($value === null) {
$insert_values[] = 'NULL'; // 插入 NULL
} else {
$insert_values[] = "'" . $value . "'";
}
}
}
// 拼接字段和值
$insert_fields_str = implode(', ', $insert_fields);
$insert_values_str = implode(', ', $insert_values);
// 生成hf_mes_production_battery_map的插入语句
$insert_sql = sprintf(
"INSERT INTO hf_mes_production_battery_map (%s) VALUES (%s);",
$insert_fields_str,
$insert_values_str
);
// 执行插入
$ret = Db::query($insert_sql);
if (empty($ret)) {
throw new Exception("插入battery_map表失败");
}
// ===============================复制BKV表数据===============================================
// 查询bkv表结构获取字段列表
$table_info_sql_bkv = "SELECT column_name FROM information_schema.columns WHERE table_name = '$subbatch_table' AND table_schema = 'public';";
$columns_bkv = Db::query($table_info_sql_bkv);
// 获取字段名,去除自增主键'id',因为插入时不需要提供它
$fields_bkv = array_column($columns_bkv, 'column_name');
$fields_bkv = array_filter($fields_bkv, function ($field_bkv) {
return $field_bkv != 'id'; // 假设 'id' 是自增主键
});
$battery_status[0]['lot'] = $current_timestamp;
$battery_status[0]['class'] = null;
$battery_status[0]['classname'] = null;
$battery_status[0]['active'] = 1;
// 构造插入语句中的字段和对应的值
$insert_fields_bkv = [];
$insert_values_bkv = [];
foreach ($fields_bkv as $field_bkv => $data_type_bkv) {
$value_bkv = $battery_status[0][$fields_bkv[$field_bkv]];
$insert_fields_bkv[] = '"' . $fields_bkv[$field_bkv] . '"';
// 处理数据类型,确保整数类型使用 NULL
if ($data_type_bkv === 'integer' || $data_type_bkv === 'bigint') {
$insert_values_bkv[] = ($value_bkv === null || $value_bkv === '') ? 'NULL' : intval($value_bkv);
} else {
// 处理其他类型,使用 addslashes 转义字符串
if ($value_bkv === null) {
$insert_values_bkv[] = 'NULL'; // 插入 NULL
} else {
$insert_values_bkv[] = "'" . $value_bkv . "'";
}
}
}
// 拼接字段和值
$insert_fields_bkv_str = implode(', ', $insert_fields_bkv);
$insert_values_bkv_str = implode(', ', $insert_values_bkv);
// 生成hf_mes_production_battery_map的插入语句
$insert_sql_bkv = sprintf(
"INSERT INTO " . '"' . $subbatch_table . '"' . " (%s) VALUES (%s);",
$insert_fields_bkv_str,
$insert_values_bkv_str
);
// 执行插入
$ret = Db::query($insert_sql_bkv);
if (empty($ret)) {
throw new Exception("插入bkv表失败");
}
// ===============================复制hf_mes_bkv_batch_process_log表数据===============================================
$sql = sprintf(
"SELECT * FROM hf_mes_bkv_batch_process_log WHERE item_id='%s' ORDER BY id DESC LIMIT 1;",
$battery_id,
$lot
);
$process_log_info = Db::query($sql);
if (empty($process_log_info)) {
throw new Exception("电池 {$battery_id} 在表 hf_mes_bkv_batch_process_log 中不存在!");
}
$table_info_sql_log = "SELECT column_name FROM information_schema.columns WHERE table_name = 'hf_mes_bkv_batch_process_log' AND table_schema = 'public';";
$columns_log = Db::query($table_info_sql_log);
// 获取字段名,去除自增主键'id',因为插入时不需要提供它
$fields_log = array_column($columns_log, 'column_name');
$fields_log = array_filter($fields_log, function ($field_log) {
return $field_log != 'id'; // 假设 'id' 是自增主键
});
$process_log_info['lot'] = $current_timestamp;
$process_log_info['ng_code'] = 0;
$process_log_info['class'] = null;
$process_log_info['classname'] = null;
$process_log_info['last_status'] = 1;
// 构造插入语句中的字段和对应的值
$insert_fields_log = [];
$insert_values_log = [];
foreach ($fields_log as $field_log => $data_type_log) {
$value_log = $process_log_info[$fields_log[$field_log]];
$insert_fields_log[] = '"' . $fields_log[$field_log] . '"';
// 处理数据类型,确保整数类型使用 NULL
if ($data_type_log === 'integer' || $data_type_log === 'bigint') {
$insert_values_log[] = ($value_log === null || $value_log === '') ? 'NULL' : intval($value_log);
} else {
// 处理其他类型,使用 addslashes 转义字符串
if ($value_log === null) {
$insert_values_log[] = 'NULL'; // 插入 NULL
} else {
$insert_values_log[] = "'" . $value_log . "'";
}
}
}
// 拼接字段和值
$insert_fields_log_str = implode(', ', $insert_fields_log);
$insert_values_log_str = implode(', ', $insert_values_log);
// 生成hf_mes_bkv_batch_process_log的插入语句
$insert_sql_log = sprintf(
"INSERT INTO hf_mes_bkv_batch_process_log (%s) VALUES (%s);",
$insert_fields_log_str,
$insert_values_log_str
);
// 标记最后一条数据的状态
Event::emit('SetProcessLogEvent.updateLastStatus', $battery_ids);
// 执行插入
$ret = Db::query($insert_sql_log);
if (empty($ret)) {
throw new Exception("插入hf_mes_bkv_batch_process_log表失败");
}
}
} catch (Exception $e) {
Db::rollBackTrans();
throw new Exception($e->getMessage());
}
Db::commitTrans();
return '';
}
}