All files / src/views/logic Inspect.logic.ts

98.58% Statements 907/920
80.7% Branches 92/114
100% Functions 20/20
98.58% Lines 907/920

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 9211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x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x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 2x 2x 2x 4x 4x 2x 2x 2x 2x 4x 5x 1x 5x 5x  
/**
 * 检查任务管理业务逻辑模块
 * 提供检查任务的创建、编辑、删除、查询等功能
 * 
 * @fileOverview 检查任务管理模块,基于Vue 3 Composition API和Element Plus实现
 * @author InspectionFlowSystem Team
 * @version 1.0.0
 * @since 2025-09-16
 */
 
import { ref, computed } from 'vue';
import api, { taskAPI } from '../../api/index';
import { ElMessage, ElMessageBox } from 'element-plus';
 
/**
 * 表单验证配置对象
 * @typedef {Object} ValidationConfig
 * @description 用于配置表单字段的验证规则,支持动态修改验证规则
 */
 
/**
 * 验证配置 - 可根据业务需要修改这些配置
 * @constant {ValidationConfig}
 * @description 定义所有表单字段的验证规则,包括必填性、错误信息等
 * @example
 * // 修改某个字段的验证规则
 * validationConfig.product_name.required = false;
 * validationConfig.product_count.min = 5;
 */
export const validationConfig = {
  // 基本信息必填字段
  product_name: { required: true, message: '请输入产品名称' },
  test_property: { required: true, message: '请输入实验性质' },
  test_basis: { required: true, message: '请输入实验依据' },
  model_spec: { required: true, message: '请输入型号规格' },
  package_form: { required: true, message: '请输入封装形式' },
  prod_batch: { required: true, message: '请输入生产批次' },
  detect_batch: { required: true, message: '请输入检测批次' },
  product_count: { required: true, message: '请输入产品数量', min: 1 },
 
  // 人员信息必填字段
  manager_id: { required: true, message: '请输入项目负责人' },
  drafter_id: { required: false, message: '请输入拟制人' },
  reviewer_id: { required: false, message: '请输入审核人' },
  approver_id: { required: false, message: '请输入批准人' },
 
  // 子项目必填字段
  subtaskItem: { required: true, message: '请输入检测项目' },
  subtaskCondition: { required: true, message: '请输入实验条件' },
 
  // 其他字段
  remark: { required: false, message: '请输入备注信息' }
};
 
/**
 * 任务状态枚举
 * @enum {string}
 * @description 定义任务的各种状态
 */
export const TASK_STATUS = {
  DRAFT: 'draft',           // 草稿
  PENDING: 'pending',       // 待接取
  IN_PROGRESS: 'in_progress', // 进行中
  COMPLETED: 'completed',   // 已完成
  REVIEWED: 'reviewed',     // 已审核
  AUTHORIZED: 'authorized', // 已授权
  TERMINATED: 'terminated', // 已中止
  ARCHIVED: 'archived'      // 已归档
};
 
/**
 * 任务状态显示映射
 * @constant {Object}
 * @description 状态到中文显示的映射
 */
export const STATUS_LABELS = {
  [TASK_STATUS.DRAFT]: '草稿',
  [TASK_STATUS.PENDING]: '待接取',
  [TASK_STATUS.IN_PROGRESS]: '进行中',
  [TASK_STATUS.COMPLETED]: '已完成',
  [TASK_STATUS.REVIEWED]: '已审核',
  [TASK_STATUS.AUTHORIZED]: '已授权',
  [TASK_STATUS.TERMINATED]: '已中止',
  [TASK_STATUS.ARCHIVED]: '已归档'
};
 
/**
 * 任务状态标签页配置
 * @constant {Array}
 * @description 定义标签页的显示和过滤配置
 */
