ration_glj_facade.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /**
  2. * Created by chen on 2017/6/29.
  3. */
  4. module.exports = { //先 exports再require 防止循环引用
  5. save: save,
  6. getData: getData,
  7. deleteByRation: deleteByRation,
  8. getQuantityByProjectGLJ: getQuantityByProjectGLJ,
  9. getLibInfo: getLibInfo,
  10. getLibOptions,
  11. getLibOptionsForCompilation,
  12. getGLJData: getGLJData,
  13. getGLJDataByCodes: getGLJDataByCodes,
  14. addGLJ: addGLJ,
  15. deleteRationGLJ: deleteRationGLJ,
  16. deleteGLJ: deleteGLJ,
  17. insertAddTypeGLJ: insertAddTypeGLJ,
  18. replaceGLJ: replaceGLJ,
  19. replaceGLJByData: replaceGLJByData,
  20. mReplaceGLJ: mReplaceGLJ,
  21. updateRationGLJByEdit: updateRationGLJByEdit,
  22. getGLJClass: getGLJClass,
  23. insertGLJAsRation: insertGLJAsRation,
  24. getRationTypeGLJQuantity: getRationTypeGLJQuantity,
  25. getInfoFromProjectGLJ: getInfoFromProjectGLJ,
  26. createNewRecord: createNewRecord,
  27. getGLJSearchInfo: getGLJSearchInfo,
  28. updateRationGLJFromDoc: updateRationGLJFromDoc,
  29. getGLJLibByEngineerID: getGLJLibByEngineerID,
  30. prepareExtData: prepareExtData,
  31. setPropertyFromStd: setPropertyFromStd,
  32. updateProportion: updateProportion
  33. };
  34. let mongoose = require('mongoose');
  35. const uuidV1 = require('uuid/v1');
  36. let consts = require('../../main/models/project_consts')
  37. let commonConsts = consts.commonConst;
  38. let _ = require("lodash");
  39. let ration_glj = mongoose.model('ration_glj');
  40. const GLJListModel = require('../../glj/models/glj_list_model');
  41. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  42. let async_n = require("async");
  43. let ration = mongoose.model('ration');
  44. let ration_coe_facade = require('./ration_coe_facade');
  45. let ration_coe = mongoose.model('ration_coe');
  46. let std_ration_lib_ration_items = mongoose.model('std_ration_lib_ration_items');
  47. let glj_calculate_facade = require('./glj_calculate_facade');
  48. let glj_type_util = require('../../../public/cache/std_glj_type_util');
  49. let quantity_detail_facade = require('../../main/facade/quantity_detail_facade');
  50. let ration_installation_facade = require('../../main/facade/ration_installation_facade');
  51. let ration_facade = require('../../main/facade/ration_facade');
  52. let logger = require("../../../logs/log_helper").logger;
  53. const stdgljutil = require("../../../public/cache/std_glj_type_util");
  54. const EngineeringLibModel = require("../../users/models/engineering_lib_model");
  55. const GljDao = require("../../complementary_glj_lib/models/gljModel");
  56. const gljType = require("../../common/const/glj_type_const.js");
  57. const complementaryGljModel = mongoose.model('complementary_glj_lib');
  58. const stdGljModel = mongoose.model('std_glj_lib_gljList');
  59. const gljClassModel = mongoose.model('std_glj_lib_gljClass');
  60. const projectDao = require('../../pm/models/project_model').project;
  61. const pmFacade = require('../../pm/facade/pm_facade');
  62. const commonFacade = require('../../main/facade/common_facade');
  63. const compleClassModel = mongoose.model('complementary_glj_section');
  64. const stdRationLibModel = mongoose.model('std_ration_lib_map');
  65. const compilationModel = mongoose.model('compilation');
  66. const engineeringModel = mongoose.model('engineering_lib');
  67. let gljUtil = require('../../../public/gljUtil');
  68. let operationMap = {
  69. 'ut_create': create_ration_glj,
  70. 'ut_update': update_ration_glj,
  71. 'ut_delete': delete_ration_glj
  72. };
  73. let updateFunctionMap = {
  74. 'normalUpdate': normalUpdate
  75. };
  76. /**
  77. * 根据项目工料机ID和项目ID取消耗量
  78. *
  79. * @param {object} condition
  80. * @return Array
  81. */
  82. async function getQuantityByProjectGLJ(condition) {
  83. let query = {
  84. 'projectID': condition.projectID,
  85. };
  86. if (condition.projectGLJIDList) {
  87. query['projectGLJID'] = {
  88. $in: condition.projectGLJIDList
  89. };
  90. }
  91. let startTime = +new Date();
  92. let results = await ration_glj.find(query, ['projectGLJID', 'quantity', 'rationID'], {
  93. sort: {
  94. projectGLJID: 1
  95. }
  96. });
  97. let rationList = _.uniq(_.map(results, 'rationID'));
  98. let getQuantity = +new Date();
  99. console.log("取工料机消耗量时间-----" + (getQuantity - startTime));
  100. let rationQuery = {
  101. 'projectID': condition.projectID,
  102. 'ID': {
  103. $in: rationList
  104. },
  105. 'deleteInfo': null
  106. };
  107. /* $and: [
  108. {'projectID': condition.projectID},
  109. {'ID': {$in: rationList}},
  110. {'deleteInfo': null}
  111. ]*/
  112. let rations = await ration.find(rationQuery, ['ID', 'quantity']);
  113. let rationsTime = +new Date();
  114. console.log("取定额消耗量时间-----" + (rationsTime - getQuantity));
  115. return combineQuantity(results, rations);
  116. }
  117. function combineQuantity(results, rations) {
  118. let resultList = [];
  119. let rationMap = _.indexBy(rations, 'ID');
  120. for (let r of results) {
  121. let tmp = {
  122. projectGLJID: r.projectGLJID,
  123. quantity: Number(r.quantity)
  124. }
  125. let ration = rationMap[r.rationID]; //_.find(rations, {ID: r.rationID})
  126. if (ration) {
  127. tmp.rationID = ration.ID;
  128. tmp.rationQuantity = ration.quantity ? Number(ration.quantity) : undefined;
  129. }
  130. resultList.push(tmp);
  131. }
  132. return resultList;
  133. return resultList;
  134. }
  135. function get_lib_glj_info(ration_glj) {
  136. return function (result, cb) {
  137. std_glj_lib_gljList_model.findOne({
  138. 'ID': ration_glj.GLJID
  139. }, (err, glj) => {
  140. if (err) {
  141. cb(err, '')
  142. } else if (glj) {
  143. ration_glj.name = glj.name;
  144. ration_glj.code = glj.code;
  145. ration_glj.original_code = glj.code;
  146. ration_glj.unit = glj.unit;
  147. ration_glj.specs = glj.specs;
  148. ration_glj.basePrice = glj.basePrice;
  149. ration_glj.marketPrice = glj.basePrice;
  150. ration_glj.shortName = glj.shortName;
  151. ration_glj.type = glj.gljType;
  152. ration_glj.repositoryId = glj.repositoryId;
  153. ration_glj.adjCoe = glj.adjCoe;
  154. getInfoFromProjectGLJ(ration_glj).then(function (info) {
  155. if (info && info.length > 0) {
  156. let tem = {};
  157. tem.newRecode = createNewRecord(info[0]);
  158. tem.showData = info[0];
  159. result.datas.push(tem);
  160. cb(null, result);
  161. } else {
  162. cb(new Error('get project glj error'), null);
  163. }
  164. });
  165. } else {
  166. cb(null, result);
  167. }
  168. })
  169. }
  170. }
  171. function createNewRecord(ration_glj) {
  172. let newRecoed = {};
  173. newRecoed.ID = ration_glj.ID;
  174. newRecoed.projectID = ration_glj.projectID;
  175. newRecoed.GLJID = ration_glj.GLJID;
  176. newRecoed.rationID = ration_glj.rationID;
  177. newRecoed.rationItemQuantity = ration_glj.rationItemQuantity;
  178. newRecoed.customQuantity = ration_glj.customQuantity;
  179. newRecoed.quantity = ration_glj.quantity;
  180. newRecoed.rationProportion = ration_glj.rationProportion;
  181. newRecoed.adjustProportion = ration_glj.adjustProportion;
  182. newRecoed.name = ration_glj.name;
  183. newRecoed.code = ration_glj.code;
  184. newRecoed.original_code = ration_glj.original_code;
  185. newRecoed.unit = ration_glj.unit;
  186. newRecoed.specs = ration_glj.specs;
  187. newRecoed.from = ration_glj.from ? ration_glj.from : 'std';
  188. newRecoed.createType = ration_glj.createType ? ration_glj.createType : 'normal';
  189. newRecoed.shortName = ration_glj.shortName;
  190. newRecoed.billsItemID = ration_glj.billsItemID;
  191. newRecoed.type = ration_glj.type;
  192. newRecoed.model = ration_glj.model;
  193. newRecoed.repositoryId = ration_glj.repositoryId;
  194. newRecoed.projectGLJID = ration_glj.projectGLJID;
  195. newRecoed.adjCoe = ration_glj.adjCoe;
  196. if (ration_glj.fromUser) {
  197. newRecoed.fromUser = ration_glj.fromUser;
  198. }
  199. return newRecoed
  200. }
  201. async function getInfoFromProjectGLJ(ration_glj, unitPriceFileId, ext) {
  202. let data = getGLJSearchInfo(ration_glj);
  203. try {
  204. let projectGljModel = new GLJListModel();
  205. let result = await projectGljModel.addList(data, unitPriceFileId, ext);
  206. let typeString = result.type + "";
  207. ration_glj.marketPrice = result.unit_price.market_price;
  208. ration_glj.adjustPrice = result.unit_price.base_price;
  209. ration_glj.basePrice = result.unit_price.base_price;
  210. ration_glj.projectGLJID = result.id;
  211. if (typeString.startsWith("2") || typeString == '4' || typeString == '5') { //只有材料类型才显示是否暂估
  212. ration_glj.isEstimate = result.is_evaluate;
  213. }
  214. if (result.hasOwnProperty('subList') && result.subList.length > 0) {
  215. ration_glj.subList = getMixRatioShowDatas(result.subList);
  216. }
  217. return [ration_glj, result];
  218. } catch (err) {
  219. logger.err(err);
  220. return null;
  221. }
  222. }
  223. function getMixRatioShowDatas(subList) {
  224. var temRationGLJs = [];
  225. for (let pg of subList) {
  226. var tem = {
  227. projectGLJID: pg.id,
  228. code: pg.code,
  229. name: pg.name,
  230. specs: pg.specs,
  231. unit: pg.unit,
  232. shortName: pg.unit_price.short_name,
  233. rationItemQuantity: pg.ratio_data.consumption,
  234. basePrice: pg.unit_price.base_price,
  235. marketPrice: pg.unit_price.market_price,
  236. adjustPrice: pg.adjust_price,
  237. isEstimate: pg.is_evaluate,
  238. isMixRatio: true,
  239. isAdd: pg.unit_price.is_add,
  240. GLJID: pg.glj_id
  241. }
  242. temRationGLJs.push(tem);
  243. }
  244. temRationGLJs = _.sortBy(temRationGLJs, 'code');
  245. return temRationGLJs;
  246. }
  247. function create_ration_glj(user_id, datas) {
  248. return function (callback) {
  249. let ration_glj_list = datas.ration_glj_list;
  250. var tasks = [];
  251. tasks.push(startingTask("get glj info"))
  252. for (let i = 0; i < ration_glj_list.length; i++) {
  253. ration_glj_list[i].ID = uuidV1();
  254. tasks.push(get_lib_glj_info(ration_glj_list[i]))
  255. }
  256. async_n.waterfall(tasks, (err, results) => {
  257. if (err) {
  258. callback(err, results)
  259. } else {
  260. let newRecords = [];
  261. let showDatas = [];
  262. for (let r of results.datas) {
  263. if (r) {
  264. newRecords.push(r.newRecode);
  265. showDatas.push(r.showData);
  266. }
  267. }
  268. if (newRecords.length > 0) {
  269. ration_glj.insertMany(newRecords, (err, doc) => {
  270. if (err) {
  271. callback(err, null);
  272. } else {
  273. let returndata = {
  274. updateTpye: commonConsts.UT_CREATE,
  275. moduleName: 'ration_glj',
  276. data: {
  277. newRecords: newRecords,
  278. showDatas: showDatas
  279. }
  280. }
  281. callback(null, returndata)
  282. }
  283. });
  284. } else {
  285. logger.info("can't find gljs")
  286. callback(null, null)
  287. }
  288. }
  289. })
  290. }
  291. }
  292. function update_ration_glj(user_id, datas) {
  293. if (datas.updateFunction) {
  294. return updateFunctionMap[datas.updateFunction](user_id, datas);
  295. } else {
  296. return normalUpdate(user_id, datas);
  297. }
  298. }
  299. function normalUpdate(user_id, datas) {
  300. return function (callback) {
  301. ration_glj.update(datas.query, datas.doc, (err, result) => {
  302. if (err) {
  303. callback(err, '');
  304. } else {
  305. let returndata = {
  306. moduleName: 'ration_glj',
  307. data: {
  308. updateTpye: commonConsts.UT_UPDATE,
  309. query: datas.query,
  310. doc: datas.doc
  311. }
  312. }
  313. callback(null, returndata)
  314. }
  315. })
  316. }
  317. }
  318. async function doCustomQuantityUpdate(datas) {
  319. let result = await ration_glj.findOneAndUpdate(datas.query, datas.doc);
  320. let cal_result = await glj_calculate_facade.calculateQuantity({
  321. projectID: datas.query.projectID,
  322. rationID: datas.query.rationID
  323. });
  324. cal_result.glj_result.forEach(function (item) {
  325. if (!item.doc.hasOwnProperty('customQuantity')) {
  326. item.doc.customQuantity = null;
  327. }
  328. });
  329. return cal_result;
  330. }
  331. function delete_ration_glj(user_id, datas) {
  332. return function (callback) {
  333. deleteByID(datas, callback);
  334. //callback(new Error("删除子数据失败!"),null)
  335. //这个方法已经不用,先注释,稳定后再删除
  336. /* if (datas.deleteType == "RATION") {
  337. deleteByRation(datas, callback);
  338. } else if (datas.deleteType == "BILL") {
  339. deleteByBill(user_id, datas, callback);
  340. } else {
  341. deleteByID(datas, callback);
  342. }*/
  343. }
  344. }
  345. function deleteByRation(datas, callback) {
  346. let data = datas.updateData;
  347. let tasks = [];
  348. tasks.push(deleteGLJList(data));
  349. tasks.push(ration_coe_facade.delete_ration_coe(data));
  350. tasks.push(quantity_detail_facade.deleteByRation(data));
  351. tasks.push(ration_installation_facade.deleteByRation(data));
  352. async_n.parallel(tasks, function (err, result) {
  353. commonCallback(callback, result, err)
  354. })
  355. }
  356. function deleteGLJList(data) {
  357. return function (callback) {
  358. ration_glj.deleteMany({
  359. projectID: data.projectID,
  360. rationID: data.ID
  361. }, (err, result) => {
  362. commonCallback(callback, result, err)
  363. });
  364. }
  365. }
  366. function deleteByBill(user_id, datas, callback) {
  367. let tasks = [];
  368. tasks.push(startingTask("deleteByBill"));
  369. tasks.push(getRationsByBill(datas));
  370. tasks.push(deleteRationsbyBill(user_id, datas));
  371. tasks.push(deleteByMultiRations(datas));
  372. async_n.waterfall(tasks, function (err, results) {
  373. if (err) {
  374. callback(err, '');
  375. } else {
  376. callback(null, results);
  377. }
  378. })
  379. }
  380. function deleteByID(datas, callback) {
  381. deleteAndUpdateState(datas).then(function (result) {
  382. if (result.err) {
  383. callback(result.err, '');
  384. } else {
  385. let returndata = {
  386. moduleName: 'ration_glj',
  387. data: {
  388. updateTpye: commonConsts.UT_DELETE,
  389. query: datas.query,
  390. adjustState: result.adjustState
  391. }
  392. }
  393. callback(null, returndata)
  394. }
  395. })
  396. }
  397. async function deleteGLJ(IDs) {
  398. await ration_glj.deleteMany({
  399. 'ID': {
  400. $in: IDs
  401. }
  402. });
  403. }
  404. async function deleteAndUpdateState(datas) {
  405. let result = {
  406. err: null
  407. }
  408. try {
  409. await ration_glj.deleteOne(datas.query);
  410. let stateResult = await glj_calculate_facade.calculateQuantity({
  411. projectID: datas.query.projectID,
  412. rationID: datas.doc.rationID
  413. });
  414. result.adjustState = stateResult.adjustState;
  415. } catch (err) {
  416. result.err = err;
  417. }
  418. return result;
  419. }
  420. function startingTask(processName) {
  421. return function (asyncCallBack) {
  422. var result = {
  423. processName: processName,
  424. datas: []
  425. };
  426. asyncCallBack(null, result);
  427. };
  428. }
  429. function getRationsByBill(datas) {
  430. return function (results, callback) {
  431. ration.find({
  432. projectID: datas.updateData.projectID,
  433. billsItemID: datas.updateData.ID
  434. }, function (err, rations) {
  435. if (err) {
  436. callback(err, '')
  437. } else {
  438. results.rations = rations;
  439. callback(null, results)
  440. }
  441. })
  442. }
  443. }
  444. function deleteRationsbyBill(user_id, datas) {
  445. return function (results, callback) {
  446. let deleteInfo = {
  447. deleteInfo: {
  448. deleted: true,
  449. deleteDateTime: new Date(),
  450. deleteBy: user_id
  451. }
  452. };
  453. ration.update({
  454. projectID: datas.updateData.projectID,
  455. billsItemID: datas.updateData.ID
  456. }, deleteInfo, {
  457. multi: true
  458. }, (err, deleteresults) => {
  459. if (err) {
  460. callback(err, '');
  461. } else {
  462. callback(null, results);
  463. }
  464. });
  465. }
  466. }
  467. function deleteByMultiRations(datas) {
  468. return function (results, deleteCallBack) {
  469. var delete_tasks = [];
  470. var deleteOne = function (ration) {
  471. return function (callback) {
  472. ration_glj.deleteMany({
  473. projectID: ration.projectID,
  474. rationID: ration.ID
  475. }, function (err, result) {
  476. commonCallback(callback, result, err)
  477. });
  478. }
  479. }
  480. let rations = results.rations;
  481. for (let i = 0; i < rations.length; i++) {
  482. delete_tasks.push(deleteOne(rations[i]._doc));
  483. delete_tasks.push(ration_coe_facade.delete_ration_coe(rations[i]._doc));
  484. delete_tasks.push(quantity_detail_facade.deleteByRation(rations[i]._doc));
  485. delete_tasks.push(ration_installation_facade.deleteByRation(rations[i]._doc));
  486. }
  487. delete_tasks.push(quantity_detail_facade.deleteByBill(datas.updateData));
  488. async_n.parallel(delete_tasks, (err, results) => {
  489. if (err) {
  490. deleteCallBack(err, '')
  491. } else {
  492. deleteCallBack(null, results)
  493. }
  494. })
  495. }
  496. }
  497. /*
  498. function deleteByRation(doc) {
  499. return function (callback){
  500. ration_glj.deleteMany({projectID: doc.updateData.projectID, rationID: doc.updateData.ID},callback);
  501. }
  502. }
  503. */
  504. function save(user_id, datas, callback) {
  505. let operations = [];
  506. if (_.isArray(datas)) {
  507. for (let i = 0; i < datas.length; i++) {
  508. operations.push(operationMap[datas[i].updateType](user_id, datas[i]));
  509. }
  510. } else {
  511. operations.push(operationMap[datas.updateType](user_id, datas));
  512. }
  513. async_n.parallel(operations, function (err, results) {
  514. if (err) {
  515. callback(err, '');
  516. } else {
  517. if (results.length == 1) {
  518. callback(null, results[0])
  519. } else {
  520. callback(null, results)
  521. }
  522. }
  523. })
  524. }
  525. async function getLibInfo(req) {
  526. let gljLibId = null,
  527. engineerID, sessionCompilation = req.session.sessionCompilation;
  528. engineerID = req.params.engineerID;
  529. if (engineerID) {
  530. gljLibId = await getGLJLibByEngineerID(engineerID);
  531. } else {
  532. throw new Error("工程专业ID为空!");
  533. }
  534. let data = {
  535. userID: req.session.sessionUser.id,
  536. gljLibId: gljLibId,
  537. compilationId: sessionCompilation._id
  538. };
  539. return data;
  540. }
  541. async function getGLJLibByEngineerID(engineerID) {
  542. let engineeringLibModel = new EngineeringLibModel();
  543. let engineeringInfo = await engineeringLibModel.findDataByCondition({
  544. '_id': engineerID
  545. });
  546. let gljLibId = engineeringInfo.glj_lib.length > 0 && typeof engineeringInfo.glj_lib !== 'undefined' ? engineeringInfo.glj_lib[0].id : null;
  547. return gljLibId
  548. }
  549. // 获取人材机选择页面,可选的定额库-人材机库映射选项
  550. async function getLibOptions(engineerID) {
  551. // 找到工程专业绑定的定额库,再获取这些定额库引用的人材机库
  552. const engineeringLibModel = new EngineeringLibModel();
  553. const engineeringInfo = await engineeringLibModel.findDataByCondition({
  554. _id: engineerID
  555. });
  556. const rationLibs = engineeringInfo.ration_lib;
  557. const rationLibIDs = rationLibs.map(lib => lib.id);
  558. const rationLibData = await stdRationLibModel.find({
  559. ID: {
  560. $in: rationLibIDs
  561. }
  562. }, 'ID gljLib').lean();
  563. const rst = [];
  564. rationLibs.forEach(lib => {
  565. rationLibData.forEach(stdLib => {
  566. if (+lib.id === +stdLib.ID) {
  567. lib.gljLibId = stdLib.gljLib;
  568. rst.push(lib);
  569. }
  570. });
  571. });
  572. return rst;
  573. }
  574. // 获取人材机选择库页面,可选的定额库为费用定额下,所有工程专业的定额库(需要去重)
  575. async function getLibOptionsForCompilation(compilationId) {
  576. const compilation = await compilationModel.findOne({
  577. _id: mongoose.Types.ObjectId(compilationId)
  578. }, 'ration_valuation bill_valuation');
  579. const valuationIDs = [];
  580. if (compilation.ration_valuation[0] && compilation.ration_valuation[0].enable) {
  581. valuationIDs.push(compilation.ration_valuation[0].id);
  582. }
  583. if (compilation.bill_valuation[0] && compilation.bill_valuation[0].enable) {
  584. valuationIDs.push(compilation.bill_valuation[0].id);
  585. }
  586. const engineeringLibs = await engineeringModel.find({
  587. valuationID: {
  588. $in: valuationIDs
  589. },
  590. visible: true
  591. }, 'ration_lib');
  592. const rationLibs = [];
  593. const rationLibIDs = [];
  594. let hasDefalut = false;
  595. engineeringLibs.forEach(lib => {
  596. lib.ration_lib.forEach(rLib => {
  597. if (!rationLibIDs.includes(rLib.id)) {
  598. rationLibIDs.push(rLib.id);
  599. rationLibs.push({
  600. isDefault: hasDefalut ? false : rLib.isDefault,
  601. name: rLib.name,
  602. id: rLib.id
  603. });
  604. if (rLib.isDefault) {
  605. hasDefalut = true;
  606. }
  607. }
  608. });
  609. });
  610. const stdRationLibs = await stdRationLibModel.find({
  611. ID: {
  612. $in: rationLibIDs
  613. }
  614. }, 'ID gljLib').lean();
  615. rationLibs.forEach(lib => {
  616. stdRationLibs.forEach(stdLib => {
  617. if (+lib.id === +stdLib.ID) {
  618. lib.gljLibId = stdLib.gljLib;
  619. }
  620. });
  621. });
  622. return rationLibs;
  623. }
  624. function getGLJData(info, callback) {
  625. let gljDao = new GljDao();
  626. let datas = {};
  627. let gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  628. datas.distTypeTree = gljDistTypeCache;
  629. async_n.parallel([
  630. async function (cb) {
  631. try {
  632. datas.treeData = await gljDao.getMixedTree(info.gljLibId, info.userID, info.compilationId);
  633. cb(null);
  634. } catch (err) {
  635. cb(err);
  636. }
  637. },
  638. function (cb) {
  639. gljDao.getGljItems(info.gljLibId, info.userID, info.compilationId, {
  640. _id: 0
  641. }, function (err, data) {
  642. if (err) {
  643. cb(err);
  644. } else {
  645. datas.stdGLJ = data.stdGljs;
  646. datas.complementaryGLJs = data.complementaryGljs;
  647. cb(null);
  648. }
  649. });
  650. }
  651. ], function (err) {
  652. if (err) {
  653. callback(true, null);
  654. } else {
  655. callback(false, datas);
  656. }
  657. })
  658. }
  659. function getGLJSearchInfo(ration_glj) {
  660. let data = {
  661. glj_id: ration_glj.GLJID,
  662. project_id: ration_glj.projectID,
  663. code: ration_glj.code,
  664. original_code: ration_glj.original_code,
  665. name: ration_glj.name,
  666. //shortName: ration_glj.shortName,
  667. specs: ration_glj.specs ? ration_glj.specs : '',
  668. unit: ration_glj.unit ? ration_glj.unit : '',
  669. type: ration_glj.subType ? ration_glj.subType : ration_glj.type, //如果有subType,则是通过插入定额级的工料机进来的
  670. model: ration_glj.model,
  671. type_of_work: ration_glj.subType ? ration_glj.subType : ration_glj.type,
  672. base_price: ration_glj.basePrice,
  673. market_price: ration_glj.marketPrice,
  674. repositoryId: ration_glj.repositoryId,
  675. adjCoe: ration_glj.adjCoe,
  676. materialType: ration_glj.materialType,
  677. is_main_material: ration_glj.is_main_material ? ration_glj.is_main_material : 1,
  678. materialCoe: ration_glj.materialCoe,
  679. grossWeightCoe: ration_glj.grossWeightCoe,
  680. purchaseStorageRate: ration_glj.purchaseStorageRate,
  681. offSiteTransportLossRate: ration_glj.offSiteTransportLossRate,
  682. handlingLossRate: ration_glj.handlingLossRate,
  683. is_adjust_price: 0,
  684. is_evaluate: 0,
  685. is_eval_material: 0,
  686. from: ration_glj.from ? ration_glj.from : 'std', //std:标准工料机库, cpt:补充工料机库
  687. fromUser: ration_glj.fromUser,
  688. };
  689. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  690. let type = glj_type_object.getItemById(data.type);
  691. data.shortName = type.shortName;
  692. if (data.from == 'cpt') { //从补充工料机来的数据即为新增数据
  693. data.is_add = 1;
  694. }
  695. return data;
  696. }
  697. async function prepareExtData(projectID, compilation) {
  698. let ext, unitFileId;
  699. let property = await projectDao.getProjectProperty(projectID);
  700. ext = pmFacade.getExtendData(property, compilation);
  701. unitFileId = property.unitPriceFile !== undefined ? property.unitPriceFile.id : 0;
  702. return [unitFileId, ext];
  703. }
  704. function setPropertyFromStd(newGLJ, std_glj) {
  705. newGLJ.GLJID = std_glj.ID;
  706. newGLJ.name = std_glj.name;
  707. newGLJ.code = std_glj.code;
  708. newGLJ.original_code = std_glj.code;
  709. newGLJ.unit = std_glj.unit;
  710. newGLJ.specs = std_glj.specs;
  711. newGLJ.model = std_glj.model;
  712. newGLJ.basePrice = std_glj.basePrice;
  713. newGLJ.marketPrice = std_glj.basePrice;
  714. newGLJ.shortName = std_glj.shortName;
  715. newGLJ.type = std_glj.gljType;
  716. newGLJ.repositoryId = std_glj.repositoryId;
  717. newGLJ.adjCoe = std_glj.adjCoe;
  718. newGLJ.materialType = std_glj.materialType;
  719. newGLJ.materialCoe = std_glj.materialCoe;
  720. newGLJ.grossWeightCoe = std_glj.grossWeightCoe;
  721. newGLJ.purchaseStorageRate = std_glj.purchaseStorageRate;
  722. newGLJ.offSiteTransportLossRate = std_glj.offSiteTransportLossRate;
  723. newGLJ.handlingLossRate = std_glj.handlingLossRate;
  724. newGLJ.createType = 'normal';
  725. }
  726. async function addGLJ(rgList, compilation) {
  727. if (rgList.length <= 0) return {};
  728. let [newRecodes, projectGLJList] = await insertAddTypeGLJ(rgList, compilation);
  729. let stateResult = await glj_calculate_facade.calculateQuantity({
  730. projectID: rgList[0].projectID,
  731. rationID: rgList[0].rationID
  732. });
  733. let result = {
  734. newRecodes: newRecodes,
  735. projectGLJList: projectGLJList,
  736. showData: rgList,
  737. adjustState: stateResult.adjustState
  738. };
  739. return result;
  740. }
  741. async function insertAddTypeGLJ(rgList, compilation, needInsert = true) {
  742. let newRecodes = [],
  743. GLJMap = null;
  744. let projectGLJList = [];
  745. let [unitFileId, ext] = await prepareExtData(rgList[0].projectID, compilation);
  746. for (let g of rgList) {
  747. ration_facade.transGljType(compilation,g);
  748. let projectGljModel = new GLJListModel();
  749. let result = await projectGljModel.addList(getGLJSearchInfo(g), unitFileId, ext);
  750. let typeString = result.type + '';
  751. g.marketPrice = result.unit_price.market_price;
  752. g.adjustPrice = result.unit_price.base_price;
  753. g.basePrice = result.unit_price.base_price;
  754. g.isAdd = result.unit_price.is_add;
  755. g.projectGLJID = result.id;
  756. if (typeString.startsWith("2") || typeString == '4' || typeString == '5') { //只有材料类型才显示是否暂估
  757. g.isEstimate = result.is_evaluate;
  758. }
  759. g.ID = uuidV1();
  760. if (result.subList && result.subList.length > 0) {
  761. g.subList = getMixRatioShowDatas(result.subList);
  762. //对于混凝土,砂浆,配合比,组成物还要插入定额工料机
  763. if (gljUtil.isConcreteType(g.type)) {
  764. if (GLJMap == null) {
  765. let oldGLJList = await ration_glj.find({
  766. 'rationID': g.rationID
  767. });
  768. GLJMap = _.indexBy(oldGLJList, "projectGLJID");
  769. }
  770. addMixRatioToRationGLJ(g, result.subList, newRecodes, GLJMap);
  771. }
  772. }
  773. newRecodes.push(createNewRecord(g));
  774. projectGLJList.push(result);
  775. }
  776. //分摊那里调用时不用真的插入
  777. if (needInsert == true) await ration_glj.insertMany(newRecodes);
  778. return [newRecodes, projectGLJList];
  779. }
  780. async function deleteRationGLJ(data) {
  781. let deleteIDs = [data.ID];
  782. if (gljUtil.isConcreteType(data.type)) { //如果是混凝土、砂浆、配合比,还要删除没有被引用的组成物工料机
  783. let projectGljModel = new GLJListModel();
  784. let rationGLJList = await ration_glj.find({
  785. 'rationID': data.rationID
  786. });
  787. let unitFileId = await commonFacade.getUnitPriceFileId(data.projectID);
  788. let projectGLJMap = {},
  789. referenceMap = {};
  790. let deleteMix = [];
  791. for (let r of rationGLJList) {
  792. projectGLJMap[r.projectGLJID] = r;
  793. if (gljUtil.isConcreteType(r.type)) {
  794. if (r.ID == data.ID) { //是要删除的工料机
  795. deleteMix = await projectGljModel.getCompositionGLJByData(r, unitFileId);
  796. } else {
  797. let comList = await projectGljModel.getCompositionGLJByData(r, unitFileId);
  798. for (let c of comList) {
  799. referenceMap[c.id] = c;
  800. }
  801. }
  802. }
  803. }
  804. for (let d of deleteMix) {
  805. //删除不属于其它的组成物并且定额消耗为0
  806. if (referenceMap[d.id]) continue;
  807. if (projectGLJMap[d.id] && (projectGLJMap[d.id].rationItemQuantity == '0' || projectGLJMap[d.id].rationItemQuantity == 0)) {
  808. deleteIDs.push(projectGLJMap[d.id].ID);
  809. }
  810. }
  811. await ration_glj.deleteMany({
  812. 'ID': {
  813. "$in": deleteIDs
  814. }
  815. });
  816. } else {
  817. await ration_glj.deleteOne({
  818. ID: data.ID
  819. });
  820. }
  821. let calcResult = await glj_calculate_facade.calculateQuantity({
  822. projectID: data.projectID,
  823. rationID: data.rationID
  824. });
  825. calcResult.deleteList = deleteIDs;
  826. return calcResult;
  827. }
  828. function addMixRatioToRationGLJ(g, subList, newRecodes, GLJMap) {
  829. let newMap = {};
  830. for (let mr of subList) {
  831. //先查找该定额下是否已经有了工料机了,有就不用再插入了
  832. newMap[mr.id] = mr; //新增的定额工料机映射表
  833. if (GLJMap[mr.id] || _.find(newRecodes, {
  834. 'projectGLJID': mr.id
  835. })) continue;
  836. //没有的情况下,生成一条定额工料机计录
  837. let newMr = {
  838. projectID: g.projectID,
  839. GLJID: mr.glj_id,
  840. rationID: g.rationID,
  841. rationItemQuantity: 0,
  842. quantity: 0,
  843. name: mr.name,
  844. code: mr.code,
  845. original_code: mr.original_code,
  846. unit: mr.unit,
  847. specs: mr.specs,
  848. from: mr.from,
  849. createType: 'add',
  850. shortName: mr.unit_price.short_name,
  851. billsItemID: g.billsItemID,
  852. type: mr.type,
  853. model: mr.model,
  854. repositoryId: g.repositoryId,
  855. projectGLJID: mr.id,
  856. adjCoe: mr.adjCoe
  857. };
  858. newMr.ID = uuidV1();
  859. newRecodes.push(newMr);
  860. }
  861. return newMap;
  862. }
  863. async function replaceGLJByData(data, compilation, needUpdate = true, rationGLJList) {
  864. let projectGljModel = new GLJListModel(),
  865. newRecodes = [],
  866. deleteList = [];
  867. let [unitFileId, ext] = await prepareExtData(data.projectID, compilation);
  868. let result = await projectGljModel.addList(getGLJSearchInfo(data), unitFileId, ext);
  869. data.projectGLJID = result.id;
  870. if (data.toCommercial == true) { //从混凝土改成商品混凝土,
  871. let [contype, newR] = await concreteTypeToCommercial(data, needUpdate);
  872. newRecodes.push(newR);
  873. data = contype;
  874. } else {
  875. let [newList, tdelList] = await replaceMixRatio(data, result, unitFileId, needUpdate, rationGLJList);
  876. newRecodes = newList;
  877. deleteList = tdelList;
  878. if (needUpdate == true) await ration_glj.findOneAndUpdate({
  879. ID: data.ID,
  880. projectID: data.projectID
  881. }, data); //更新定额工料机
  882. }
  883. return {
  884. data: data,
  885. newRecodes: newRecodes,
  886. deleteList: deleteList,
  887. projectGLJ: result
  888. };
  889. }
  890. async function concreteTypeToCommercial(data, needUpdate) {
  891. //旧的自定义消耗量改为0
  892. let contype = null;
  893. if (needUpdate == true) {
  894. contype = await ration_glj.findOneAndUpdate({
  895. ID: data.originalID
  896. }, {
  897. customQuantity: '0'
  898. });
  899. } else {
  900. contype = {
  901. customQuantity: '0'
  902. }
  903. }
  904. //因为商品混凝土是没有组成物的,所以不用考虑组成物的情况
  905. let new_glj = createComercialConcreteData(data);
  906. if (needUpdate == true) await ration_glj.create(new_glj);
  907. return [contype, new_glj];
  908. }
  909. function createComercialConcreteData(data) {
  910. data.ID = uuidV1();
  911. return createNewRecord(data);
  912. }
  913. async function replaceMixRatio(g, result, unitFileId, needUpdate, trationGLJList) {
  914. let newRecodes = [],
  915. deleteList = [];
  916. if (gljUtil.isConcreteType(g.type) || gljUtil.isCommercialConcreteType(g.type)) { //混凝土大类,商品混凝土属于相同大类,替换前和替换后只判断一个就好了
  917. let IDMap = {},
  918. projectGLJMap = {},
  919. referenceMap = {},
  920. concreteList = [],
  921. newMap = {};
  922. let projectGljModel = new GLJListModel();
  923. let rationGLJList = trationGLJList ? trationGLJList : await ration_glj.find({
  924. 'rationID': g.rationID
  925. });
  926. for (let r of rationGLJList) {
  927. IDMap[r.ID] = r;
  928. projectGLJMap[r.projectGLJID] = r;
  929. if (gljUtil.isConcreteType(r.type) && r.ID != g.ID) concreteList.push(r) //除了本身,记录一下其它混凝土类型
  930. }
  931. if (result.subList && result.subList.length > 0) {
  932. newMap = addMixRatioToRationGLJ(g, result.subList, newRecodes, projectGLJMap); //先生成要添加的工料机
  933. }
  934. let oldMixList = await projectGljModel.getCompositionGLJByData(IDMap[g.ID], unitFileId);
  935. for (let c of concreteList) { //找出要删除的子定额工料机(没人引用,并且定额消耗量为0)
  936. let temList = await projectGljModel.getCompositionGLJByData(c, unitFileId);
  937. for (let t of temList) {
  938. referenceMap[t.id] = t;
  939. }
  940. }
  941. for (let o of oldMixList) {
  942. if (newMap[o.id] || referenceMap[o.id]) continue; //如果两个地方有一个存在,那么就不用删除
  943. //没有被其它地方引用并且定额消耗量为0,就删除对应的定额工料机
  944. if (projectGLJMap[o.id] && (projectGLJMap[o.id].rationItemQuantity == '0' || projectGLJMap[o.id].rationItemQuantity == 0)) deleteList.push(projectGLJMap[o.id].ID)
  945. }
  946. }
  947. if (deleteList.length > 0 && needUpdate == true) await ration_glj.deleteMany({
  948. 'ID': {
  949. "$in": deleteList
  950. }
  951. }); //删除定额工料机
  952. if (newRecodes.length > 0 && needUpdate == true) await ration_glj.insertMany(newRecodes);
  953. return [newRecodes, deleteList]
  954. }
  955. async function replaceGLJ(data, compilation) {
  956. let rdata = {};
  957. let r_result = await replaceGLJByData(data, compilation);
  958. data = r_result.data;
  959. let stateResult = await glj_calculate_facade.calculateQuantity({
  960. projectID: data.projectID,
  961. rationID: data.rationID
  962. }, null, true);
  963. rdata.data = data;
  964. rdata.projectGLJ = r_result.projectGLJ;
  965. rdata.adjustState = stateResult.adjustState;
  966. rdata.name = stateResult.rationName;
  967. rdata.newRecodes = r_result.newRecodes;
  968. rdata.deleteList = r_result.deleteList;
  969. rdata.glj_result = stateResult.glj_result;
  970. return rdata;
  971. }
  972. async function replaceMixRatioForMReplace(tasks, result, unitFileId) {
  973. let allNewRecodes = [],
  974. allDeleteList = [];
  975. for (let t of tasks) { //要新增一条商品混凝土
  976. if (t.insertOne) {
  977. t.insertOne.document.projectGLJID = result.id;
  978. t.insertOne.document = createComercialConcreteData(t.insertOne.document);
  979. allNewRecodes.push(t.insertOne.document);
  980. } else if (t.updateOne && t.updateOne.update.isConcrete == true) { //过滤掉只更新自定义消耗量的task
  981. let tem = _.cloneDeep(t.updateOne.update);
  982. tem.ID = t.updateOne.filter.ID;
  983. tem.rationID = t.updateOne.filter.rationID;
  984. delete t.updateOne.filter.rationID;
  985. delete t.updateOne.update.isConcrete;
  986. let [newRecodes, deleteList] = await replaceMixRatio(tem, result, unitFileId);
  987. allNewRecodes = allNewRecodes.concat(newRecodes);
  988. allDeleteList = allDeleteList.concat(deleteList);
  989. }
  990. }
  991. return {
  992. newRecodes: allNewRecodes,
  993. deleteList: allDeleteList
  994. }
  995. }
  996. async function mReplaceGLJ(data, compilation) {
  997. let mresult = {},
  998. mixResult = null,
  999. noNeedCal = true;
  1000. let projectGljModel = new GLJListModel();
  1001. let [unitFileId, ext] = await prepareExtData(data.doc.projectID, compilation);
  1002. //
  1003. let result = await projectGljModel.addList(getGLJSearchInfo(data.doc), unitFileId, ext);
  1004. let typeString = result.type + '';
  1005. let newDoc = {};
  1006. newDoc.projectGLJID = result.id;
  1007. let rationList = []; //await ration_glj.distinct('rationID', data.query);
  1008. for (let t of data.tasks) {
  1009. if (t.updateOne) {
  1010. rationList.push(t.updateOne.filter.rationID);
  1011. if (t.updateOne.update.code) t.updateOne.update.projectGLJID = result.id; //如果是不是只修改自定义消耗的task,更新项目工料机ID
  1012. }
  1013. }
  1014. if (gljUtil.isConcreteType(result.unit_price.type) || gljUtil.isCommercialConcreteType(result.unit_price.type)) {
  1015. mixResult = await replaceMixRatioForMReplace(data.tasks, result, unitFileId);
  1016. noNeedCal = null;
  1017. }
  1018. await ration_glj.bulkWrite(data.tasks);
  1019. let [stateList, glj_result] = await changAdjustState(data, rationList, noNeedCal);
  1020. data.doc = newDoc;
  1021. mresult.data = data;
  1022. mresult.stateList = stateList;
  1023. mresult.newRecodes = mixResult ? mixResult.newRecodes : [];
  1024. mresult.deleteList = mixResult ? mixResult.deleteList : [];
  1025. mresult.glj_result = glj_result;
  1026. mresult.projectGLJ = result;
  1027. return mresult
  1028. }
  1029. async function updateRationGLJByEdit(data) {
  1030. var doc = data.doc;
  1031. var result;
  1032. if (doc.hasOwnProperty('customQuantity')) {
  1033. result = await doCustomQuantityUpdate(data)
  1034. } else {
  1035. result = await doRationGLJUpdate(data);
  1036. }
  1037. return result;
  1038. }
  1039. async function updateRationGLJFromDoc(rg, doc, priceInfo) {
  1040. let gljListModel = new GLJListModel();
  1041. let projectGLJ = getGLJSearchInfo(rg);
  1042. for (let key in doc) {
  1043. projectGLJ[key] = doc[key]
  1044. }
  1045. projectGLJ.base_price = priceInfo.base_price;
  1046. projectGLJ.market_price = priceInfo.market_price;
  1047. let projcetGLJ_n = await gljListModel.modifyGLJ(projectGLJ, rg);
  1048. doc.code = projcetGLJ_n.code;
  1049. doc.projectGLJID = projcetGLJ_n.id;
  1050. if (projcetGLJ_n.unit_price.is_add == 1) {
  1051. doc.createType = 'replace';
  1052. doc.rcode = projcetGLJ_n.original_code;
  1053. } else {
  1054. doc.createType = 'normal';
  1055. doc.rcode = '';
  1056. }
  1057. await ration_glj.findOneAndUpdate({
  1058. ID: rg.ID
  1059. }, doc);
  1060. return [projcetGLJ_n, doc]
  1061. }
  1062. async function doRationGLJUpdate(data) {
  1063. let resutl = {};
  1064. let priceInfo = data.priceInfo;
  1065. let rg = await ration_glj.findOne(data.query);
  1066. let [projcetGLJ_n, doc] = await updateRationGLJFromDoc(rg, data.doc, priceInfo);
  1067. //取价格
  1068. let gljListModel = new GLJListModel();
  1069. gljListModel.getGLJPrice(projcetGLJ_n);
  1070. doc.basePrice = projcetGLJ_n.unit_price.base_price;
  1071. doc.marketPrice = projcetGLJ_n.unit_price.market_price;
  1072. doc.adjustPrice = projcetGLJ_n.adjust_price;
  1073. doc.isAdd = projcetGLJ_n.unit_price.is_add;
  1074. resutl.doc = doc;
  1075. let stateResult = await glj_calculate_facade.calculateQuantity({
  1076. projectID: data.query.projectID,
  1077. rationID: data.query.rationID
  1078. }, null, true);
  1079. resutl.adjustState = stateResult.adjustState;
  1080. resutl.name = stateResult.rationName;
  1081. return resutl;
  1082. }
  1083. async function updateProportion(proportionList, projectID, rationID) {
  1084. const bulks = proportionList.map(item => {
  1085. return {
  1086. updateOne: {
  1087. filter: {
  1088. ID: item.ID
  1089. },
  1090. update: {
  1091. $set: {
  1092. adjustProportion: item.adjustProportion
  1093. }
  1094. }
  1095. }
  1096. }
  1097. });
  1098. if (bulks.length) {
  1099. await ration_glj.bulkWrite(bulks);
  1100. }
  1101. const calcRst = await glj_calculate_facade.calculateQuantity({
  1102. projectID,
  1103. rationID
  1104. }, null, true);
  1105. const rationGLJ = {
  1106. quantityRefresh: true,
  1107. glj_result: calcRst.glj_result
  1108. };
  1109. const ration = {
  1110. ID: calcRst.rationID,
  1111. adjustState: calcRst.adjustState,
  1112. name: calcRst.rationName
  1113. };
  1114. return {
  1115. ration_glj: rationGLJ,
  1116. ration,
  1117. add: [],
  1118. delete: [],
  1119. replace: []
  1120. };
  1121. }
  1122. async function getGLJClass(info, data) {
  1123. let result = {
  1124. exist: false
  1125. }
  1126. //检查补充工料机中是否已经存在一样的记录了
  1127. let condition = {
  1128. userId: info.userID,
  1129. compilationId: info.compilationId,
  1130. code: data.code,
  1131. name: data.name,
  1132. unit: data.unit,
  1133. gljType: data.type,
  1134. basePrice: data.basePrice
  1135. }
  1136. if (data.specs != null && data.specs != undefined && data.specs != '') {
  1137. condition['specs'] = data.specs;
  1138. }
  1139. let glj = await complementaryGljModel.find(condition);
  1140. if (glj.length > 0) { //如果已存在就直接返回,不用再新增了
  1141. result.exist = true;
  1142. return result
  1143. }
  1144. //查找工料机类型树
  1145. let items = await compleClassModel.find({
  1146. userId: info.userID,
  1147. compilationId: info.compilationId
  1148. });
  1149. result.items = items;
  1150. return result;
  1151. }
  1152. async function insertGLJAsRation(data, compilation) {
  1153. let gljList = data.gljList;
  1154. if (data.hasOwnProperty("selectedSerialNo")) { //如果需要,更新序列号。
  1155. let query = {
  1156. projectID: data.projectID,
  1157. billsItemID: data.billsItemID,
  1158. serialNo: {
  1159. $gt: data.selectedSerialNo
  1160. }
  1161. }
  1162. await ration.update(query, {
  1163. $inc: {
  1164. serialNo: gljList.length
  1165. }
  1166. }, {
  1167. multi: true
  1168. });
  1169. }
  1170. let [unitFileId, ext] = await prepareExtData(data.projectID, compilation);
  1171. for (let glj of gljList) {
  1172. let p_glj = getGLJSearchInfo(glj);
  1173. let projectGljModel = new GLJListModel();
  1174. let result = await projectGljModel.addList(p_glj, unitFileId, ext); //逐条添加到项目工料机
  1175. let typeString = result.type + '';
  1176. glj.marketPrice = result.unit_price.market_price;
  1177. glj.adjustPrice = result.unit_price.base_price;
  1178. glj.basePrice = result.unit_price.base_price;
  1179. glj.isAdd = result.unit_price.is_add;
  1180. glj.projectGLJID = result.id;
  1181. if (typeString.startsWith("2") || typeString == '4' || typeString == '5') { //只有材料类型才显示是否暂估
  1182. glj.isEstimate = result.is_evaluate;
  1183. }
  1184. }
  1185. await ration.insertMany(gljList);
  1186. return gljList;
  1187. }
  1188. async function getRationTypeGLJQuantity(projectID) {
  1189. let rations = await ration.find({
  1190. 'projectID': projectID,
  1191. 'type': 3,
  1192. 'deleteInfo': null
  1193. }, ['ID', 'projectGLJID', 'quantity']);
  1194. return rations;
  1195. }
  1196. async function changAdjustState(data, rationList, noNeedCal) {
  1197. let stateList = [],
  1198. glj_result = [];
  1199. for (let r of rationList) {
  1200. let stateResult = await glj_calculate_facade.calculateQuantity({
  1201. projectID: data.query.projectID,
  1202. rationID: r
  1203. }, noNeedCal, true);
  1204. if (stateResult) {
  1205. stateList.push({
  1206. rationID: r,
  1207. adjustState: stateResult.adjustState,
  1208. name: stateResult.rationName
  1209. });
  1210. glj_result = glj_result.concat(stateResult.glj_result)
  1211. }
  1212. }
  1213. return [stateList, glj_result];
  1214. }
  1215. async function getGLJDataByCodes(data, compilation) {
  1216. let gljLibId = await getGLJLibByEngineerID(data.engineerID);
  1217. let gljDatas = [];
  1218. if (gljLibId) {
  1219. let stdList = await std_glj_lib_gljList_model.find({
  1220. 'repositoryId': gljLibId,
  1221. code: {
  1222. '$in': data.codes
  1223. }
  1224. });
  1225. if (stdList.length > 0) {
  1226. let property = await projectDao.getProjectProperty(data.projectID);
  1227. let ext = pmFacade.getExtendData(property, compilation); //多单价处理
  1228. for (let s of stdList) {
  1229. let tem = JSON.parse(JSON.stringify(s));
  1230. if (ext && ext.priceField && tem && tem.priceProperty) {
  1231. tem.basePrice = tem.priceProperty[ext.priceField];
  1232. }
  1233. gljDatas.push(tem);
  1234. }
  1235. }
  1236. }
  1237. return gljDatas
  1238. }
  1239. async function testError() {
  1240. throw new Error('test Error');
  1241. }
  1242. function getData(projectID, callback, isReport) {
  1243. function findRation(rations, ID) {
  1244. let ration = rations.find(function getElement(element) {
  1245. return element.ID == ID;
  1246. });
  1247. return ration && ration._doc;
  1248. };
  1249. ration_glj.find({
  1250. 'projectID': projectID
  1251. }, (err, datas) => {
  1252. if (err) {
  1253. callback(1, '', null);
  1254. } else {
  1255. if (isReport) { // 调价中间件机制
  1256. ration.find({
  1257. 'projectID': projectID
  1258. }, ['ID', 'code', 'name', 'quantityCoe', 'quantity', 'rationQuantityCoe'],
  1259. function cbData(err, rations) {
  1260. if (!err) {
  1261. for (let i = 0; i < datas.length; i++) {
  1262. let glj = datas[i]._doc;
  1263. let ration = findRation(rations, glj.rationID);
  1264. let coe = 1;
  1265. if (ration && ration.quantityCoe) {
  1266. if ([gljType.LABOUR].includes(glj.type)) {
  1267. if (ration.quantityCoe._doc.labour)
  1268. coe = ration.quantityCoe._doc.labour;
  1269. } else if (
  1270. [gljType.GENERAL_MATERIAL,
  1271. gljType.CONCRETE,
  1272. gljType.MORTAR,
  1273. gljType.MIX_RATIO,
  1274. gljType.COMMERCIAL_CONCRETE,
  1275. gljType.COMMERCIAL_MORTAR,
  1276. gljType.OTHER_MATERIAL
  1277. ].includes(glj.type)) {
  1278. if (ration.quantityCoe._doc.material)
  1279. coe = ration.quantityCoe._doc.material;
  1280. } else if ([gljType.GENERAL_MACHINE,
  1281. gljType.MACHINE_COMPOSITION,
  1282. gljType.MACHINE_LABOUR
  1283. ].includes(glj.type)) {
  1284. if (ration.quantityCoe._doc.machine)
  1285. coe = ration.quantityCoe._doc.machine;
  1286. } else if ([gljType.MAIN_MATERIAL].includes(glj.type)) {
  1287. if (ration.quantityCoe._doc.main)
  1288. coe = ration.quantityCoe._doc.main;
  1289. } else if ([gljType.EQUIPMENT].includes(glj.type)) {
  1290. if (ration.quantityCoe._doc.equipment)
  1291. coe = ration.quantityCoe._doc.equipment;
  1292. };
  1293. };
  1294. if ((!coe) || (coe == '0')) coe = 1;
  1295. glj.quantity = glj.quantity * coe;
  1296. }
  1297. }
  1298. });
  1299. };
  1300. callback(0, consts.projectConst.RATION_GLJ, datas);
  1301. }
  1302. })
  1303. }
  1304. function commonCallback(callback, result, err) {
  1305. if (err) {
  1306. callback(err, '');
  1307. } else {
  1308. callback(null, result);
  1309. }
  1310. }