qiuenguang 1 неделя назад
Родитель
Сommit
d06960690f

+ 0 - 17
application/api/controller/EmployeeDailySalary.php

@@ -206,7 +206,6 @@ class EmployeeDailySalary extends Api
         $arr1 = db()->table($arr1 . ' t')
             ->group('t.bh, LEFT(t.sczl_rq, 7)')
             ->column('t.bh,count(t.sczl_rq) as num');
-// halt($arr1);
         //查询该员工当月停机工时
         $res1=db('绩效工资汇总')
             ->join('人事_基本资料','人事_基本资料.员工编号=绩效工资汇总.bh')
@@ -226,7 +225,6 @@ class EmployeeDailySalary extends Api
             ->where(['r.所在部门'=>['like',$req['department'].'%']])
             ->group('j.bh,j.sczl_rq,j.sczl_gdbh,j.千件工价')
             ->select();
-            // halt($arr2);
         //计算每条记录的计时补差
         $brr = [];
         foreach($arr2 as $v){
@@ -372,7 +370,6 @@ class EmployeeDailySalary extends Api
             ->group('绩效工资汇总.bh,LEFT(sczl_rq, 10)')
             ->where('sys_ny',$req['date'])
             ->where('bh',$req['search'])
-            // ->where(['人事_基本资料.所在部门'=>['like',$req['department'].'%']])
             ->column('CONCAT(bh, "-", DATE_FORMAT(sczl_rq,"%Y/%m/%d")) AS rq_bh,bh,rtrim(xm) as 员工姓名,DATE_FORMAT(sczl_rq,"%Y/%m/%d") as sczl_rq,sum(个人计件工资) as 计件工资,
             sum(个人加班工资) as 加班工资,sum(异常停机工时) as 计时时数');
 
@@ -481,11 +478,9 @@ class EmployeeDailySalary extends Api
             ->where('j.sys_ny',$req['date'])
              ->where('j.达标定额','<>',0)
             ->where('j.bh',$req['search'])
-            // ->where(['r.所在部门'=>['like',$req['department'].'%']])
             ->buildSql();
         $arr1 = db()->table($arr1 . ' t')
             ->group('t.bh, LEFT(t.sczl_rq, 7)')
-//            ->column('t.bh,t.法定天数 as num');
             ->column('t.bh,count(t.sczl_rq) as num');
 
         //查询该员工当月停机工时
@@ -494,7 +489,6 @@ class EmployeeDailySalary extends Api
             ->group('绩效工资汇总.bh,LEFT(sczl_rq, 7)')
             ->where('sys_ny',$req['date'])
             ->where('bh',$req['search'])
-            // ->where(['人事_基本资料.所在部门'=>['like',$req['department'].'%']])
             ->column('bh,rtrim(xm) as 员工姓名,DATE_FORMAT(sczl_rq,"%Y/%m/%d") as sczl_rq,
             sum(异常停机工时) as 计时时数');
 
@@ -504,21 +498,10 @@ class EmployeeDailySalary extends Api
             sum(j.达标定额) as 达标定额, j.Rate, j.千件工价, sum(j.异常停机工时) as 异常停机工时')
             ->join('人事_基本资料 r','r.员工编号=j.bh')
             ->where('j.sys_ny',$req['date'])
-            // ->where('j.达标定额','<>',0)
             ->where('j.bh',$req['search'])
-            // ->where(['r.所在部门'=>['like',$req['department'].'%']])
             ->group('j.bh,j.sczl_rq,j.sczl_gdbh,j.千件工价')
             ->select();
 
-       //查询每月有达标定额的天数
-//        $arr3 = db('绩效工资汇总')->alias('j')
-//            ->join('人事_基本资料 r','r.员工编号=j.bh')
-//            ->where('j.sys_ny',$req['date'])
-//             ->where('j.达标定额','<>',0)
-//            ->where('j.bh',$req['search'])
-//            ->group('j.bh,j.sczl_rq')
-//            ->count();
-
         //计算每条记录的计时补差
         $brr = [];
         foreach($arr2 as $v){

+ 341 - 46
application/api/controller/Product.php

@@ -280,7 +280,12 @@ class Product extends Api
         if (!isset($params) || !isset($params[0]['UniqID'])){
             $this->error('参数不能为空');
         }
+        $operator = $this->getProductLogOperator($params);
+        if ($operator === '' && isset($params[0])) {
+            $operator = $this->getProductLogOperator($params[0]);
+        }
         $i = 0;
+        $productLogs = [];
         foreach ($params as $key=>$value){
             $data = [];
             if (!empty($value['A_power'])){
@@ -307,12 +312,13 @@ class Product extends Api
             if (!empty($value['Gy0_site'])){
                 $data['Gy0_site'] = $value['Gy0_site'];
             }
+            $oldGy = \db('产品_工艺资料')->where('UniqID', $value['UniqID'])->find();
             // 如果修改了设备编号 → 同步未开工工单_工艺资料
             $newSbbh = isset($value['machine']) ? trim((string)$value['machine']) : '';
             $syncSql = '';
             if ($newSbbh !== '') {
                 // 取产品_工艺资料的 Gy0_cpdh / Gy0_yjno / Gy0_gxh
-                $gyInfo = \db('产品_工艺资料')->where('UniqID', $value['UniqID'])->field('Gy0_cpdh, Gy0_yjno, Gy0_gxh')->find();
+                $gyInfo = $oldGy;
                 if (!empty($gyInfo)) {
                     $cpdh = trim($gyInfo['Gy0_cpdh']);
                     $yjno = trim($gyInfo['Gy0_yjno']);
@@ -334,12 +340,37 @@ class Product extends Api
             $res = Db::query($sql);
             if ($res !== false){
                 $i++;
+                if (!empty($oldGy) && !empty($data)) {
+                    $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, [
+                        '产品编号' => isset($oldGy['Gy0_cpdh']) ? trim($oldGy['Gy0_cpdh']) : '',
+                        '印件号' => isset($oldGy['Gy0_yjno']) ? $oldGy['Gy0_yjno'] : '',
+                        '工序号' => isset($oldGy['Gy0_gxh']) ? $oldGy['Gy0_gxh'] : '',
+                        '方案' => isset($oldGy['Gy0_方案']) ? $oldGy['Gy0_方案'] : '',
+                        '业务表' => '产品_工艺资料',
+                        'RecordUniqid' => $value['UniqID'],
+                        'sys_id' => $operator,
+                    ]));
+                }
             }
             // 执行级联同步(不影响主更新的成功判定)
             if ($syncSql !== '') {
                 Db::execute($syncSql);
+                $productLogs[] = [
+                    '产品编号' => !empty($oldGy['Gy0_cpdh']) ? trim($oldGy['Gy0_cpdh']) : '',
+                    '印件号' => isset($oldGy['Gy0_yjno']) ? $oldGy['Gy0_yjno'] : '',
+                    '工序号' => isset($oldGy['Gy0_gxh']) ? $oldGy['Gy0_gxh'] : '',
+                    '方案' => isset($oldGy['Gy0_方案']) ? $oldGy['Gy0_方案'] : '',
+                    '业务表' => '工单_工艺资料',
+                    'RecordUniqid' => $value['UniqID'],
+                    '操作类型' => '修改',
+                    '字段名' => 'Gy0_sbbh',
+                    '修改后' => $newSbbh,
+                    'sys_id' => $operator,
+                    '备注' => '同步未开工工单机台',
+                ];
             }
         }
+        $this->saveProductOperateLog($productLogs);
         if ($i !== 0){
             $this->success('更新成功');
         }else{
@@ -366,9 +397,21 @@ class Product extends Api
         }
         $code = $params['印品代号'];
         unset($params['印品代号']);
+        $operator = $this->getProductLogOperator($params);
+        unset($params['sys_id']);
+        $oldProduct = db('产品_基本资料')->where('产品编号',$code)->find();
         $sql = db('产品_基本资料')->where('产品编号',$code)->fetchSql(true)->update($params);
         $res = Db::query($sql);
         if ($res !== false){
+            if (!empty($oldProduct)) {
+                $this->saveProductOperateLog($this->buildProductFieldChangeLogs($oldProduct, $params, [
+                    '产品编号' => $code,
+                    '产品名称' => isset($oldProduct['产品名称']) ? $oldProduct['产品名称'] : '',
+                    '业务表' => '产品_基本资料',
+                    'RecordUniqid' => isset($oldProduct['UniqID']) ? $oldProduct['UniqID'] : '',
+                    'sys_id' => $operator,
+                ]));
+            }
             $this->success('更新成功');
         }else{
             $this->error('更新失败');
@@ -394,9 +437,22 @@ class Product extends Api
         if ($params['status'] == 2){
             $status = '停产';
         }
+        $operator = $this->getProductLogOperator($params);
+        $oldProduct = db('产品_基本资料')->where('产品编号',$code)->field('UniqID,产品名称,状态')->find();
         $sql = db('产品_基本资料')->where('产品编号',$code)->fetchSql(true)->setField('状态',$status);
         $res = Db::query($sql);
         if ($res !== false){
+            $this->saveProductOperateLog([[
+                '产品编号' => $code,
+                '产品名称' => !empty($oldProduct['产品名称']) ? $oldProduct['产品名称'] : '',
+                '业务表' => '产品_基本资料',
+                'RecordUniqid' => !empty($oldProduct['UniqID']) ? $oldProduct['UniqID'] : '',
+                '操作类型' => '停产',
+                '字段名' => '状态',
+                '修改前' => !empty($oldProduct['状态']) ? $oldProduct['状态'] : '',
+                '修改后' => $status,
+                'sys_id' => $operator,
+            ]]);
             $this->success('更新成功');
         }else{
             $this->error('更新失败');
@@ -556,6 +612,14 @@ class Product extends Api
                 }
 
                 db()->commit();
+                $this->saveProductOperateLog([[
+                    '产品编号' => $params['to_code'],
+                    '方案' => isset($params['to_pro']) ? $params['to_pro'] : '',
+                    '业务表' => $strategy['table'],
+                    '操作类型' => '复制',
+                    'sys_id' => $sys_id,
+                    '备注' => '从'.$params['from_code'].'复制'.count($processedData).'条',
+                ]]);
             } catch (\Exception $e) {
                 db()->rollback();
                 $this->error("复制{$strategy['table']}数据失败: " . $e->getMessage());
@@ -588,6 +652,17 @@ class Product extends Api
         $sql = db('产品_工艺资料')->where($where)->fetchSql(true)->setField('Gy0_方案',$params['new_name']);
         $res = Db::query($sql);
         if ($res !== false){
+            $this->saveProductOperateLog([[
+                '产品编号' => $params['code'],
+                '方案' => $params['new_name'],
+                '业务表' => '产品_工艺资料',
+                '操作类型' => '修改',
+                '字段名' => 'Gy0_方案',
+                '修改前' => $params['old_name'],
+                '修改后' => $params['new_name'],
+                'sys_id' => $this->getProductLogOperator($params),
+                '备注' => '工艺方案更名',
+            ]]);
             $this->success('更新成功');
         }else{
             $this->error('更新失败');
@@ -607,7 +682,9 @@ class Product extends Api
         if (empty($params) || !isset($params[0]['UniqID'])){
             $this->error('参数不能为空');
         }
+        $operator = $this->pickProductLogOperator($params);
         $i = 0;
+        $productLogs = [];
         foreach ($params as $key=>$value){
             $data = [];
             if (!empty($value['Gy0_yjno'])){
@@ -622,12 +699,17 @@ class Product extends Api
             if (!empty($value['Gy0_ls'])){
                 $data['Gy0_ls'] = $value['Gy0_ls'];
             }
+            $oldGy = \db('产品_工艺资料')->where('UniqID',$value['UniqID'])->find();
             $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
             $res = Db::query($sql);
             if ($res !== false){
                 $i++;
+                if (!empty($oldGy)) {
+                    $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, $this->gyLogBase($oldGy, $value['UniqID'], $operator)));
+                }
             }
         }
+        $this->saveProductOperateLog($productLogs);
         if ($i !== 0){
             $this->success('更新成功');
         }else{
@@ -648,7 +730,9 @@ class Product extends Api
         if (empty($params) || !isset($params[0]['UniqID'])){
             $this->error('参数不能为空');
         }
+        $operator = $this->pickProductLogOperator($params);
         $i = 0;
+        $productLogs = [];
         foreach ($params as $key=>$value){
             $data = [];
             if (!empty($value['Gy0_Ms'])){
@@ -663,12 +747,17 @@ class Product extends Api
             $data['Add_gxmc'] = $value['Add_gxmc'];
             $data['工序备注'] = $value['remark'];
 
+            $oldGy = \db('产品_工艺资料')->where('UniqID',$value['UniqID'])->find();
             $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
             $res = Db::query($sql);
             if ($res !== false){
                 $i++;
+                if (!empty($oldGy)) {
+                    $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, $this->gyLogBase($oldGy, $value['UniqID'], $operator)));
+                }
             }
         }
+        $this->saveProductOperateLog($productLogs);
         if ($i !== 0){
             $this->success('更新成功');
         }else{
@@ -689,7 +778,9 @@ class Product extends Api
         if (empty($params) || !isset($params[0]['UniqID'])){
             $this->error('参数不能为空');
         }
+        $operator = $this->pickProductLogOperator($params);
         $i = 0;
+        $productLogs = [];
         foreach ($params as $key=>$value){
             $data = [];
             if (!empty($value['artificial_zp'])){
@@ -710,12 +801,17 @@ class Product extends Api
             if (!empty($value['machine_fj'])){
                 $data['机检_废检'] = $value['machine_fj'];
             }
+            $oldGy = \db('产品_工艺资料')->where('UniqID',$value['UniqID'])->find();
             $sql = db('产品_工艺资料')->where('UniqID',$value['UniqID'])->fetchSql(true)->update($data);
             $res = Db::query($sql);
             if ($res !== false){
                 $i++;
+                if (!empty($oldGy)) {
+                    $productLogs = array_merge($productLogs, $this->buildProductFieldChangeLogs($oldGy, $data, $this->gyLogBase($oldGy, $value['UniqID'], $operator)));
+                }
             }
         }
+        $this->saveProductOperateLog($productLogs);
         if ($i !== 0){
             $this->success('更新成功');
         }else{
@@ -755,9 +851,21 @@ class Product extends Api
             $this->error('参数不能为空');
         }
         $UniqId = $params['UniqId'];
+        $operator = $this->getProductLogOperator($params);
         unset($params['UniqId']);
+        $oldYj = \db('产品_印件资料')->where('UniqId',$UniqId)->find();
         $res = \db('产品_印件资料')->where('UniqId',$UniqId)->update($params);
         if ($res !== false){
+            if (!empty($oldYj)) {
+                $this->saveProductOperateLog($this->buildProductFieldChangeLogs($oldYj, $params, [
+                    '产品编号' => isset($oldYj['yj_cpdh']) ? $oldYj['yj_cpdh'] : '',
+                    '产品名称' => isset($oldYj['yj_yjmc']) ? $oldYj['yj_yjmc'] : '',
+                    '印件号' => isset($params['yj_yjno']) ? $params['yj_yjno'] : (isset($oldYj['yj_yjno']) ? $oldYj['yj_yjno'] : ''),
+                    '业务表' => '产品_印件资料',
+                    'RecordUniqid' => $UniqId,
+                    'sys_id' => $operator,
+                ]));
+            }
             $this->success('更新成功');
         }else{
             $this->error('更新失败');
@@ -781,8 +889,18 @@ class Product extends Api
         }
         $params['UniqId'] = $UniqId;
         $params['sys_rq'] = date('Y-m-d H:i:s');
+        $operator = $this->getProductLogOperator($params);
         $res = \db('产品_印件资料')->insert($params);
         if ($res !== false){
+            $this->saveProductOperateLog([[
+                '产品编号' => isset($params['yj_cpdh']) ? $params['yj_cpdh'] : '',
+                '产品名称' => isset($params['yj_yjmc']) ? $params['yj_yjmc'] : '',
+                '印件号' => isset($params['yj_yjno']) ? $params['yj_yjno'] : '',
+                '业务表' => '产品_印件资料',
+                'RecordUniqid' => $UniqId,
+                '操作类型' => '新增',
+                'sys_id' => $operator,
+            ]]);
             $this->success('新增成功');
         }else{
             $this->error('新增失败');
@@ -1009,9 +1127,20 @@ class Product extends Api
         }
         $params['UniqID'] = $UniqId;
         $params['Sys_rq'] = date('Y-m-d H:i:s');
+        $operator = $this->getProductLogOperator($params);
         $sql = \db('产品_工艺资料')->fetchSql(true)->insert($params);
         $res = Db::query($sql);
         if ($res !== false){
+            $this->saveProductOperateLog([[
+                '产品编号' => isset($params['Gy0_cpdh']) ? $params['Gy0_cpdh'] : '',
+                '印件号' => isset($params['Gy0_yjno']) ? $params['Gy0_yjno'] : '',
+                '工序号' => isset($params['Gy0_gxh']) ? $params['Gy0_gxh'] : '',
+                '方案' => isset($params['Gy0_方案']) ? $params['Gy0_方案'] : '',
+                '业务表' => '产品_工艺资料',
+                'RecordUniqid' => $UniqId,
+                '操作类型' => '新增',
+                'sys_id' => $operator,
+            ]]);
             $this->success('新增成功');
         }else{
             $this->error('新增失败');
@@ -1073,10 +1202,23 @@ class Product extends Api
             $this->error('参数不能为空');
         }
         $UniqId = $params['UniqId'];
+        $operator = $this->getProductLogOperator($params);
         unset($params['UniqId']);
+        $oldYb = \db('产品_印版资料')->where('UniqId',$UniqId)->find();
         $sql = \db('产品_印版资料')->where('UniqId',$UniqId)->fetchSql(true)->update($params);
         $res = Db::query($sql);
         if ($res !== false){
+            if (!empty($oldYb)) {
+                $this->saveProductOperateLog($this->buildProductFieldChangeLogs($oldYb, $params, [
+                    '产品编号' => isset($oldYb['YB_Cpdh']) ? $oldYb['YB_Cpdh'] : '',
+                    '印件号' => isset($oldYb['YB_Yjno']) ? $oldYb['YB_Yjno'] : '',
+                    '工序号' => isset($oldYb['YB_gxh']) ? $oldYb['YB_gxh'] : '',
+                    '方案' => isset($oldYb['YB_方案']) ? $oldYb['YB_方案'] : '',
+                    '业务表' => '产品_印版资料',
+                    'RecordUniqid' => $UniqId,
+                    'sys_id' => $operator,
+                ]));
+            }
             $this->success('更新成功');
         }else{
             $this->error('更新失败');
@@ -1167,9 +1309,20 @@ class Product extends Api
         $params['UniqID'] = $UniqId;
         $params['考核印数'] = '0.00';
         $params['Sys_rq'] = date('Y-m-d H:i:s');
+        $operator = $this->getProductLogOperator($params);
         $sql = \db('产品_印版资料')->fetchSql(true)->insert($params);
         $res = Db::query($sql);
         if ($res !== false){
+            $this->saveProductOperateLog([[
+                '产品编号' => isset($params['YB_Cpdh']) ? $params['YB_Cpdh'] : '',
+                '印件号' => isset($params['YB_Yjno']) ? $params['YB_Yjno'] : '',
+                '工序号' => isset($params['YB_gxh']) ? $params['YB_gxh'] : '',
+                '方案' => isset($params['YB_方案']) ? $params['YB_方案'] : '',
+                '业务表' => '产品_印版资料',
+                'RecordUniqid' => $UniqId,
+                '操作类型' => '新增',
+                'sys_id' => $operator,
+            ]]);
             $this->success('新增成功');
         }else{
             $this->error('新增失败');
@@ -1234,28 +1387,27 @@ class Product extends Api
         // 5. 查询要删除的工艺详情
         $processDetails = Db::name('产品_工艺资料')
             ->where('UniqID', 'in', $uniqIds)
-            ->field('UniqID, Gy0_cpdh, Gy0_yjno, Gy0_gxh')
+            ->field('UniqID, Gy0_cpdh, Gy0_yjno, Gy0_gxh, Gy0_方案')
             ->select();
 
         if (empty($processDetails)) {
             $this->error('未找到要删除的工艺详情');
         }
 
-        // 6. 准备日志记录
         $logArray = [];
-        $currentTime = date('Y-m-d H:i:s');
-
         foreach ($processDetails as $item) {
             $logArray[] = [
-                'Gd_gdbh'      => $item['Gy0_cpdh'] ?? '',
-                'yjno'         => $item['Gy0_yjno'] ?? '',
-                'gxh'          => $item['Gy0_gxh'] ?? '',
-                'ModifyUser'   => $userId,
-                'ModifyTime'   => $currentTime,
-                'FieldName'    => '产品工艺删除',
-                'OldValue'     => '存在', // 记录删除前的状态
-                'NewValue'     => '已删除',
-                'ModifySource' => '产品工艺删除'
+                '产品编号' => $item['Gy0_cpdh'] ?? '',
+                '印件号' => $item['Gy0_yjno'] ?? '',
+                '工序号' => $item['Gy0_gxh'] ?? '',
+                '方案' => $item['Gy0_方案'] ?? '',
+                '业务表' => '产品_工艺资料',
+                'RecordUniqid' => $item['UniqID'] ?? '',
+                '操作类型' => '删除',
+                '修改前' => '存在',
+                '修改后' => '已删除',
+                'sys_id' => $userId,
+                '备注' => '产品工艺删除',
             ];
         }
 
@@ -1268,14 +1420,7 @@ class Product extends Api
             $this->error('删除工艺详情失败');
         }
 
-        // 8. 记录操作日志(即使日志记录失败,也不影响主操作)
-        if (!empty($logArray)) {
-            $logResult = Db::name('系统操作日志表')->insertAll($logArray);
-            if ($logResult === false) {
-                // 记录日志失败,但不影响主操作
-                \think\Log::write('记录操作日志失败: ' . Db::name('系统操作日志表')->getLastSql());
-            }
-        }
+        $this->saveProductOperateLog($logArray);
 
         // 9. 返回成功
         $this->success('成功删除 ' . $deleteResult . ' 条工艺详情');
@@ -1296,8 +1441,23 @@ class Product extends Api
             $this->error('参数错误');
         }
         $printId = explode(',',$param['UniqId']);
+        $printRows = \db('产品_印件资料')->where('UniqID','in',$printId)->select();
         $res = \db('产品_印件资料')->where('UniqID','in',$printId)->delete();
         if ($res){
+            $logs = [];
+            foreach ($printRows as $row) {
+                $logs[] = [
+                    '产品编号' => isset($row['yj_cpdh']) ? $row['yj_cpdh'] : '',
+                    '产品名称' => isset($row['yj_yjmc']) ? $row['yj_yjmc'] : '',
+                    '印件号' => isset($row['yj_yjno']) ? $row['yj_yjno'] : '',
+                    '业务表' => '产品_印件资料',
+                    'RecordUniqid' => isset($row['UniqId']) ? $row['UniqId'] : (isset($row['UniqID']) ? $row['UniqID'] : ''),
+                    '操作类型' => '删除',
+                    '修改前' => isset($row['yj_yjmc']) ? trim((string)$row['yj_yjmc']) : '',
+                    'sys_id' => $this->getProductLogOperator($param),
+                ];
+            }
+            $this->saveProductOperateLog($logs);
             $this->success('删除成功');
         }else{
             $this->error('删除失败');
@@ -1324,9 +1484,18 @@ class Product extends Api
            $params['UniqID'] = $uniqId + 1;
        }
        $params['Sys_rq'] = date('Y-m-d H:i:s');
+       $operator = $this->getProductLogOperator($params);
        $sql = \db('产品_基本资料')->fetchSql(true)->insert($params);
        $res = Db::query($sql);
        if ($res !== false){
+           $this->saveProductOperateLog([[
+               '产品编号' => isset($params['产品编号']) ? $params['产品编号'] : '',
+               '产品名称' => isset($params['产品名称']) ? $params['产品名称'] : '',
+               '业务表' => '产品_基本资料',
+               'RecordUniqid' => $params['UniqID'],
+               '操作类型' => '新增',
+               'sys_id' => $operator,
+           ]]);
            $this->success('新增成功');
        }else{
            $this->error('新增失败');
@@ -1348,13 +1517,15 @@ class Product extends Api
             $this->error('参数错误');
         }
         $WorkOrderId = explode(',',$param['UniqId']);
+        $operator = $this->getProductLogOperator($param);
         $i = 0;
+        $logs = [];
         foreach ($WorkOrderId as $key=>$value){
-            //获取产品编号
-            $product = \db('产品_基本资料')
+            $productRow = \db('产品_基本资料')
                 ->where('UniqID',$value)
-                ->value('rtrim(产品编号)');
-            //删除工单资料、工艺资料、印件资料
+                ->field('rtrim(产品编号) as 产品编号,rtrim(产品名称) as 产品名称')
+                ->find();
+            $product = empty($productRow['产品编号']) ? '' : $productRow['产品编号'];
             \db()->startTrans();
             try {
                 \db('产品_基本资料')->where('UniqID',$value)->delete();
@@ -1365,11 +1536,21 @@ class Product extends Api
                     \db('产品_印件资料')->where('yj_cpdh',$product)->delete();
                 }
                 \db()->commit();
+                $logs[] = [
+                    '产品编号' => $product,
+                    '产品名称' => empty($productRow['产品名称']) ? '' : $productRow['产品名称'],
+                    '业务表' => '产品_基本资料',
+                    'RecordUniqid' => $value,
+                    '操作类型' => '删除',
+                    'sys_id' => $operator,
+                    '备注' => '同时删除该产品工艺资料和印件资料',
+                ];
             }catch (\Exception $e){
                 \db()->rollback();
                 $i++;
             }
         }
+        $this->saveProductOperateLog($logs);
         if ($i === 0){
             $this->success('删除成功');
         }else{
@@ -1434,15 +1615,18 @@ class Product extends Api
                     $updateData[$key] = $newValue;
 
                     $logArray[] = [
-                        'Gd_gdbh'      => $processDetail['Gy0_cpdh'] ?? '',
-                        'yjno'         => $processDetail['Gy0_yjno'] ?? '',
-                        'gxh'          => $processDetail['Gy0_gxh'] ?? '',
-                        'ModifyUser'   => $modifyUser,
-                        'ModifyTime'   => $currentTime,
-                        'FieldName'    => $key,
-                        'OldValue'     => $oldValue,
-                        'NewValue'     => $newValue,
-                        'ModifySource' => '产品工艺调整'
+                        '产品编号' => $processDetail['Gy0_cpdh'] ?? '',
+                        '印件号' => $processDetail['Gy0_yjno'] ?? '',
+                        '工序号' => $processDetail['Gy0_gxh'] ?? '',
+                        '方案' => $processDetail['Gy0_方案'] ?? '',
+                        '业务表' => '产品_工艺资料',
+                        'RecordUniqid' => $uniqId,
+                        '操作类型' => '修改',
+                        '字段名' => $key,
+                        '修改前' => $oldValue,
+                        '修改后' => $newValue,
+                        'sys_id' => $modifyUser,
+                        '备注' => '产品工艺调整',
                     ];
                 }
             }
@@ -1470,16 +1654,7 @@ class Product extends Api
             $this->error('更新产品工艺资料失败');
         }
 
-        // 11. 记录操作日志(即使日志记录失败,也不影响主操作)
-        if (!empty($logArray)) {
-            $logResult = Db::name('系统操作日志表')
-                ->insertAll($logArray);
-
-            if ($logResult === false) {
-                // 记录日志失败,但不影响主操作
-                \think\Log::write('记录操作日志失败: ' . Db::name('系统操作日志表')->getLastSql());
-            }
-        }
+        $this->saveProductOperateLog($logArray);
 
         // 12. 返回成功
         $this->success('修改成功');
@@ -1545,6 +1720,8 @@ class Product extends Api
             ->where($where)
             ->order('Gy0_gxh,Sys_rq desc,Mod_rq desc')
             ->column('Gy0_gxh', 'UniqID');
+        $operator = $this->getProductLogOperator($param);
+        $logs = [];
         // 更新排序字段
         $i = 1;
         foreach ($list as $key => $value) {
@@ -1554,13 +1731,131 @@ class Product extends Api
                     ->update(['Gy0_gxh' => $i]);
 
                 if ($result !== false) {
+                    if ((string)$value !== (string)$i) {
+                        $logs[] = [
+                            '产品编号' => $param['cpdh'],
+                            '印件号' => $param['yjno'],
+                            '工序号' => $i,
+                            '业务表' => '产品_工艺资料',
+                            'RecordUniqid' => $key,
+                            '操作类型' => '排序',
+                            '字段名' => 'Gy0_gxh',
+                            '修改前' => $value,
+                            '修改后' => $i,
+                            'sys_id' => $operator,
+                        ];
+                    }
                     $i++;
                 } else {
-                    // 记录失败日志
                     \think\facade\Log::error("更新排序失败,UniqID: {$key}");
                 }
             }
         }
+        $this->saveProductOperateLog($logs);
         $this->success('成功');
     }
+
+    /**
+     * 批量参数里取操作人,列表请求可能把 sys_id 放在首条或外层
+     */
+    protected function pickProductLogOperator($params)
+    {
+        $operator = $this->getProductLogOperator($params);
+        if ($operator === '' && isset($params[0]) && is_array($params[0])) {
+            $operator = $this->getProductLogOperator($params[0]);
+        }
+        return $operator;
+    }
+
+    /**
+     * 读取操作人,兼容 sys_id / Sys_id
+     */
+    protected function getProductLogOperator($params)
+    {
+        if (!is_array($params)) {
+            return '';
+        }
+        if (isset($params['sys_id']) && $params['sys_id'] !== '') {
+            return trim((string)$params['sys_id']);
+        }
+        if (isset($params['Sys_id']) && $params['Sys_id'] !== '') {
+            return trim((string)$params['Sys_id']);
+        }
+        return '';
+    }
+
+    /**
+     * 工艺资料日志的公共字段
+     */
+    protected function gyLogBase(array $old, $uniqId, $operator)
+    {
+        return [
+            '产品编号' => isset($old['Gy0_cpdh']) ? trim((string)$old['Gy0_cpdh']) : '',
+            '印件号' => isset($old['Gy0_yjno']) ? $old['Gy0_yjno'] : '',
+            '工序号' => isset($old['Gy0_gxh']) ? $old['Gy0_gxh'] : '',
+            '方案' => isset($old['Gy0_方案']) ? $old['Gy0_方案'] : '',
+            '业务表' => '产品_工艺资料',
+            'RecordUniqid' => $uniqId,
+            'sys_id' => $operator,
+        ];
+    }
+
+    /**
+     * 按字段对比生成修改日志
+     */
+    protected function buildProductFieldChangeLogs(array $old, array $new, array $base)
+    {
+        $skip = ['sys_id', 'Sys_id', 'sys_rq', 'Sys_rq', 'mod_rq', 'Mod_rq', 'UniqId', 'UniqID'];
+        $logs = [];
+        foreach ($new as $field => $newValue) {
+            if (in_array($field, $skip, true) || !array_key_exists($field, $old)) {
+                continue;
+            }
+            if (trim((string)$old[$field]) === trim((string)$newValue)) {
+                continue;
+            }
+            $logs[] = array_merge($base, [
+                '操作类型' => isset($base['操作类型']) ? $base['操作类型'] : '修改',
+                '字段名' => $field,
+                '修改前' => trim((string)$old[$field]),
+                '修改后' => trim((string)$newValue),
+            ]);
+        }
+        return $logs;
+    }
+
+    /**
+     * 写入产品操作日志,失败不影响主操作
+     */
+    protected function saveProductOperateLog(array $rows)
+    {
+        if (empty($rows)) {
+            return;
+        }
+        $now = date('Y-m-d H:i:s');
+        $logs = [];
+        foreach ($rows as $row) {
+            $logs[] = [
+                '产品编号' => isset($row['产品编号']) ? $row['产品编号'] : '',
+                '产品名称' => isset($row['产品名称']) ? $row['产品名称'] : '',
+                '印件号' => isset($row['印件号']) ? $row['印件号'] : '',
+                '工序号' => isset($row['工序号']) ? $row['工序号'] : '',
+                '方案' => isset($row['方案']) ? $row['方案'] : '',
+                '业务表' => isset($row['业务表']) ? $row['业务表'] : '',
+                'RecordUniqid' => isset($row['RecordUniqid']) ? $row['RecordUniqid'] : '',
+                '操作类型' => isset($row['操作类型']) ? $row['操作类型'] : '',
+                '字段名' => isset($row['字段名']) ? $row['字段名'] : '',
+                '修改前' => isset($row['修改前']) ? $row['修改前'] : '',
+                '修改后' => isset($row['修改后']) ? $row['修改后'] : '',
+                'sys_id' => isset($row['sys_id']) ? $row['sys_id'] : '',
+                'sys_rq' => $now,
+                '备注' => isset($row['备注']) ? $row['备注'] : '',
+            ];
+        }
+        try {
+            \db('产品_操作日志')->insertAll($logs);
+        } catch (\Exception $e) {
+            \think\Log::write('记录产品操作日志失败: ' . $e->getMessage());
+        }
+    }
 }

+ 37 - 7
application/api/controller/Synchronization.php

@@ -132,9 +132,7 @@ class Synchronization extends Api
                         $db3->execute($sqlString);
                     }
                 } else {
-                    unset($data['Uniqid']);
-                    unset($data['行号']);
-                    $updateSql = \db('工单_基本资料')->where('U8UID', $value['U8_UID'])->fetchSql(true)->update($data);
+                    $updateSql = \db('工单_基本资料')->where('U8UID', $value['U8_UID'])->fetchSql(true)->update($this->workOrderResyncData($data));
                     $updateRes = \db()->query($updateSql);
                     if ($updateRes === false) {
                         $j++;
@@ -222,7 +220,7 @@ class Synchronization extends Api
                     $db3->execute($sqlString);
                 }
             } else {
-                unset($data['UNIQID']);
+                unset($data['UNIQID'], $data['BOM_工单行号']);
                 $updateSql = \db('工单_bom资料')
                     ->where('U8UID', $value['U8_UID'])
                     ->fetchSql(true)
@@ -1328,8 +1326,7 @@ class Synchronization extends Api
                     $db7->execute($sqlString);
                 }
             } else {
-                unset($data['行号']);
-                $updateSql = \db('工单_基本资料')->where('U8UID', $value['U8_UID'])->fetchSql(true)->update($data);
+                $updateSql = \db('工单_基本资料')->where('U8UID', $value['U8_UID'])->fetchSql(true)->update($this->workOrderResyncData($data));
                 $updateRes = \db()->query($updateSql);
                 if ($updateRes === false) {
                     $j++;
@@ -1417,7 +1414,7 @@ class Synchronization extends Api
                     $db7->execute($sqlString);
                 }
             } else {
-                unset($data['UNIQID']);
+                unset($data['UNIQID'], $data['BOM_工单行号']);
                 $updateSql = \db('工单_bom资料')
                     ->where('U8UID', $value['U8_UID'])
                     ->fetchSql(true)
@@ -2338,4 +2335,37 @@ class Synchronization extends Api
             throw new \Exception('中间表 insert 返回 false');
         }
     }
+
+    /**
+     * 工单二次同步只刷新 ERP 主导字段,保留 MES 已改的行号、备注、状态和建档信息
+     * @param array $data
+     * @return array
+     */
+    protected function workOrderResyncData(array $data)
+    {
+        $fields = [
+            '销售订单号',
+            'Gd_客户代号',
+            'Gd_客户名称',
+            'Gd_khdh',
+            'Gd_khmc',
+            '客户料号',
+            '成品代号',
+            '成品名称',
+            '订单数量',
+            '实际投料',
+            '产品单价',
+            '接单日期',
+            '开单日期',
+            '交货日期',
+            'Mod_rq',
+        ];
+        $update = [];
+        foreach ($fields as $field) {
+            if (array_key_exists($field, $data)) {
+                $update[$field] = $data[$field];
+            }
+        }
+        return $update;
+    }
 }

+ 70 - 36
application/api/controller/WorkOrder.php

@@ -501,8 +501,7 @@ class WorkOrder extends Api
                     'ModifySource' => '工单编辑'
                 ];
             }
-            $logSql = \db('系统操作日志表')->fetchSql(true)->insertAll($logData);
-            $result = \db()->query($logSql);
+            $this->saveWorkOrderOperateLog($logData);
         }
         //修改订单数量该工单下的订单数量
         //1. 查询该id下的工单编号, 防止其手动修改工单编号导致条件错误
