util.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. export const formatTime = (date: Date) => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return (
  9. [year, month, day].map(formatNumber).join('/') +
  10. ' ' +
  11. [hour, minute, second].map(formatNumber).join(':')
  12. )
  13. }
  14. const formatNumber = (n: number) => {
  15. const s = n.toString()
  16. return s[1] ? s : '0' + s
  17. }
  18. export function groupBy<T>(items: Iterable<T>, callbackFn: (element: T, index: number) => any): Record<string, T[]> {
  19. const obj = Object.create(null);
  20. let k = 0;
  21. for (const value of items) {
  22. const key = callbackFn(value, k++);
  23. if (key in obj) { obj[key].push(value); } else { obj[key] = [value]; }
  24. }
  25. return obj;
  26. }
  27. export const HealthReportSymptomItemConfig = {
  28. 有一点: 2.0,
  29. 偶尔: 2.0,
  30. 轻: 2.0,
  31. 有些: 3.0,
  32. 有时: 3.0,
  33. 中: 3.0,
  34. 相当: 4.0,
  35. 经常: 4.0,
  36. 重: 4.0,
  37. 非常: 5.0,
  38. 总是: 5.0,
  39. 非常重: 5.0,
  40. } as const;
  41. export interface HealthReportSymptomItemVo {
  42. /**
  43. * 症状ID
  44. */
  45. id: string;
  46. /**
  47. * 症状名称
  48. */
  49. name: string;
  50. /**
  51. * 症状描述
  52. */
  53. label: string;
  54. /**
  55. * 症状得分
  56. */
  57. value: number;
  58. }
  59. export interface HealthReportSymptomVo {
  60. items: HealthReportSymptomItemVo[];
  61. value?: string;
  62. duration?: string;
  63. influence?: string;
  64. }
  65. export interface HealthReportDTO {
  66. tonguefaceAnalysisReportId: string;
  67. healthAnalysisReportId: string;
  68. reportTime: string;
  69. pickedSymptomList?: { id: string; name: string; value: string; score: number }[];
  70. pickedSymptom?: string;
  71. duration?: string;
  72. influenceDegree?: string;
  73. willillStateName: string;
  74. willillDegreeName: string;
  75. willillFunctionName: string;
  76. willillSocialName: string;
  77. constitutionGroupName: string;
  78. constitutionGroupDefinition: string;
  79. factorItemSummary: string;
  80. diagnoseSyndromeSummary: string;
  81. }
  82. export function fromHealthReportSymptom(data: Partial<HealthReportDTO>, config = HealthReportSymptomItemConfig): HealthReportSymptomVo {
  83. const result: HealthReportSymptomVo = {
  84. value: data.pickedSymptom,
  85. items:
  86. data.pickedSymptomList?.map(({ id, name, ...item }) => ({
  87. id,
  88. name,
  89. label: item.value,
  90. value: +item.score,
  91. })) ?? [],
  92. duration: data.duration,
  93. influence: data.influenceDegree,
  94. };
  95. if (!result.items?.length && result.value) {
  96. const matches = result.value?.matchAll(/([^,(]+)(([^)]+))/g) ?? [];
  97. for (const [_, name, label] of matches) {
  98. // @ts-ignore
  99. const value = config[label] ?? 0;
  100. result.items.push({ id: name, name, label, value });
  101. }
  102. }
  103. return result;
  104. }