spring
8 天以前 e9066f0c6478091278cca7131347dbc896e73278
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<template>
  <view class="inspection-page">
    <CardTitle title="巡检范围及温度详情" :hideAction="false">
      <template v-if="!readonly" #action>
        <view class="header-actions">
          <wd-button size="small" plain icon="add" @click="addRecord">新增</wd-button>
          <wd-button size="small" plain type="warning" @click="editRecords">编辑</wd-button>
          <wd-button size="small" :loading="saving" @click="saveRecords">保存</wd-button>
        </view>
      </template>
    </CardTitle>
 
    <view v-if="loading" class="state-box">
      <wd-loading />
      <text>加载中...</text>
    </view>
    <wd-status-tip v-else-if="!records.length" image="content" tip="暂无巡检温度记录" />
    <view v-else class="record-list">
      <wd-card v-for="(record, index) in records" :key="record.id || index">
        <template #title>
          <view class="record-title">
            <text>巡检记录 {{ index + 1 }}</text>
            <wd-button
              v-if="!readonly"
              type="text"
              size="small"
              custom-class="delete-button"
              @click="deleteRecord(record, index)"
            >
              删除
            </wd-button>
          </view>
        </template>
 
        <wd-cell-group v-if="readonly || (record.id && !record._editing)">
          <wd-cell title="巡检员" :value="record.routingInspectionUser || '-'" />
          <wd-cell title="一区" :value="temperatureValue(record.zoneOne)" />
          <wd-cell title="二区" :value="temperatureValue(record.zoneTwo)" />
          <wd-cell title="三区" :value="temperatureValue(record.zoneThree)" />
          <wd-cell title="四区" :value="temperatureValue(record.zoneFour)" />
          <wd-cell title="五区" :value="temperatureValue(record.zoneFive)" />
          <wd-cell title="六区" :value="temperatureValue(record.zoneSix)" />
          <wd-cell title="巡检时间" :value="record.createTime || record.inspectionTime || '-'" />
        </wd-cell-group>
 
        <wd-cell-group v-else border>
          <wd-input v-model="record.routingInspectionUser" label="巡检员" readonly />
          <wd-input v-model="record.zoneOne" label="一区(℃)" type="digit" placeholder="请输入" />
          <wd-input v-model="record.zoneTwo" label="二区(℃)" type="digit" placeholder="请输入" />
          <wd-input v-model="record.zoneThree" label="三区(℃)" type="digit" placeholder="请输入" />
          <wd-input v-model="record.zoneFour" label="四区(℃)" type="digit" placeholder="请输入" />
          <wd-input v-model="record.zoneFive" label="五区(℃)" type="digit" placeholder="请输入" />
          <wd-input v-model="record.zoneSix" label="六区(℃)" type="digit" placeholder="请输入" />
        </wd-cell-group>
      </wd-card>
    </view>
 
    <wd-toast />
  </view>
</template>
 
<script setup lang="ts">
import { onLoad } from "@dcloudio/uni-app";
import { useToast } from "wot-design-uni";
import CardTitle from "@/components/card-title/index.vue";
import TimeSensitiveTaskApi, {
  USE_TIME_SENSITIVE_MOCK,
  type TimelinessInspectionDetailForm,
} from "@/api/timeSensitiveTask";
import { useUserStore } from "@/store/modules/user";
import { getPrepareId } from "@/utils/cache";
 
type InspectionRecord = TimelinessInspectionDetailForm & {
  createTime?: string;
  inspectionTime?: string;
  _editing?: boolean;
};
 
const toast = useToast();
const userStore = useUserStore();
const inspectionId = ref<number | string>("");
const readonly = ref(false);
const loading = ref(false);
const saving = ref(false);
const records = ref<InspectionRecord[]>([]);
 
function currentPerson() {
  const user = userStore.userInfo as any;
  return user?.nickName || user?.nickname || user?.userName || user?.username || "";
}
 
function temperatureValue(value: unknown) {
  return value === undefined || value === null || value === "" ? "-" : `${value}℃`;
}
 
