qaz 3 днів тому
батько
коміт
8e0aa3cf4f

+ 63 - 23
src/views/system/borrow/component/CollectEquipment/index.vue

@@ -311,7 +311,7 @@
                             ></textarea>
                             <el-button type="primary" style="margin-left: 16px" @click="handleAdd" :disabled="isView">手动归还</el-button>
                         </div>
-                        <el-table :data="returnDeviceList" border style="width: 100%" >
+                        <el-table :data="sortedReturnDeviceList" border style="width: 100%" >
                             <!-- <el-table-column type="selection" width="55" align="center" />@selection-change="handleSelectionChange" -->
                             <el-table-column type="index" label="序号" width="60" align="center" />
                             <el-table-column prop="device_code" label="设备编号" align="center" />
@@ -373,17 +373,17 @@
                               </template>
                             </el-table-column>
                             <el-table-column label="操作" width="120" align="center">
-                                <template #default="{ $index,row }">
+                                <template #default="{ row }">
                                  <!--  && (!returnDeviceList[$index]?.condition || returnDeviceList[$index]?.photos?.length==0)&& row.problem_records?.length ==0 -->
                                   <el-button :disabled="isView" type="text" style="color: red;"  
                                    v-if="!isView"
-                                  @click="handleAbnormal($index)">异常操作</el-button>
+                                  @click="handleAbnormal(row)">异常操作</el-button>
                                   <el-button 
                                     
                                     type="text" 
                                     style="color: #409EFF;margin-left:0" 
-                                    @click="handleViewAbnormal($index)"
-                                    v-if="row.problem_records?.length || (returnDeviceList[$index]?.condition || returnDeviceList[$index]?.photos?.length)"
+                                    @click="handleViewAbnormal(row)"
+                                    v-if="row.problem_records?.length || row.condition || row.photos?.length"
                                   >查看异常</el-button>
                                     <!-- <el-button type="text" v-if="!isView" :disabled="isView" style="color: red" @click="handleReturnRemove($index)">移除</el-button> -->
                                 </template>
@@ -593,6 +593,10 @@ interface DeviceListItem {
   returned_quantity?: number;
   problem_records?: any[];
   damage_type?: number;
+  /** 借用单明细行接口返回的更新时间,用于列表排序 */
+  line_update_at?: string;
+  /** 本页对行做了归还/数量等操作时的排序时间戳,与 line_update_at 取较大者参与排序 */
+  sort_touched_at?: number;
 }
 
 interface FormItem {
@@ -931,9 +935,31 @@ function getPendingUnreturnedFromItem(item: any): number {
   return Math.max(0, borrowed - returned);
 }
 
+/** 从借用单 items 行解析「明细更新时间」(兼容常见后端字段名,避免写死单一路径) */
+function getItemLineUpdateTimeForSort(item: any): string | undefined {
+  if (item == null || typeof item !== 'object') return undefined;
+  const v =
+    item.update_datetime ?? item.update_time ?? item.modified_time ?? item.update_at;
+  if (v == null || v === '') return undefined;
+  return String(v);
+}
+
+function getReturnRowSortMs(row: DeviceListItem): number {
+  const serverRaw = row.line_update_at;
+  const serverMs =
+    serverRaw && dayjs(serverRaw).isValid() ? dayjs(serverRaw).valueOf() : 0;
+  const localMs = row.sort_touched_at ?? 0;
+  return Math.max(serverMs, localMs);
+}
+
+function touchReturnRowSort(row: DeviceListItem) {
+  row.sort_touched_at = Date.now();
+}
+
 /** 将 form.items 单行映射为归还列表行:上限为待归还;归还数量由扫码/手工录入,从 0 起算 */
 function mapFormItemToReturnDeviceListRow(item: any): DeviceListItem {
   const pending = getPendingUnreturnedFromItem(item);
+  const lineUpdate = getItemLineUpdateTimeForSort(item);
   return {
     device_no: item.device,
     device_code: item.device_code,
@@ -947,7 +973,9 @@ function mapFormItemToReturnDeviceListRow(item: any): DeviceListItem {
     return_status: item.return_status,
     return_time: item.return_times,
     available_quantity: pending,
-    returned_quantity: Math.max(0, Number(item.returned_quantity) || 0)
+    returned_quantity: Math.max(0, Number(item.returned_quantity) || 0),
+    line_update_at: lineUpdate,
+    sort_touched_at: 0
   };
 }
 
@@ -961,6 +989,7 @@ function clampReturnBorrowCount(row: DeviceListItem) {
   if (n < 0) n = 0;
   if (n > max) n = max;
   row.borrow_count = n;
+  touchReturnRowSort(row);
 }
 
 /** 是否仍有待归还(有未归还可还):用于表格列在「只展示已归还」与「展示待还+本次归还」间切换 */
@@ -986,6 +1015,7 @@ function tryApplyReturnScanIncrement(row: DeviceListItem): string | null {
     row.is_return = true;
     row.return_time = dayjs().format('YYYY-MM-DD HH:mm:ss');
   }
+  touchReturnRowSort(row);
   return null;
 }
 
@@ -1011,6 +1041,13 @@ const showUnreturnedReturnTableColumns = computed(() =>
   returnDeviceList.value.some((d) => (Number(d.available_quantity) || 0) > 0)
 );
 
+/** 归还设备表按「明细/本页最近修改时间」降序,最近在前 */
+const sortedReturnDeviceList = computed(() =>
+  [...returnDeviceList.value].sort(
+    (a, b) => getReturnRowSortMs(b) - getReturnRowSortMs(a)
+  )
+);
+
 //审批步骤
 const steps = ref<any[]>([]);
 // 仓库映射(id -> name)
@@ -1079,7 +1116,7 @@ const borrowForm = ref<any>(null);
 
 // 添加异常操作相关的状态
 const showAbnormalDialog = ref(false);
-const currentAbnormalIndex = ref(-1);
+const currentAbnormalDeviceNo = ref<string | number | null>(null);
 
 // 添加查看异常相关的状态
 const showViewAbnormalDialog = ref(false);
@@ -1168,29 +1205,31 @@ function buildSteps(data: any) {
   }
 }
 