export const STATUS_TABS = [
  { key: 'all', label: '全部', status: null },
  { key: TASK_STATUS.DRAFT, label: STATUS_LABELS[TASK_STATUS.DRAFT], status: TASK_STATUS.DRAFT },
  { key: TASK_STATUS.PENDING, label: STATUS_LABELS[TASK_STATUS.PENDING], status: TASK_STATUS.PENDING },
  { key: TASK_STATUS.IN_PROGRESS, label: STATUS_LABELS[TASK_STATUS.IN_PROGRESS], status: TASK_STATUS.IN_PROGRESS },
  { key: TASK_STATUS.COMPLETED, label: STATUS_LABELS[TASK_STATUS.COMPLETED], status: TASK_STATUS.COMPLETED },
  { key: TASK_STATUS.REVIEWED, label: STATUS_LABELS[TASK_STATUS.REVIEWED], status: TASK_STATUS.REVIEWED },
  { key: TASK_STATUS.AUTHORIZED, label: STATUS_LABELS[TASK_STATUS.AUTHORIZED], status: TASK_STATUS.AUTHORIZED },
  { key: TASK_STATUS.TERMINATED, label: STATUS_LABELS[TASK_STATUS.TERMINATED], status: TASK_STATUS.TERMINATED },
  { key: TASK_STATUS.ARCHIVED, label: STATUS_LABELS[TASK_STATUS.ARCHIVED], status: TASK_STATUS.ARCHIVED }
];
 
/**
 * 任务列表响应式引用
 * @type {import('vue').Ref<Task[]>}
 * @description 存储所有检查任务的列表数据
 */
export const tasks = ref<Task[]>([]);
 
/**
 * 当前激活的状态标签页
 * @type {import('vue').Ref<string>}
 * @description 当前选中的任务状态标签页
 */
export const activeTab = ref('all');
 
/**
 * 过滤后的任务列表
 * @type {import('vue').ComputedRef<Task[]>}
 * @description 根据当前激活标签页过滤后的任务列表
 */
export const filteredTasks = computed(() => {
  if (activeTab.value === 'all') {
    return tasks.value;
  }
  return tasks.value.filter(task => task.status === activeTab.value);
});
 
/**
 * 对话框显示状态响应式引用
 * @type {import('vue').Ref<boolean>}
 * @description 控制任务编辑对话框的显示和隐藏
 */
export const dialogVisible = ref(false);
/** 审核对话框显示状态 */
export const reviewDialogVisible = ref(false);
/** 授权对话框显示状态 */
export const authorizeDialogVisible = ref(false);
/** 当前审核的任务 */
export const currentReviewTask = ref<Task | null>(null);
 
/**
 * 子任务接口定义
 * @interface Subtask
 * @description 定义检查任务中子项目的数据结构
 * @since 1.0.0
 */
export interface Subtask {
  /** 检测项目名称 */
  item: string;
  /** 实验条件描述 */
  condition: string;
}
 
/**
 * 任务数据接口
 * @interface Task
 * @description 定义任务的完整数据结构
 * @since 1.0.0
 */
export interface Task {
  /** 任务ID */
  id: number;
  /** 产品名称 */
  product_name: string;
  /** 实验性质 */
  test_property: string;
  /** 实验依据 */
  test_basis: string;
  /** 型号规格 */
  model_spec: string;
  /** 封装形式 */
  package_form: string;
  /** 生产批次 */
  prod_batch: string;
  /** 检测批次 */
  detect_batch: string;
  /** 产品数量 */
  product_count: number;
  /** 项目负责人 */
  manager_id: number;
  /** 拟制人 */
  drafter_id: number;
  /** 审核人 */
  reviewer_id: number;
  /** 批准人 */
  approver_id: number;
  /** 子项目信息列表 */
  subtasks: Subtask[];
  /** 备注信息 */
  remark: string;
  /** 任务状态 */
  status: string;
  /** 创建时间 */
  created_at?: string;
  /** 审核意见 */
  reviewComment?: string;
  /** 授权信息 */
  authorizationInfo?: {
    authorizedBy: string;
    authorizedAt: string;
  };
}
 
/**
 * 检查任务表单数据接口
 * @interface InspectForm
 * @description 定义检查任务表单的完整数据结构
 * @since 1.0.0
 */