@@ -516,16 +515,14 @@ class WorkOrder extends Api
         $res1 = Db::query($sql);
         // 记录订单数量单独变更
         if ($res1 && $oldQuantity != $row['订单数量']) {
-            $logSql = \db('系统操作日志表')->fetchSql(true)->insert([
+            $this->saveWorkOrderOperateLog([[
                 'Gd_gdbh'      => $gdbh,
                 'ModifyUser'   => $param['sys_id'],
-                'ModifyTime'   => date('Y-m-d H:i:s'),
                 'FieldName'    => '订单数量',
                 'OldValue'     => $oldQuantity,
                 'NewValue'     => $row['订单数量'],
                 'ModifySource' => '数量修正'
-            ]);
-            $result = \db()->query($logSql);
+            ]]);
         }
         if ($res !== false && $res1 !== false){
             $this->success('成功');
@@ -1233,6 +1230,7 @@ class WorkOrder extends Api
                         'Gd_gdbh'      => $oldData['Gy0_gdbh'],
                         'yjno'         => $oldData['Gy0_yjno'],
                         'gxh'          => $oldData['Gy0_gxh'],
+                        'gxmc'         => isset($oldData['Gy0_gxmc']) ? $oldData['Gy0_gxmc'] : '',
                         'ModifyUser'   => $param['Sys_id'],
                         'ModifyTime'   => date('Y-m-d H:i:s'),
                         'FieldName'    => $change['field'],
@@ -1242,7 +1240,7 @@ class WorkOrder extends Api
                     );
                 }
 
