ration_glj_facade.js 26 KB

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