SCTMES_V5/mes_in_sct/app/action/SetSublibrary.php

162 lines
6.7 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;
/**
* ERP库区同步接口
* Class SetSublibrary
* @package app\action
*/
class SetSublibrary
{
public function execute($post)
{
// 验证数据
$param = check_valid($post['action'], [
['listData', 'array', '子库数据集'],
], $post['param']);
try {
$listData = $param['listData'];
// 检索是否存在必要字段
foreach ($listData as $index => $item) {
$idx = $index + 1;
$check_feilds = [
['name' => 'name', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'code', 'feild_type' => 'string', 'required' => true, 'len' => 100],
['name' => 'create_time', 'feild_type' => 'timestamp', 'required' => true, 'len' => 6],
['name' => 'update_time', 'feild_type' => 'timestamp', 'required' => true, 'len' => 6],
['name' => 'effective_date', 'feild_type' => 'timestamp', 'required' => false, 'len' => 6],
];
foreach ($check_feilds as $val) {
if (!isset($item[$val['name']])) {
throw new Exception("子库数据集第[{$idx}]位置数据对象不存在字段[{$val['name']}]", 4001);
}
if ($val['required']) {
if ($val['feild_type'] == 'string') {
// 判断是否是一个字符串
if (!is_string($item[$val['name']])) {
throw new Exception("子库数据集第[{$idx}]位置数据对象[{$val['name']}]的值不是字符串类型,请检查!", 4001);
}
// 判断长度
if (strlen($item[$val['name']]) > $val['len']) {
throw new Exception("子库数据集第[{$idx}]位置数据对象[{$val['name']}]的值不符合规定的长度[{$val['len']}],请检查!", 4001);
}
}
// 判断创建修改时间格式是否正确
if ($val['name'] == 'create_time' || $val['name'] == 'update_time') {
if (date('Y-m-d H:i:s', strtotime($item[$val['name']])) != $item[$val['name']]) {
throw new Exception("子库数据集第[{$idx}]位置数据对象字段[{$val['name']}]的时间格式不正确正确例子如2023-01-01 09:09:09", 4001);
}
}
if (empty($item[$val['name']])) {
throw new Exception("子库数据集第[{$idx}]位置数据对象字段[{$val['name']}]的值不能为空", 4001);
}
}
}
$listData[$index]['status'] = '';
if (empty($item['effective_date'])) {
$listData[$index]['status'] = 1;
} else {
if (date('Y-m-d', strtotime($item['effective_date'])) != $item['effective_date']) {
throw new Exception("子库数据集第[{$idx}]位置数据对象字段[effective_date]的时间格式不正确正确例子如2023-01-01", 4001);
}
if (strtotime($item['effective_date']) < time()) {
$listData[$index]['status'] = 0;
} else {
$listData[$index]['status'] = 1;
}
}
}
foreach ($listData as $key => $value) {
// 检测编码是否存在
$sql = sprintf(
"SELECT id
FROM hf_mes_wms_erp_sublibrary
WHERE code='%s' LIMIT 1;",
$value['code']
);
$ret = Db::fetch($sql);
if (!empty($ret)) {
// 修改数据
$update_keys = [
'name',
'code',
'effective_date',
'create_time',
'update_time',
'status'
];
$update_str = '';
foreach ($update_keys as $key) {
if (isset($value[$key])) {
if (!empty($value[$key])) {
if (in_array($key, ['status'])) {
$update_str .= "\"{$key}\"={$value[$key]},";
} else {
$update_str .= "\"{$key}\"='{$value[$key]}',";
}
}
}
}
$sql = sprintf(
"UPDATE \"%s\" SET %s WHERE id='%s'",
'hf_mes_wms_erp_sublibrary',
rtrim($update_str, ','),
$ret['id']
);
Db::query($sql);
} else {
// 新增数据
$insert_keys = [
'name',
'code',
'effective_date',
'create_time',
'update_time',
'status'
];
$insert_key_str = '';
$insert_val_str = '';
foreach ($insert_keys as $key) {
if (isset($value[$key])) {
if (!empty($value[$key])) {
$insert_key_str .= "\"{$key}\",";
if (in_array($key, ['status'])) {
$insert_val_str .= "{$value[$key]},";
} else {
$insert_val_str .= "'{$value[$key]}',";
}
}
}
}
$sql = sprintf(
"INSERT INTO %s(%s) VALUES (%s)",
'hf_mes_wms_erp_sublibrary',
rtrim($insert_key_str, ','),
rtrim($insert_val_str, ',')
);
Db::query($sql);
}
}
} catch (Exception $e) {
throw new Exception($e->getMessage(), $e->getCode() ? $e->getCode() : 4001);
}
return '';
}
}