export interface InspectForm {
  /** 产品名称 */
  product_name: string;
  /** 实验性质 */
  test_property: string;
  /** 实验依据 */
  test_basis: string;
  /** 型号规格 */
  model_spec: string;
  /** 封装形式 */
  package_form: string;
  /** 生产批次 */
  prod_batch: string;
  /** 检测批次 */
  detect_batch: string;
  /** 产品数量 */
  product_count: number;
 
  /** 项目负责人ID */
  manager_id: number;
  /** 拟制人ID */
  drafter_id: number;
  /** 审核人ID */
  reviewer_id: number;
  /** 批准人ID */
  approver_id: number;
 
  /** 子项目信息列表 */
  subtasks: Subtask[];
 
  /** 备注信息 */
  remark: string;
 
  /** 任务状态 */
  status?: string;
}
 
/**
 * 表单响应式数据
 * @type {import('vue').Ref<InspectForm>}
 * @description 绑定到检查任务编辑表单的数据对象
 */
export const form = ref<InspectForm>({
  product_name: '',
  test_property: '',
  test_basis: '',
  model_spec: '',
  package_form: '',
  prod_batch: '',
  detect_batch: '',
  product_count: 1,
  manager_id: 0,
  drafter_id: 0,
  reviewer_id: 0,
  approver_id: 0,
  subtasks: [{
    item: '',
    condition: ''
  }],
  remark: '',
  status: TASK_STATUS.DRAFT
});
 
/**
 * 编辑状态标识
 * @type {import('vue').Ref<number|null>}
 * @description 存储当前正在编辑的任务ID,为null时表示新建模式
 */
export const editId = ref<number | null>(null);
 
/**
 * 获取任务列表
 * 
 * @async
 * @function fetchTasks
 * @returns {void}
 * @description 从后端 API 获取所有检查任务列表,更新本地 tasks 响应式数据
 * 
 * @example
 * // 在组件加载时获取任务列表
 * onMounted(() => {
 *   fetchTasks();
 * });
 * 
 * @throws {Error} 当API请求失败时会显示错误消息
 * @since 1.0.0
 */
export function fetchTasks() {
  taskAPI.getTasks()
    .then(res => {
      // 转换后端返回的数字状态为前端使用的字符串状态
      const statusMap = {
        1: TASK_STATUS.DRAFT,
        2: TASK_STATUS.PENDING,
        3: TASK_STATUS.IN_PROGRESS,
        4: TASK_STATUS.COMPLETED,
        5: TASK_STATUS.REVIEWED,
        6: TASK_STATUS.AUTHORIZED,
        7: TASK_STATUS.TERMINATED,
        8: TASK_STATUS.ARCHIVED
      };
      
      tasks.value = res.data.map(task => ({
        ...task,
        // 只转换状态字段,其他字段保持与数据库一致
        status: statusMap[task.status] || task.status,
        subtasks: task.subtasks ? task.subtasks.map((subtask: any) => ({
          ...subtask
        })) : []
      }));
      console.log('获取任务列表成功:', tasks.value);
    })
    .catch(error => {
      if (error.response?.status === 401 || error.response?.status === 403) {
        console.error('获取任务列表失败:', error);
        ElMessage.error('获取任务列表失败,未登录或登录超时');
      } else {
        console.error('获取任务列表失败:', error);
        const errorMessage = error.response?.data?.message || '未知错误';
        ElMessage.error(`获取任务列表失败: ${errorMessage}`);
      }
    });
}
 
/**
 * 打开对话框(新建模式)
 * 
 * @function openDialog
 * @returns {void}
 * @description 打开任务编辑对话框并重置表单为新建模式,清空所有表单数据
 * 
 * @example
 * // 点击新建任务按钮
 * <el-button @click="openDialog">新建任务</el-button>
 * 
 * @since 1.0.0
 */
