|
@@ -7,6 +7,7 @@ use app\common\library\token\driver\Redis;
|
|
|
use think\Db;
|
|
use think\Db;
|
|
|
use Overtrue\Pinyin;
|
|
use Overtrue\Pinyin;
|
|
|
use think\Exception;
|
|
use think\Exception;
|
|
|
|
|
+use think\Log;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 中间表数据同步
|
|
* 中间表数据同步
|
|
@@ -2104,70 +2105,97 @@ class Synchronization extends Api
|
|
|
*/
|
|
*/
|
|
|
public function syncFinishStockMiddle($uniqId, $insertType = '新增', $iQtyOverride = null, $usernameOverride = null)
|
|
public function syncFinishStockMiddle($uniqId, $insertType = '新增', $iQtyOverride = null, $usernameOverride = null)
|
|
|
{
|
|
{
|
|
|
- if (empty($uniqId)) {
|
|
|
|
|
- throw new \Exception('同步失败:UniqId 为空');
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $syncTime = date('Y-m-d H:i:s');
|
|
|
|
|
+ $record = [
|
|
|
|
|
+ 'UniqId' => $uniqId,
|
|
|
|
|
+ 'jjcp_gdbh' => '',
|
|
|
|
|
+ 'jjcp_cpdh' => '',
|
|
|
|
|
+ 'jjcp_cpmc' => '',
|
|
|
|
|
+ 'jjcp_yjno' => 0,
|
|
|
|
|
+ 'Sys_id' => '',
|
|
|
|
|
+ ];
|
|
|
|
|
|
|
|
- $record = db('成品入仓')->where('UniqId', $uniqId)->find();
|
|
|
|
|
-
|
|
|
|
|
- if (empty($record)) {
|
|
|
|
|
- throw new \Exception('同步失败:未找到成品入仓记录 UniqId=' . $uniqId);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (empty($uniqId)) {
|
|
|
|
|
+ throw new \Exception('同步失败:UniqId 为空');
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if ($usernameOverride !== null && trim((string)$usernameOverride) !== '') {
|
|
|
|
|
- $record['Sys_id'] = $usernameOverride;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $found = db('成品入仓')->where('UniqId', $uniqId)->find();
|
|
|
|
|
+ if (empty($found)) {
|
|
|
|
|
+ throw new \Exception('同步失败:未找到成品入仓记录 UniqId=' . $uniqId);
|
|
|
|
|
+ }
|
|
|
|
|
+ $record = $found;
|
|
|
|
|
+
|
|
|
|
|
+ if ($usernameOverride !== null && trim((string)$usernameOverride) !== '') {
|
|
|
|
|
+ $record['Sys_id'] = $usernameOverride;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 写入目标:本地测试库 database.db8(MySQL)
|
|
|
|
|
- // 是否同步的判定在 shouldSyncFinishStockMiddle:用正式库 database.db3 查 U8_06工单资料是否存在
|
|
|
|
|
- $targetDb = Db::connect(config('database.db8'));
|
|
|
|
|
- $targetTable = 'U8_成品入仓';
|
|
|
|
|
|
|
+ // 写入目标:本地测试库 database.db8(MySQL)
|
|
|
|
|
+ // 是否同步的判定在 shouldSyncFinishStockMiddle:用正式库 database.db3 查 U8_06工单资料是否存在
|
|
|
|
|
+ $targetDb = Db::connect(config('database.db8'));
|
|
|
|
|
+ $targetTable = 'U8_08成品入库';
|
|
|
|
|
|
|
|
- $syncTime = date('Y-m-d H:i:s');
|
|
|
|
|
- $smb = preg_replace('/^[\s\x{3000}]+|[\s\x{3000}]+$/u', '', (string)$record['jjcp_smb']);
|
|
|
|
|
- $isEndBoard = ($smb !== '' && mb_substr($smb, 0, 1, 'UTF-8') === '末') ? '末板' : '';
|
|
|
|
|
- $id = 0;
|
|
|
|
|
- $errorMsg = '';
|
|
|
|
|
- $iQty = $iQtyOverride !== null ? $iQtyOverride : $record['jjcp_sl'];
|
|
|
|
|
|
|
+ $smb = preg_replace('/^[\s\x{3000}]+|[\s\x{3000}]+$/u', '', (string)$record['jjcp_smb']);
|
|
|
|
|
+ $isEndBoard = ($smb !== '' && mb_substr($smb, 0, 1, 'UTF-8') === '末') ? '末板' : '';
|
|
|
|
|
+ $iQty = $iQtyOverride !== null ? $iQtyOverride : $record['jjcp_sl'];
|
|
|
|
|
|
|
|
- try {
|
|
|
|
|
$maxId = (int)$targetDb->name($targetTable)->max('id');
|
|
$maxId = (int)$targetDb->name($targetTable)->max('id');
|
|
|
$id = $maxId + 1;
|
|
$id = $maxId + 1;
|
|
|
|
|
|
|
|
$data = [
|
|
$data = [
|
|
|
- 'id' => $id, // 主键ID(目标表不自增,按条数递增)
|
|
|
|
|
- 'MESCode' => '000000', // 默认编码
|
|
|
|
|
- 'dDate' => $record['jjcp_sj'], // 入仓日期
|
|
|
|
|
- 'cWhCode' => $record['仓库编号'], // 仓库编码
|
|
|
|
|
- 'cMoCode' => $record['jjcp_gdbh'], // 工单编号
|
|
|
|
|
- 'cMemo' => $record['jjcp_desc'], // 备注
|
|
|
|
|
- 'cInvCode' => $record['jjcp_cpdh'], // 物料编码
|
|
|
|
|
- 'cInvName' => $record['jjcp_cpmc'], // 物料名称
|
|
|
|
|
- 'iQty' => $iQty, // 数量(修改数量时可传差值)
|
|
|
|
|
- 'cBatch' => $record['订单编号'], // 批次号
|
|
|
|
|
- 'username' => $record['Sys_id'], // 用户名
|
|
|
|
|
- '是否末版' => $isEndBoard, // 是否末版
|
|
|
|
|
- 'MES插入类型' => $insertType, // 插入类型
|
|
|
|
|
- 'MES传递时间' => $syncTime, // 传递时间
|
|
|
|
|
- 'U8接收时间' => null, // 接收时间为空
|
|
|
|
|
- 'U8接收状态' => '0', // 状态默认 0
|
|
|
|
|
- '印件编码' => $record['jjcp_cpdh'], // 印件编码
|
|
|
|
|
- '印件名称' => $record['jjcp_cpmc'], // 印件名称
|
|
|
|
|
- '成品编码' => $record['成品编码'], // 成品编码
|
|
|
|
|
- '成品名称' => $record['成品名称'], // 成品名称
|
|
|
|
|
- 'UniqId' => $record['UniqId'], // 唯一ID
|
|
|
|
|
|
|
+ 'id' => $id,
|
|
|
|
|
+ 'MESCode' => '000000',
|
|
|
|
|
+ 'dDate' => $record['jjcp_sj'],
|
|
|
|
|
+ 'cWhCode' => $record['仓库编号'],
|
|
|
|
|
+ 'cMoCode' => $record['jjcp_gdbh'],
|
|
|
|
|
+ 'cMemo' => $record['jjcp_desc'],
|
|
|
|
|
+ 'cInvCode' => $record['jjcp_cpdh'],
|
|
|
|
|
+ 'cInvName' => $record['jjcp_cpmc'],
|
|
|
|
|
+ 'iQty' => $iQty,
|
|
|
|
|
+ 'cBatch' => $record['订单编号'],
|
|
|
|
|
+ 'username' => $record['Sys_id'],
|
|
|
|
|
+ '是否末版' => $isEndBoard,
|
|
|
|
|
+ 'MES插入类型' => $insertType,
|
|
|
|
|
+ 'MES传递时间' => $syncTime,
|
|
|
|
|
+ 'U8接收时间' => null,
|
|
|
|
|
+ 'U8接收状态' => '0',
|
|
|
|
|
+ '印件编码' => $record['jjcp_cpdh'],
|
|
|
|
|
+ '印件名称' => $record['jjcp_cpmc'],
|
|
|
|
|
+ '成品编码' => $record['成品编码'],
|
|
|
|
|
+ '成品名称' => $record['成品名称'],
|
|
|
|
|
+ 'UniqId' => $record['UniqId'],
|
|
|
];
|
|
];
|
|
|
- // 中文字段名不能走 ThinkPHP 命名绑定(PDO 会截断 :data__MES插入类型),改用位置绑定
|
|
|
|
|
- $this->insertFinishStockMiddle($targetDb, $targetTable, $data);
|
|
|
|
|
|
|
|
|
|
|
|
+ $this->insertFinishStockMiddle($targetDb, $targetTable, $data);
|
|
|
$this->writeFinishStockSyncLog($record, $uniqId, $insertType, $syncTime, true, '同步成功');
|
|
$this->writeFinishStockSyncLog($record, $uniqId, $insertType, $syncTime, true, '同步成功');
|
|
|
|
|
+ return true;
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
$errorMsg = $e->getMessage();
|
|
$errorMsg = $e->getMessage();
|
|
|
$this->writeFinishStockSyncLog($record, $uniqId, $insertType, $syncTime, false, $errorMsg);
|
|
$this->writeFinishStockSyncLog($record, $uniqId, $insertType, $syncTime, false, $errorMsg);
|
|
|
throw new \Exception('同步中间表失败:' . $errorMsg);
|
|
throw new \Exception('同步中间表失败:' . $errorMsg);
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 对外写入成品入仓同步日志(成功/失败/未同步均可)
|
|
|
|
|
+ * @param array $record
|
|
|
|
|
+ * @param int|string $uniqId
|
|
|
|
|
+ * @param string $insertType
|
|
|
|
|
+ * @param string $status 同步成功/同步失败/未同步
|
|
|
|
|
+ * @param string $statusMsg 原因说明
|
|
|
|
|
+ */
|
|
|
|
|
+ public function logFinishStockSync($record, $uniqId, $insertType, $status, $statusMsg = '')
|
|
|
|
|
+ {
|
|
|
|
|
+ $success = ($status === '同步成功');
|
|
|
|
|
+ $this->writeFinishStockSyncLog(
|
|
|
|
|
+ $record,
|
|
|
|
|
+ $uniqId,
|
|
|
|
|
+ $insertType,
|
|
|
|
|
+ date('Y-m-d H:i:s'),
|
|
|
|
|
+ $success,
|
|
|
|
|
+ $statusMsg,
|
|
|
|
|
+ $status
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -2178,24 +2206,35 @@ class Synchronization extends Api
|
|
|
* @param string $insertType 新增/修改/删除
|
|
* @param string $insertType 新增/修改/删除
|
|
|
* @param string $syncTime
|
|
* @param string $syncTime
|
|
|
* @param bool $success
|
|
* @param bool $success
|
|
|
- * @param string $statusMsg
|
|
|
|
|
|
|
+ * @param string $statusMsg 失败或跳过原因(写入 status_msg)
|
|
|
|
|
+ * @param string|null $statusOverride 覆盖 status 文案
|
|
|
*/
|
|
*/
|
|
|
- protected function writeFinishStockSyncLog($record, $uniqId, $insertType, $syncTime, $success, $statusMsg = '')
|
|
|
|
|
|
|
+ protected function writeFinishStockSyncLog($record, $uniqId, $insertType, $syncTime, $success, $statusMsg = '', $statusOverride = null)
|
|
|
{
|
|
{
|
|
|
- // 操作人取成品入仓 Sys_id(通常为字符串,如 [272/超级用户])
|
|
|
|
|
$sysId = $record['Sys_id'] ?? ($record['sys_id'] ?? '');
|
|
$sysId = $record['Sys_id'] ?? ($record['sys_id'] ?? '');
|
|
|
|
|
+ $status = $statusOverride !== null
|
|
|
|
|
+ ? (string)$statusOverride
|
|
|
|
|
+ : ($success ? '同步成功' : '同步失败');
|
|
|
|
|
+
|
|
|
|
|
+ // 保留失败原因,避免字段过长导致整行插入失败
|
|
|
|
|
+ $msg = (string)$statusMsg;
|
|
|
|
|
+ if (function_exists('mb_substr')) {
|
|
|
|
|
+ $msg = mb_substr($msg, 0, 1000, 'UTF-8');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $msg = substr($msg, 0, 1000);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
$log = [
|
|
$log = [
|
|
|
'Gdbh' => $record['jjcp_gdbh'] ?? '',
|
|
'Gdbh' => $record['jjcp_gdbh'] ?? '',
|
|
|
'wlbm' => $record['jjcp_cpdh'] ?? '',
|
|
'wlbm' => $record['jjcp_cpdh'] ?? '',
|
|
|
'yjmc' => $record['jjcp_cpmc'] ?? '',
|
|
'yjmc' => $record['jjcp_cpmc'] ?? '',
|
|
|
- 'Yjdh' => $record['jjcp_cpdh'] ?? '', // 印件代号
|
|
|
|
|
|
|
+ 'Yjdh' => $record['jjcp_cpdh'] ?? '',
|
|
|
'Yjno' => (int)($record['jjcp_yjno'] ?? 0),
|
|
'Yjno' => (int)($record['jjcp_yjno'] ?? 0),
|
|
|
- 'sys_id' => (string)$sysId, // 操作人
|
|
|
|
|
|
|
+ 'sys_id' => (string)$sysId,
|
|
|
'sys_rq' => $syncTime,
|
|
'sys_rq' => $syncTime,
|
|
|
'type' => $insertType,
|
|
'type' => $insertType,
|
|
|
- 'status' => $success ? '同步成功' : '同步失败',
|
|
|
|
|
- 'status_msg' => $success ? '' : (string)$statusMsg,
|
|
|
|
|
|
|
+ 'status' => $status,
|
|
|
|
|
+ 'status_msg' => $msg,
|
|
|
'UniqId' => (int)$uniqId,
|
|
'UniqId' => (int)$uniqId,
|
|
|
];
|
|
];
|
|
|
|
|
|
|
@@ -2208,7 +2247,19 @@ class Synchronization extends Api
|
|
|
throw new \Exception('日志 insert 返回 false');
|
|
throw new \Exception('日志 insert 返回 false');
|
|
|
}
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
- // 不阻断主流程
|
|
|
|
|
|
|
+ // 降级再试一次;仍失败则写运行日志,避免完全无痕迹
|
|
|
|
|
+ try {
|
|
|
|
|
+ unset($log['id']);
|
|
|
|
|
+ db('工单_成品入仓同步操作日志')->insert($log);
|
|
|
|
|
+ } catch (\Exception $e2) {
|
|
|
|
|
+ Log::write(
|
|
|
|
|
+ '成品入仓同步日志写入失败: ' . $e2->getMessage()
|
|
|
|
|
+ . ' | status=' . $status
|
|
|
|
|
+ . ' | UniqId=' . $uniqId
|
|
|
|
|
+ . ' | msg=' . $msg,
|
|
|
|
|
+ 'error'
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|