Advertisement
vddastranger

Untitled

May 9th, 2021
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @ts-ignore
  2. import * as _ from 'lodash/fp';
  3.  
  4. enum CalculationType { // тип расчета
  5.   UMK,
  6.   TUB,
  7.   SBR,
  8. }
  9.  
  10. enum AxisQuantity { // количество осей
  11.   two = 2,
  12.   three,
  13.   four,
  14. }
  15.  
  16. enum MaterialTypes { // типы материалов
  17.   nailsWithCount, // гвозди 6мм, штуки
  18.   nailsWithWeight, // гвозди 6мм, кг
  19.   typicalThrustBar, // типовой упорный брусок
  20.   typicalSideBar// типовой боковой брусок
  21. }
  22.  
  23. export const MATERIAL_TYPES = [
  24.   MaterialTypes.nailsWithCount,
  25.   MaterialTypes.nailsWithWeight,
  26.   MaterialTypes.typicalThrustBar,
  27.   MaterialTypes.typicalSideBar,
  28. ];
  29.  
  30. export const TYPE_MAP = {
  31.   [MaterialTypes.nailsWithCount]: 'гвозди 6мм, штуки',
  32.   [MaterialTypes.nailsWithWeight]: 'гвозди 6мм, кг',
  33.   [MaterialTypes.typicalThrustBar]: 'типовой упорный брусок',
  34.   [MaterialTypes.typicalSideBar]: 'типовой боковой брусок',
  35.  
  36. }
  37.  
  38. export const AXIS_TYPES = [AxisQuantity.two, AxisQuantity.three, AxisQuantity.four];
  39.  
  40. interface Machine {
  41.   hasBrakes: boolean;
  42.   weight: { from: number; to: number };
  43.   calculationType: CalculationType;
  44.   [AxisQuantity.two]?: number;
  45.   [AxisQuantity.three]?: number;
  46.   [AxisQuantity.four]?: number;
  47. }
  48.  
  49. interface MachineData {
  50.   weight: { from: number; to: number };
  51.   materials: { [key in MaterialTypes]: { [key in AxisQuantity]: number } }
  52. }
  53.  
  54. // РАСЧЕТЫ ТУБ
  55.  
  56. const MATERIALS_DATA: MachineData[] = [
  57.   {
  58.     weight: {
  59.       from: 0,
  60.       to: 2,
  61.     },
  62.     materials: {
  63.       [MaterialTypes.nailsWithCount]: {
  64.         [AxisQuantity.two]: 40,
  65.         [AxisQuantity.three]: 40,
  66.         [AxisQuantity.four]: 40,
  67.       },
  68.       [MaterialTypes.nailsWithWeight]: {
  69.         [AxisQuantity.two]: 1.8,
  70.         [AxisQuantity.three]: 1.8,
  71.         [AxisQuantity.four]: 1.8,
  72.       },
  73.       [MaterialTypes.typicalThrustBar]: {
  74.         [AxisQuantity.two]: 4,
  75.         [AxisQuantity.three]: 4,
  76.         [AxisQuantity.four]: 4,
  77.       },
  78.       [MaterialTypes.typicalSideBar]: {
  79.         [AxisQuantity.two]: 4,
  80.         [AxisQuantity.three]: 4,
  81.         [AxisQuantity.four]: 4,
  82.       },
  83.     },
  84.   },
  85.   {
  86.     weight: {
  87.       from: 2.1,
  88.       to: 4,
  89.     },
  90.     materials: {
  91.       [MaterialTypes.nailsWithCount]: {
  92.         [AxisQuantity.two]: 40,
  93.         [AxisQuantity.three]: 40,
  94.         [AxisQuantity.four]: 40,
  95.       },
  96.       [MaterialTypes.nailsWithWeight]: {
  97.         [AxisQuantity.two]: 1.8,
  98.         [AxisQuantity.three]: 1.8,
  99.         [AxisQuantity.four]: 1.8,
  100.       },
  101.       [MaterialTypes.typicalThrustBar]: {
  102.         [AxisQuantity.two]: 4,
  103.         [AxisQuantity.three]: 4,
  104.         [AxisQuantity.four]: 4,
  105.       },
  106.       [MaterialTypes.typicalSideBar]: {
  107.         [AxisQuantity.two]: 4,
  108.         [AxisQuantity.three]: 4,
  109.         [AxisQuantity.four]: 4,
  110.       },
  111.     },
  112.   },
  113.   {
  114.     weight: {
  115.       from: 4.1,
  116.       to: 5.5,
  117.     },
  118.     materials: {
  119.       [MaterialTypes.nailsWithCount]: {
  120.         [AxisQuantity.two]: 40,
  121.         [AxisQuantity.three]: 40,
  122.         [AxisQuantity.four]: 40,
  123.       },
  124.       [MaterialTypes.nailsWithWeight]: {
  125.         [AxisQuantity.two]: 1.8,
  126.         [AxisQuantity.three]: 1.8,
  127.         [AxisQuantity.four]: 1.8,
  128.       },
  129.       [MaterialTypes.typicalThrustBar]: {
  130.         [AxisQuantity.two]: 4,
  131.         [AxisQuantity.three]: 4,
  132.         [AxisQuantity.four]: 4,
  133.       },
  134.       [MaterialTypes.typicalSideBar]: {
  135.         [AxisQuantity.two]: 4,
  136.         [AxisQuantity.three]: 4,
  137.         [AxisQuantity.four]: 4,
  138.       },
  139.     },
  140.   },
  141.   {
  142.     weight: {
  143.       from: 5.6,
  144.       to: 6.3,
  145.     },
  146.     materials: {
  147.       [MaterialTypes.nailsWithCount]: {
  148.         [AxisQuantity.two]: 64,
  149.         [AxisQuantity.three]: 64,
  150.         [AxisQuantity.four]: 64,
  151.       },
  152.       [MaterialTypes.nailsWithWeight]: {
  153.         [AxisQuantity.two]: 2.9,
  154.         [AxisQuantity.three]: 2.9,
  155.         [AxisQuantity.four]: 2.9,
  156.       },
  157.       [MaterialTypes.typicalThrustBar]: {
  158.         [AxisQuantity.two]: 8,
  159.         [AxisQuantity.three]: 8,
  160.         [AxisQuantity.four]: 8,
  161.       },
  162.       [MaterialTypes.typicalSideBar]: {
  163.         [AxisQuantity.two]: 4,
  164.         [AxisQuantity.three]: 4,
  165.         [AxisQuantity.four]: 4,
  166.       },
  167.     },
  168.   },
  169.   {
  170.     weight: {
  171.       from: 6.4,
  172.       to: 12,
  173.     },
  174.     materials: {
  175.       [MaterialTypes.nailsWithCount]: {
  176.         [AxisQuantity.two]: 64,
  177.         [AxisQuantity.three]: 64,
  178.         [AxisQuantity.four]: 64,
  179.       },
  180.       [MaterialTypes.nailsWithWeight]: {
  181.         [AxisQuantity.two]: 2.9,
  182.         [AxisQuantity.three]: 2.9,
  183.         [AxisQuantity.four]: 2.9,
  184.       },
  185.       [MaterialTypes.typicalThrustBar]: {
  186.         [AxisQuantity.two]: 8,
  187.         [AxisQuantity.three]: 8,
  188.         [AxisQuantity.four]: 8,
  189.       },
  190.       [MaterialTypes.typicalSideBar]: {
  191.         [AxisQuantity.two]: 4,
  192.         [AxisQuantity.three]: 4,
  193.         [AxisQuantity.four]: 4,
  194.       },
  195.     },
  196.   },
  197. ];
  198.  
  199. const MACHINES: Machine[] = [
  200.   {
  201.     weight: {
  202.       from: 0,
  203.       to: 2,
  204.     },
  205.     calculationType: CalculationType.TUB,
  206.     hasBrakes: true,
  207.     [AxisQuantity.two]: 4,
  208.     [AxisQuantity.three]: 5,
  209.     [AxisQuantity.four]: 6,
  210.   },
  211.   {
  212.     weight: {
  213.       from: 2.1,
  214.       to: 4,
  215.     },
  216.     calculationType: CalculationType.TUB,
  217.     hasBrakes: true,
  218.     [AxisQuantity.two]: 5,
  219.     [AxisQuantity.three]: 0,
  220.     [AxisQuantity.four]: 0,
  221.   },
  222.   {
  223.     weight: {
  224.       from: 4.1,
  225.       to: 5.5,
  226.     },
  227.     calculationType: CalculationType.TUB,
  228.     hasBrakes: true,
  229.     [AxisQuantity.two]: 6,
  230.     [AxisQuantity.three]: 0,
  231.     [AxisQuantity.four]: 4,
  232.   },
  233.   {
  234.     weight: {
  235.       from: 5.6,
  236.       to: 6.3,
  237.     },
  238.     calculationType: CalculationType.TUB,
  239.     hasBrakes: true,
  240.     [AxisQuantity.two]: 0,
  241.     [AxisQuantity.three]: 7,
  242.     [AxisQuantity.four]: 0,
  243.   },
  244.   {
  245.     weight: {
  246.       from: 6.4,
  247.       to: 12,
  248.     },
  249.     calculationType: CalculationType.TUB,
  250.     hasBrakes: true,
  251.     [AxisQuantity.two]: 1,
  252.     [AxisQuantity.three]: 3,
  253.     [AxisQuantity.four]: 2,
  254.   },
  255.   {
  256.     weight: {
  257.       from: 12.1,
  258.       to: 40,
  259.     },
  260.     hasBrakes: true,
  261.     calculationType: CalculationType.SBR,
  262.   },
  263.   {
  264.     weight: {
  265.       from: 0,
  266.       to: 10,
  267.     },
  268.     hasBrakes: false,
  269.     calculationType: CalculationType.SBR,
  270.   },
  271. ];
  272.  
  273. type TubResult = { [key in MaterialTypes]: number } & { weight?: { from: number; to: number } };
  274.  
  275. // функция, расчитывающая промежуточный результат для одной машины
  276. export function TUB(machine: Machine, data: MachineData): TubResult {
  277.   const result: TubResult = {
  278.     weight: machine.weight,
  279.     [MaterialTypes.nailsWithCount]: 0,
  280.     [MaterialTypes.nailsWithWeight]: 0,
  281.     [MaterialTypes.typicalThrustBar]: 0,
  282.     [MaterialTypes.typicalSideBar]: 0,
  283.   };
  284.  
  285.   MATERIAL_TYPES
  286.     .forEach(materialKey => {
  287.       result[materialKey] = AXIS_TYPES.reduce((prev, axis) => {
  288.         return prev + data.materials[materialKey][axis] * machine[axis];
  289.       }, 0);
  290.     });
  291.  
  292.   return result;
  293. }
  294.  
  295. // главная функция для расчета ТУБ для всех машин
  296. function getTubResults(machines: Machine[], materialsData: MachineData[]): { [key: string]: number } {
  297.  
  298.   // промежуточные результаты для каждой из машин
  299.   const tubResults: TubResult[] = [];
  300.  
  301.   machines.forEach(machine => {
  302.     materialsData.forEach(data => {
  303.       if (_.isEqual(machine.weight, data.weight)) {
  304.         tubResults.push(TUB(machine, data));
  305.       }
  306.     });
  307.   });
  308.  
  309.   // сумма всех промежуточных в финальный результат
  310.   const tubFinalResult: { [key: string]: number } = {
  311.   };
  312.  
  313.   MATERIAL_TYPES.forEach(materialKey => {
  314.     tubFinalResult[TYPE_MAP[materialKey]] = tubResults.reduce((prev, result) => {
  315.       return prev + result[materialKey];
  316.     }, 0);
  317.   });
  318.  
  319.   return tubFinalResult;
  320. }
  321.  
  322. console.log(getTubResults(MACHINES, MATERIALS_DATA));
  323. /*
  324.   Вывод результата расчетов:
  325.   гвозди 6мм, кг: 91.69999999999999
  326.   гвозди 6мм, штуки: 2032
  327.   типовой боковой брусок: 172
  328.   типовой упорный брусок: 224
  329. * */
  330.  
  331.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement