Jelajahi Sumber

工单完工定时任务

unknown 1 Minggu lalu
induk
melakukan
12d4cd947a
1 mengubah file dengan 123 tambahan dan 0 penghapusan
  1. 123 0
      application/api/controller/WorkOrder.php

+ 123 - 0
application/api/controller/WorkOrder.php

@@ -3208,4 +3208,127 @@ class WorkOrder extends Api
         }
         $this->success('成功',['data'=>$data,'total'=>$total]);
     }
+
+
+
+    /**
+     * 成品入仓之后定时任务,更新工单状态(建议每天凌晨执行)
+     * 条件:已录入末板,且最后一条入仓时间超过1个月,工单未完工
+     * @return void
+     */
+    public function updateWorkOrderStatus()
+    {
+        $deadline = date('Y-m-d H:i:s', strtotime('-1 month'));
+        $now = date('Y-m-d H:i:s');
+        $endBoardOrders = \db('成品入仓')
+            ->where('jjcp_smb', 'like', '末%')
+            ->field('jjcp_gdbh,jjcp_yjno')
+            ->group('jjcp_gdbh,jjcp_yjno')
+            ->select();
+
+        $updated = 0;
+        $skipped = 0;
+        $logs = [];
+
+        foreach ($endBoardOrders as $order) {
+            $gdbh = trim((string)$order['jjcp_gdbh']);
+            $yjno = trim((string)$order['jjcp_yjno']);
+            if ($gdbh === '' || $yjno === '') {
+                $skipped++;
+                continue;
+            }
+
+            $lastSj = \db('成品入仓')
+                ->where('jjcp_gdbh', $gdbh)
+                ->where('jjcp_yjno', $yjno)
+                ->max('jjcp_sj');
+            if (empty($lastSj) || $lastSj > $deadline) {
+                $skipped++;
+                continue;
+            }
+
+            $workOrder = \db('工单_基本资料')
+                ->where('Gd_gdbh', $gdbh)
+                ->where('行号', $yjno)
+                ->field('Gd_gdbh,行号,rtrim(gd_statu) as gd_statu,工单完工日期')
+                ->find();
+            if (empty($workOrder)) {
+                $skipped++;
+                continue;
+            }
+
+            if (!$this->isEmptyWorkOrderFinishDate($workOrder['工单完工日期'])) {
+                $skipped++;
+                continue;
+            }
+            if (trim((string)$workOrder['gd_statu']) === '1-已完工') {
+                $skipped++;
+                continue;
+            }
+
+            $updateResult = \db('工单_基本资料')
+                ->where('Gd_gdbh', $gdbh)
+                ->where('行号', $yjno)
+                ->update([
+                    '工单完工日期' => $now,
+                    'gd_statu'     => '1-已完工',
+                    'Mod_rq'       => $now,
+                ]);
+            if ($updateResult === false) {
+                $skipped++;
+                continue;
+            }
+
+            $updated++;
+            $logs[] = [
+                'Gd_gdbh'      => $gdbh,
+                'yjno'         => $yjno,
+                'ModifyUser'   => 'system',
+                'ModifyTime'   => $now,
+                'FieldName'    => 'gd_statu',
+                'OldValue'     => trim((string)$workOrder['gd_statu']),
+                'NewValue'     => '1-已完工',
+                'ModifySource' => '成品入仓定时任务',
+            ];
+            $logs[] = [
+                'Gd_gdbh'      => $gdbh,
+                'yjno'         => $yjno,
+                'ModifyUser'   => 'system',
+                'ModifyTime'   => $now,
+                'FieldName'    => '工单完工日期',
+                'OldValue'     => trim((string)$workOrder['工单完工日期']),
+                'NewValue'     => $now,
+                'ModifySource' => '成品入仓定时任务',
+            ];
+        }
+
+        if (!empty($logs)) {
+            \db('系统操作日志表')->insertAll($logs);
+        }
+
+        $this->success('定时任务执行完成', [
+            'updated'       => $updated,
+            'skipped'       => $skipped,
+            'total_checked' => count($endBoardOrders),
+            'deadline'      => $deadline,
+        ]);
+    }
+
+    /**
+     * 判断工单完工日期是否为空
+     * @param mixed $date
+     * @return bool
+     */
+    protected function isEmptyWorkOrderFinishDate($date)
+    {
+        if ($date === null) {
+            return true;
+        }
+        $date = trim((string)$date);
+        if ($date === '') {
+            return true;
+        }
+        $timestamp = strtotime($date);
+        return $timestamp === false || $timestamp <= strtotime('1900-01-02');
+    }
 }