health-index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. function gather(
  2. data: App.Health.Index.Data[],
  3. transform: (model: App.Health.Index.Model) => any,
  4. skip = false,
  5. link = { 血压: ['收缩压', '舒张压'], }
  6. ) {
  7. const ref = ((link) => {
  8. const ref: Record<string, { id: string; name: string; }> = {};
  9. Object.entries(link).forEach(([name, keys]) => {
  10. const _ = { id: name, name };
  11. keys.forEach(key => { ref[key] = _; })
  12. })
  13. return ref;
  14. })(link);
  15. const cache = new Map<{ id: string; name: string; }, App.Health.Index.Ruler[]>();
  16. for (const item of data) {
  17. if (skip && item.isAuto === 'Y') continue;
  18. const model = createHealthIndex(item);
  19. const _ = ref[model.name] ?? { id: model.id, name: model.name };
  20. if (ref[model.id]) model.name = model.name.replace(_.name, '');
  21. if (cache.has(_)) {
  22. cache.get(_)?.push(transform(model))
  23. } else {
  24. cache.set(_, [transform(model)])
  25. }
  26. }
  27. return [...cache].map(gather => ({ ...gather[0], options: gather[1] }))
  28. }
  29. export function healthIndex2Ruler(model: App.Health.Index.Model): App.Health.Index.Ruler {
  30. let { scope, range, precision, values, ...ruler } = model;
  31. const min = range?.[0] ?? Math.floor(scope[0] * 0.5);
  32. const max = range?.[1] ?? Math.floor(scope[1] * 1.5);
  33. return {
  34. ...ruler, min, max, precision,
  35. markLine: [
  36. { value: scope[0], style: {} },
  37. { value: scope[1], style: {} },
  38. ],
  39. value: values?.slice(-1)[0]?.value,
  40. }
  41. }
  42. export function healthIndex2Progress(model: App.Health.Index.Model[]): AnyObject[] {
  43. return model.filter((item) => item.values?.length).map(item => {
  44. const { scope, range, values } = item;
  45. const { value, abnormal, description = '' } = values?.slice(-1)[0]!
  46. const length = range[1] - range[0];
  47. const scopeOffsetLeft = `${Math.floor((scope[0] - range[0]) * 100 / length)}%`;
  48. const scopeOffsetRight = `${100 - Math.floor((scope[1] - range[0]) * 100 / length)}%`;
  49. let valueOffset = Math.floor((value - range[0]) * 100 / length);
  50. let valueOffsetLeft = '0';
  51. let valueOffsetRight = '0';
  52. let valueType;
  53. if (value > scope[1]) {
  54. valueType = 'big';
  55. valueOffsetLeft = scopeOffsetLeft;
  56. valueOffsetRight = `${100 - valueOffset}%`;
  57. } else if (value < scope[0]) {
  58. valueType = 'small';
  59. valueOffsetLeft = `${valueOffset}%`;
  60. valueOffsetRight = scopeOffsetRight;
  61. } else {
  62. valueType = 'normal';
  63. valueOffsetLeft = scopeOffsetLeft;
  64. valueOffsetRight = scopeOffsetRight;
  65. }
  66. return {
  67. name: item.name, unit: item.unit,
  68. value, min: scope[0], max: scope[1],
  69. abnormal, description: description?.replace(item.name, ''),
  70. scopeOffsetLeft, scopeOffsetRight,
  71. valueType, valueOffset: `${valueOffset}%`,
  72. valueOffsetLeft, valueOffsetRight,
  73. }
  74. })
  75. }
  76. export function createHealthIndex(data: App.Health.Index.Data): App.Health.Index.Model {
  77. const { quotaId: id, minVal, maxVal, inputMin, inputMax, inputPrecision, patientQuotaRecordDTOS, ...model } = data;
  78. const scope = [+minVal, +maxVal] as const;
  79. const range = [
  80. +inputMin! || Math.floor(scope[0] * 0.75),
  81. +inputMax! || Math.floor(scope[1] * 1.25),
  82. ]
  83. return {
  84. ...model, id,
  85. precision: ({ 0: 0.01, 1: 0.1, 2: 2, 3: 1 } as any)[inputPrecision ?? 3],
  86. range, scope,
  87. values: patientQuotaRecordDTOS?.map(item => {
  88. return { value: +item.quotaVal, abnormal: item.abnormal, description: item.abnormalDesc, date: item.time2 }
  89. })?.filter(item => !!item.value),
  90. } as App.Health.Index.Model
  91. }
  92. export function transformHealthIndex2Ruler(data: App.Health.Index.Data[]): { id: string, name: string, options: App.Health.Index.Ruler[] }[] {
  93. return gather(data, healthIndex2Ruler, true);
  94. }
  95. export function healthIndex2Chart(model: App.Health.Index.Model): AnyObject {
  96. return {
  97. name: model.name,
  98. type: 'line', smooth: true,
  99. data: model.values?.map(t => [t.date, t.value]),
  100. markLine: {
  101. data: model.scope.map(value => { return { yAxis: value } })
  102. }
  103. }
  104. }
  105. export function transformHealthIndex2Chart(data: App.Health.Index.Data[]): AnyObject[] {
  106. const list = gather(data, healthIndex2Chart)
  107. const charts = [];
  108. for (const item of list) {
  109. const xAxis = { type: 'category', axisLabel: { overflow: 'breakAll' } }
  110. const yAxis = { type: 'value', scale: true, }
  111. const series = item.options.filter((item: any) => item.data?.length)
  112. if (!series.length) continue;
  113. charts.push({
  114. id: item.id,
  115. title: { text: `${item.name}` },
  116. tooltip: { trigger: 'axis' },
  117. legend: { show: series.length > 1 },
  118. xAxis, yAxis,
  119. series,
  120. })
  121. }
  122. return charts;
  123. }