-// 处理异常操作
-const handleAbnormal = (index: number) => {
-  currentAbnormalIndex.value = index;
+// 处理异常操作(按 device_no 定位,避免与「按时间排序的表格」下标错位)
+const handleAbnormal = (row: DeviceListItem) => {
+  currentAbnormalDeviceNo.value = row.device_no;
   showAbnormalDialog.value = true;
-  console.log("showAbnormalDialog:::", showAbnormalDialog.value, "currentAbnormalIndex:::", currentAbnormalIndex.value);
 };
 
 // 处理异常操作确认
-const onAbnormalConfirm = (data: { condition: string; photos: any[] }) => {
-  console.log("异常操作确认数据:", data);
-  if (currentAbnormalIndex.value !== -1) {
-    const device = returnDeviceList.value[currentAbnormalIndex.value];
-    device.condition = data.condition;
-    device.photos = data.photos;
-    device.damage_type = data.damage_type;
-    ElMessage.success('异常信息已保存');
-  }
-  console.log("returnDeviceList.value:::",returnDeviceList.value);
+const onAbnormalConfirm = (data: { condition: string; photos: any[]; damage_type?: number }) => {
+  const key = currentAbnormalDeviceNo.value;
+  if (key != null && key !== '') {
+    const device = returnDeviceList.value.find((d) => d.device_no === key);
+    if (device) {
+      device.condition = data.condition;
+      device.photos = data.photos;
+      device.damage_type = data.damage_type;
+      touchReturnRowSort(device);
+      ElMessage.success('异常信息已保存');
+    }
+  }
+  currentAbnormalDeviceNo.value = null;
 };
 
 // 处理查看异常
-const handleViewAbnormal = (index: number) => {
-  const device = returnDeviceList.value[index];
+const handleViewAbnormal = (row: DeviceListItem) => {
+  const device = row;
   console.log("device:::",device);
   if (device) {
     if(device.problem_records?.length){
@@ -3073,6 +3112,7 @@ function onDeviceSelected(devices: Device[]) {
         if (row.return_status) {
           row.return_status = { value: 2, label: '已归还' };
         }
+        touchReturnRowSort(row);
         ElMessage.success(`已将该设备本次归还记为 ${cap} 件`);
       } else {
         // 如果设备不存在,添加新记录