ration_glj_facade.js 25 KB

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