-                Db::name('系统操作日志表')->insertAll($logs);
+                $this->saveWorkOrderOperateLog($logs);
             }
 
             //获取工单第一道工艺编号
@@ -1265,7 +1263,7 @@ class WorkOrder extends Api
                     'NewValue'     => '2-生产中',
                     'ModifySource' => '工单状态变更'
                 ];
-                \db('系统操作日志表')->insert($logData);
+                $this->saveWorkOrderOperateLog([$logData]);
             }
             //重新分配工序计划产量
             if ((int)$list['计划接货数'] > 0){
@@ -1715,7 +1713,7 @@ class WorkOrder extends Api
             'ModifyTime'   => date('Y-m-d H:i:s'),
             'ModifySource' => '工单添加'
         );
-        \db('系统操作日志表')->insert($logs);
+        $this->saveWorkOrderOperateLog([$logs]);
         if ($res !== false){
             $this->success('成功');
         }else{
@@ -1948,11 +1946,12 @@ class WorkOrder extends Api
             'Gd_gdbh'      => $param['Gy0_gdbh'],
             'yjno'         => $param['Gy0_yjno'],
             'gxh'          => $param['Gy0_gxh'],
+            'gxmc'         => isset($param['Gy0_gxmc']) ? $param['Gy0_gxmc'] : '',
             'ModifyUser'   => $param['Sys_id'],
             'ModifyTime'   => date('Y-m-d H:i:s'),
             'ModifySource' => '工艺新增'
         );
