ration_glj_facade.js 24 KB

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