facades.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/6/1
  7. * @version
  8. */
  9. import mongoose from 'mongoose';
  10. import CompilationModel from "../../users/models/compilation_model";
  11. import moment from 'moment';
  12. const uuidV1 = require('uuid/v1');
  13. const billsLibModel = mongoose.model('std_bills_lib_list');
  14. const billsGuideLibModel = mongoose.model('std_billsGuidance_lib');
  15. const billsGuideItemsModel = mongoose.model('std_billsGuidance_items');
  16. const stdBillsLibModel = mongoose.model('std_bills_lib_list');
  17. const stdBillsModel = mongoose.model('std_bills_lib_bills');
  18. const stdBillsJobsModel = mongoose.model('std_bills_lib_jobContent');
  19. const stdRationModel = mongoose.model('std_ration_lib_ration_items');
  20. const _ = require('lodash');
  21. module.exports = {
  22. getLibWithBills,
  23. getItemsBybills,
  24. getItemsByCode,
  25. };
  26. async function getBillsGuideLibs(findData) {
  27. return await billsGuideLibModel.find(findData);
  28. }
  29. async function getLibWithBills(libID){
  30. /*let guidanceLib = await getBillsGuideLibs({ID: libID});
  31. if(guidanceLib.length === 0){
  32. throw '不存在此指引库!';
  33. }*/
  34. let billsLib = await stdBillsLibModel.findOne({billsLibId: libID});
  35. if(!billsLib){
  36. throw '引用的清单规则库不存在!';
  37. }
  38. let bills = await stdBillsModel.find({billsLibId: billsLib.billsLibId}, '-_id').lean();
  39. //return {guidanceLib: guidanceLib[0], bills};
  40. return {bills};
  41. }
  42. function getAttrs(field, datas){
  43. let rst = [];
  44. for(let data of datas){
  45. if(data[field]){
  46. rst.push(data[field]);
  47. }
  48. }
  49. return rst;
  50. }
  51. //定额项目指所引用定额是否被删除
  52. function rationAllExist(rationItems, stdRationIdx) {
  53. for(let item of rationItems){
  54. if(!stdRationIdx[item.rationID]){
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. //将同层树结构转为顺序数组
  61. function chainToArr(nodes){
  62. let rst = [];
  63. let tempIdx = {};
  64. let nodeIdx = {};
  65. //建索引
  66. for(let node of nodes){
  67. tempIdx[node.ID] = {ID: node.ID, NextSiblingID: node.NextSiblingID, preSibling: null, nextSibling: null};
  68. nodeIdx[node.ID] = node;
  69. }
  70. //建链
  71. for(let i in tempIdx){
  72. let temp = tempIdx[i];
  73. if(temp.NextSiblingID != -1){
  74. let next = tempIdx[temp.NextSiblingID];
  75. temp.nextSibling = next;
  76. next.preSibling = temp;
  77. }
  78. }
  79. let firstNode = null;
  80. for(let i in tempIdx){
  81. if(!tempIdx[i].preSibling){
  82. firstNode = tempIdx[i];
  83. break;
  84. }
  85. }
  86. //获得顺序队列
  87. while(firstNode){
  88. rst.push(nodeIdx[firstNode.ID]);
  89. firstNode = firstNode.nextSibling;
  90. }
  91. return rst;
  92. }
  93. async function getItemsBybills(guidanceLibID, billsID){
  94. const type = {job: 0, ration: 1};
  95. let items = await billsGuideItemsModel.find({libID: guidanceLibID, billsID: billsID, deleted: false});
  96. let rationItems = _.filter(items, {type: type.ration});
  97. let rationIds = getAttrs('rationID', rationItems);
  98. let stdRations = await stdRationModel.find({ID: {$in: rationIds}, $or: [{isDeleted: null}, {isDeleted: false}]});
  99. let stdRationIndex = {};
  100. for(let stdRation of stdRations){
  101. stdRationIndex[stdRation.ID] = stdRation;
  102. }
  103. //判断定额完整性
  104. if(!rationAllExist(rationItems, stdRationIndex)){
  105. //建定额链, 排序后再清除不存在的定额,保证顺序正确性
  106. rationItems = chainToArr(rationItems);
  107. //清除已被删除的定额
  108. let removeIds = [];
  109. _.remove(rationItems, function (item) {
  110. if(!stdRationIndex[item.rationID]){
  111. removeIds.push(item.ID);
  112. return true;
  113. }
  114. return false;
  115. });
  116. _.remove(items, function (item) {
  117. return removeIds.includes(item.ID);
  118. });
  119. await billsGuideItemsModel.remove({ID: {$in: removeIds}});
  120. //重组树结构
  121. let bulkArr = [];
  122. for(let i = 0, len = rationItems.length; i < len; i++){
  123. rationItems[i].NextSiblingID = rationItems[i + 1] ? rationItems[i + 1].ID : -1;
  124. bulkArr.push({updateOne: {filter: {ID: rationItems[i].ID}, update: {$set: {NextSiblingID: rationItems[i].NextSiblingID}}}});
  125. }
  126. await billsGuideItemsModel.bulkWrite(bulkArr);
  127. }
  128. return items;
  129. }
  130. async function getItemsByCode(guidanceLibID, code){
  131. let rst = [];
  132. let guidanceLib = await billsGuideLibModel.findOne({ID: guidanceLibID});
  133. if (!guidanceLib) {
  134. return rst;
  135. }
  136. let stdBills = await stdBillsModel.findOne({code: code, billsLibId: guidanceLib.billsLibId});
  137. if (!stdBills) {
  138. return rst;
  139. }
  140. let items = await getItemsBybills(guidanceLibID, stdBills.ID);
  141. return items;
  142. }