WXL (wul)
4 天以前 38c2fe23d77c9f27d52941dab77a100bb1839dd7
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<template>
  <div class="merge-questionnaire-container">
    <div class="merge-header">
      <h3>合并编辑问卷 (共 {{ services.length }} 个未完成服务)</h3>
      <el-tag
        v-for="service in services"
        :key="service.id"
        type="info"
        style="margin-right: 10px"
      >
        {{ service.taskName }} ({{ service.sendname }})
      </el-tag>
    </div>
 
    <div class="merge-content">
      <div class="question-list">
        <div
          v-for="(question, index) in mergedQuestions"
          :key="question.uniqueKey"
          class="question-item"
        >
          <!-- 题目展示 - 与父组件保持一致 -->
          <div
            :class="
              question.isabnormal ? 'scriptTopic-isabnormal' : 'scriptTopic-dev'
            "
          >
            <div class="dev-text">
              {{ index + 1 }}、[{{
                getQuestionType(question.scriptType)
              }}]<span> {{ question.scriptContent }}</span>
            </div>
 
            <!-- 单选 -->
            <div class="dev-xx" v-if="question.scriptType == 1">
              <el-radio-group
                v-model="question.mergedResult"
                @change="
                  handleOptionChange(
                    $event,
                    index,
                    question.svyLibTemplateTargetoptions,
                    question
                  )
                "
                >11
                <el-radio
                  v-for="(
                    option, optIndex
                  ) in question.svyLibTemplateTargetoptions"
                  :key="optIndex"
                  :label="option.optioncontent"
                  :class="option.isabnormal ? 'red-star' : ''"
                >
                  {{ option.optioncontent }}
                </el-radio>
              </el-radio-group>
            </div>
 
            <!-- 多选 -->
            <div class="dev-xx" v-if="question.scriptType == 2">
              <el-checkbox-group
                v-model="question.mergedResult"
                @change="updateScore($event, index, question.options, question)"
              >
                <el-checkbox
                  v-for="(option, optIndex) in question.options"
                  :key="optIndex"
                  :label="option.optioncontent"
                  :class="option.isabnormal ? 'red-star' : ''"
                >
                  {{ option.optioncontent }}
                </el-checkbox>
              </el-checkbox-group>
            </div>
 
            <!-- 填空 -->
            <div class="dev-xx" v-if="question.scriptType == 4">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入答案"
                v-model="question.mergedResult"
                clearable
              />
            </div>
 
            <!-- 附加输入框 -->
            <div v-if="question.showAppendInput" class="append-input-container">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入具体信息"
                v-model="question.answerps"
                clearable
              />
            </div>
 
            <!-- 提示信息 -->
            <div v-show="question.prompt">
              <el-alert :title="question.prompt" type="warning" />
            </div>
          </div>
        </div>
      </div>
    </div>
 
    <div class="merge-footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSave" :loading="isSaving"
        >保存到所有问卷</el-button
      >
    </div>
  </div>
</template>
 
<script>
import {
  getsearchrResults,
  serviceSubtaskDetailedit,
  serviceSubtaskDetailadd,
  Editsingletaskson,
  getTaskservelist,
  alterpatient,
} from "@/api/AiCentre/index";
 