function ensurePrepared(action: string) {
  if (USE_TIME_SENSITIVE_MOCK || getPrepareId()) return true;
  toast.error(`请完成生产准备确认,再进行${action}操作`);
  return false;
}
 
async function loadRecords() {
  loading.value = true;
  try {
    const { data } = await TimeSensitiveTaskApi.getInspectionDetails(inspectionId.value);
    const list = Array.isArray(data) ? data : data ? [data] : [];
    records.value = list.map((item: any) => ({
      ...item,
      routingInspectionUser: item.routingInspectionUser || currentPerson(),
      timelinessInspectionId: item.timelinessInspectionId || inspectionId.value,
      _editing: false,
    }));
  } catch (error: any) {
    records.value = [];
    toast.error(error?.message || "加载巡检详情失败");
  } finally {
    loading.value = false;
  }
}
 
function addRecord() {
  if (readonly.value || !ensurePrepared("新增巡检范围温度")) return;
  records.value.push({
    routingInspectionUser: currentPerson(),
    zoneOne: "",
    zoneTwo: "",
    zoneThree: "",
    zoneFour: "",
    zoneFive: "",
    zoneSix: "",
    timelinessInspectionId: inspectionId.value,
    _editing: true,
  });
}
 
function editRecords() {
  if (readonly.value || !ensurePrepared("编辑巡检范围温度")) return;
  if (!records.value.some((item) => item.id)) return toast.error("没有已保存的数据可编辑");
  records.value.forEach((item) => {
    if (item.id) item._editing = true;
  });
  toast.show("已进入编辑模式,修改后请点击保存");
}
 
async function saveRecords() {
  if (readonly.value || !ensurePrepared("保存巡检范围温度")) return;
  if (!records.value.length) return toast.error("没有数据需要保存");
  saving.value = true;
  try {
    const payload = records.value.map((item) => ({
      id: item.id,
      routingInspectionUser: item.routingInspectionUser || currentPerson(),
      zoneOne: item.zoneOne,
      zoneTwo: item.zoneTwo,
      zoneThree: item.zoneThree,
      zoneFour: item.zoneFour,
      zoneFive: item.zoneFive,
      zoneSix: item.zoneSix,
      timelinessInspectionId: item.timelinessInspectionId || inspectionId.value,
    }));
    await TimeSensitiveTaskApi.saveInspectionDetails(payload);
    toast.success("保存成功");
    await loadRecords();
  } catch (error: any) {
    toast.error(error?.message || "保存失败");
  } finally {
    saving.value = false;
  }
}
 
function showDeleteConfirm() {
  return new Promise<boolean>((resolve) => {
    uni.showModal({
      title: "提示",
      content: "确定删除该巡检详情吗?",
      confirmText: "删除",
      success: (result) => resolve(Boolean(result.confirm)),
      fail: () => resolve(false),
    });
  });
}
 
async function deleteRecord(record: InspectionRecord, index: number) {
  if (readonly.value) return;
  const confirmed = await showDeleteConfirm();
  if (!confirmed) return;
  if (!record.id) {
    records.value.splice(index, 1);
    toast.success("删除成功");
    return;
  }
  try {
    await TimeSensitiveTaskApi.deleteInspectionDetail(record.id);
    toast.success("删除成功");
    await loadRecords();
  } catch (error: any) {
    toast.error(error?.message || "删除失败");
  }
}
 
onLoad((options: any) => {
  inspectionId.value = options?.id || "";
  readonly.value = String(options?.readonly || "0") === "1";
  if (!inspectionId.value) return toast.error("巡检记录ID不能为空");
  loadRecords();
});
</script>
 
<style lang="scss" scoped>
.inspection-page {
  min-height: 100vh;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  box-sizing: border-box;
  background: #f3f9f8;
}
 
.header-actions,
.record-title {
  display: flex;
  gap: 6px;
  align-items: center;
  justify-content: space-between;
}
 
.record-list {
  padding: 10px;
}
 
.record-list :deep(.wd-card) {
  margin: 0 0 10px;
}
 
.record-title {
  font-weight: 600;
}
 
:deep(.delete-button .wd-button__content) {
  color: #fa4350;
}
 
.state-box {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  min-height: 180px;
  color: #999999;
}
</style>