ration_item.js 50 KB

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