ration_item.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. callback(true, "Fail to save", false);
  354. } else {
  355. if (addItems && addItems.length > 0) {
  356. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  357. } else {
  358. callback(false, "Save successfully", results);
  359. }
  360. }
  361. });
  362. }
  363. })
  364. }
  365. };
  366. rationItemDAO.prototype.removeRationItems = function(rationLibId, lastOpr, rIds,callback){
  367. if (rIds.length > 0) {
  368. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  369. if (err) {
  370. callback(true, "Fail to remove", 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. rationItemModel.update({rationRepId: rationLibId}, {$pull: {rationTemplateList: {rationID: {$in: rIds}}}}, function (theErr) {
  375. if (!theErr) {
  376. callback(false, "Remove successfully", docs);
  377. } else {
  378. callback(true, "Fail to remove", false);
  379. }
  380. });
  381. } else {
  382. callback(true, "Fail to remove", false);
  383. }
  384. })
  385. }
  386. })
  387. } else {
  388. callback(false, "No records were deleted!", null);
  389. }
  390. };
  391. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  392. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  393. if(err) callback(true, "Fail to get items", "")
  394. else callback(false,"Get items successfully", data);
  395. })
  396. };
  397. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  398. var filter = {
  399. 'rationRepId': repId,
  400. '$and': [{
  401. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  402. }, {
  403. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  404. }]
  405. };
  406. rationItemModel.find(filter, function (err, data) {
  407. if (err) {
  408. callback(true, 'Fail to find ration', null);
  409. } else {
  410. callback(false, '', data);
  411. }
  412. })
  413. }
  414. rationItemDAO.prototype.getRationItem = async function (repId, code) {
  415. let ration = await rationItemModel.findOne({rationRepId: repId, code: code});
  416. return ration;
  417. };
  418. rationItemDAO.prototype.addRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  419. if (items && items.length > 0) {
  420. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  421. var maxId = result.sequence_value;
  422. var arr = [];
  423. for (var i = 0; i < items.length; i++) {
  424. var obj = new rationItemModel(items[i]);
  425. obj.ID = (maxId - (items.length - 1) + i);
  426. obj.sectionId = sectionId;
  427. obj.rationRepId = rationLibId;
  428. arr.push(obj);
  429. }
  430. rationItemModel.collection.insert(arr, null, function(err, docs){
  431. if (err) {
  432. callback(true, "Fail to save", false);
  433. } else {
  434. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  435. if(err){
  436. callback(true, "Fail to sava operator", false);
  437. }
  438. else{
  439. callback(false, "Save successfully", docs);
  440. }
  441. })
  442. }
  443. })
  444. });
  445. } else {
  446. callback(true, "Source error!", false);
  447. }
  448. };
  449. rationItemDAO.prototype.updateRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  450. console.log('enter============');
  451. var functions = [];
  452. for (var i=0; i < items.length; i++) {
  453. functions.push((function(doc) {
  454. return function(cb) {
  455. var filter = {};
  456. if (doc.ID) {
  457. filter.ID = doc.ID;
  458. } else {
  459. filter.sectionId = sectionId;
  460. if (rationLibId) filter.rationRepId = rationLibId;
  461. filter.code = doc.code;
  462. }
  463. rationItemModel.update(filter, doc, cb);
  464. };
  465. })(items[i]));
  466. }
  467. functions.push((function () {
  468. return function (cb) {
  469. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  470. if(err){
  471. cb(err);
  472. }
  473. else{
  474. cb(null);
  475. }
  476. });
  477. }
  478. })());
  479. async.parallel(functions, function(err, results) {
  480. callback(err, results);
  481. });
  482. };
  483. //ration round func
  484. function round(v,e){
  485. var t=1;
  486. for(;e>0;t*=10,e--);
  487. for(;e<0;t/=10,e++);
  488. return Math.round(v*t)/t;
  489. }
  490. function calcRation(gljArr) {
  491. let labourPrc = [],
  492. materialPrc = [],
  493. machinePrc = [],
  494. managePrc = [],
  495. profitPrc = [],
  496. riskPrc = [],
  497. singlePrc,
  498. updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, managePrice: 0, profitPrice: 0, riskPrice: 0, basePrice: 0};
  499. gljArr.forEach(function (gljItem) {
  500. if(gljItem.gljParentType !== -1){
  501. singlePrc = scMathUtil.roundTo(gljItem.basePrice * gljItem.consumeAmt, -3);
  502. if(gljItem.gljParentType === 1){
  503. labourPrc.push(singlePrc);
  504. }
  505. else if(gljItem.gljParentType ===2){
  506. materialPrc.push(singlePrc);
  507. }
  508. else if(gljItem.gljParentType === 3){
  509. machinePrc.push(singlePrc);
  510. }
  511. else if(gljItem.gljParentType === 6){
  512. managePrc.push(singlePrc);
  513. }
  514. else if(gljItem.gljParentType === 7){
  515. profitPrc.push(singlePrc);
  516. }
  517. else if(gljItem.gljParentType === 8){
  518. riskPrc.push(singlePrc);
  519. }
  520. }
  521. });
  522. if(labourPrc.length > 0){
  523. let sumLaP = 0;
  524. for(let i=0; i<labourPrc.length; i++){
  525. sumLaP = scMathUtil.roundTo(sumLaP + labourPrc[i], -6);
  526. }
  527. updatePrc.labourPrice = scMathUtil.roundTo(sumLaP, -2);
  528. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.labourPrice, -2);
  529. }
  530. if(materialPrc.length > 0){
  531. let sumMtP = 0;
  532. for(let i= 0; i<materialPrc.length; i++){
  533. sumMtP = scMathUtil.roundTo(sumMtP + materialPrc[i], -6);
  534. }
  535. updatePrc.materialPrice = scMathUtil.roundTo(sumMtP, -2);
  536. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.materialPrice, -2);
  537. }
  538. if(machinePrc.length > 0){
  539. let sumMaP = 0;
  540. for(let i =0; i< machinePrc.length; i++){
  541. sumMaP = scMathUtil.roundTo(sumMaP + machinePrc[i], -6);
  542. }
  543. updatePrc.machinePrice = scMathUtil.roundTo(sumMaP, -2);
  544. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.machinePrice, -2);
  545. }
  546. if(managePrc.length > 0){
  547. let sumMgP = 0;
  548. for(let i =0; i< managePrc.length; i++){
  549. sumMgP = scMathUtil.roundTo(sumMgP + managePrc[i], -6);
  550. }
  551. updatePrc.managePrice = scMathUtil.roundTo(sumMgP, -2);
  552. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.managePrice, -2);
  553. }
  554. if(profitPrc.length > 0){
  555. let sumPfP = 0;
  556. for(let i =0; i< profitPrc.length; i++){
  557. sumPfP = scMathUtil.roundTo(sumPfP + profitPrc[i], -6);
  558. }
  559. updatePrc.profitPrice = scMathUtil.roundTo(sumPfP, -2);
  560. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.profitPrice, -2);
  561. }
  562. if(riskPrc.length > 0){
  563. let sumRkP = 0;
  564. for(let i =0; i< riskPrc.length; i++){
  565. sumRkP = scMathUtil.roundTo(sumRkP + riskPrc[i], -6);
  566. }
  567. updatePrc.riskPrice = scMathUtil.roundTo(sumRkP, -2);
  568. updatePrc.basePrice = scMathUtil.roundTo(updatePrc.basePrice + updatePrc.riskPrice, -2);
  569. }
  570. return updatePrc;
  571. }
  572. rationItemDAO.prototype.updateRationBasePrc = function (basePrcArr, overWriteUrl, callback) {
  573. async.each(basePrcArr, function (basePrcObj, finalCb) {
  574. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  575. async.waterfall([
  576. function (cb) {
  577. if(typeof basePrcObj.delete !== 'undefined' && basePrcObj.delete === 1){
  578. rationItemModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, result) {
  579. if(err){
  580. cb(err);
  581. }
  582. else{
  583. //删除
  584. rationItemModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  585. if(err){
  586. cb(err);
  587. }
  588. else{
  589. //补充定额
  590. compleRationModel.find({'rationGljList.gljId': adjGljId},{ID: 1, rationGljList: 1}, function (err, compleRst) {
  591. if(err){
  592. cb(err);
  593. }
  594. else {
  595. compleRationModel.update({'rationGljList.gljId': adjGljId}, {$pull: {rationGljList: {gljId: adjGljId}}}, {multi: true}, function (err) {
  596. if(err){
  597. cb(err);
  598. }
  599. else {
  600. for(let i = 0, len = compleRst.length; i < len; i++){
  601. compleRst[i]._doc.type = 'complementary';
  602. }
  603. cb(null, result.concat(compleRst));
  604. }
  605. });
  606. }
  607. });
  608. }
  609. });
  610. }
  611. });
  612. }
  613. else{
  614. rationItemModel.find({'rationGljList.gljId': adjGljId}, {ID: 1, rationGljList: 1}, function (err, result) {
  615. if(err){
  616. cb(err);
  617. }
  618. else{
  619. compleRationModel.find({'rationGljList.gljId': adjGljId}, {ID: 1, rationGljList: 1}, function (err, compleRst) {
  620. if(err){
  621. cb(err);
  622. }
  623. else {
  624. for(let i = 0, len = compleRst.length; i < len; i++){
  625. compleRst[i]._doc.type = 'complementary';
  626. }
  627. cb(null, result.concat(compleRst));
  628. }
  629. });
  630. }
  631. });
  632. }
  633. },
  634. function (result, cb) {
  635. let compleRTasks = [], stdRTasks = [];
  636. //重算时需要用到的所有工料机,一次性取
  637. let compleGljIds = [], stdGljIds = [];
  638. for(let ration of result){
  639. for(let glj of ration.rationGljList){
  640. if(glj.type !== undefined && glj.type === 'complementary'){
  641. compleGljIds.push(glj.gljId);
  642. }
  643. else {
  644. stdGljIds.push(glj.gljId);
  645. }
  646. }
  647. }
  648. gljDao.getStdCompleGljItems(compleGljIds, stdGljIds, function (err, allGljs) {
  649. if(err){
  650. cb(err);
  651. }
  652. else {
  653. let gljIndex = {};
  654. for(let glj of allGljs){
  655. gljIndex[glj.ID] = glj;
  656. }
  657. async.each(result, function (rationItem, ecb) {
  658. let rationGljList = rationItem.rationGljList;
  659. let gljArr = [];
  660. for(let i=0; i<rationGljList.length; i++){
  661. let theGlj = gljIndex[rationGljList[i].gljId];
  662. if(theGlj !== undefined && theGlj){
  663. let gljParentType = -1;
  664. if(theGlj.ID === adjGljId){
  665. theGlj.gljType = adjGljType;
  666. }
  667. if(theGlj.gljType <= 3){
  668. gljParentType = theGlj.gljType;
  669. } else if(theGlj.gljType > 200 && theGlj.gljType < 300){
  670. gljParentType = 2;
  671. } else if(theGlj.gljType > 300 && theGlj.gljType < 400){
  672. gljParentType = 3;
  673. } else if(theGlj.gljType === 6){ //管理费
  674. gljParentType = 6;
  675. } else if(theGlj.gljType === 7){ //利润
  676. gljParentType = 7;
  677. } else if(theGlj.gljType === 8){ //风险费
  678. gljParentType = 8;
  679. }
  680. if(theGlj)
  681. if(theGlj.ID === adjGljId){
  682. gljArr.push({gljId: theGlj.ID, basePrice: adjBasePrice, gljParentType: gljParentType, unit: theGlj.unit});
  683. } else {
  684. if(theGlj.priceProperty && Object.keys(theGlj.priceProperty).length > 0){
  685. gljArr.push({
  686. gljId: theGlj.ID,
  687. basePrice: parseFloat(theGlj.priceProperty.price1),
  688. gljParentType: gljParentType,
  689. unit: theGlj.unit});
  690. } else {
  691. gljArr.push({
  692. gljId: theGlj.ID,
  693. basePrice: parseFloat(theGlj.basePrice),
  694. gljParentType: gljParentType,
  695. unit: theGlj.unit});
  696. }
  697. }
  698. }
  699. }
  700. gljArr.forEach(function (gljItem) {
  701. rationGljList.forEach(function (rationGlj) {
  702. if(gljItem.gljId === rationGlj.gljId){
  703. gljItem.consumeAmt = parseFloat(rationGlj.consumeAmt);
  704. }
  705. })
  706. });
  707. let updatePrc = null;
  708. let overWriteCalc = false; //需要重写算法
  709. if (overWriteUrl) {
  710. let overWriteExports = require(overWriteUrl);
  711. if (typeof overWriteExports.calcRation !== 'undefined') {
  712. overWriteCalc = true;
  713. updatePrc = overWriteExports.calcRation(gljArr, scMathUtil);
  714. }
  715. }
  716. if (!overWriteCalc) {
  717. updatePrc = calcRation(gljArr);
  718. }
  719. let task = {
  720. updateOne: {
  721. filter: {
  722. ID: rationItem.ID
  723. },
  724. update: {
  725. labourPrice: updatePrc.labourPrice.toString(),
  726. materialPrice: updatePrc.materialPrice.toString(),
  727. machinePrice: updatePrc.machinePrice.toString(),
  728. basePrice: updatePrc.basePrice.toString()
  729. }
  730. }
  731. };
  732. //updateDataBase
  733. if(rationItem._doc.type !== undefined && rationItem._doc.type === 'complementary'){
  734. compleRTasks.push(task);
  735. ecb(null);
  736. }
  737. else {
  738. stdRTasks.push(task);
  739. ecb(null);
  740. }
  741. }, async function(err){
  742. if(err){
  743. cb(err);
  744. }
  745. else {
  746. //do sth
  747. try{
  748. if(compleRTasks.length > 0){
  749. await compleRationModel.bulkWrite(compleRTasks);
  750. }
  751. if(stdRTasks.length > 0){
  752. await rationItemModel.bulkWrite(stdRTasks);
  753. }
  754. }
  755. catch(e){
  756. cb(err);
  757. }
  758. cb(null);
  759. }
  760. });
  761. }
  762. });
  763. },
  764. ], function (err) {
  765. if(err){
  766. finalCb(err);
  767. }
  768. else{
  769. finalCb(null);
  770. }
  771. });
  772. }, function (err) {
  773. if(err){
  774. callback(err, 'Error');
  775. }
  776. else{
  777. callback(null, '');
  778. }
  779. });
  780. };
  781. rationItemDAO.prototype.getRationGljIds = function (data, callback) {
  782. let repId = data.repId;
  783. rationItemModel.find({rationRepId: repId}, function (err, result) {
  784. if(err){
  785. callback(err, 'Error', null);
  786. }
  787. else{
  788. let rstIds = [], newRst = [];
  789. result.forEach(function (data) {
  790. if(data.rationGljList.length >0){
  791. data.rationGljList.forEach(function (gljObj) {
  792. rstIds.push(gljObj.gljId);
  793. })
  794. }
  795. });
  796. for(let i= 0; i< rstIds.length; i++){
  797. if(newRst.indexOf(rstIds[i]) === -1){
  798. newRst.push(rstIds[i]);
  799. }
  800. }
  801. callback(null, '', newRst);
  802. }
  803. });
  804. };
  805. rationItemDAO.prototype.getRationsCodes = function (data, callback) {
  806. let repId = data.repId;
  807. rationItemModel.find({rationRepId: repId, isDeleted: false}, function (err, result) {
  808. if(err){
  809. callback(err, 'Error', null);
  810. }
  811. else{
  812. let rstCodes = [];
  813. result.forEach(function (rationItem) {
  814. rstCodes.push(rationItem.code);
  815. });
  816. callback(null, 'get all rationCodes success', rstCodes);
  817. }
  818. })
  819. };
  820. rationItemDAO.prototype.updateJobContent = function (lastOpr, repId, updateArr, callback) {
  821. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  822. async.each(updateArr, function (obj, cb) {
  823. rationItemModel.update({rationRepId: repId, code: obj.code}, {$set: {jobContent: obj.jobContent}}, function (err) {
  824. if(err){
  825. cb(err);
  826. }
  827. else{
  828. cb(null);
  829. }
  830. })
  831. }, function (err) {
  832. if(err){
  833. callback(err);
  834. }
  835. else{
  836. callback(null);
  837. }
  838. });
  839. });
  840. }
  841. rationItemDAO.prototype.updateAnnotation = function (lastOpr, repId, updateArr, callback) {
  842. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  843. async.each(updateArr, function (obj, cb) {
  844. rationItemModel.update({rationRepId: repId, code: obj.code}, {$set: {annotation: obj.annotation}}, function (err) {
  845. if(err){
  846. cb(err);
  847. }
  848. else{
  849. cb(null);
  850. }
  851. })
  852. }, function (err) {
  853. if(err){
  854. callback(err);
  855. }
  856. else{
  857. callback(null);
  858. }
  859. });
  860. });
  861. };
  862. //更新定额下模板关联
  863. rationItemDAO.prototype.updateRationTemplate = async function (rationRepId, rationID, templateData) {
  864. //自动匹配定额
  865. let matachCodes = [],
  866. matchRations = [];
  867. //要保存的数据
  868. let saveData = [];
  869. for (let data of templateData) {
  870. if (data.code) {
  871. matachCodes.push(data.code);
  872. }
  873. }
  874. matachCodes = Array.from(new Set(matachCodes));
  875. if (matachCodes.length > 0) {
  876. matchRations = await rationItemModel.find({rationRepId: rationRepId, code: {$in: matachCodes}}, '-_id ID code name');
  877. }
  878. let validData = [];
  879. //设置展示数据
  880. for (let data of templateData) {
  881. let match = false;
  882. for (let ration of matchRations) {
  883. if (data.code && data.code === ration.code) {
  884. match = true;
  885. data.name = ration.name;
  886. data.rationID = ration.ID;
  887. break;
  888. }
  889. }
  890. if (!match) {
  891. data.code = '';
  892. data.name = '';
  893. }
  894. if (data.type || data.code || data.name || data.billsLocation) {
  895. validData.push(data);
  896. }
  897. }
  898. for (let data of validData) {
  899. saveData.push({rationID: data.rationID ? data.rationID : null, type: data.type, billsLocation: data.billsLocation});
  900. }
  901. //更新
  902. await rationItemModel.update({ID: rationID}, {$set: {rationTemplateList: saveData}});
  903. return validData;
  904. };
  905. // 根据章节列表批量更新定额
  906. rationItemDAO.prototype.updateRationBySection = async function (rationRepId, sectionList, updateData) {
  907. await rationItemModel.updateMany({rationRepId, sectionId: {$in: sectionList}}, updateData);
  908. }
  909. //计算导入数据的价格
  910. rationItemDAO.prototype.calcForRation = function (stdGljList, ration, overWriteUrl) {
  911. let rationGljList = ration.rationGljList,
  912. gljArr = [];
  913. for(let rationGlj of rationGljList) {
  914. let glj = stdGljList[rationGlj.gljId];
  915. let gljPType = parseInt(glj.gljType.toString().match(/\d+?/)[0]);
  916. let newGlj = {
  917. gljId: glj.ID,
  918. consumeAmt: rationGlj.consumeAmt,
  919. gljParentType: gljPType,
  920. unit: glj.unit
  921. };
  922. newGlj.basePrice = glj.priceProperty && Object.keys(glj.priceProperty).length > 0
  923. ? parseFloat(glj.priceProperty.price1)
  924. : parseFloat(glj.basePrice);
  925. gljArr.push(newGlj);
  926. }
  927. let updatePrc = null;
  928. let overWriteCalc = false; //需要重写算法
  929. if (overWriteUrl) {
  930. let overWriteExports = require(overWriteUrl);
  931. if (typeof overWriteExports.calcRation !== 'undefined') {
  932. overWriteCalc = true;
  933. updatePrc = overWriteExports.calcRation(gljArr, scMathUtil);
  934. }
  935. }
  936. if (!overWriteCalc) {
  937. updatePrc = calcRation(gljArr);
  938. }
  939. ration.labourPrice = updatePrc.labourPrice.toString();
  940. ration.materialPrice = updatePrc.materialPrice.toString();
  941. ration.machinePrice = updatePrc.machinePrice.toString();
  942. ration.basePrice = updatePrc.basePrice.toString();
  943. };
  944. /**
  945. * 根据条件获取定额数据
  946. *
  947. * @param {Object} condition
  948. * @param {Object} fields
  949. * @param {String} indexBy
  950. * @return {Promise|Array}
  951. */
  952. rationItemDAO.prototype.getRationItemByCondition = async function (condition, fields = null, indexBy = null) {
  953. let result = [];
  954. if (Object.keys(condition).length <= 0) {
  955. return result;
  956. }
  957. result = await rationItemModel.find(condition, fields).sort({code: 1});
  958. if (indexBy !== null && result.length > 0) {
  959. let tmpResult = {};
  960. for(let tmp of result) {
  961. tmpResult[tmp[indexBy]] = tmp;
  962. }
  963. result = tmpResult;
  964. }
  965. return result;
  966. };
  967. /**
  968. * 从excel中批量新增数据
  969. *
  970. * @param {Number} rationRepId
  971. * @param {Array} data
  972. * @return {bool}
  973. */
  974. rationItemDAO.prototype.batchAddFromExcel = async function(rationRepId, data) {
  975. if (data.length <= 0) {
  976. return false;
  977. }
  978. // 获取定额库相关数据
  979. const rationRepository = await rationRepositoryDao.getRepositoryById(rationRepId);
  980. if (rationRepository.length !== 1 || rationRepository[0].gljLib === undefined) {
  981. return false;
  982. }
  983. // 获取标准工料机库数据
  984. const stdBillsLibListsModel = new STDGLJListModel();
  985. const stdGLJData = await stdBillsLibListsModel.getGljItemsByRepId(rationRepository[0].gljLib);
  986. // 整理标准工料机库数据
  987. let stdGLJList = {};
  988. let stdGLJListByID = {};
  989. for (const tmp of stdGLJData) {
  990. stdGLJList[tmp.code.toString()] = tmp.ID;
  991. stdGLJListByID[tmp.ID] = tmp;
  992. }
  993. let lastData = {};
  994. // 定额xx下提示的次数
  995. let lastFailCount = 0;
  996. const rationData = [];
  997. // 编码列表,用于查找库中是否有对应数据
  998. let rationCodeList = [];
  999. let gljCodeList = [];
  1000. // 插入失败的工料机列表(用于提示)
  1001. let failGLJList = [];
  1002. for (const tmp of data) {
  1003. if (tmp.length <= 0) {
  1004. continue;
  1005. }
  1006. // 如果第一个字段为null则是工料机数据,放入上一个数据的工料机字段
  1007. if (tmp[0] === undefined && Object.keys(lastData).length > 0) {
  1008. // 如果不存在对应的工料机库数据则跳过
  1009. if (stdGLJList[tmp[1]] === undefined) {
  1010. if (lastFailCount === 0) {
  1011. failGLJList.push('定额' + lastData.code + '下的');
  1012. lastFailCount++;
  1013. }
  1014. //const failString = '定额' + lastData.code + '下的' + tmp[1];
  1015. failGLJList.push(tmp[1]);
  1016. continue;
  1017. }
  1018. const tmpRationGlj = {
  1019. gljId: stdGLJList[tmp[1]],
  1020. consumeAmt: Math.abs(tmp[4]),
  1021. proportion: 0,
  1022. };
  1023. lastData.rationGljList.push(tmpRationGlj);
  1024. if (gljCodeList.indexOf(tmp[1]) < 0) {
  1025. gljCodeList.push(tmp[1]);
  1026. }
  1027. continue;
  1028. }
  1029. if (tmp[0] === '定额' && Object.keys(lastData).length > 0) {
  1030. rationData.push(lastData);
  1031. }
  1032. lastFailCount = 0;
  1033. // 组装数据
  1034. lastData = {
  1035. code: tmp[1],
  1036. name: tmp[2],
  1037. unit: tmp[3],
  1038. caption: tmp[2],
  1039. rationRepId: rationRepId,
  1040. sectionId: 0,
  1041. labourPrice: '0',
  1042. materialPrice: '0',
  1043. machinePrice: '0',
  1044. basePrice: '0',
  1045. rationGljList: []
  1046. };
  1047. // 防止重复加入
  1048. if (rationCodeList.indexOf(tmp[1]) < 0) {
  1049. rationCodeList.push(tmp[1]);
  1050. }
  1051. }
  1052. // 最后一个入数组
  1053. rationData.push(lastData);
  1054. // 查找数据库中是否已存在待插入数据
  1055. const condition = {
  1056. rationRepId: rationRepId,
  1057. code: { $in: rationCodeList }
  1058. };
  1059. const existCodeList = await this.getRationItemByCondition(condition, ['code'], 'code');
  1060. // 过滤插入数据
  1061. let insertData = [];
  1062. //已存在定额,则更新价格及rationGLjList字段
  1063. let updateData = [];
  1064. for (const ration of rationData) {
  1065. if (existCodeList[ration.code] !== undefined) {
  1066. updateData.push(ration);
  1067. continue;
  1068. }
  1069. insertData.push(ration);
  1070. }
  1071. //更新定额
  1072. let updateBulk = [];
  1073. for(let ration of updateData){
  1074. this.calcForRation(stdGLJListByID, ration);
  1075. updateBulk.push({
  1076. updateOne: {
  1077. filter: {rationRepId: rationRepId, code: ration.code},
  1078. update: {$set: {rationGljList: ration.rationGljList, labourPrice: ration.labourPrice, materialPrice: ration.materialPrice,
  1079. machinePrice: ration.machinePrice, basePrice: ration.basePrice}}
  1080. }
  1081. });
  1082. }
  1083. //更新数据库
  1084. if(updateBulk.length > 0){
  1085. await rationItemModel.bulkWrite(updateBulk);
  1086. }
  1087. // 如果都已经存在,直接返回
  1088. if (insertData.length <= 0) {
  1089. return failGLJList;
  1090. }
  1091. //计算价格
  1092. for(let ration of insertData){
  1093. this.calcForRation(stdGLJListByID, ration);
  1094. }
  1095. // 组织id
  1096. const counterInfo = await counter.counterDAO.getIDAfterCount(counter.moduleName.rations, insertData.length);
  1097. let maxId = counterInfo.sequence_value;
  1098. maxId = parseInt(maxId);
  1099. let count = 0;
  1100. for (const index in insertData) {
  1101. insertData[index].ID = maxId - (insertData.length - 1) + count;
  1102. count++;
  1103. }
  1104. // 插入数据库
  1105. await rationItemModel.create(insertData);
  1106. return failGLJList;
  1107. };
  1108. /**
  1109. * 导出到excel的数据
  1110. *
  1111. * @param {Number} rationRepId
  1112. * @return {Array}
  1113. */
  1114. rationItemDAO.prototype.exportExcelData = async function(rationRepId) {
  1115. const condition = {
  1116. rationRepId: rationRepId
  1117. };
  1118. const rationDataList = await this.getRationItemByCondition(condition, ['name', 'code', 'ID', 'sectionId', 'feeType', 'caption', 'basePrice', 'jobContent', 'annotation']);
  1119. // 整理数据
  1120. let rationData = [];
  1121. for (const tmp of rationDataList) {
  1122. const sectionId = tmp.sectionId <= 0 || tmp.sectionId === undefined ? null : tmp.sectionId;
  1123. const feeType = tmp.feeType === '' || tmp.feeType === undefined ? null : tmp.feeType;
  1124. const ration = [sectionId, feeType, tmp.ID, tmp.code, tmp.name, tmp.caption, tmp.basePrice, tmp.jobContent, tmp.annotation];
  1125. rationData.push(ration);
  1126. }
  1127. const excelData = [['树ID', '取费专业', '定额ID', '定额编码', '定额名', '定额显示名称', '基价', '工作内容', '附注']];
  1128. excelData.push.apply(excelData, rationData);
  1129. return excelData;
  1130. //根据编号排序,优先级:number-number-..., number, Anumber....
  1131. /*let regConnector = /-/g,
  1132. regLetter = /[a-z,A-Z]/g,
  1133. regNum = /\d+/g;
  1134. rationData.sort(function (a, b) {
  1135. let aCode = a[3],
  1136. bCode = b[3],
  1137. rst = 0;
  1138. function compareConnector(arrA, arrB) {
  1139. let lessArr = arrA.length <= arrB ? arrA : arrB;
  1140. for (let i = 0; i < lessArr.length; i++) {
  1141. let result = compareUnit(arrA[i], arrB[i]);
  1142. if (result !== 0) {
  1143. return result;
  1144. } else {
  1145. }
  1146. }
  1147. function compareUnit(uA, uB) {
  1148. let uAV = parseFloat(uA),
  1149. uBV = parseFloat(uB);
  1150. if (uAV > uBV) {
  1151. return 1;
  1152. } else if (uAV < uBV) {
  1153. return -1;
  1154. }
  1155. return 0;
  1156. }
  1157. }
  1158. if (regConnector.test(a) && !regConnector.test(b)) {
  1159. rst = -1;
  1160. } else if (!regConnector.test(a) && regConnector.test(b)) {
  1161. rst = 1;
  1162. } else if (regConnector.test(a) && regConnector.test(b)) {
  1163. }
  1164. });
  1165. rationData.sort(function (a, b) {
  1166. let aCode = a[3],
  1167. bCode = b[3],
  1168. rst = 0,
  1169. splitA = aCode.split('-'),
  1170. splitB = bCode.split('-');
  1171. if(splitA[0] > splitB[0]){
  1172. rst = 1;
  1173. }
  1174. else if(splitA[0] < splitB[0]){
  1175. rst = -1;
  1176. }
  1177. else {
  1178. if(splitA[1] && splitB[1]){
  1179. let floatA = parseFloat(splitA[1]),
  1180. floatB = parseFloat(splitB[1]);
  1181. if(floatA > floatB){
  1182. rst = 1;
  1183. }
  1184. else if(floatA < floatB){
  1185. rst = -1;
  1186. }
  1187. }
  1188. }
  1189. return rst;
  1190. });*/
  1191. /*const excelData = [['树ID', '取费专业', '定额ID', '定额编码', '定额名', '定额显示名称', '基价']];
  1192. excelData.push.apply(excelData, rationData);
  1193. return excelData;*/
  1194. };
  1195. /**
  1196. * 批量更改章节id
  1197. *
  1198. * @param {Object} data
  1199. * @return {bool}
  1200. */
  1201. rationItemDAO.prototype.batchUpdateSectionIdFromExcel = async function(data) {
  1202. if (data.length <= 0) {
  1203. return false;
  1204. }
  1205. // 批量执行update
  1206. let bulkOprs = [],
  1207. sectionIDs = [];
  1208. for (const tmp of data) {
  1209. let rationId = parseInt(tmp[2]);
  1210. rationId = isNaN(rationId) || rationId <= 0 ? 0 : rationId;
  1211. let sectionId = parseInt(tmp[0]);
  1212. sectionId = isNaN(sectionId) || sectionId <= 0 ? 0 : sectionId;
  1213. if (sectionId <= 0 || rationId <= 0) {
  1214. continue;
  1215. }
  1216. sectionIDs.push(sectionId);
  1217. // 取费专业
  1218. let feeType = tmp[1] ? parseInt(tmp[1]) : null;
  1219. feeType = isNaN(feeType) || feeType <= 0 ? null : feeType;
  1220. let name = tmp[4];
  1221. name = name ? name : '';
  1222. let caption = tmp[5];
  1223. caption = caption ? caption : '';
  1224. let jobContent = tmp[7] ? tmp[7] : '';
  1225. let annotation = tmp[8] ? tmp[8] : '';
  1226. bulkOprs.push({updateOne: {
  1227. filter: {ID: rationId},
  1228. update: {$set: {
  1229. sectionId,
  1230. feeType,
  1231. name,
  1232. caption,
  1233. jobContent,
  1234. annotation
  1235. }}}
  1236. });
  1237. }
  1238. if(bulkOprs.length <= 0){
  1239. throw '无有效数据(树ID、定额ID不为空、且为数值)';
  1240. }
  1241. await rationItemModel.bulkWrite(bulkOprs);
  1242. // 更新章节树工作内容、附注节点选项(全设置为ALL)
  1243. sectionIDs = Array.from(new Set(sectionIDs));
  1244. if (sectionIDs.length) {
  1245. await stdRationSectionModel.updateMany(
  1246. {ID: {$in: sectionIDs}},
  1247. {$set: {
  1248. jobContentSituation: 'ALL',
  1249. annotationSituation: 'ALL'
  1250. }}
  1251. )
  1252. }
  1253. };
  1254. module.exports = new rationItemDAO();