ration_glj_facade.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /**
  2. * Created by chen on 2017/6/29.
  3. */
  4. let mongoose = require('mongoose');
  5. const uuidV1 = require('uuid/v1');
  6. let consts = require('../../main/models/project_consts')
  7. let commonConsts = consts.commonConst;
  8. let _ = require("lodash");
  9. let ration_glj = mongoose.model('ration_glj');
  10. import GLJListModel from '../../glj/models/glj_list_model';
  11. let std_glj_lib_gljList_model = mongoose.model('std_glj_lib_gljList');
  12. let async_n = require("async");
  13. let ration = mongoose.model('ration');
  14. let ration_coe_facade = require('./ration_coe_facade');
  15. let ration_coe = mongoose.model('ration_coe');
  16. let std_ration_lib_ration_items = mongoose.model('std_ration_lib_ration_items');
  17. let glj_calculate_facade = require('./glj_calculate_facade');
  18. let glj_type_util = require('../../../public/cache/std_glj_type_util');
  19. let quantity_detail_facade = require('../../main/facade/quantity_detail_facade');
  20. let ration_installation_facade = require('../../main/facade/ration_installation_facade');
  21. let logger = require("../../../logs/log_helper").logger;
  22. import stdgljutil from "../../../public/cache/std_glj_type_util";
  23. import EngineeringLibModel from "../../users/models/engineering_lib_model";
  24. import GljDao from "../../complementary_glj_lib/models/gljModel";
  25. const complementaryGljModel = mongoose.model('complementary_glj_lib');
  26. const stdGljModel = mongoose.model('std_glj_lib_gljList');
  27. const gljClassModel = mongoose.model('std_glj_lib_gljClass');
  28. import projCounter from '../../main/models/proj_counter_model';
  29. module.exports = {
  30. save: save,
  31. getData: getData,
  32. deleteByRation: deleteByRation,
  33. getQuantityByProjectGLJ: getQuantityByProjectGLJ,
  34. getLibInfo: getLibInfo,
  35. getGLJData: getGLJData,
  36. addGLJ: addGLJ,
  37. replaceGLJ: replaceGLJ,
  38. mReplaceGLJ: mReplaceGLJ,
  39. updateRationGLJByEdit: updateRationGLJByEdit,
  40. getGLJClass: getGLJClass,
  41. insertGLJAsRation: insertGLJAsRation,
  42. getRationTypeGLJQuantity:getRationTypeGLJQuantity,
  43. getInfoFromProjectGLJ:getInfoFromProjectGLJ,
  44. createNewRecord:createNewRecord
  45. }
  46. let operationMap = {
  47. 'ut_create': create_ration_glj,
  48. 'ut_update': update_ration_glj,
  49. 'ut_delete': delete_ration_glj
  50. };
  51. let updateFunctionMap = {
  52. 'normalUpdate': normalUpdate
  53. };
  54. /**
  55. * 根据项目工料机ID和项目ID取消耗量
  56. *
  57. * @param {object} condition
  58. * @return Array
  59. */
  60. async function getQuantityByProjectGLJ(condition) {
  61. let query = {
  62. $and: [
  63. {'projectID': condition.projectID},
  64. {'projectGLJID': {$in: condition.projectGLJIDList}}
  65. ]
  66. }
  67. let results = await ration_glj.find(query, ['projectGLJID', 'quantity', 'rationID'], {sort: {projectGLJID: 1}});
  68. let rationList = _.uniq(_.map(results, 'rationID'));
  69. let rationQuery = {
  70. $and: [
  71. {'projectID': condition.projectID},
  72. {'ID': {$in: rationList}},
  73. {'deleteInfo': null}
  74. ]
  75. }
  76. let rations = await ration.find(rationQuery, ['ID', 'quantity']);
  77. return combineQuantity(results, rations);
  78. }
  79. function combineQuantity(results, rations) {
  80. let resultList = [];
  81. _.forEach(results, function (data) {
  82. let tmp = {
  83. projectGLJID: data.projectGLJID,
  84. quantity: Number(data.quantity)
  85. }
  86. let ration = _.find(rations, {ID: data.rationID});
  87. if (ration) {
  88. tmp.rationID = ration.ID;
  89. tmp.rationQuantity = ration.quantity ? Number(ration.quantity) : undefined;
  90. }
  91. resultList.push(tmp);
  92. /* if(resultMap.hasOwnProperty(data.projectGLJID)){
  93. resultMap[data.projectGLJID] += data.quantity;
  94. }else {
  95. resultMap[data.projectGLJID] = data.quantity;
  96. }*/
  97. });
  98. /* var resultList =[];
  99. for(let key in resultMap){
  100. let newObject = {
  101. 'projectGLJID':key,
  102. 'quantity':resultMap[key]
  103. }
  104. resultList.push(newObject)
  105. }*/
  106. return resultList;
  107. }
  108. function get_lib_glj_info(ration_glj) {
  109. return function (result, cb) {
  110. std_glj_lib_gljList_model.findOne({'ID': ration_glj.GLJID}, (err, glj) => {
  111. if (err) {
  112. cb(err, '')
  113. } else if (glj) {
  114. ration_glj.name = glj.name;
  115. ration_glj.code = glj.code;
  116. ration_glj.original_code = glj.code;
  117. ration_glj.unit = glj.unit;
  118. ration_glj.specs = glj.specs;
  119. ration_glj.basePrice = glj.basePrice;
  120. ration_glj.shortName = glj.shortName;
  121. ration_glj.type = glj.gljType;
  122. ration_glj.repositoryId = glj.repositoryId;
  123. ration_glj.adjCoe = glj.adjCoe;
  124. getInfoFromProjectGLJ(ration_glj).then(function (info) {
  125. if (info) {
  126. let tem = {};
  127. tem.newRecode = createNewRecord(info);
  128. tem.showData = info;
  129. result.datas.push(tem);
  130. cb(null, result);
  131. } else {
  132. cb(new Error('get project glj error'), null);
  133. }
  134. });
  135. } else {
  136. cb(null, result);
  137. }
  138. })
  139. }
  140. }
  141. function createNewRecord(ration_glj) {
  142. let newRecoed = {};
  143. newRecoed.ID = ration_glj.ID;
  144. newRecoed.projectID = ration_glj.projectID;
  145. newRecoed.GLJID = ration_glj.GLJID;
  146. newRecoed.rationID = ration_glj.rationID;
  147. newRecoed.rationItemQuantity = ration_glj.rationItemQuantity;
  148. newRecoed.quantity = ration_glj.quantity;
  149. newRecoed.name = ration_glj.name;
  150. newRecoed.code = ration_glj.code;
  151. newRecoed.original_code = ration_glj.original_code;
  152. newRecoed.unit = ration_glj.unit;
  153. newRecoed.specs = ration_glj.specs;
  154. newRecoed.from = ration_glj.from ? ration_glj.from : undefined;
  155. newRecoed.createType = ration_glj.createType ? ration_glj.createType : undefined;
  156. newRecoed.shortName = ration_glj.shortName;
  157. newRecoed.billsItemID = ration_glj.billsItemID;
  158. newRecoed.type = ration_glj.type;
  159. newRecoed.repositoryId = ration_glj.repositoryId;
  160. newRecoed.projectGLJID = ration_glj.projectGLJID;
  161. newRecoed.adjCoe = ration_glj.adjCoe
  162. return newRecoed
  163. }
  164. async function getInfoFromProjectGLJ(ration_glj) {
  165. let data = getGLJSearchInfo(ration_glj);
  166. try {
  167. let projectGljModel = new GLJListModel();
  168. let result = await projectGljModel.addList(data);
  169. let typeString = result.type+"";
  170. ration_glj.marketPrice = result.unit_price.market_price;
  171. ration_glj.adjustPrice = result.unit_price.base_price;
  172. ration_glj.basePrice = result.unit_price.base_price;
  173. ration_glj.projectGLJID = result.id;
  174. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  175. ration_glj.isEstimate = result.is_evaluate;
  176. }
  177. if (result.hasOwnProperty('subList') && result.subList.length > 0) {
  178. ration_glj.subList = getMixRatioShowDatas(result.subList);
  179. }
  180. return ration_glj;
  181. } catch (err) {
  182. logger.err(err);
  183. return null;
  184. }
  185. }
  186. function getMixRatioShowDatas(subList) {
  187. var temRationGLJs = [];
  188. for (let pg of subList) {
  189. var tem = {
  190. projectGLJID: pg.id,
  191. code: pg.code,
  192. name: pg.name,
  193. specs: pg.specs,
  194. unit: pg.unit,
  195. shortName: pg.unit_price.short_name,
  196. rationItemQuantity: pg.ratio_data.consumption,
  197. basePrice: pg.unit_price.base_price,
  198. marketPrice: pg.unit_price.market_price,
  199. adjustPrice: pg.adjust_price,
  200. isEstimate: pg.is_evaluate,
  201. isMixRatio: true,
  202. isAdd: pg.unit_price.is_add,
  203. GLJID: pg.glj_id
  204. }
  205. temRationGLJs.push(tem);
  206. }
  207. temRationGLJs = _.sortBy(temRationGLJs, 'code');
  208. return temRationGLJs;
  209. }
  210. function create_ration_glj(user_id, datas) {
  211. return function (callback) {
  212. let ration_glj_list = datas.ration_glj_list;
  213. var tasks = [];
  214. tasks.push(startingTask("get glj info"))
  215. for (let i = 0; i < ration_glj_list.length; i++) {
  216. ration_glj_list[i].ID = uuidV1();
  217. tasks.push(get_lib_glj_info(ration_glj_list[i]))
  218. }
  219. async_n.waterfall(tasks, (err, results) => {
  220. if (err) {
  221. callback(err, results)
  222. } else {
  223. let newRecords = [];
  224. let showDatas = [];
  225. for (let r of results.datas) {
  226. if (r) {
  227. newRecords.push(r.newRecode);
  228. showDatas.push(r.showData);
  229. }
  230. }
  231. if (newRecords.length > 0) {
  232. ration_glj.insertMany(newRecords, (err, doc) => {
  233. if (err) {
  234. callback(err, null);
  235. } else {
  236. let returndata = {
  237. updateTpye: commonConsts.UT_CREATE,
  238. moduleName: 'ration_glj',
  239. data: {
  240. newRecords: newRecords,
  241. showDatas: showDatas
  242. }
  243. }
  244. callback(null, returndata)
  245. }
  246. });
  247. } else {
  248. logger.info("can't find gljs")
  249. callback(null, null)
  250. }
  251. }
  252. })
  253. }
  254. }
  255. function update_ration_glj(user_id, datas) {
  256. if (datas.updateFunction) {
  257. return updateFunctionMap[datas.updateFunction](user_id, datas);
  258. } else {
  259. return normalUpdate(user_id, datas);
  260. }
  261. }
  262. function normalUpdate(user_id, datas) {
  263. return function (callback) {
  264. ration_glj.update(datas.query, datas.doc, (err, result) => {
  265. if (err) {
  266. callback(err, '');
  267. } else {
  268. let returndata = {
  269. moduleName: 'ration_glj',
  270. data: {
  271. updateTpye: commonConsts.UT_UPDATE,
  272. query: datas.query,
  273. doc: datas.doc
  274. }
  275. }
  276. callback(null, returndata)
  277. }
  278. })
  279. }
  280. }
  281. async function doCustomQuantityUpdate(datas) {
  282. let result = await ration_glj.findOneAndUpdate(datas.query, datas.doc);
  283. let cal_result = await glj_calculate_facade.calculateQuantity({
  284. projectID: datas.query.projectID,
  285. rationID: datas.query.rationID
  286. });
  287. cal_result.glj_result.forEach(function (item) {
  288. if (!item.doc.hasOwnProperty('customQuantity')) {
  289. item.doc.customQuantity = null;
  290. }
  291. });
  292. return cal_result;
  293. }
  294. function delete_ration_glj(user_id, datas) {
  295. return function (callback) {
  296. if (datas.deleteType == "RATION") {
  297. deleteByRation(datas, callback);
  298. } else if (datas.deleteType == "BILL") {
  299. deleteByBill(user_id, datas, callback);
  300. } else {
  301. deleteByID(datas, callback);
  302. }
  303. }
  304. }
  305. function deleteByRation(datas, callback) {
  306. let data = datas.updateData;
  307. let tasks = [];
  308. tasks.push(deleteGLJList(data));
  309. tasks.push(ration_coe_facade.delete_ration_coe(data));
  310. tasks.push(quantity_detail_facade.deleteByRation(data));
  311. tasks.push(ration_installation_facade.deleteByRation(data));
  312. async_n.parallel(tasks, function (err, result) {
  313. commonCallback(callback, result, err)
  314. })
  315. }
  316. function deleteGLJList(data) {
  317. return function (callback) {
  318. ration_glj.deleteMany({projectID: data.projectID, rationID: data.ID}, (err, result) => {
  319. commonCallback(callback, result, err)
  320. });
  321. }
  322. }
  323. function deleteByBill(user_id, datas, callback) {
  324. let tasks = [];
  325. tasks.push(startingTask("deleteByBill"));
  326. tasks.push(getRationsByBill(datas));
  327. tasks.push(deleteRationsbyBill(user_id, datas));
  328. tasks.push(deleteByMultiRations(datas));
  329. async_n.waterfall(tasks, function (err, results) {
  330. if (err) {
  331. callback(err, '');
  332. } else {
  333. callback(null, results);
  334. }
  335. })
  336. }
  337. function deleteByID(datas, callback) {
  338. deleteAndUpdateState(datas).then(function (result) {
  339. if (result.err) {
  340. callback(result.err, '');
  341. } else {
  342. let returndata = {
  343. moduleName: 'ration_glj',
  344. data: {
  345. updateTpye: commonConsts.UT_DELETE,
  346. query: datas.query,
  347. adjustState: result.adjustState
  348. }
  349. }
  350. callback(null, returndata)
  351. }
  352. })
  353. }
  354. async function deleteAndUpdateState(datas) {
  355. let result = {
  356. err: null
  357. }
  358. try {
  359. await ration_glj.deleteOne(datas.query);
  360. let stateResult = await glj_calculate_facade.calculateQuantity({
  361. projectID: datas.query.projectID,
  362. rationID: datas.doc.rationID
  363. });
  364. result.adjustState = stateResult.adjustState;
  365. } catch (err) {
  366. result.err = err;
  367. }
  368. return result;
  369. }
  370. function startingTask(processName) {
  371. return function (asyncCallBack) {
  372. var result = {
  373. processName: processName,
  374. datas: []
  375. };
  376. asyncCallBack(null, result);
  377. };
  378. }
  379. function getRationsByBill(datas) {
  380. return function (results, callback) {
  381. ration.find({
  382. projectID: datas.updateData.projectID,
  383. billsItemID: datas.updateData.ID,
  384. deleteInfo: null
  385. }, function (err, rations) {
  386. if (err) {
  387. callback(err, '')
  388. } else {
  389. results.rations = rations;
  390. callback(null, results)
  391. }
  392. })
  393. }
  394. }
  395. function deleteRationsbyBill(user_id, datas) {
  396. return function (results, callback) {
  397. let deleteInfo = {
  398. deleteInfo: {deleted: true, deleteDateTime: new Date(), deleteBy: user_id}
  399. };
  400. ration.update({
  401. projectID: datas.updateData.projectID,
  402. billsItemID: datas.updateData.ID
  403. }, deleteInfo, {multi: true}, (err, deleteresults) => {
  404. if (err) {
  405. callback(err, '');
  406. } else {
  407. callback(null, results);
  408. }
  409. });
  410. }
  411. }
  412. function deleteByMultiRations(datas) {
  413. return function (results, deleteCallBack) {
  414. var delete_tasks = [];
  415. var deleteOne = function (ration) {
  416. return function (callback) {
  417. ration_glj.deleteMany({projectID: ration.projectID, rationID: ration.ID}, function (err, result) {
  418. commonCallback(callback, result, err)
  419. });
  420. }
  421. }
  422. let rations = results.rations;
  423. for (let i = 0; i < rations.length; i++) {
  424. delete_tasks.push(deleteOne(rations[i]._doc));
  425. delete_tasks.push(ration_coe_facade.delete_ration_coe(rations[i]._doc));
  426. delete_tasks.push(quantity_detail_facade.deleteByRation(rations[i]._doc));
  427. delete_tasks.push(ration_installation_facade.deleteByRation(rations[i]._doc));
  428. }
  429. delete_tasks.push(quantity_detail_facade.deleteByBill(datas.updateData));
  430. async_n.parallel(delete_tasks, (err, results) => {
  431. if (err) {
  432. deleteCallBack(err, '')
  433. } else {
  434. deleteCallBack(null, results)
  435. }
  436. })
  437. }
  438. }
  439. /*
  440. function deleteByRation(doc) {
  441. return function (callback){
  442. ration_glj.deleteMany({projectID: doc.updateData.projectID, rationID: doc.updateData.ID},callback);
  443. }
  444. }
  445. */
  446. function save(user_id, datas, callback) {
  447. let operations = [];
  448. if (_.isArray(datas)) {
  449. for (let i = 0; i < datas.length; i++) {
  450. operations.push(operationMap[datas[i].updateType](user_id, datas[i]));
  451. }
  452. } else {
  453. operations.push(operationMap[datas.updateType](user_id, datas));
  454. }
  455. async_n.parallel(operations, function (err, results) {
  456. if (err) {
  457. callback(err, '');
  458. } else {
  459. if (results.length == 1) {
  460. callback(null, results[0])
  461. } else {
  462. callback(null, results)
  463. }
  464. }
  465. })
  466. }
  467. async function getLibInfo(req) {
  468. let gljLibId = null, engineeringId, sessionCompilation = req.session.sessionCompilation,
  469. rationValuation = sessionCompilation.ration_valuation,
  470. billValuation = sessionCompilation.bill_valuation,
  471. engineeringLibModel = new EngineeringLibModel();
  472. if (rationValuation[0]) {
  473. let engineeringList = rationValuation[0].engineering_list;
  474. engineeringId = engineeringList.length > 0 ? engineeringList[0].engineering_id : null;
  475. let engineeringInfo = await engineeringLibModel.getEngineering(engineeringId);
  476. gljLibId = engineeringInfo.glj_lib.length > 0 && typeof engineeringInfo.glj_lib !== 'undefined' ? engineeringInfo.glj_lib[0].id : null;
  477. }
  478. else if (billValuation[0]) {
  479. let engineeringList = billValuation[0].engineering_list;
  480. engineeringId = engineeringList.length > 0 ? engineeringList[0].engineering_id : null;
  481. let engineeringInfo = await engineeringLibModel.getEngineering(engineeringId);
  482. gljLibId = engineeringInfo.glj_lib.length > 0 && typeof engineeringInfo.glj_lib !== 'undefined' ? engineeringInfo.glj_lib[0].id : null;
  483. }
  484. let data = {
  485. userID: req.session.sessionUser.ssoId,
  486. gljLibId: gljLibId,
  487. compilationId: sessionCompilation._id
  488. };
  489. return data;
  490. }
  491. function getGLJData(info, callback) {
  492. let gljDao = new GljDao();
  493. let datas = {};
  494. let gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
  495. datas.distTypeTree = gljDistTypeCache;
  496. async_n.parallel([
  497. function (cb) {
  498. gljDao.getGljTypes(info.gljLibId, function (err, data) {
  499. if (err) {
  500. cb(err);
  501. } else {
  502. datas.treeData = data;
  503. cb(null);
  504. }
  505. })
  506. },
  507. function (cb) {
  508. gljDao.getGljItems(info.gljLibId, info.userID, info.compilationId, function (err, data) {
  509. if (err) {
  510. cb(err);
  511. } else {
  512. datas.stdGLJ = data.stdGljs;
  513. datas.complementaryGLJs = data.complementaryGljs;
  514. cb(null);
  515. }
  516. });
  517. }
  518. ], function (err) {
  519. if (err) {
  520. callback(true, null);
  521. }
  522. else {
  523. callback(false, datas);
  524. }
  525. })
  526. }
  527. function getGLJSearchInfo(ration_glj) {
  528. let data = {
  529. glj_id: ration_glj.GLJID,
  530. project_id: ration_glj.projectID,
  531. code: ration_glj.code,
  532. original_code: ration_glj.original_code,
  533. name: ration_glj.name,
  534. //shortName: ration_glj.shortName,
  535. specs: ration_glj.specs,
  536. unit: ration_glj.unit,
  537. type: ration_glj.subType ? ration_glj.subType : ration_glj.type,//如果有subType,则是通过插入定额级的工料机进来的
  538. type_of_work: ration_glj.subType ? ration_glj.subType : ration_glj.type,
  539. base_price: ration_glj.basePrice,
  540. market_price: ration_glj.basePrice,
  541. repositoryId: ration_glj.repositoryId,
  542. adjCoe: ration_glj.adjCoe,
  543. from: ration_glj.from ? ration_glj.from : 'std'//std:标准工料机库, cpt:补充工料机库
  544. };
  545. let glj_type_object = glj_type_util.getStdGljTypeCacheObj();
  546. let type = glj_type_object.getItemById(data.type);
  547. data.shortName = type.shortName;
  548. if (data.from == 'cpt') {//从补充工料机来的数据即为新增数据
  549. data.is_add = 1;
  550. }
  551. return data;
  552. }
  553. async function addGLJ(rgList) {
  554. let newRecodes = [];
  555. for (let g of rgList) {
  556. let projectGljModel = new GLJListModel();
  557. let result = await projectGljModel.addList(getGLJSearchInfo(g));
  558. let typeString = result.type+'';
  559. g.marketPrice = result.unit_price.market_price;
  560. g.adjustPrice = result.unit_price.base_price;
  561. g.basePrice = result.unit_price.base_price;
  562. g.isAdd = result.unit_price.is_add;
  563. g.projectGLJID = result.id;
  564. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  565. g.isEstimate = result.is_evaluate;
  566. }
  567. g.ID = uuidV1();
  568. if (result.hasOwnProperty('subList') && result.subList.length > 0) {
  569. g.subList = getMixRatioShowDatas(result.subList);
  570. }
  571. newRecodes.push(createNewRecord(g));
  572. }
  573. await ration_glj.insertMany(newRecodes);
  574. let stateResult = await glj_calculate_facade.calculateQuantity({
  575. projectID: rgList[0].projectID,
  576. rationID: rgList[0].rationID
  577. });
  578. let result = {
  579. newRecodes: newRecodes,
  580. showData: rgList,
  581. adjustState: stateResult.adjustState
  582. }
  583. return result;
  584. }
  585. async function replaceGLJ(data) {
  586. let rdata = {};
  587. let projectGljModel = new GLJListModel();
  588. let result = await projectGljModel.addList(getGLJSearchInfo(data));
  589. let typeString = result.type+'';
  590. data.projectGLJID = result.id;
  591. let updateResult = await ration_glj.findOneAndUpdate({ID: data.ID, projectID: data.projectID}, data);//更新定额工料机
  592. //组装回传数据
  593. data.marketPrice = result.unit_price.market_price;
  594. data.adjustPrice = result.unit_price.base_price;
  595. data.basePrice = result.unit_price.base_price;
  596. data.isAdd = result.unit_price.is_add;
  597. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  598. data.isEstimate = result.is_evaluate;
  599. }
  600. if (result.hasOwnProperty('subList') && result.subList.length > 0) {
  601. data.subList = getMixRatioShowDatas(result.subList);
  602. }
  603. let stateResult = await glj_calculate_facade.calculateQuantity({
  604. projectID: data.projectID,
  605. rationID: data.rationID
  606. }, true);
  607. rdata.data = data;
  608. rdata.adjustState = stateResult.adjustState;
  609. return rdata;
  610. }
  611. async function mReplaceGLJ(data) {
  612. let mresult = {};
  613. let projectGljModel = new GLJListModel();
  614. let result = await projectGljModel.addList(getGLJSearchInfo(data.doc));
  615. let typeString = result.type+'';
  616. data.doc.projectGLJID = result.id;
  617. let rationList = await ration_glj.distinct('rationID', data.query);
  618. let updateResult = await ration_glj.update(data.query, data.doc, {multi: true});
  619. data.doc.marketPrice = result.unit_price.market_price;
  620. data.doc.adjustPrice = result.unit_price.base_price;
  621. data.doc.basePrice = result.unit_price.base_price;
  622. data.doc.isAdd = result.unit_price.is_add;
  623. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  624. data.doc.isEstimate = result.is_evaluate;
  625. }
  626. if (result.hasOwnProperty('subList') && result.subList.length > 0) {
  627. data.doc.subList = getMixRatioShowDatas(result.subList);
  628. }
  629. let stateList = await changAdjustState(data, rationList);
  630. mresult.data = data;
  631. mresult.stateList = stateList;
  632. return mresult
  633. }
  634. async function updateRationGLJByEdit(data) {
  635. var doc = data.doc;
  636. var result;
  637. if (doc.hasOwnProperty('customQuantity')) {
  638. result = await doCustomQuantityUpdate(data)
  639. } else {
  640. result = await doRationGLJUpdate(data);
  641. }
  642. return result;
  643. }
  644. async function doRationGLJUpdate(data) {
  645. let resutl = {};
  646. let doc = data.doc;
  647. let priceInfo = data.priceInfo;
  648. let rg = await ration_glj.findOne(data.query);
  649. let gljListModel = new GLJListModel();
  650. let projectGLJ = getGLJSearchInfo(rg);
  651. for (let key in doc) {
  652. projectGLJ[key] = doc[key]
  653. }
  654. projectGLJ.base_price = priceInfo.base_price;
  655. projectGLJ.market_price = priceInfo.market_price;
  656. let projcetGLJ_n = await gljListModel.modifyGLJ(projectGLJ, rg);
  657. doc.code = projcetGLJ_n.code;
  658. doc.projectGLJID = projcetGLJ_n.id;
  659. if (projcetGLJ_n.unit_price.is_add == 1) {
  660. doc.createType = 'replace';
  661. doc.rcode = projcetGLJ_n.original_code;
  662. } else {
  663. doc.createType = 'normal';
  664. doc.rcode = '';
  665. }
  666. await ration_glj.findOneAndUpdate(data.query, doc);
  667. //取价格
  668. gljListModel.getGLJPrice(projcetGLJ_n);
  669. doc.basePrice = projcetGLJ_n.unit_price.base_price;
  670. doc.marketPrice = projcetGLJ_n.unit_price.market_price;
  671. doc.adjustPrice = projcetGLJ_n.adjust_price;
  672. doc.isAdd = projcetGLJ_n.unit_price.is_add;
  673. resutl.doc = doc;
  674. let stateResult = await glj_calculate_facade.calculateQuantity({
  675. projectID: data.query.projectID,
  676. rationID: data.query.rationID
  677. });
  678. resutl.adjustState = stateResult.adjustState;
  679. return resutl;
  680. }
  681. async function getGLJClass(info, data) {
  682. let result = {
  683. exist: false
  684. }
  685. //检查补充工料机中是否已经存在一样的记录了
  686. let condition = {
  687. userId: info.userID,
  688. compilationId: info.compilationId,
  689. code: data.code,
  690. name: data.name,
  691. unit: data.unit,
  692. gljType: data.type,
  693. basePrice: data.basePrice
  694. }
  695. if (data.specs != null && data.specs != undefined && data.specs != '') {
  696. condition['specs'] = data.specs;
  697. }
  698. let glj = await complementaryGljModel.find(condition);
  699. if (glj.length > 0) { //如果已存在就直接返回,不用再新增了
  700. result.exist = true;
  701. return result
  702. }
  703. //查找工料机类型树
  704. let items = await gljClassModel.find({
  705. "repositoryId": info.gljLibId,
  706. "$or": [{"isDeleted": null}, {"isDeleted": false}]
  707. });
  708. result.items = items;
  709. return result;
  710. }
  711. async function insertGLJAsRation(data) {
  712. let gljList = data.gljList;
  713. //先更新counter
  714. let counter = await projCounter.model.findOneAndUpdate({projectID: data.projectID}, {ration: data.rationCount}, {new: true});
  715. if (data.hasOwnProperty("selectedSerialNo")) { //如果需要,更新序列号。
  716. let query = {
  717. projectID: data.projectID,
  718. billsItemID: data.billsItemID,
  719. serialNo: {$gt: data.selectedSerialNo}
  720. }
  721. await ration.update(query, {$inc: {serialNo: gljList.length}}, {multi: true});
  722. }
  723. for (let glj of gljList) {
  724. let p_glj = getGLJSearchInfo(glj);
  725. let projectGljModel = new GLJListModel();
  726. let result = await projectGljModel.addList(p_glj);//逐条添加到项目工料机
  727. let typeString = result.type+'';
  728. glj.marketPrice = result.unit_price.market_price;
  729. glj.adjustPrice = result.unit_price.base_price;
  730. glj.basePrice = result.unit_price.base_price;
  731. glj.isAdd = result.unit_price.is_add;
  732. glj.projectGLJID = result.id;
  733. if (typeString.startsWith("2")||typeString=='4'||typeString=='5') {//只有材料类型才显示是否暂估
  734. glj.isEstimate = result.is_evaluate;
  735. }
  736. }
  737. await ration.insertMany(gljList);
  738. console.log(gljList);
  739. return gljList;
  740. }
  741. async function getRationTypeGLJQuantity(projectID) {
  742. let rations = await ration.find({'projectID': projectID,'type':3,'deleteInfo': null}, ['ID', 'projectGLJID','quantity']);
  743. return rations;
  744. }
  745. async function changAdjustState(data, rationList) {
  746. let stateList = [];
  747. for (let r of rationList) {
  748. let stateResult = await glj_calculate_facade.calculateQuantity({
  749. projectID: data.query.projectID,
  750. rationID: r
  751. }, true);
  752. if(stateResult){
  753. stateList.push({rationID: r, adjustState: stateResult.adjustState});
  754. }
  755. }
  756. return stateList;
  757. }
  758. async function testError() {
  759. throw new Error('test Error');
  760. }
  761. function getData(projectID, callback) {
  762. ration_glj.find({'projectID': projectID}, (err, datas) => {
  763. if (err) {
  764. callback(1, '', null);
  765. } else {
  766. callback(0, consts.projectConst.RATION_GLJ, datas);
  767. }
  768. })
  769. }
  770. function commonCallback(callback, result, err) {
  771. if (err) {
  772. callback(err, '');
  773. } else {
  774. callback(null, result);
  775. }
  776. }