export function openDialog() {
  dialogVisible.value = true;
  editId.value = null;
 
  // 重置表单数据
  form.value = {
    product_name: '',
    test_property: '',
    test_basis: '',
    model_spec: '',
    package_form: '',
    prod_batch: '',
    detect_batch: '',
    product_count: 1,
    manager_id: 0,
    drafter_id: 0,
    reviewer_id: 0,
    approver_id: 0,
    subtasks: [{
      item: '',
      condition: ''
    }],
    remark: '',
    status: TASK_STATUS.DRAFT
  };
}
 
/**
 * 关闭对话框
 * 
 * @function closeDialog
 * @returns {void}
 * @description 关闭任务编辑对话框并清空编辑状态
 * 
 * @example
 * // 点击取消按钮
 * <el-button @click="closeDialog">取消</el-button>
 * 
 * @since 1.0.0
 */
export function closeDialog() {
  dialogVisible.value = false;
  editId.value = null;
}
 
/**
 * 添加子任务
 * 
 * @function addSubtask
 * @returns {void}
 * @description 在当前表单中添加一个新的空子任务项目
 * 
 * @example
 * // 点击添加子任务按钮
 * <el-button @click="addSubtask">添加子项目</el-button>
 * 
 * @since 1.0.0
 */
export function addSubtask() {
  form.value.subtasks.push({
    item: '',
    condition: ''
  });
}
 
/**
 * 删除子任务
 * 
 * @function removeSubtask
 * @param {number} index - 要删除的子任务在数组中的索引
 * @returns {void}
 * @description 根据索引删除指定的子任务,但至少保留一个子任务
 * 
 * @example
 * // 在模板中删除某个子任务
 * <el-button @click="removeSubtask(index)">删除</el-button>
 * 
 * @since 1.0.0
 */
export function removeSubtask(index: number) {
  if (form.value.subtasks.length > 1) {
    form.value.subtasks.splice(index, 1);
  }
}
 
/**
 * 表单验证功能
 * 
 * @private
 * @function validateForm
 * @returns {boolean} 验证是否通过
 * @description 基于配置的灵活验证,检查表单所有必填字段和数据格式
 * 
 * @example
 * // 在提交前验证表单
 * if (validateForm()) {
 *   // 执行提交逻辑
 * }
 * 
 * @throws {ElMessage} 验证失败时会弹出错误消息
 * @since 1.0.0
 */
function validateForm(): boolean {
  // 基本信息验证
  if (validationConfig.product_name.required && !form.value.product_name.trim()) {
    ElMessage.error(validationConfig.product_name.message);
    return false;
  }
 
  if (validationConfig.test_property.required && !form.value.test_property.trim()) {
    ElMessage.error(validationConfig.test_property.message);
    return false;
  }
 
  if (validationConfig.test_basis.required && !form.value.test_basis.trim()) {
    ElMessage.error(validationConfig.test_basis.message);
    return false;
  }
 
  if (validationConfig.model_spec.required && !form.value.model_spec.trim()) {
    ElMessage.error(validationConfig.model_spec.message);
    return false;
  }
 
  if (validationConfig.package_form.required && !form.value.package_form.trim()) {
    ElMessage.error(validationConfig.package_form.message);
    return false;
  }
 
  if (validationConfig.prod_batch.required && !form.value.prod_batch.trim()) {
    ElMessage.error(validationConfig.prod_batch.message);
    return false;
  }
 
  if (validationConfig.detect_batch.required && !form.value.detect_batch.trim()) {
    ElMessage.error(validationConfig.detect_batch.message);
    return false;
  }
 
  if (validationConfig.product_count.required) {
    if (form.value.product_count <= 0) {
      ElMessage.error(validationConfig.product_count.message);
      return false;
    }
    if (validationConfig.product_count.min && form.value.product_count < validationConfig.product_count.min) {
      ElMessage.error(`产品数量不能少于${validationConfig.product_count.min}`);
      return false;
    }
  }
 
  // 人员信息验证(数字类型不需要trim)
  if (validationConfig.manager_id.required && !form.value.manager_id) {
    ElMessage.error(validationConfig.manager_id.message);
    return false;
  }
 
  if (validationConfig.drafter_id.required && !form.value.drafter_id) {
    ElMessage.error(validationConfig.drafter_id.message);
    return false;
  }
 
  if (validationConfig.reviewer_id.required && !form.value.reviewer_id) {
    ElMessage.error(validationConfig.reviewer_id.message);
    return false;
  }
 
  if (validationConfig.approver_id.required && !form.value.approver_id) {
    ElMessage.error(validationConfig.approver_id.message);
    return false;
  }
 
  // 子项目验证
  for (let i = 0; i < form.value.subtasks.length; i++) {
    const subtask = form.value.subtasks[i];
    if (validationConfig.subtaskItem.required && !subtask.item.trim()) {
      ElMessage.error(`第${i + 1}行:${validationConfig.subtaskItem.message}`);
      return false;
    }
    if (validationConfig.subtaskCondition.required && !subtask.condition.trim()) {
      ElMessage.error(`第${i + 1}行:${validationConfig.subtaskCondition.message}`);
      return false;
    }
  }
 
  // 备注验证
  if (validationConfig.remark.required && !form.value.remark.trim()) {
    ElMessage.error(validationConfig.remark.message);
    return false;
  }
 
  return true;
}
 
