Преглед на файлове

feat(util): 辅助定额方法

zhangweicheng преди 4 години
родител
ревизия
70ab5347c8
променени са 7 файла, в които са добавени 130 реда и са изтрити 32 реда
  1. 1 0
      util/.eslintrc.js
  2. 5 2
      util/package.json
  3. 1 0
      util/prettier.config.js
  4. 4 17
      util/src/bill.ts
  5. 1 0
      util/src/index.ts
  6. 3 13
      util/src/math.ts
  7. 115 0
      util/src/rationAss.ts

+ 1 - 0
util/.eslintrc.js

@@ -26,6 +26,7 @@ module.exports = {
     'import/no-unresolved': 'off',
     '@typescript-eslint/no-empty-function': 'off',
     '@typescript-eslint/no-explicit-any': 'off',
+    '@typescript-eslint/explicit-module-boundary-types': 'off',
     'no-unused-expressions': [
       'error',
       {

+ 5 - 2
util/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@sc/util",
-  "version": "1.0.2",
+  "version": "1.0.3",
   "description": "共用的工具包",
   "main": "./dist/index.cjs.js",
   "module": "./dist/index.esm.js",
@@ -24,6 +24,7 @@
     "@types/chai": "^4.2.14",
     "@types/mocha": "^8.0.4",
     "@types/ms": "^0.7.31",
+    "@types/lodash": "^4.14.168",
     "@typescript-eslint/eslint-plugin": "^4.4.1",
     "@typescript-eslint/parser": "^4.4.1",
     "chai": "^4.2.0",
@@ -46,5 +47,7 @@
     "tslib": "^2.0.3",
     "typescript": "^4.0.3"
   },
-  "dependencies": {}
+  "dependencies": {
+    "lodash": "^4.17.21"
+  }
 }

+ 1 - 0
util/prettier.config.js

@@ -3,4 +3,5 @@ module.exports = {
   trailingComma: 'es5', // 对象末尾以逗号结束
   arrowParens: 'avoid', // 箭头函数只有一个参数的时候,不使用()
   endOfLine: 'auto', // CRLF,LF都可以
+  printWidth: 120,
 };

+ 4 - 17
util/src/bill.ts

@@ -1,10 +1,4 @@
-import {
-  IJobContent,
-  IItemCharacter,
-  IStdBill,
-  IStdJobContent,
-  IStdItemCharacter,
-} from '@sc/types';
+import { IJobContent, IItemCharacter, IStdBill, IStdJobContent, IStdItemCharacter } from '@sc/types';
 
 interface IJobsAndCharacterText {
   jobContentText: string;
@@ -34,8 +28,7 @@ export const getJobsAndCharacterText = (
   const textArray: string[] = ['[项目特征]'];
   let i = 1;
   for (const item of itemCharacters) {
-    if (item.isChecked)
-      textArray.push(`${i}. ${item.character || ''}: ${item.eigenvalue || ''}`);
+    if (item.isChecked) textArray.push(`${i}. ${item.character || ''}: ${item.eigenvalue || ''}`);
     i += 1;
   }
 
@@ -49,10 +42,7 @@ export const getJobsAndCharacterText = (
   return { jobContentText, itemCharacterText };
 };
 
-export const getJobContents = (
-  stdBill: IStdBill,
-  stdJobContentMap: IJobContentMap
-): IJobContent[] => {
+export const getJobContents = (stdBill: IStdBill, stdJobContentMap: IJobContentMap): IJobContent[] => {
   const jobs: IJobContent[] = [];
   let i = 1;
   for (const j of stdBill.jobs) {
@@ -66,10 +56,7 @@ export const getJobContents = (
   return jobs;
 };
 
-export const getItemCharacters = (
-  stdBill: IStdBill,
-  itemCharacterMap: IItemCharacterMap
-): IItemCharacter[] => {
+export const getItemCharacters = (stdBill: IStdBill, itemCharacterMap: IItemCharacterMap): IItemCharacter[] => {
   const characters: IItemCharacter[] = [];
   let i = 1;
   for (const item of stdBill.items) {

+ 1 - 0
util/src/index.ts

@@ -1,2 +1,3 @@
 export * from './math';
 export * from './bill';
+export * from './rationAss';

+ 3 - 13
util/src/math.ts

@@ -5,11 +5,7 @@ export const isNumber = (value: string | number): boolean => {
   return /^(-|\+)?\d+(\.\d+)?$/.test(value as string);
 };
 
-export const innerRound = (
-  num: number,
-  length: number,
-  decimal: number
-): number => {
+export const innerRound = (num: number, length: number, decimal: number): number => {
   let value;
   let pre = 1;
   if (length === 0) return num;
@@ -25,10 +21,7 @@ export const innerRound = (
 };
 
 // 4舍5入
-export const roundForObj = (
-  obj: string | number | undefined | null,
-  decimal: number
-): number => {
+export const roundForObj = (obj: string | number | undefined | null, decimal: number): number => {
   let value;
   if (obj === undefined || obj === null || isNaN(obj as number)) return 0;
   const length = 10;
@@ -40,10 +33,7 @@ export const roundForObj = (
   return value;
 };
 
-export const roundToString = (
-  obj: string | number,
-  decimal: number
-): string => {
+export const roundToString = (obj: string | number, decimal: number): string => {
   const value = roundForObj(obj, decimal);
   return value.toFixed(decimal);
 };

+ 115 - 0
util/src/rationAss.ts

@@ -0,0 +1,115 @@
+import { IRationAss, IThirdRation } from '@sc/types';
+import { ceil, floor, round, sortBy } from 'lodash';
+import { roundForObj } from './math';
+
+export interface IAssTime {
+  times: number;
+  ass: IRationAss | IThirdRation;
+}
+
+export interface IStateList {
+  index: number;
+  content: string;
+  addCode?: string; // 添加工料机的编号,用来标记,如果是子目换算中的单个人材机生成的,改成 调:...
+}
+
+export const stateSeq = {
+  ass: 1,
+  replace: 2,
+  coe: 3,
+  add: 4,
+  cusQuantity: 5,
+  cusCoe: 6,
+  area: 7,
+  adjMak: 8,
+};
+// 处理多个辅助定额的情况
+export const handleMulAss = (rationAssList: IRationAss[]) => {
+  const assList = [];
+  for (const ass of rationAssList) {
+    // 处理多个辅助定额的情况
+    if (ass.groupList && ass.groupList.length > 0) {
+      const newAss = { ...ass };
+      const newList = sortBy(ass.groupList, item => parseFloat(item.param)); // 按参数排序
+      let pre = 0;
+      for (const tem of newList) {
+        if (ass.actualValue) {
+          if (ass.actualValue > pre && ass.actualValue <= parseFloat(tem.param)) {
+            // 落在中间,则用组里的这条定额
+            newAss.param = tem.param;
+            newAss.paramName = tem.paramName;
+            newAss.assistCode = tem.assistCode;
+            break;
+          }
+          pre = parseFloat(tem.param);
+        }
+      }
+      assList.push(newAss);
+    } else {
+      assList.push(ass);
+    }
+  }
+  return assList;
+};
+
+export const calcAssTime = (ass: IRationAss) => {
+  if (ass.isAdjust === false) return undefined;
+  const actualValue = ass.actualValue ? ass.actualValue : 0;
+  const stdValue = ass.stdValue ? parseInt(ass.stdValue, 10) : 0;
+  const stepValue = ass.stepValue ? parseInt(ass.stepValue, 10) : 1;
+  let times = (actualValue - stdValue) / stepValue;
+  let r = false; // 是否负数
+  if (times < 0) {
+    r = true;
+    times *= -1;
+  }
+
+  if (ass.carryBit === '四舍五入') {
+    times = round(times, ass.decimal);
+  } else if (ass.carryBit === '进一') {
+    times = ceil(times, ass.decimal);
+  } else if (ass.carryBit === '舍一') {
+    times = floor(times, ass.decimal);
+  }
+  if (r) {
+    times *= -1;
+  }
+  return roundForObj(times, 6);
+};
+
+export const calcRationAssTimes = (
+  rationAssList?: IRationAss[],
+  thirdRation?: IThirdRation,
+  stateList?: IStateList[]
+) => {
+  const timeList: IAssTime[] = [];
+  if (rationAssList) {
+    const assList = handleMulAss(rationAssList);
+    const temTimes = [];
+    const thirdRationCodes = [];
+    for (const rationAss of assList) {
+      const times = calcAssTime(rationAss);
+      if (times) {
+        const { thirdRationCode } = rationAss;
+        if (thirdRationCode) {
+          temTimes.push(times);
+          thirdRationCodes.push(thirdRationCode);
+        }
+        timeList.push({ times, ass: rationAss });
+        if (stateList)
+          stateList.push({
+            index: stateSeq.ass,
+            content: `${rationAss.name} ${rationAss.actualValue} : ${rationAss.assistCode}x${times}`,
+          });
+      }
+    }
+    if (thirdRation && temTimes.length === 2 && thirdRationCodes[0] === thirdRationCodes[1]) {
+      // 说明有第三定额
+      const timesT = temTimes[0] * temTimes[1];
+      timeList.push({ times: timesT, ass: thirdRation });
+      if (stateList) stateList.push({ index: stateSeq.ass, content: `+${thirdRationCodes[0]}x${timesT}` });
+    }
+  }
+
+  return timeList;
+};