bill.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { IJobContent, IItemCharacter, IStdBill, IStdJobContent, IStdItemCharacter } from '@sc/types';
  2. interface IJobsAndCharacterText {
  3. jobContentText: string;
  4. itemCharacterText: string;
  5. onlyItemCharacterText: string;
  6. onlyJobContentText: string;
  7. }
  8. interface IJobContentMap {
  9. [id: string]: IStdJobContent;
  10. }
  11. interface IItemCharacterMap {
  12. [id: string]: IStdItemCharacter;
  13. }
  14. export interface IJobAndCharacter extends IJobsAndCharacterText {
  15. jobContents: IJobContent[];
  16. itemCharacters: IItemCharacter[];
  17. }
  18. export const getJobsAndCharacterText = (
  19. jobContents: IJobContent[],
  20. itemCharacters: IItemCharacter[],
  21. divided = false // 分别对应
  22. ): IJobsAndCharacterText => {
  23. let jobContentText = '';
  24. let itemCharacterText = '';
  25. let onlyJobContentText = '';
  26. let onlyItemCharacterText = '';
  27. jobContents.sort((a, b) => a.seq - b.seq);
  28. itemCharacters.sort((a, b) => a.seq - b.seq);
  29. const characterTextArray: string[] = divided ? [] : ['[项目特征]'];
  30. let i = 1;
  31. for (const item of itemCharacters) {
  32. if (item.isChecked) characterTextArray.push(`${i}. ${item.character || ''}: ${item.eigenvalue || ''}`);
  33. i += 1;
  34. }
  35. onlyItemCharacterText = characterTextArray.join('\n');
  36. const jobTextArray = divided ? [] : ['[工作内容]'];
  37. i = 1;
  38. for (const job of jobContents) {
  39. if (job.isChecked) jobTextArray.push(`${i}. ${job.content || ''}`);
  40. i += 1;
  41. }
  42. onlyJobContentText = jobTextArray.join('\n');
  43. jobContentText = divided ? onlyJobContentText : '';
  44. itemCharacterText = divided ? onlyItemCharacterText : `${onlyItemCharacterText}\n${onlyJobContentText}`;
  45. return { jobContentText, itemCharacterText, onlyItemCharacterText, onlyJobContentText };
  46. };
  47. export const getJobContents = (stdBill: IStdBill, stdJobContentMap: IJobContentMap): IJobContent[] => {
  48. const jobs: IJobContent[] = [];
  49. let i = 1;
  50. for (const j of stdBill.jobs) {
  51. const job = stdJobContentMap[j.id];
  52. if (job) {
  53. jobs.push({ seq: i + 1, content: job.content, isChecked: true });
  54. i += 1;
  55. }
  56. }
  57. return jobs;
  58. };
  59. export const getItemCharacters = (stdBill: IStdBill, itemCharacterMap: IItemCharacterMap): IItemCharacter[] => {
  60. const characters: IItemCharacter[] = [];
  61. let i = 1;
  62. for (const item of stdBill.items) {
  63. const character = itemCharacterMap[item.id];
  64. if (character) {
  65. const newItem: IItemCharacter = {
  66. seq: i + 1,
  67. character: character.content,
  68. eigenvalueList: [],
  69. eigenvalue: '',
  70. isChecked: false,
  71. };
  72. const eigenvalues = character.itemValue;
  73. for (let j = 0; j < eigenvalues.length; j += 1) {
  74. newItem.eigenvalueList.push(eigenvalues[j].value);
  75. }
  76. characters.push(newItem);
  77. i += 1;
  78. }
  79. }
  80. return characters;
  81. };
  82. // 对于含有中文的清单编号,分离出中文和数字
  83. export const separateBillCode = (code: string) => {
  84. const result = code.match(/^([\u4e00-\u9fa5]+)(\d+)/);
  85. if (result && result.length === 3) {
  86. const pre = result[1];
  87. const subCode = result[2];
  88. return { pre, code: subCode };
  89. }
  90. return { pre: '', code };
  91. };
  92. /**
  93. *
  94. * @param sourceCode 清单ID
  95. *
  96. * 返回: isStd是否来自标准清单 code:标准清单编号
  97. */
  98. export const matchAndGetStdBillCode = (sourceCode: string) => {
  99. const { pre, code } = separateBillCode(sourceCode);
  100. if (code.length === 12) {
  101. return { isStd: true, code: pre + code.substring(0, 9) };
  102. }
  103. if (code.length === 9) {
  104. return { isStd: true, code: pre + code };
  105. }
  106. return { isStd: false, code: sourceCode };
  107. };
  108. // 取标准清单编号
  109. export const getStdBillCode = (sourceCode: string) => {
  110. const { isStd, code } = matchAndGetStdBillCode(sourceCode);
  111. if (isStd) {
  112. return code;
  113. }
  114. return '';
  115. };