/**
 * 切换状态标签页
 * 
 * @function switchTab
 * @param {string} tabKey - 标签页的key值
 * @returns {void}
 * @description 切换到指定的状态标签页
 * 
 * @since 1.0.0
 */
export function switchTab(tabKey: string) {
  activeTab.value = tabKey;
}
 
/**
 * 保存草稿
 * 
 * @async
 * @function saveDraft
 * @returns {void}
 * @description 将任务保存为草稿状态
 * 
 * @since 1.0.0
 */
export function saveDraft() {
  form.value.status = TASK_STATUS.DRAFT;
  submitTaskWithStatus('草稿保存成功!');
}
 
/**
 * 确认并发布
 * 
 * @async
 * @function publishTask
 * @returns {void}
 * @description 发布任务,状态设置为待接取
 * 
 * @since 1.0.0
 */
export function publishTask() {
  form.value.status = TASK_STATUS.PENDING;
  submitTaskWithStatus('任务发布成功!');
}
 
/**
 * 提交任务单(创建或更新)
 * 
 * @async
 * @function submitTask
 * @returns {void}
 * @description 验证表单数据并提交到后端,根据editId判断是创建还是更新操作
 * 
 * @example
 * // 点击保存按钮
 * <el-button @click="submitTask">保存</el-button>
 * 
 * @throws {Error} 当API请求失败时会显示错误消息
 * @since 1.0.0
 * 
 * @see {@link validateForm} - 表单验证函数
 * @see {@link fetchTasks} - 刷新任务列表
 * @see {@link closeDialog} - 关闭对话框
 */
export function submitTask() {
  // 表单验证
  if (!validateForm()) {
    return;
  }
 
  // 转换前端使用的字符串状态为后端需要的数字状态
  const reverseStatusMap: Record<string, number> = {
    [TASK_STATUS.DRAFT]: 1,
    [TASK_STATUS.PENDING]: 2,
    [TASK_STATUS.IN_PROGRESS]: 3,
    [TASK_STATUS.COMPLETED]: 4,
    [TASK_STATUS.REVIEWED]: 5,
    [TASK_STATUS.AUTHORIZED]: 6,
    [TASK_STATUS.TERMINATED]: 7,
    [TASK_STATUS.ARCHIVED]: 8
  };
  
  const requestData = {
    ...form.value,
    // 确保数字类型正确
    product_count: Number(form.value.product_count),
    // 转换状态字段
    status: (form.value.status && reverseStatusMap[form.value.status]) ? reverseStatusMap[form.value.status] : form.value.status
  };
 
  const request = editId.value
    ? taskAPI.updateTask(editId.value, requestData)
    : taskAPI.createTask(requestData);
  console.log('发送请求:', requestData);
  request
    .then(response => {
      console.log(editId.value ? '更新任务成功:' : '创建任务成功:', response.data);
      fetchTasks(); // 刷新列表
      closeDialog(); // 关闭对话框
 
      // 成功提示
      ElMessage.success('草稿保存成功!');
    })
    .catch(error => {
      console.error(editId.value ? '更新任务失败:' : '创建任务失败:', error);
 
      // 错误提示
      const errorMsg = error.response?.data?.message || '操作失败,请重试';
      ElMessage.error(errorMsg);
    });
}
 
