SCTMES_V5/mes_in_sct/app/action/SetMaterialBom.php
2025-06-14 18:55:09 +08:00

201 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\action;
use Exception;
use libs\db\Db;
// 同步EPR物料bom数据
class SetMaterialBom
{
public function execute($post)
{
// 验证数据
$param = check_valid($post['action'], [
['listData', 'array', '物料Bom数据集'],
], $post['param']);
try {
$listData = $param['listData'];
// 检索是否存在必要字段
foreach ($listData as $index => $item) {
$idx = $index + 1;
$check_feilds = [
['name' => 'code', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'name', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'version', 'feild_type' => 'string', 'required' => false, 'len' => 20],
['name' => 'workingsubclass', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'assembly_item', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'input_quantity', 'feild_type' => 'float', 'required' => true, 'len' => 1],
['name' => 'output_rate', 'feild_type' => 'float', 'required' => true, 'len' => 1],
['name' => 'start_effective_date', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'end_effective_date', 'feild_type' => 'string', 'required' => false, 'len' => 100],
];
foreach ($check_feilds as $val) {
if (!isset($item[$val['name']])) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象不存在字段[{$val['name']}]", 4001);
}
if ($val['required']) {
if ($val['feild_type'] == 'string') {
// 判断是否是一个字符串
if (!is_string($item[$val['name']])) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象[{$val['name']}]的值不是字符串类型,请检查!", 4001);
}
// 判断长度
if (strlen($item[$val['name']]) > $val['len']) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象[{$val['name']}]的值不符合规定的长度[{$val['len']}],请检查!", 4001);
}
} elseif ($val['feild_type'] == 'int') {
if (!is_int($item[$val['name']])) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象对应的字段[{$val['name']}]值不是整数数字类型", 4001);
}
if ($val == 'active') {
if (!in_array($item[$val['name']], [0, 1])) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象对应的字段[{$val['name']}]值只能是0或者1", 4001);
}
}
} elseif ($val['feild_type'] == 'float') {
if (!is_numeric($item[$val['name']])) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象对应的字段[{$val['name']}]值不是浮点型", 4001);
}
}
// 判断创建修改时间格式是否正确
if ($val['name'] == 'create_time' || $val['name'] == 'update_time' || $val['name'] == 'start_effective_date') {
if (date('Y-m-d H:i:s', strtotime($item[$val['name']])) != $item[$val['name']]) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象字段[{$val['name']}]的时间格式不正确正确例子如2023-01-01 09:09:09", 4001);
}
}
if (empty($item[$val['name']])) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象字段[{$val['name']}]的值不能为空", 4001);
}
} else {
if ($val['name'] == 'end_effective_date' && !empty($item['end_effective_date'])) {
if (date('Y-m-d H:i:s', strtotime($item['end_effective_date'])) != $item['end_effective_date']) {
throw new Exception("物料Bom数据集第[{$idx}]位置数据对象字段[end_effective_date]的时间格式不正确正确例子如2023-01-01 09:09:09", 4001);
}
}
}
}
}
// 判断物料code是否重复
$duplicates = findDuplicates($listData, ['code']);
if (!empty($duplicates)) {
$duplicates_str = '上传物料Bom数据集';
foreach ($duplicates as $key => $value) {
$duplicates_str .= '参数' . $key . '存在重复值【' . implode("", $value) . '】,';
}
throw new Exception($duplicates_str, 4001);
}
$code_arr = array_column($listData, 'code');
foreach ($listData as $idx => $value) {
if($value['assembly_item'] != '-1' && !in_array($value['assembly_item'],$code_arr)){
throw new Exception("物料Bom数据集第[".($idx+1)."]位置的母件料号assembly_item值[".$value['assembly_item']."]不存在对应物料编码");
}
// 判断物料编码是否存在
$sql = sprintf(
"SELECT id
FROM hf_mes_product_bom_source
WHERE code='%s' LIMIT 1;",
$value['code']
);
$ret = Db::fetch($sql);
if (empty($ret)) {
throw new Exception("物料Bom数据集第[" . ($idx + 1) . "]位置的物料编码code值[" . $value['code'] . "]不存在", 4001);
}
$listData[$idx]['bom_source_id'] = $ret['id'];
}
$sql = "SELECT id as product_model_id FROM hf_mes_product_model ORDER BY id DESC LIMIT 1";
$ret = Db::fetch($sql);
if(empty($ret)){
throw new Exception("电池型号不存在,请检查是否添加电池型号");
}
$product_model_id = $ret["product_model_id"];
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode() ? $e->getCode() : 4001);
}
// 工序单元映射信息
$workingsubclass_info = [
"1" => "ZJJB", // 正极搅拌
"2" => "ZJTB", // 正极涂布
];
// 开启事务
Db::beginTrans();
try {
// a、往hf_mes_product_bom插入一条数据
// 遍历bom结构数据
// b、当前code的值存在其他assembly_itempid代表是其他code的产出物料
// c、当前assembly_itempid的值存在其他code上代表是其他code的投入物料
// d、往hf_mes_product_bom_relationship上插入数据bom_source_id、bom_id、in_workingsubclass、out_workingsubclass、input_quantity上必须要有数据
// code、name采用当前时间点
$tmp_code = date("YmdHis");
$create_time = date("Y-m-d H:i:s");
$sql = "INSERT INTO hf_mes_product_bom (product_model_id, code, name, status, create_time) VALUES ('$product_model_id','$tmp_code','$tmp_code','1','$create_time')";
$row = Db::query($sql);
if ($row === NULL) {
throw new Exception("新增物料BOM清单失败", 4001);
}
$bom_id = $row;
foreach($listData as $value){
$current_code = $value["code"];
$current_assembly_item = $value["assembly_item"];
$current_bom_source_id = $value["bom_source_id"];
$current_workingsubclass = $workingsubclass_info[$value["workingsubclass"]];
$current_input_quantity = $value["input_quantity"];
// 产出物料编码缓存,产出只有一个,需要判断是否为一个
$output_codes = [];
foreach($listData as $val){
$other_code = $val["code"];
$other_assembly_item = $val["assembly_item"];
$other_bom_source_id = $val["bom_source_id"];
$other_workingsubclass = $workingsubclass_info[$val["workingsubclass"]];
$other_input_quantity = $val["input_quantity"];
// 当前code的值存在其他assembly_itempid代表当前code是其他code的产出物料产出只有一个需要判断是否为一个
// 当前assembly_itempid的值存在其他code上代表当前assembly_itempid是其他code的投入物料
if($current_code == $other_assembly_item && !in_array($current_code,$output_codes)){
$sql = "INSERT INTO hf_mes_product_bom_relationship (bom_source_id, bom_id, out_workingsubclass) VALUES ('$current_bom_source_id','$bom_id','$current_workingsubclass')";
$ret = Db::query($sql);
if ($ret === NULL) {
throw new Exception("产出物料编码[".$current_code."]插入bom物料清单关系表失败", 4001);
}
$output_codes[] = $current_code;
}else if($current_assembly_item == $other_code){
$sql = "INSERT INTO hf_mes_product_bom_relationship (bom_source_id, bom_id, in_workingsubclass, input_quantity) VALUES ('$current_bom_source_id','$bom_id','$other_workingsubclass','$current_input_quantity')";
$ret = Db::query($sql);
if ($ret === NULL) {
throw new Exception("投入物料编码[".$current_code."]插入bom物料清单关系表失败", 4001);
}
}
}
}
} catch(Exception $e) {
Db::rollBackTrans();
throw new Exception($e->getMessage(), $e->getCode() ? $e->getCode() : 4001);
}
Db::commitTrans();
return '';
}
}