ration_glj_facade.js 23 KB

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