/**
 * 带状态的任务提交
 * 
 * @async
 * @function submitTaskWithStatus
 * @param {string} successMessage - 成功提示消息
 * @returns {void}
 * @description 验证表单数据并提交到后端,带有状态信息
 * 
 * @since 1.0.0
 */
function submitTaskWithStatus(successMessage: string) {
  // 表单验证
  if (!validateForm()) {
    return;
  }
 
  // 转换前端使用的字符串状态为后端需要的数字状态
  const reverseStatusMap: Record<string, number> = {
    [TASK_STATUS.DRAFT]: 1,
    [TASK_STATUS.PENDING]: 2,
    [TASK_STATUS.IN_PROGRESS]: 3,
    [TASK_STATUS.COMPLETED]: 4,
    [TASK_STATUS.REVIEWED]: 5,
    [TASK_STATUS.AUTHORIZED]: 6,
    [TASK_STATUS.TERMINATED]: 7,
    [TASK_STATUS.ARCHIVED]: 8
  };
  
  const requestData = {
    ...form.value,
    // 确保数字类型正确
    product_count: Number(form.value.product_count),
    // 转换状态字段
    status: (form.value.status && reverseStatusMap[form.value.status]) ? reverseStatusMap[form.value.status] : form.value.status
  };
 
  const request = editId.value
    ? taskAPI.updateTask(editId.value, requestData)
    : taskAPI.createTask(requestData);
  console.log('发送请求:', requestData);
  request
    .then(response => {
      console.log(editId.value ? '更新任务成功:' : '创建任务成功:', response.data);
      fetchTasks(); // 刷新列表
      closeDialog(); // 关闭对话框
 
      // 成功提示
      ElMessage.success(successMessage);
    })
    .catch(error => {
      console.error(editId.value ? '更新任务失败:' : '创建任务失败:', error);

      // 错误提示
      const errorMsg = error.response?.data?.message || '操作失败,请重试';
      ElMessage.error(errorMsg);
    });
}
 
/**
 * 编辑检查任务
 * 
 * @function editInspect
 * @param {Task} item - 要编辑的任务对象
 * @returns {void}
 * @description 将指定任务的数据填充到编辑表单中,进入编辑模式
 * 
 * @example
 * // 点击编辑按钮
 * <el-button @click="editInspect(task)">编辑</el-button>
 * 
 * @since 1.0.0
 */
export function editInspect(item: Task) {
  dialogVisible.value = true;
  editId.value = item.id;
 
  // 转换后端返回的数字状态为前端使用的字符串状态
  const statusMap = {
    1: TASK_STATUS.DRAFT,
    2: TASK_STATUS.PENDING,
    3: TASK_STATUS.IN_PROGRESS,
    4: TASK_STATUS.COMPLETED,
    5: TASK_STATUS.REVIEWED,
    6: TASK_STATUS.AUTHORIZED,
    7: TASK_STATUS.TERMINATED,
    8: TASK_STATUS.ARCHIVED
  };
  
  // 填充表单数据
  form.value = {
    product_name: item.product_name || '',
    test_property: item.test_property || '',
    test_basis: item.test_basis || '',
    model_spec: item.model_spec || '',
    package_form: item.package_form || '',
    prod_batch: item.prod_batch || '',
    detect_batch: item.detect_batch || '',
    product_count: item.product_count || 1,
    manager_id: item.manager_id || 0,
    drafter_id: item.drafter_id || 0,
    reviewer_id: item.reviewer_id || 0,
    approver_id: item.approver_id || 0,
    subtasks: item.subtasks && item.subtasks.length > 0 ? [...item.subtasks] : [{ item: '', condition: '' }],
    remark: item.remark || '',
    status: statusMap[item.status] || item.status
  };
}
 
