ration_item.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. const mongoose = require('mongoose');
  5. let async = require("async");
  6. let moment = require('moment');
  7. let counter = require('../../../public/counter/counter');
  8. let gljDao = require('./glj_repository');
  9. let rationRepositoryDao = require('./repository_map');
  10. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  11. const rationItemModel = mongoose.model('std_ration_lib_ration_items');
  12. const stdRationLibModel = mongoose.model('std_ration_lib_map');
  13. const compleRationModel = mongoose.model('complementary_ration_items');
  14. import STDGLJListModel from '../../std_glj_lib/models/gljModel';
  15. import InstallationDao from '../models/installation';
  16. const installationDao = new InstallationDao();
  17. import GljDao from "../../std_glj_lib/models/gljModel";
  18. const stdGljDao = new GljDao();
  19. import stdgljutil from "../../../public/cache/std_glj_type_util";
  20. var rationItemDAO = function(){};
  21. /* rationItemDAO.prototype.copyLib = async function (sourceLibID, targetLibID) {
  22. // coe-list
  23. const coeIDMap = {};
  24. const newCoeData = [];
  25. const sourceCoeData = await _stdRationCoeModel.find({ libID: sourceLibID }, '-_id').lean();
  26. const coeCount = await counter.counterDAO.getIDAfterCount(counter.moduleName.coeList, sourceCoeData.length);
  27. const coeIdx = coeCount.sequence_value - (sourceCoeData.length - 1);
  28. sourceCoeData.forEach((coe, index) => {
  29. coeIDMap[coe.ID] = coeIdx + index;
  30. newCoeData.push({
  31. ...coe,
  32. libID: targetLibID,
  33. ID: coeIDMap[coe.ID]
  34. });
  35. });
  36. await stdRationCoeModel.insertMany(newCoeData);
  37. // ration-section
  38. const sectionIDMap = {};
  39. const newSectionData = [];
  40. const sourceSectionData = await _stdRationSectionModel.find({ rationRepId: sourceLibID }, '-_id').lean();
  41. const sectionCount = await counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, sourceSectionData.length);
  42. const sectionIdx = sectionCount.sequence_value - (sourceSectionData.length - 1);
  43. sourceSectionData.forEach((section, index) => {
  44. sectionIDMap[section.ID] = sectionIdx + index;
  45. });
  46. sourceSectionData.forEach(section => {
  47. newSectionData.push({
  48. ...section,
  49. rationRepId: targetLibID,
  50. ID: sectionIDMap[section.ID],
  51. ParentID: sectionIDMap[section.ParentID] || -1,
  52. NextSiblingID: sectionIDMap[section.NextSiblingID] || -1
  53. });
  54. });
  55. await stdRationSectionModel.insertMany(newSectionData);
  56. // glj
  57. const sourceGLJData = await stdGLJModel.find({ repositoryId: 182 }, '-_id').lean();
  58. const gljIDMap = {};
  59. sourceGLJData.forEach(glj => {
  60. gljIDMap[glj.orgID] = glj.ID;
  61. });
  62. const newGLJData = sourceGLJData.map(glj => {
  63. delete glj.orgID;
  64. return glj;
  65. });
  66. await stdGLJModel.remove({ repositoryId: 182 }); // 5412
  67. await stdGLJModel.insertMany(newGLJData);
  68. // ration
  69. const newRationData = [];
  70. const sourceRationData = await _rationItemModel.find({ rationRepId: sourceLibID }, '-_id').lean();
  71. const rationCount = await counter.counterDAO.getIDAfterCount(counter.moduleName.rations, sourceRationData.length);
  72. const rationIdx = rationCount.sequence_value - (sourceRationData.length - 1);
  73. sourceRationData.forEach((ration, index) => {
  74. const rationID = rationIdx + index;
  75. const sectionID = sectionIDMap[ration.sectionId];
  76. const newRationCoeList = (ration.rationCoeList || []).map(rCoe => ({
  77. no: rCoe.no,
  78. ID: coeIDMap[rCoe.ID]
  79. }));
  80. const newRationGLJList = (ration.rationGljList || []).map(rGLJ => ({
  81. ...rGLJ,
  82. gljId: gljIDMap[rGLJ.gljId],
  83. }));
  84. newRationData.push({
  85. ...ration,
  86. rationRepId: targetLibID,
  87. ID: rationID,
  88. sectionId: sectionID,
  89. rationCoeList: newRationCoeList,
  90. rationGljList: newRationGLJList
  91. });
  92. });
  93. await rationItemModel.insertMany(newRationData);
  94. };
  95. */
  96. // 由于导入excel时,excel数据存在负的工程量,所以导入后一些定额人材机的消耗量可能为负,需要处理
  97. rationItemDAO.prototype.handleMinusQuantity = async function() {
  98. const updateTask = [];
  99. const repIDs = new Set();
  100. const rations = await rationItemModel.find({'rationGljList.consumeAmt': {$lt: 0}}).lean();
  101. for (const ration of rations) {
  102. repIDs.add(ration.rationRepId);
  103. const rationGLJList = [];
  104. for (const rGLJ of ration.rationGljList) {
  105. rationGLJList.push({
  106. gljId: rGLJ.gljId,
  107. consumeAmt: Math.abs(rGLJ.consumeAmt),
  108. proportion: rGLJ.proportion
  109. });
  110. }
  111. updateTask.push({
  112. updateOne: {
  113. filter: { ID: ration.ID },
  114. update: { $set: { rationGljList: rationGLJList } }
  115. }
  116. });
  117. }
  118. if (updateTask.length) {
  119. await rationItemModel.bulkWrite(updateTask);
  120. }
  121. };
  122. rationItemDAO.prototype.prepareInitData = async function (rationRepId) {
  123. // 定额库
  124. const libTask = stdRationLibModel.findOne({ID: rationRepId}, '-_id ID dispName gljLib');
  125. // 定额编码
  126. const codesTask = rationItemModel.find({rationRepId}, '-_id code', {lean: true});
  127. // 定额章节树
  128. const sectionTreeTask = stdRationSectionModel.find({rationRepId}, '-_id', {lean: true});
  129. // 安装增加费
  130. const installationTask = installationDao.getInstallation(rationRepId);
  131. const [libInfo, codesArr, sectionTree, installationList] = await Promise.all([libTask, codesTask, sectionTreeTask, installationTask]);
  132. const rationsCodes = codesArr.reduce((acc, cur) => {
  133. acc.push(cur.code);
  134. return acc;
  135. }, []);
  136. // 人材机分类树
  137. const gljLibId = libInfo.gljLib;
  138. const gljTreeTask = stdGljDao.getGljTreeSync(gljLibId);
  139. const gljTask = stdGljDao.getGljItemsSync(gljLibId);
  140. const [gljTree, gljList] = await Promise.all([gljTreeTask, gljTask]);
  141. const gljDistTypeList = stdgljutil.getStdGljTypeCacheObj().toArray();
  142. return {
  143. libInfo,
  144. rationsCodes,
  145. sectionTree,
  146. installationList,
  147. gljTree,
  148. gljList,
  149. gljDistTypeList
  150. };
  151. };
  152. rationItemDAO.prototype.getRationItemsByLib = async function (rationRepId, showHint = null, returnFields = '') {
  153. let rationLib = await stdRationLibModel.findOne({ID: rationRepId, deleted: false});
  154. if(!rationLib){
  155. return [];
  156. }
  157. let startDate = new Date();
  158. let rations = await rationItemModel.find({rationRepId: rationRepId}, returnFields);
  159. console.log(`Date: ${new Date() - startDate}====================================`);
  160. if(!showHint){
  161. return rations;
  162. }
  163. else {
  164. const stdBillsLibListsModel = new STDGLJListModel();
  165. const stdGLJData = await stdBillsLibListsModel.getGljItemsByRepId(rationLib.gljLib, '-_id ID code name unit gljType');
  166. let gljMapping = {};
  167. for(let glj of stdGLJData){
  168. gljMapping[glj.ID] = glj;
  169. }
  170. //设置悬浮
  171. for(let ration of rations){
  172. let hintsArr = [];
  173. //对人材机进行排序
  174. ration.rationGljList.sort(function (a, b) {
  175. let gljA = gljMapping[a.gljId],
  176. gljB = gljMapping[b.gljId];
  177. if(gljA && gljB){
  178. let aV = gljA.gljType + gljA.code,
  179. bV = gljB.gljType + gljB.code;
  180. if(aV > bV) {
  181. return 1;
  182. } else if(aV < bV) {
  183. return -1;
  184. }
  185. }
  186. return 0;
  187. });
  188. for(let rationGlj of ration.rationGljList){
  189. let subGlj = gljMapping[rationGlj.gljId];
  190. if(subGlj){
  191. hintsArr.push(` ${subGlj.code} ${subGlj.name}${subGlj.specs ? '&nbsp;&nbsp;&nbsp;' + subGlj.specs : ''}&nbsp;&nbsp&nbsp;${subGlj.unit}&nbsp;&nbsp;&nbsp;${rationGlj.consumeAmt}`);
  192. }
  193. }
  194. hintsArr.push(`基价 元 ${ration.basePrice}`);
  195. if(ration.jobContent && ration.jobContent.toString().trim() !== ''){
  196. hintsArr.push(`工作内容:`);
  197. hintsArr = hintsArr.concat(ration.jobContent.split('\n'));
  198. }
  199. if(ration.annotation && ration.annotation.toString().trim() !== ''){
  200. hintsArr.push(`附注:`);
  201. hintsArr = hintsArr.concat(ration.annotation.split('\n'));
  202. }
  203. ration._doc.hint = hintsArr.join('<br>');
  204. }
  205. return rations;
  206. }
  207. };
  208. rationItemDAO.prototype.sortToNumber = function (datas) {
  209. for(let i = 0, len = datas.length; i < len; i++){
  210. let data = datas[i]._doc;
  211. if(_exist(data, 'labourPrice')){
  212. data['labourPrice'] = parseFloat(data['labourPrice']);
  213. }
  214. if(_exist(data, 'materialPrice')){
  215. data['materialPrice'] = parseFloat(data['materialPrice']);
  216. }
  217. if(_exist(data, 'machinePrice')){
  218. data['machinePrice'] = parseFloat(data['machinePrice']);
  219. }
  220. if(_exist(data, 'basePrice')){
  221. data['basePrice'] = parseFloat(data['basePrice']);
  222. }
  223. if(_exist(data, 'rationGljList')){
  224. for(let j = 0, jLen = data['rationGljList'].length; j < jLen; j++){
  225. let raGljObj = data['rationGljList'][j]._doc;
  226. if(_exist(raGljObj, 'consumeAmt')){
  227. raGljObj['consumeAmt'] = parseFloat(raGljObj['consumeAmt']);
  228. }
  229. }
  230. }
  231. }
  232. function _exist(data, attr){
  233. return data && data[attr] !== undefined && data[attr];
  234. }
  235. };
  236. rationItemDAO.prototype.getRationItemsBySection = async function(rationRepId, sectionId,callback){
  237. let me = this;
  238. try {
  239. let rations = await rationItemModel.find({rationRepId: rationRepId, sectionId: sectionId});
  240. me.sortToNumber(rations);
  241. let matchRationIDs = [],
  242. matchRations = [];
  243. for (let ration of rations) {
  244. if (ration.rationTemplateList) {
  245. for (let rt of ration.rationTemplateList) {
  246. if (rt.rationID) {
  247. matchRationIDs.push(rt.rationID);
  248. }
  249. }
  250. }
  251. }
  252. if (matchRationIDs.length > 0) {
  253. matchRations = await rationItemModel.find({ID: {$in: matchRationIDs}}, '-_id ID code name');
  254. }
  255. for (let mr of matchRations) {
  256. for (let ration of rations) {
  257. if (ration.rationTemplateList) {
  258. for (let rt of ration.rationTemplateList) {
  259. if (rt.rationID && rt.rationID === mr.ID) {
  260. rt.code = mr.code ? mr.code : '';
  261. rt.name = mr.name ? mr.name : '';
  262. }
  263. }
  264. }
  265. }
  266. }
  267. callback(false,"Get items successfully", rations);
  268. } catch (err) {
  269. console.log(err);
  270. callback(true, "Fail to get items", "");
  271. }
  272. /* rationItemModel.find({"rationRepId": rationRepId, "sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  273. if(err) callback(true, "Fail to get items", "");
  274. else {
  275. me.sortToNumber(data);
  276. callback(false,"Get items successfully", data);
  277. }
  278. })*/
  279. };
  280. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, lastOpr, sectionId, updateItems, addItems, rIds, callback){
  281. var me = this;
  282. if (updateItems.length == 0 && rIds.length == 0) {
  283. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  284. } else {
  285. me.removeRationItems(rationLibId, lastOpr, rIds, function(err, message, docs) {
  286. if (err) {
  287. callback(true, "Fail to remove", false);
  288. } else {
  289. me.updateRationItems(rationLibId, lastOpr, sectionId, updateItems, function(err, results){
  290. if (err) {
  291. callback(true, "Fail to save", false);
  292. } else {
  293. if (addItems && addItems.length > 0) {
  294. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  295. } else {
  296. callback(false, "Save successfully", results);
  297. }
  298. }
  299. });
  300. }
  301. })
  302. }
  303. };
  304. rationItemDAO.prototype.removeRationItems = function(rationLibId, lastOpr, rIds,callback){
  305. if (rIds.length > 0) {
  306. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  307. if (err) {
  308. callback(true, "Fail to remove", false);
  309. } else {
  310. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  311. if(!err){
  312. rationItemModel.update({rationRepId: rationLibId}, {$pull: {rationTemplateList: {rationID: {$in: rIds}}}}, function (theErr) {
  313. if (!theErr) {
  314. callback(false, "Remove successfully", docs);
  315. } else {
  316. callback(true, "Fail to remove", false);
  317. }
  318. });
  319. } else {
  320. callback(true, "Fail to remove", false);
  321. }
  322. })
  323. }
  324. })
  325. } else {
  326. callback(false, "No records were deleted!", null);
  327. }
  328. };
  329. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  330. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  331. if(err) callback(true, "Fail to get items", "")
  332. else callback(false,"Get items successfully", data);
  333. })
  334. };
  335. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  336. var filter = {
  337. 'rationRepId': repId,
  338. '$and': [{
  339. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  340. }, {
  341. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  342. }]
  343. };
  344. rationItemModel.find(filter, function (err, data) {
  345. if (err) {
  346. callback(true, 'Fail to find ration', null);
  347. } else {
  348. callback(false, '', data);
  349. }
  350. })
  351. }
  352. rationItemDAO.prototype.getRationItem = async function (repId, code) {
  353. let ration = await rationItemModel.findOne({rationRepId: repId, code: code});
  354. return ration;
  355. };
  356. rationItemDAO.prototype.addRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  357. if (items && items.length > 0) {
  358. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  359. var maxId = result.sequence_value;
  360. var arr = [];
  361. for (var i = 0; i < items.length; i++) {
  362. var obj = new rationItemModel(items[i]);
  363. obj.ID = (maxId - (items.length - 1) + i);
  364. obj.sectionId = sectionId;
  365. obj.rationRepId = rationLibId;
  366. arr.push(obj);
  367. }
  368. rationItemModel.collection.insert(arr, null, function(err, docs){
  369. if (err) {
  370. callback(true, "Fail to save", false);
  371. } else {
  372. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  373. if(err){
  374. callback(true, "Fail to sava operator", false);
  375. }
  376. else{
  377. callback(false, "Save successfully", docs);
  378. }
  379. })
  380. }
  381. })
  382. });
  383. } else {
  384. callback(true, "Source error!", false);
  385. }
  386. };
  387. rationItemDAO.prototype.updateRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  388. console.log('enter============');
  389. var functions = [];
  390. for (var i=0; i < items.length; i++) {
  391. functions.push((function(doc) {
  392. return function(cb) {
  393. var filter = {};
  394. if (doc.ID) {
  395. filter.ID = doc.ID;
  396. } else {
  397. filter.sectionId = sectionId;
  398. if (rationLibId) filter.rationRepId = rationLibId;
  399. filter.code = doc.code;
  400. }
  401. rationItemModel.update(filter, doc, cb);
  402. };
  403. })(items[i]));
  404. }
  405. functions.push((function () {
  406. return function (cb) {
  407. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  408. if(err){
  409. cb(err);
  410. }
  411. else{
  412. cb(null);
  413. }
  414. });
  415. }
  416. })());
  417. async.parallel(functions, function(err, results) {
  418. callback(err, results);
  419. });
  420. };
  421. //ration round func
  422. function round(v,e){
  423. var t=1;
  424. for(;e>0;t*=10,e--);
  425. for(;e<0;t/=10,e++);
  426. return Math.round(v*t)/t;
  427. }
  428. function calcRation(gljArr) {
  429. let labourPrc = [],
  430. materialPrc = [],
  431. machinePrc = [],
  432. managePrc = [],
  433. profitPrc = [],
  434. riskPrc = [],
  435. singlePrc,
  436. updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, managePrice: 0, profitPrice: 0, riskPrice: 0, basePrice: 0};
  437. gljArr.forEach(function (gljItem) {
  438. if(gljItem.gljParentType !== -1){
  439. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  440. if(gljItem.gljParentType === 1){
  441. labourPrc.push(singlePrc);
  442. }
  443. else if(gljItem.gljParentType ===2){
  444. materialPrc.push(singlePrc);
  445. }
  446. else if(gljItem.gljParentType === 3){
  447. machinePrc.push(singlePrc);
  448. }
  449. else if(gljItem.gljParentType === 6){
  450. managePrc.push(singlePrc);
  451. }
  452. else if(gljItem.gljParentType === 7){
  453. profitPrc.push(singlePrc);
  454. }
  455. else if(gljItem.gljParentType === 8){
  456. riskPrc.push(singlePrc);
  457. }
  458. }
  459. });
  460. if(labourPrc.length > 0){
  461. let sumLaP = 0;
  462. for(let i=0; i<labourPrc.length; i++){
  463. sumLaP = scMathUtil.roundTo(sumLaP + labourPrc[i], -6);
  464. }
  465. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  466. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.labourPrice, -2);
  467. }
  468. if(materialPrc.length > 0){
  469. let sumMtP = 0;
  470. for(let i= 0; i<materialPrc.length; i++){
  471. sumMtP = scMathUtil.roundTo(sumMtP + materialPrc[i], -6);
  472. }
  473. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  474. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.materialPrice, -2);
  475. }
  476. if(machinePrc.length > 0){
  477. let sumMaP = 0;
  478. for(let i =0; i< machinePrc.length; i++){
  479. sumMaP = scMathUtil.roundTo(sumMaP + machinePrc[i], -6);
  480. }
  481. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  482. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.machinePrice, -2);
  483. }
  484. if(managePrc.length > 0){
  485. let sumMgP = 0;
  486. for(let i =0; i< managePrc.length; i++){
  487. sumMgP = scMathUtil.roundTo(sumMgP + managePrc[i], -6);
  488. }
  489. updatePrc.managePrice = scMathUtil.roundTo(sumMgP, -2);
  490. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.managePrice, -2);
  491. }
  492. if(profitPrc.length > 0){
  493. let sumPfP = 0;
  494. for(let i =0; i< profitPrc.length; i++){
  495. sumPfP = scMathUtil.roundTo(sumPfP + profitPrc[i], -6);
  496. }
  497. updatePrc.profitPrice = scMathUtil.roundTo(sumPfP, -2);
  498. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.profitPrice, -2);
  499. }
  500. if(riskPrc.length > 0){
  501. let sumRkP = 0;
  502. for(let i =0; i< riskPrc.length; i++){
  503. sumRkP = scMathUtil.roundTo(sumRkP + riskPrc[i], -6);
  504. }
  505. updatePrc.riskPrice = scMathUtil.roundTo(sumRkP, -2);
  506. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.riskPrice, -2);
  507. }
  508. return updatePrc;
  509. }
  510. rationItemDAO.prototype.updateRationBasePrc = function (basePrcArr, overWriteUrl, callback) {
  511. async.each(basePrcArr, function (basePrcObj, finalCb) {
  512. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  513. async.waterfall([
  514. function (cb) {
  515. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  516. rationItemModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, result) {
  517. if(err){
  518. cb(err);
  519. }
  520. else{
  521. //删除
  522. rationItemModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  523. if(err){
  524. cb(err);
  525. }
  526. else{
  527. //补充定额
  528. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  529. if(err){
  530. cb(err);
  531. }
  532. else {
  533. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  534. if(err){
  535. cb(err);
  536. }
  537. else {
  538. for(let i = 0, len = compleRst.length; i < len; i++){
  539. compleRst[i]._doc.type = 'complementary';
  540. }
  541. cb(null, result.concat(compleRst));
  542. }
  543. });
  544. }
  545. });
  546. }
  547. });
  548. }
  549. });
  550. }
  551. else{
  552. rationItemModel.find({'rationGljList.gljId': adjGljId}, {ID: 1, rationGljList: 1}, function (err, result) {
  553. if(err){
  554. cb(err);
  555. }
  556. else{
  557. compleRationModel.find({'rationGljList.gljId': adjGljId}, {ID: 1, rationGljList: 1}, function (err, compleRst) {
  558. if(err){
  559. cb(err);
  560. }
  561. else {
  562. for(let i = 0, len = compleRst.length; i < len; i++){
  563. compleRst[i]._doc.type = 'complementary';
  564. }
  565. cb(null, result.concat(compleRst));
  566. }
  567. });
  568. }
  569. });
  570. }
  571. },
  572. function (result, cb) {
  573. let compleRTasks = [], stdRTasks = [];
  574. //重算时需要用到的所有工料机,一次性取
  575. let compleGljIds = [], stdGljIds = [];
  576. for(let ration of result){
  577. for(let glj of ration.rationGljList){
  578. if(glj.type !== undefined && glj.type === 'complementary'){
  579. compleGljIds.push(glj.gljId);
  580. }
  581. else {
  582. stdGljIds.push(glj.gljId);
  583. }
  584. }
  585. }
  586. gljDao.getStdCompleGljItems(compleGljIds, stdGljIds, function (err, allGljs) {
  587. if(err){
  588. cb(err);
  589. }
  590. else {
  591. let gljIndex = {};
  592. for(let glj of allGljs){
  593. gljIndex[glj.ID] = glj;
  594. }
  595. async.each(result, function (rationItem, ecb) {
  596. let rationGljList = rationItem.rationGljList;
  597. let gljArr = [];
  598. for(let i=0; i<rationGljList.length; i++){
  599. let theGlj = gljIndex[rationGljList[i].gljId];
  600. if(theGlj !== undefined && theGlj){
  601. let gljParentType = -1;
  602. if(theGlj.ID === adjGljId){
  603. theGlj.gljType = adjGljType;
  604. }
  605. if(theGlj.gljType <= 3){
  606. gljParentType = theGlj.gljType;
  607. } else if(theGlj.gljType > 200 && theGlj.gljType < 300){
  608. gljParentType = 2;
  609. } else if(theGlj.gljType > 300 && theGlj.gljType < 400){
  610. gljParentType = 3;
  611. } else if(theGlj.gljType === 6){ //管理费
  612. gljParentType = 6;
  613. } else if(theGlj.gljType === 7){ //利润
  614. gljParentType = 7;
  615. } else if(theGlj.gljType === 8){ //风险费
  616. gljParentType = 8;
  617. }
  618. if(theGlj)
  619. if(theGlj.ID === adjGljId){
  620. gljArr.push({gljId: theGlj.ID, basePrice: adjBasePrice, gljParentType: gljParentType, unit: theGlj.unit});
  621. } else {
  622. if(theGlj.priceProperty && Object.keys(theGlj.priceProperty).length > 0){
  623. gljArr.push({
  624. gljId: theGlj.ID,
  625. basePrice: parseFloat(theGlj.priceProperty.price1),
  626. gljParentType: gljParentType,
  627. unit: theGlj.unit});
  628. } else {
  629. gljArr.push({
  630. gljId: theGlj.ID,
  631. basePrice: parseFloat(theGlj.basePrice),
  632. gljParentType: gljParentType,
  633. unit: theGlj.unit});
  634. }
  635. }
  636. }
  637. }
  638. gljArr.forEach(function (gljItem) {
  639. rationGljList.forEach(function (rationGlj) {
  640. if(gljItem.gljId === rationGlj.gljId){
  641. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  642. }
  643. })
  644. });
  645. let updatePrc = null;
  646. let overWriteCalc = false; //需要重写算法
  647. if (overWriteUrl) {
  648. let overWriteExports = require(overWriteUrl);
  649. if (typeof overWriteExports.calcRation !== 'undefined') {
  650. overWriteCalc = true;
  651. updatePrc = overWriteExports.calcRation(gljArr, scMathUtil);
  652. }
  653. }
  654. if (!overWriteCalc) {
  655. updatePrc = calcRation(gljArr);
  656. }
  657. let task = {
  658. updateOne: {
  659. filter: {
  660. ID: rationItem.ID
  661. },
  662. update: {
  663. labourPrice: updatePrc.labourPrice.toString(),
  664. materialPrice: updatePrc.materialPrice.toString(),
  665. machinePrice: updatePrc.machinePrice.toString(),
  666. basePrice: updatePrc.basePrice.toString()
  667. }
  668. }
  669. };
  670. //updateDataBase
  671. if(rationItem._doc.type !== undefined && rationItem._doc.type === 'complementary'){
  672. compleRTasks.push(task);
  673. ecb(null);
  674. }
  675. else {
  676. stdRTasks.push(task);
  677. ecb(null);
  678. }
  679. }, async function(err){
  680. if(err){
  681. cb(err);
  682. }
  683. else {
  684. //do sth
  685. try{
  686. if(compleRTasks.length > 0){
  687. await compleRationModel.bulkWrite(compleRTasks);
  688. }
  689. if(stdRTasks.length > 0){
  690. await rationItemModel.bulkWrite(stdRTasks);
  691. }
  692. }
  693. catch(e){
  694. cb(err);
  695. }
  696. cb(null);
  697. }
  698. });
  699. }
  700. });
  701. },
  702. ], function (err) {
  703. if(err){
  704. finalCb(err);
  705. }
  706. else{
  707. finalCb(null);
  708. }
  709. });
  710. }, function (err) {
  711. if(err){
  712. callback(err, 'Error');
  713. }
  714. else{
  715. callback(null, '');
  716. }
  717. });
  718. };
  719. rationItemDAO.prototype.getRationGljIds = function (data, callback) {
  720. let repId = data.repId;
  721. rationItemModel.find({rationRepId: repId}, function (err, result) {
  722. if(err){
  723. callback(err, 'Error', null);
  724. }
  725. else{
  726. let rstIds = [], newRst = [];
  727. result.forEach(function (data) {
  728. if(data.rationGljList.length >0){
  729. data.rationGljList.forEach(function (gljObj) {
  730. rstIds.push(gljObj.gljId);
  731. })
  732. }
  733. });
  734. for(let i= 0; i< rstIds.length; i++){
  735. if(newRst.indexOf(rstIds[i]) === -1){
  736. newRst.push(rstIds[i]);
  737. }
  738. }
  739. callback(null, '', newRst);
  740. }
  741. });
  742. };
  743. rationItemDAO.prototype.getRationsCodes = function (data, callback) {
  744. let repId = data.repId;
  745. rationItemModel.find({rationRepId: repId, isDeleted: false}, function (err, result) {
  746. if(err){
  747. callback(err, 'Error', null);
  748. }
  749. else{
  750. let rstCodes = [];
  751. result.forEach(function (rationItem) {
  752. rstCodes.push(rationItem.code);
  753. });
  754. callback(null, 'get all rationCodes success', rstCodes);
  755. }
  756. })
  757. };
  758. rationItemDAO.prototype.updateJobContent = function (lastOpr, repId, updateArr, callback) {
  759. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  760. async.each(updateArr, function (obj, cb) {
  761. rationItemModel.update({rationRepId: repId, code: obj.code}, {$set: {jobContent: obj.jobContent}}, function (err) {
  762. if(err){
  763. cb(err);
  764. }
  765. else{
  766. cb(null);
  767. }
  768. })
  769. }, function (err) {
  770. if(err){
  771. callback(err);
  772. }
  773. else{
  774. callback(null);
  775. }
  776. });
  777. });
  778. }
  779. rationItemDAO.prototype.updateAnnotation = function (lastOpr, repId, updateArr, callback) {
  780. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  781. async.each(updateArr, function (obj, cb) {
  782. rationItemModel.update({rationRepId: repId, code: obj.code}, {$set: {annotation: obj.annotation}}, function (err) {
  783. if(err){
  784. cb(err);
  785. }
  786. else{
  787. cb(null);
  788. }
  789. })
  790. }, function (err) {
  791. if(err){
  792. callback(err);
  793. }
  794. else{
  795. callback(null);
  796. }
  797. });
  798. });
  799. };
  800. //更新定额下模板关联
  801. rationItemDAO.prototype.updateRationTemplate = async function (rationRepId, rationID, templateData) {
  802. //自动匹配定额
  803. let matachCodes = [],
  804. matchRations = [];
  805. //要保存的数据
  806. let saveData = [];
  807. for (let data of templateData) {
  808. if (data.code) {
  809. matachCodes.push(data.code);
  810. }
  811. }
  812. matachCodes = Array.from(new Set(matachCodes));
  813. if (matachCodes.length > 0) {
  814. matchRations = await rationItemModel.find({rationRepId: rationRepId, code: {$in: matachCodes}}, '-_id ID code name');
  815. }
  816. let validData = [];
  817. //设置展示数据
  818. for (let data of templateData) {
  819. let match = false;
  820. for (let ration of matchRations) {
  821. if (data.code && data.code === ration.code) {
  822. match = true;
  823. data.name = ration.name;
  824. data.rationID = ration.ID;
  825. break;
  826. }
  827. }
  828. if (!match) {
  829. data.code = '';
  830. data.name = '';
  831. }
  832. if (data.type || data.code || data.name || data.billsLocation) {
  833. validData.push(data);
  834. }
  835. }
  836. for (let data of validData) {
  837. saveData.push({rationID: data.rationID ? data.rationID : null, type: data.type, billsLocation: data.billsLocation});
  838. }
  839. //更新
  840. await rationItemModel.update({ID: rationID}, {$set: {rationTemplateList: saveData}});
  841. return validData;
  842. };
  843. // 根据章节列表批量更新定额
  844. rationItemDAO.prototype.updateRationBySection = async function (rationRepId, sectionList, updateData) {
  845. await rationItemModel.updateMany({rationRepId, sectionId: {$in: sectionList}}, updateData);
  846. }
  847. //计算导入数据的价格
  848. rationItemDAO.prototype.calcForRation = function (stdGljList, ration, overWriteUrl) {
  849. let rationGljList = ration.rationGljList,
  850. gljArr = [];
  851. for(let rationGlj of rationGljList) {
  852. let glj = stdGljList[rationGlj.gljId];
  853. let gljPType = parseInt(glj.gljType.toString().match(/\d+?/)[0]);
  854. let newGlj = {
  855. gljId: glj.ID,
  856. consumeAmt: rationGlj.consumeAmt,
  857. gljParentType: gljPType,
  858. unit: glj.unit
  859. };
  860. newGlj.basePrice = glj.priceProperty && Object.keys(glj.priceProperty).length > 0
  861. ? parseFloat(glj.priceProperty.price1)
  862. : parseFloat(glj.basePrice);
  863. gljArr.push(newGlj);
  864. }
  865. let updatePrc = null;
  866. let overWriteCalc = false; //需要重写算法
  867. if (overWriteUrl) {
  868. let overWriteExports = require(overWriteUrl);
  869. if (typeof overWriteExports.calcRation !== 'undefined') {
  870. overWriteCalc = true;
  871. updatePrc = overWriteExports.calcRation(gljArr, scMathUtil);
  872. }
  873. }
  874. if (!overWriteCalc) {
  875. updatePrc = calcRation(gljArr);
  876. }
  877. ration.labourPrice = updatePrc.labourPrice.toString();
  878. ration.materialPrice = updatePrc.materialPrice.toString();
  879. ration.machinePrice = updatePrc.machinePrice.toString();
  880. ration.basePrice = updatePrc.basePrice.toString();
  881. };
  882. /**
  883. * 根据条件获取定额数据
  884. *
  885. * @param {Object} condition
  886. * @param {Object} fields
  887. * @param {String} indexBy
  888. * @return {Promise|Array}
  889. */
  890. rationItemDAO.prototype.getRationItemByCondition = async function (condition, fields = null, indexBy = null) {
  891. let result = [];
  892. if (Object.keys(condition).length <= 0) {
  893. return result;
  894. }
  895. result = await rationItemModel.find(condition, fields).sort({code: 1});
  896. if (indexBy !== null && result.length > 0) {
  897. let tmpResult = {};
  898. for(let tmp of result) {
  899. tmpResult[tmp[indexBy]] = tmp;
  900. }
  901. result = tmpResult;
  902. }
  903. return result;
  904. };
  905. /**
  906. * 从excel中批量新增数据
  907. *
  908. * @param {Number} rationRepId
  909. * @param {Array} data
  910. * @return {bool}
  911. */
  912. rationItemDAO.prototype.batchAddFromExcel = async function(rationRepId, data) {
  913. if (data.length <= 0) {
  914. return false;
  915. }
  916. // 获取定额库相关数据
  917. const rationRepository = await rationRepositoryDao.getRepositoryById(rationRepId);
  918. if (rationRepository.length !== 1 || rationRepository[0].gljLib === undefined) {
  919. return false;
  920. }
  921. // 获取标准工料机库数据
  922. const stdBillsLibListsModel = new STDGLJListModel();
  923. const stdGLJData = await stdBillsLibListsModel.getGljItemsByRepId(rationRepository[0].gljLib);
  924. // 整理标准工料机库数据
  925. let stdGLJList = {};
  926. let stdGLJListByID = {};
  927. for (const tmp of stdGLJData) {
  928. stdGLJList[tmp.code.toString()] = tmp.ID;
  929. stdGLJListByID[tmp.ID] = tmp;
  930. }
  931. let lastData = {};
  932. // 定额xx下提示的次数
  933. let lastFailCount = 0;
  934. const rationData = [];
  935. // 编码列表,用于查找库中是否有对应数据
  936. let rationCodeList = [];
  937. let gljCodeList = [];
  938. // 插入失败的工料机列表(用于提示)
  939. let failGLJList = [];
  940. for (const tmp of data) {
  941. if (tmp.length <= 0) {
  942. continue;
  943. }
  944. // 如果第一个字段为null则是工料机数据,放入上一个数据的工料机字段
  945. if (tmp[0] === undefined && Object.keys(lastData).length > 0) {
  946. // 如果不存在对应的工料机库数据则跳过
  947. if (stdGLJList[tmp[1]] === undefined) {
  948. if (lastFailCount === 0) {
  949. failGLJList.push('定额' + lastData.code + '下的');
  950. lastFailCount++;
  951. }
  952. //const failString = '定额' + lastData.code + '下的' + tmp[1];
  953. failGLJList.push(tmp[1]);
  954. continue;
  955. }
  956. const tmpRationGlj = {
  957. gljId: stdGLJList[tmp[1]],
  958. consumeAmt: Math.abs(tmp[4]),
  959. proportion: 0,
  960. };
  961. lastData.rationGljList.push(tmpRationGlj);
  962. if (gljCodeList.indexOf(tmp[1]) < 0) {
  963. gljCodeList.push(tmp[1]);
  964. }
  965. continue;
  966. }
  967. if (tmp[0] === '定额' && Object.keys(lastData).length > 0) {
  968. rationData.push(lastData);
  969. }
  970. lastFailCount = 0;
  971. // 组装数据
  972. lastData = {
  973. code: tmp[1],
  974. name: tmp[2],
  975. unit: tmp[3],
  976. caption: tmp[2],
  977. rationRepId: rationRepId,
  978. sectionId: 0,
  979. labourPrice: '0',
  980. materialPrice: '0',
  981. machinePrice: '0',
  982. basePrice: '0',
  983. rationGljList: []
  984. };
  985. // 防止重复加入
  986. if (rationCodeList.indexOf(tmp[1]) < 0) {
  987. rationCodeList.push(tmp[1]);
  988. }
  989. }
  990. // 最后一个入数组
  991. rationData.push(lastData);
  992. // 查找数据库中是否已存在待插入数据
  993. const condition = {
  994. rationRepId: rationRepId,
  995. code: { $in: rationCodeList }
  996. };
  997. const existCodeList = await this.getRationItemByCondition(condition, ['code'], 'code');
  998. // 过滤插入数据
  999. let insertData = [];
  1000. //已存在定额,则更新价格及rationGLjList字段
  1001. let updateData = [];
  1002. for (const ration of rationData) {
  1003. if (existCodeList[ration.code] !== undefined) {
  1004. updateData.push(ration);
  1005. continue;
  1006. }
  1007. insertData.push(ration);
  1008. }
  1009. //更新定额
  1010. let updateBulk = [];
  1011. for(let ration of updateData){
  1012. this.calcForRation(stdGLJListByID, ration);
  1013. updateBulk.push({
  1014. updateOne: {
  1015. filter: {rationRepId: rationRepId, code: ration.code},
  1016. update: {$set: {rationGljList: ration.rationGljList, labourPrice: ration.labourPrice, materialPrice: ration.materialPrice,
  1017. machinePrice: ration.machinePrice, basePrice: ration.basePrice}}
  1018. }
  1019. });
  1020. }
  1021. //更新数据库
  1022. if(updateBulk.length > 0){
  1023. await rationItemModel.bulkWrite(updateBulk);
  1024. }
  1025. // 如果都已经存在,直接返回
  1026. if (insertData.length <= 0) {
  1027. return failGLJList;
  1028. }
  1029. //计算价格
  1030. for(let ration of insertData){
  1031. this.calcForRation(stdGLJListByID, ration);
  1032. }
  1033. // 组织id
  1034. const counterInfo = await counter.counterDAO.getIDAfterCount(counter.moduleName.rations, insertData.length);
  1035. let maxId = counterInfo.sequence_value;
  1036. maxId = parseInt(maxId);
  1037. let count = 0;
  1038. for (const index in insertData) {
  1039. insertData[index].ID = maxId - (insertData.length - 1) + count;
  1040. count++;
  1041. }
  1042. // 插入数据库
  1043. await rationItemModel.create(insertData);
  1044. return failGLJList;
  1045. };
  1046. /**
  1047. * 导出到excel的数据
  1048. *
  1049. * @param {Number} rationRepId
  1050. * @return {Array}
  1051. */
  1052. rationItemDAO.prototype.exportExcelData = async function(rationRepId) {
  1053. const condition = {
  1054. rationRepId: rationRepId
  1055. };
  1056. const rationDataList = await this.getRationItemByCondition(condition, ['name', 'code', 'ID', 'sectionId', 'feeType', 'caption', 'basePrice', 'jobContent', 'annotation']);
  1057. // 整理数据
  1058. let rationData = [];
  1059. for (const tmp of rationDataList) {
  1060. const sectionId = tmp.sectionId <= 0 || tmp.sectionId === undefined ? null : tmp.sectionId;
  1061. const feeType = tmp.feeType === '' || tmp.feeType === undefined ? null : tmp.feeType;
  1062. const ration = [sectionId, feeType, tmp.ID, tmp.code, tmp.name, tmp.caption, tmp.basePrice, tmp.jobContent, tmp.annotation];
  1063. rationData.push(ration);
  1064. }
  1065. const excelData = [['树ID', '取费专业', '定额ID', '定额编码', '定额名', '定额显示名称', '基价', '工作内容', '附注']];
  1066. excelData.push.apply(excelData, rationData);
  1067. return excelData;
  1068. //根据编号排序,优先级:number-number-..., number, Anumber....
  1069. /*let regConnector = /-/g,
  1070. regLetter = /[a-z,A-Z]/g,
  1071. regNum = /\d+/g;
  1072. rationData.sort(function (a, b) {
  1073. let aCode = a[3],
  1074. bCode = b[3],
  1075. rst = 0;
  1076. function compareConnector(arrA, arrB) {
  1077. let lessArr = arrA.length <= arrB ? arrA : arrB;
  1078. for (let i = 0; i < lessArr.length; i++) {
  1079. let result = compareUnit(arrA[i], arrB[i]);
  1080. if (result !== 0) {
  1081. return result;
  1082. } else {
  1083. }
  1084. }
  1085. function compareUnit(uA, uB) {
  1086. let uAV = parseFloat(uA),
  1087. uBV = parseFloat(uB);
  1088. if (uAV > uBV) {
  1089. return 1;
  1090. } else if (uAV < uBV) {
  1091. return -1;
  1092. }
  1093. return 0;
  1094. }
  1095. }
  1096. if (regConnector.test(a) && !regConnector.test(b)) {
  1097. rst = -1;
  1098. } else if (!regConnector.test(a) && regConnector.test(b)) {
  1099. rst = 1;
  1100. } else if (regConnector.test(a) && regConnector.test(b)) {
  1101. }
  1102. });
  1103. rationData.sort(function (a, b) {
  1104. let aCode = a[3],
  1105. bCode = b[3],
  1106. rst = 0,
  1107. splitA = aCode.split('-'),
  1108. splitB = bCode.split('-');
  1109. if(splitA[0] > splitB[0]){
  1110. rst = 1;
  1111. }
  1112. else if(splitA[0] < splitB[0]){
  1113. rst = -1;
  1114. }
  1115. else {
  1116. if(splitA[1] && splitB[1]){
  1117. let floatA = parseFloat(splitA[1]),
  1118. floatB = parseFloat(splitB[1]);
  1119. if(floatA > floatB){
  1120. rst = 1;
  1121. }
  1122. else if(floatA < floatB){
  1123. rst = -1;
  1124. }
  1125. }
  1126. }
  1127. return rst;
  1128. });*/
  1129. /*const excelData = [['树ID', '取费专业', '定额ID', '定额编码', '定额名', '定额显示名称', '基价']];
  1130. excelData.push.apply(excelData, rationData);
  1131. return excelData;*/
  1132. };
  1133. /**
  1134. * 批量更改章节id
  1135. *
  1136. * @param {Object} data
  1137. * @return {bool}
  1138. */
  1139. rationItemDAO.prototype.batchUpdateSectionIdFromExcel = async function(data) {
  1140. if (data.length <= 0) {
  1141. return false;
  1142. }
  1143. // 批量执行update
  1144. let bulkOprs = [],
  1145. sectionIDs = [];
  1146. for (const tmp of data) {
  1147. let rationId = parseInt(tmp[2]);
  1148. rationId = isNaN(rationId) || rationId <= 0 ? 0 : rationId;
  1149. let sectionId = parseInt(tmp[0]);
  1150. sectionId = isNaN(sectionId) || sectionId <= 0 ? 0 : sectionId;
  1151. if (sectionId <= 0 || rationId <= 0) {
  1152. continue;
  1153. }
  1154. sectionIDs.push(sectionId);
  1155. // 取费专业
  1156. let feeType = tmp[1] ? parseInt(tmp[1]) : null;
  1157. feeType = isNaN(feeType) || feeType <= 0 ? null : feeType;
  1158. let name = tmp[4];
  1159. name = name ? name : '';
  1160. let caption = tmp[5];
  1161. caption = caption ? caption : '';
  1162. let jobContent = tmp[7] ? tmp[7] : '';
  1163. let annotation = tmp[8] ? tmp[8] : '';
  1164. bulkOprs.push({updateOne: {
  1165. filter: {ID: rationId},
  1166. update: {$set: {
  1167. sectionId,
  1168. feeType,
  1169. name,
  1170. caption,
  1171. jobContent,
  1172. annotation
  1173. }}}
  1174. });
  1175. }
  1176. if(bulkOprs.length <= 0){
  1177. throw '无有效数据(树ID、定额ID不为空、且为数值)';
  1178. }
  1179. await rationItemModel.bulkWrite(bulkOprs);
  1180. // 更新章节树工作内容、附注节点选项(全设置为ALL)
  1181. sectionIDs = Array.from(new Set(sectionIDs));
  1182. if (sectionIDs.length) {
  1183. await stdRationSectionModel.updateMany(
  1184. {ID: {$in: sectionIDs}},
  1185. {$set: {
  1186. jobContentSituation: 'ALL',
  1187. annotationSituation: 'ALL'
  1188. }}
  1189. )
  1190. }
  1191. };
  1192. module.exports = new rationItemDAO();