facades.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. getComBillsLibInfo,
  23. getBillsGuideLibs,
  24. initBillsGuideLib,
  25. updateBillsGuideLib,
  26. getLibWithBills,
  27. getItemsBybills,
  28. updateItems
  29. };
  30. async function getCompilationList() {
  31. let compilationModel = new CompilationModel();
  32. return await compilationModel.getCompilationList();
  33. }
  34. async function getComBillsLibInfo() {
  35. let rst = [];
  36. let compilationList = await getCompilationList();
  37. if(compilationList.length <= 0){
  38. throw '没有数据';
  39. }
  40. else{
  41. for(let compilation of compilationList){
  42. let billsLibs = await billsLibModel.find({compilationId: compilation._id, deleted: false}, '-_id billsLibId billsLibName');
  43. rst.push({_id: compilation._id, name: compilation.name, billsLibs: billsLibs});
  44. }
  45. return rst;
  46. }
  47. }
  48. async function getBillsGuideLibs(findData) {
  49. return await billsGuideLibModel.find(findData);
  50. }
  51. //拷贝工作内容并转化为树结构,形成项目指引数据
  52. async function genGuidanceItems(guidanceLibId, billsLibId){
  53. let bills = await stdBillsModel.find({billsLibId: billsLibId, deleted: false, 'jobs.0': {$exists: true}});
  54. //设置工作内容数据
  55. let jobIds = [];
  56. let totalJobs = [];
  57. for(let bill of bills){
  58. for(let job of bill.jobs){
  59. jobIds.push(job.id);
  60. }
  61. }
  62. jobIds = Array.from(new Set(jobIds));
  63. if(jobIds.length > 0){
  64. totalJobs = await stdBillsJobsModel.find({deleted: false, id: {$in: jobIds}});
  65. }
  66. if(totalJobs.length > 0){
  67. let jobIdIndex = {};//id索引
  68. for(let job of totalJobs){
  69. jobIdIndex[job.id] = job;
  70. }
  71. let insertArr = [];
  72. for(let bill of bills){
  73. //排序后根据serialNo转换成NextSiblingID,倒序
  74. bill.jobs.sort(function (a, b) {
  75. let rst = 0;
  76. if(a.serialNo > b.serialNo){
  77. rst = -1;
  78. }
  79. else if(a.serialNo < b.serialNo){
  80. rst = 1;
  81. }
  82. return rst;
  83. });
  84. let jobNoIndex = {};//下标索引
  85. for(let i = 0; i < bill.jobs.length; i++){
  86. let newItem = {libID: guidanceLibId, ID: uuidV1(), ParentID: -1, NextSiblingID: jobNoIndex[i - 1] ? jobNoIndex[i - 1]['ID'] : -1,
  87. name: jobIdIndex[bill.jobs[i]['id']]['content'], type: 0, billsID: bill.ID};
  88. jobNoIndex[i] = newItem;
  89. insertArr.push({insertOne: {document: newItem}});
  90. }
  91. }
  92. await billsGuideItemsModel.bulkWrite(insertArr);
  93. }
  94. }
  95. async function initBillsGuideLib(updateData){
  96. await billsGuideLibModel.create(updateData);
  97. await genGuidanceItems(updateData.ID, updateData.billsLibId);
  98. }
  99. async function updateBillsGuideLib(data) {
  100. await billsGuideLibModel.update(data.findData, {$set: data.updateData});
  101. if(data.updateType === 'delete'){
  102. //假删除所有条目
  103. await billsGuideItemsModel.update({libID: data.findData.ID}, {$set: {deleted: true}}, {multi: true});
  104. }
  105. }
  106. async function getLibWithBills(libID){
  107. let guidanceLib = await getBillsGuideLibs({ID: libID, deleted: false});
  108. if(guidanceLib.length === 0){
  109. throw '不存在此指引库!';
  110. }
  111. let billsLib = await stdBillsLibModel.findOne({billsLibId: guidanceLib[0].billsLibId, deleted: false});
  112. if(!billsLib){
  113. throw '引用的清单规则库不存在!';
  114. }
  115. let bills = await stdBillsModel.find({billsLibId: billsLib.billsLibId, deleted: false}, '-_id code name ID NextSiblingID ParentID');
  116. return {guidanceLib: guidanceLib[0], bills};
  117. }
  118. function getAttrs(field, datas){
  119. let rst = [];
  120. for(let data of datas){
  121. if(data[field]){
  122. rst.push(data[field]);
  123. }
  124. }
  125. return rst;
  126. }
  127. //定额项目指所引用定额是否被删除
  128. function rationAllExist(rationItems, stdRationIdx) {
  129. for(let item of rationItems){
  130. if(!stdRationIdx[item.rationID]){
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. //将同层树结构转为顺序数组
  137. function chainToArr(nodes){
  138. let rst = [];
  139. let tempIdx = {};
  140. let nodeIdx = {};
  141. //建索引
  142. for(let node of nodes){
  143. tempIdx[node.ID] = {ID: node.ID, NextSiblingID: node.NextSiblingID, preSibling: null, nextSibling: null};
  144. nodeIdx[node.ID] = node;
  145. }
  146. //建链
  147. for(let i in tempIdx){
  148. let temp = tempIdx[i];
  149. if(temp.NextSiblingID != -1){
  150. let next = tempIdx[temp.NextSiblingID];
  151. temp.nextSibling = next;
  152. next.preSibling = temp;
  153. }
  154. }
  155. let firstNode = null;
  156. for(let i in tempIdx){
  157. if(!tempIdx[i].preSibling){
  158. firstNode = tempIdx[i];
  159. break;
  160. }
  161. }
  162. //获得顺序队列
  163. while(firstNode){
  164. rst.push(nodeIdx[firstNode.ID]);
  165. firstNode = firstNode.nextSibling;
  166. }
  167. return rst;
  168. }
  169. async function getItemsBybills(guidanceLibID, billsID){
  170. const type = {job: 0, ration: 1};
  171. let items = await billsGuideItemsModel.find({libID: guidanceLibID, billsID: billsID, deleted: false});
  172. let rationItems = _.filter(items, {type: type.ration});
  173. let rationIds = getAttrs('rationID', rationItems);
  174. let stdRations = await stdRationModel.find({ID: {$in: rationIds}, $or: [{isDeleted: null}, {isDeleted: false}]});
  175. let stdRationIndex = {};
  176. for(let stdRation of stdRations){
  177. stdRationIndex[stdRation.ID] = stdRation;
  178. }
  179. //判断定额完整性
  180. if(!rationAllExist(rationItems, stdRationIndex)){
  181. //建定额链, 排序后再清除不存在的定额,保证顺序正确性
  182. rationItems = chainToArr(rationItems);
  183. //清除已被删除的定额
  184. let removeIds = [];
  185. _.remove(rationItems, function (item) {
  186. if(!stdRationIndex[item.rationID]){
  187. removeIds.push(item.ID);
  188. return true;
  189. }
  190. return false;
  191. });
  192. _.remove(items, function (item) {
  193. return removeIds.includes(item.ID);
  194. });
  195. await billsGuideItemsModel.remove({ID: {$in: removeIds}});
  196. //重组树结构
  197. let bulkArr = [];
  198. for(let i = 0, len = rationItems.length; i < len; i++){
  199. rationItems[i].NextSiblingID = rationItems[i + 1] ? rationItems[i + 1].ID : -1;
  200. bulkArr.push({updateOne: {filter: {ID: rationItems[i].ID}, update: {$set: {NextSiblingID: rationItems[i].NextSiblingID}}}});
  201. }
  202. await billsGuideItemsModel.bulkWrite(bulkArr);
  203. }
  204. return items;
  205. }
  206. async function updateItems(updateDatas) {
  207. let bulkArr = [];
  208. for(let updateData of updateDatas){
  209. if(updateData.updateType === 'create'){
  210. bulkArr.push({insertOne: {document: updateData.updateData}});
  211. }
  212. else{
  213. bulkArr.push({updateOne: {filter: updateData.findData, update: {$set: updateData.updateData}}});
  214. }
  215. }
  216. if(bulkArr.length > 0){
  217. await billsGuideItemsModel.bulkWrite(bulkArr);
  218. }
  219. }