/**
 * 删除任务
 * 
 * @async
 * @function deleteInspect
 * @param {number} id - 要删除的任务ID
 * @returns {void}
 * @description 显示确认对话框,用户确认后删除指定的检查任务
 * 
 * @example
 * // 在任务列表中点击删除按钮
 * <el-button @click="deleteInspect(task.id)">删除</el-button>
 * 
 * @throws {Error} 当API请求失败时会显示错误消息
 * @since 1.0.0
 * 
 * @see {@link fetchTasks} - 刷新任务列表
 */
/**
 * 打开审核对话框
 * 
 * @function openReviewDialog
 * @param {Task} task - 要审核的任务
 * @returns {void}
 */
export function openReviewDialog(task: Task) {
  currentReviewTask.value = task;
  reviewDialogVisible.value = true;
}
 
/**
 * 关闭审核对话框
 * 
 * @function closeReviewDialog
 * @returns {void}
 */
export function closeReviewDialog() {
  reviewDialogVisible.value = false;
  currentReviewTask.value = null;
}
 
/**
 * 打开授权对话框
 * 
 * @function openAuthorizeDialog
 * @param {Task} task - 要授权的任务
 * @returns {void}
 */
export function openAuthorizeDialog(task: Task) {
  currentReviewTask.value = task;
  authorizeDialogVisible.value = true;
}
 
/**
 * 关闭授权对话框
 * 
 * @function closeAuthorizeDialog
 * @returns {void}
 */
export function closeAuthorizeDialog() {
  authorizeDialogVisible.value = false;
  currentReviewTask.value = null;
}
 
/**
 * 提交审核意见
 * 
 * @function submitReview
 * @param {string} comment - 审核意见
 * @returns {void}
 */
export function submitReview(comment: string) {
  if (!currentReviewTask.value) return;
 
  // 这里应该调用后端API提交审核意见
  // 模拟API调用
  console.log('提交审核意见:', comment);
 
  // 更新任务状态
  const taskId = currentReviewTask.value.id;
  taskAPI.updateTask(taskId, { comment })
    .then(() => {
      ElMessage.success('审核提交成功');
      closeReviewDialog();
      fetchTasks(); // 刷新任务列表
    })
    .catch((error) => {
      console.error('审核提交失败:', error);
      ElMessage.error('审核提交失败,请重试');
    });
}
 
/**
 * 提交授权信息
 * 
 * @function submitAuthorization
 * @param {any} authData - 授权信息
 * @returns {void}
 */
export function submitAuthorization(authData: any) {
  if (!currentReviewTask.value) return;
 
  // 这里应该调用后端API提交授权信息
  // 模拟API调用
  console.log('提交授权信息:', authData);
 
  // 更新任务状态
  const taskId = currentReviewTask.value.id;
  taskAPI.updateTask(taskId, authData)
    .then(() => {
      ElMessage.success('授权成功');
      closeAuthorizeDialog();
      fetchTasks(); // 刷新任务列表
    })
    .catch((error) => {
      console.error('授权失败:', error);
      ElMessage.error('授权失败,请重试');
    });
}
 
/**
 * 查看任务详情
 * 
 * @function viewTaskDetail
 * @param {Task} task - 要查看的任务
 * @returns {void}
 */
export function viewTaskDetail(task: Task) {
  // 这里可以打开一个任务详情对话框
  console.log('查看任务详情:', task);
  ElMessage.info('查看任务详情功能待实现');
}
 
export function deleteInspect(id: number) {
  ElMessageBox.confirm(
    '确定要删除这个任务单吗?此操作不可恢复。',
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    taskAPI.deleteTask(id)
      .then(() => {
        console.log('删除任务成功:', id);
        fetchTasks(); // 刷新列表
        ElMessage.success('任务单删除成功!');
      })
      .catch(error => {
        console.error('删除任务失败:', error);
 
        const errorMsg = error.response?.data?.message || '删除失败,请重试';
        ElMessage.error(errorMsg);
      });
  }).catch(() => {
    // 用户取消删除,什么都不做
  });
}