export default {
  props: {
    selectedServices: {
      type: Array,
      required: true,
    },
    patid: {
      type: [String, Number],
      required: true,
    },
  },
  data() {
    return {
      services: [],
      mergedQuestions: [], // 合并后的问题数据
      isSaving: false,
    };
  },
  created() {
    this.loadServicesData();
  },
  methods: {
    getQuestionType(type) {
      const types = { 1: "单选", 2: "多选", 4: "问答" };
      return types[type] || "未知";
    },
 
    async loadServicesData() {
      this.services = this.selectedServices;
 
      const loading = this.$loading({
        lock: true,
        text: "正在加载未完成问卷数据...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
 
      try {
        // 只加载未完成的服务问卷数据
        const requests = this.services.map((service) =>
          getsearchrResults({
            taskid: service.taskid,
            patid: this.patid,
            subId: service.id,
            isFinish: false, // 确保只获取未完成的问卷
          })
        );
 
        const responses = await Promise.all(requests);
 
        // 合并所有问题,去重
        const allQuestions = [];
        const questionMap = {};
 
        responses.forEach((response, index) => {
          if (response.code == 200 && response.data.scriptResult) {
            response.data.scriptResult.forEach((q) => {
              const key = `${q.scriptContent}_${q.scriptType}`;
              if (!questionMap[key]) {
                questionMap[key] = {
                  ...q,
                  uniqueKey: key,
                  mergedResult: q.scriptType == 2 ? [] : null,
                  originalServices: [], // 记录原始服务ID
                };
                allQuestions.push(questionMap[key]);
              }
              questionMap[key].originalServices.push(this.services[index].id);
            });
          }
        });
 
        // 初始化合并结果
        this.mergedQuestions = allQuestions.map((q) => ({
          ...q,
          mergedResult: q.scriptType == 2 ? [] : q.scriptResult || null,
        }));
        console.log(this.mergedQuestions);
      } catch (error) {
        this.$message.error("加载问卷数据失败: " + error.message);
      } finally {
        loading.close();
      }
    },
 
    // 处理选项变化 (与父组件保持一致)
    handleOptionChange(selectedOption, questionIndex, options, question) {
      const selectedOptionObj = options.find(
        (item) => item.optioncontent == selectedOption
      );
 
      // 设置异常状态
      question.isabnormal = !!selectedOptionObj?.isabnormal;
 
      // 处理附加输入框显示
      question.showAppendInput = selectedOptionObj?.appendflag == 1;
      if (!question.showAppendInput) {
        question.answerps = "";
      }
 
      this.$forceUpdate();
    },
 
    // 更新多选分数 (与父组件保持一致)
    updateScore(selectedValues, questionIndex, options, question) {
      const abnormalOptions = options.filter((opt) => opt.isabnormal);
      question.isabnormal = abnormalOptions.some((opt) =>
        selectedValues.includes(opt.optioncontent)
      );
      this.$forceUpdate();
    },
 
    handleCancel() {
      this.$emit("cancel");
    },
 
    async handleSave() {
      this.isSaving = true;
 
      try {
        const saveResults = [];
        const updateServicePromises = [];
 
        // 1. 保存所有问卷问题
        for (const service of this.services) {
          const serviceId = service.id;
          const questionsToSave = this.mergedQuestions
            .filter((q) => q.originalServices.includes(serviceId))
            .map((question) => ({
              scriptid: question.id,
              scriptResultId: question.scriptResultId,
              scriptType: question.scriptType,
              questiontext: question.scriptContent,
              asrtext:
                question.scriptType == 2
                  ? question.mergedResult.join("&")
                  : question.mergedResult,
              answerps: question.answerps || null,
              isabnormal: question.isabnormal || false,
            }));
 
          // 保存问题数据
          for (const question of questionsToSave) {
            const saveData = {
              taskid: service.taskid,
              patid: this.patid,
              subId: serviceId,
              ...question,
            };
 
            if (saveData.isabnormal) {
              saveData.excep = 1;
            }
 
            try {
              const response = question.scriptResultId
                ? await serviceSubtaskDetailedit(saveData)
                : await serviceSubtaskDetailadd(saveData);
 
              saveResults.push({
                serviceId,
                success: response.code === 200,
                message:
                  response.message ||
                  (response.code === 200 ? "保存成功" : "保存失败"),
              });
            } catch (error) {
              saveResults.push({
                serviceId,
                success: false,
                message: error.message || "保存失败",
              });
            }
          }
 
          // 2. 更新服务状态为已完成 (sendstate = 6)
          updateServicePromises.push(this.updateServiceStatus(serviceId));
        }
 
        // 等待所有服务状态更新完成
        const updateResults = await Promise.all(updateServicePromises);
        updateResults.forEach((result) => {
          if (!result.success) {
            saveResults.push({
              serviceId: result.serviceId,
              success: false,
              message: result.message || "服务状态更新失败",
            });
          }
        });
 
        // 统计结果
        const successCount = saveResults.filter((r) => r.success).length;
        const totalCount = saveResults.length;
 
        // 通知父组件
        this.$emit("save", {
          successCount,
          totalCount,
          results: saveResults,
        });
 
        if (successCount === totalCount) {
          this.$message.success("所有问卷和服务状态更新成功");
        } else {
          this.$message.warning(
            `成功保存 ${successCount} 项,失败 ${totalCount - successCount} 项`
          );
        }
      } catch (error) {
        this.$message.error("保存过程中发生错误: " + error.message);
      } finally {
        this.isSaving = false;
      }
    },
    // 新增方法:更新服务状态
    async updateServiceStatus(serviceId) {
      try {
        // 获取服务当前数据
        const res = await getTaskservelist({
          patid: this.patid,
          subId: serviceId,
        });
 
        if (res.code === 200) {
          const serviceData = res.rows[0].serviceSubtaskList.find(
            (item) => item.id === serviceId
          );
 
          if (serviceData) {
            // 更新服务状态为已完成 (sendstate = 6)
            const updateRes = await Editsingletaskson({
              ...serviceData,
              sendstate: 6, // 设置为已完成状态
              remark: "通过合并编辑完成", // 可选:添加备注
            });
 
            return {
              serviceId,
              success: updateRes.code === 200,
              message: updateRes.message || "服务状态更新成功",
            };
          }
        }
        return {
          serviceId,
          success: false,
          message: "获取服务数据失败",
        };
      } catch (error) {
        return {
          serviceId,
          success: false,
          message: error.message || "更新服务状态失败",
        };
      }
    },
  },
};
</script>
 
<style scoped>
.merge-questionnaire-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 20px;
}
 
.merge-header {
  margin-bottom: 20px;
}
 
.merge-content {
  flex: 1;
  overflow-y: auto;
}
 
.question-list {
  padding: 10px;
}
 
/* 与父组件一致的题目样式 */
.scriptTopic-dev {
  margin-bottom: 20px;
  padding: 15px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}
 
.scriptTopic-isabnormal {
  margin-bottom: 20px;
  padding: 15px;
  border: 1px solid #f56c6c;
  border-radius: 4px;
  background-color: #fff6f6;
}
 
.dev-text {
  font-size: 16px;
  margin-bottom: 15px;
  color: #333;
}
 
.dev-xx {
  margin-left: 20px;
}
 
.append-input-container {
  margin-top: 15px;
}
 
.red-star {
  color: #f56c6c;
}
 
.merge-footer {
  margin-top: 20px;
  text-align: right;
  padding-top: 15px;
  border-top: 1px solid #ebeef5;
}
</style>