-        \db('系统操作日志表')->insert($logs);
+        $this->saveWorkOrderOperateLog([$logs]);
         if ($res !== false){
             $this->success('成功');
         }else{
@@ -2081,6 +2080,7 @@ class WorkOrder extends Api
             ->column('rtrim(Gy0_gxh) as gxh', 'UniqId');
         //循环删除工艺资料
         $deletedGxh = [];
+        $deletedGxmc = [];
         foreach ($printId as $value){
             $process = \db('工单_工艺资料')
                 ->where('UniqId',$value)
@@ -2100,6 +2100,7 @@ class WorkOrder extends Api
                 // 记录成功删除的工序号
                 if (isset($gxhList[$value])) {
                     $deletedGxh[] = $gxhList[$value];
+                    $deletedGxmc[] = isset($process['Gy0_gxmc']) ? trim($process['Gy0_gxmc']) : '';
                 }
             } else {
                 $i++;
@@ -2110,14 +2111,15 @@ class WorkOrder extends Api
             $this->PlannedProcessYield($codeList['工单编号'],$codeList['印件号'],0,$number);
         }
         // 新增:写入操作日志
-        \db('系统操作日志表')->insert([
-            'ModifyUser'      => $param['sys_id'],
-            'ModifyTime'  => date('Y-m-d H:i:s',time()),
-            'Gd_gdbh'          => $codeList['工单编号'],
-            'yjno'          => $codeList['印件号'],
-            'gxh'           => implode(',', $deletedGxh),
-            'ModifySource'  => '工艺资料删除'
-        ]);
+        $this->saveWorkOrderOperateLog([[
+            'ModifyUser'   => $param['sys_id'],
+            'ModifyTime'   => date('Y-m-d H:i:s', time()),
+            'Gd_gdbh'      => $codeList['工单编号'],
+            'yjno'         => $codeList['印件号'],
+            'gxh'          => implode(',', $deletedGxh),
+            'gxmc'         => implode(',', $deletedGxmc),
+            'ModifySource' => '工艺资料删除'
+        ]]);
         if ($i === 0){
             $this->success('删除成功');
         }else{
@@ -2785,15 +2787,14 @@ class WorkOrder extends Api
                     \db()->commit();
 
                     // 成功删除后记录日志
-                    $logSql = \db('系统操作日志表')->fetchSql(true)->insert([
+                    $this->saveWorkOrderOperateLog([[
                         'Gd_gdbh'      => $workorder,
                         'ModifyUser'   => $param['sys_id'],
                         'ModifyTime'   => date('Y-m-d H:i:s'),
                         'FieldName'    => '工单删除',
                         'NewValue'     => '已删除',
                         'ModifySource' => '工单删除'
-                    ]);
-                    \db()->query($logSql);
+                    ]]);
                 }catch (\Exception $e){
                     \db()->rollback();
                     $i++;
@@ -3362,7 +3363,7 @@ class WorkOrder extends Api
         }
 
         if (!empty($logs)) {
-            \db('系统操作日志表')->insertAll($logs);
+            $this->saveWorkOrderOperateLog($logs);
         }
 
         $this->success('定时任务执行完成', [
@@ -3374,11 +3375,21 @@ class WorkOrder extends Api
     }
 
     /**
-     * 写入印件操作日志
+     * 印件操作日志写入工单_操作日志
      * @param array $rows
      * @return void
      */
     protected function savePrintOperateLog(array $rows)
+    {
+        $this->saveWorkOrderOperateLog($rows);
+    }
+
+    /**
+     * 写入工单_操作日志,兼容系统操作日志和印件操作日志两种字段
+     * @param array $rows
+     * @return void
+     */
+    protected function saveWorkOrderOperateLog(array $rows)
     {
         if (empty($rows)) {
             return;
@@ -3386,22 +3397,45 @@ class WorkOrder extends Api
         $now = date('Y-m-d H:i:s');
         $logs = [];
         foreach ($rows as $row) {
+            $sysRq = $now;
+            if (isset($row['sys_rq']) && $row['sys_rq'] !== '') {
+                $sysRq = $row['sys_rq'];
+            } elseif (isset($row['ModifyTime']) && $row['ModifyTime'] !== '') {
+                $sysRq = $row['ModifyTime'];
+            }
             $logs[] = [
-                'Yj_Gdbh'     => isset($row['Yj_Gdbh']) ? $row['Yj_Gdbh'] : '',
-                'yj_Yjno'     => isset($row['yj_Yjno']) ? $row['yj_Yjno'] : '',
-                'yj_Yjdh'     => isset($row['yj_Yjdh']) ? $row['yj_Yjdh'] : '',
-                'yj_yjmc'     => isset($row['yj_yjmc']) ? $row['yj_yjmc'] : '',
-                'PrintUniqid' => isset($row['PrintUniqid']) ? $row['PrintUniqid'] : '',
-                '操作类型'     => isset($row['操作类型']) ? $row['操作类型'] : '',
-                '字段名'       => isset($row['字段名']) ? $row['字段名'] : '',
-                '修改前'       => isset($row['修改前']) ? $row['修改前'] : '',
-                '修改后'       => isset($row['修改后']) ? $row['修改后'] : '',
-                'sys_id'      => isset($row['sys_id']) ? $row['sys_id'] : '',
-                'sys_rq'      => $now,
-                '备注'         => isset($row['备注']) ? $row['备注'] : '',
+                'Yj_Gdbh'     => $this->workOrderLogValue($row, ['Yj_Gdbh', 'Gd_gdbh']),
+                'yj_Yjno'     => $this->workOrderLogValue($row, ['yj_Yjno', 'yjno']),
+                '工序号'       => $this->workOrderLogValue($row, ['工序号', 'gxh']),
+                '工序名称'     => $this->workOrderLogValue($row, ['工序名称', 'gxmc']),
+                'yj_Yjdh'     => $this->workOrderLogValue($row, ['yj_Yjdh']),
+                'yj_yjmc'     => $this->workOrderLogValue($row, ['yj_yjmc']),
+                'PrintUniqid' => $this->workOrderLogValue($row, ['PrintUniqid']),
+                '操作类型'     => $this->workOrderLogValue($row, ['操作类型', 'ModifySource']),
+                '字段名'       => $this->workOrderLogValue($row, ['字段名', 'FieldName']),
+                '修改前'       => $this->workOrderLogValue($row, ['修改前', 'OldValue']),
+                '修改后'       => $this->workOrderLogValue($row, ['修改后', 'NewValue']),
+                'sys_id'      => $this->workOrderLogValue($row, ['sys_id', 'ModifyUser']),
+                'sys_rq'      => $sysRq,
+                '备注'         => $this->workOrderLogValue($row, ['备注']),
             ];
         }
-        \db('工单_印件操作日志')->insertAll($logs);
+        \db('工单_操作日志')->insertAll($logs);
+    }
+
+    /**
+     * @param array $row
+     * @param array $keys
+     * @return string
+     */
+    protected function workOrderLogValue(array $row, array $keys)
+    {
+        foreach ($keys as $key) {
+            if (array_key_exists($key, $row) && $row[$key] !== null && $row[$key] !== '') {
+                return is_scalar($row[$key]) ? (string)$row[$key] : '';
+            }
+        }
+        return '';
     }
 
     /**