quantity_detail_facade.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /**
  2. * Created by chen on 2017/7/20.
  3. */
  4. /**
  5. * Created by chen on 2017/7/10.
  6. */
  7. let mongoose = require('mongoose');
  8. let consts = require('../../main/models/project_consts');
  9. let commonConsts = consts.commonConst;
  10. let _=require("lodash");
  11. let async_n = require("async");
  12. let quantity_detail_model = mongoose.model('quantity_detail');
  13. const uuidV1 = require('uuid/v1');
  14. let ration_model = mongoose.model('ration');
  15. let bill_model=mongoose.model("bills");
  16. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  17. let decimal_facade = require('./decimal_facade');
  18. module.exports={
  19. save:save,
  20. getData:getData,
  21. deleteByRation:deleteByRation,
  22. deleteByBill:deleteByBill,
  23. quantityEditChecking:quantityEditChecking,
  24. saveQuantityDetail:saveQuantityDetail,
  25. update:update,
  26. updateRegex:updateRegex,
  27. insertRecode:insertRecode,
  28. deleteRecode:deleteRecode,
  29. swapRow:swapRow
  30. };
  31. let operationMap={
  32. 'ut_update':update_quantity_detail,
  33. };
  34. let updateFunctionMap = {
  35. 'insertRecode':insertRecode
  36. }
  37. async function saveQuantityDetail(datas) {
  38. console.log(datas);
  39. let doc = datas;
  40. doc.ID = uuidV1();
  41. if(doc.refreshQuantity==false){ //如果选择了不替换工程量,则清空
  42. await cleanQuantityDetail(doc);
  43. return
  44. }
  45. if(doc.hasOwnProperty('regex')){
  46. return await insertRecodeWithReg(doc);
  47. }else {
  48. return await createNormalRecode(doc);
  49. }
  50. }
  51. async function update(datas) {
  52. let doc = datas.doc;
  53. if(doc.hasOwnProperty('isSummation')){
  54. return await doIsSummationUpdate(datas.query,doc);
  55. }else {
  56. await quantity_detail_model.update(datas.query,doc);
  57. return datas;
  58. }
  59. }
  60. async function swapRow(datas) {
  61. for(let task of datas){
  62. await update(task);
  63. }
  64. return {refreshList:datas};
  65. }
  66. async function updateRegex(datas) {
  67. let resultObjec ={};
  68. let detailList = [];
  69. let node ={};
  70. let decimal =await decimal_facade.getProjectDecimal(datas.query.projectID);
  71. if(datas.query.hasOwnProperty('rationID')){
  72. detailList = await quantity_detail_model.find({'projectID':datas.query.projectID,'rationID':datas.query.rationID}).sort('seq').exec();
  73. node.type = consts.projectConst.RATION;
  74. node.ID = datas.query.rationID;
  75. }else {
  76. detailList = await quantity_detail_model.find({'projectID':datas.query.projectID,'billID':datas.query.billID}).sort('seq').exec();
  77. node.type = consts.projectConst.BILLS;
  78. node.ID=datas.query.billID;
  79. }
  80. let regex;
  81. let result;
  82. if(datas.doc.regex==null){
  83. result=0;
  84. datas.doc.referenceIndexs=[];
  85. }else {
  86. regex = datas.doc.regex.toUpperCase();
  87. let referenceIndexs = datas.doc.referenceIndexs;
  88. result = getEvalResult(referenceIndexs,detailList,regex,decimal);
  89. }
  90. detailList[datas.query.index].result =result;
  91. detailList[datas.query.index].regex=datas.doc.regex;
  92. detailList[datas.query.index].referenceIndexs =datas.doc.referenceIndexs;
  93. let updateTasks =[];
  94. datas.doc.result=result;
  95. updateTasks.push({
  96. query:{
  97. ID:datas.query.ID,
  98. projectID:datas.query.projectID
  99. },
  100. doc:datas.doc
  101. })
  102. updateReferenceRecode(datas.query.index+1,detailList,updateTasks);
  103. await quantity_detail_model.bulkWrite(generateUpdateTaks(updateTasks));
  104. resultObjec.refreshList=updateTasks;
  105. if(datas.query.refreshQuantity==true){
  106. let data = {};
  107. data.quantity = await summateResults(datas.query,detailList,decimal);
  108. data.isFromDetail = 1;
  109. node.data = data;
  110. resultObjec.node = node;
  111. }
  112. return resultObjec;
  113. }
  114. async function cleanQuantityDetail(doc) {
  115. let query = {
  116. projectID:doc.projectID
  117. }
  118. if(doc.hasOwnProperty('rationID')){
  119. query.rationID = doc.rationID;
  120. }else {
  121. query.billID = doc.billID;
  122. }
  123. await quantity_detail_model.deleteMany(query);
  124. }
  125. async function insertRecode(doc){
  126. let query = {
  127. projectID:doc.projectID,
  128. seq : { $gte: doc.seq }
  129. }
  130. if(doc.hasOwnProperty('rationID')){
  131. query.rationID = doc.rationID;
  132. }else {
  133. query.billID = doc.billID;
  134. }
  135. let quantity_detail_List = await getDatailList(doc,{data:{}});
  136. let update_task = getUpdateReferenceTask(quantity_detail_List,doc.seq,1);
  137. await quantity_detail_model.update(query,{$inc:{seq:1}},{multi: true});
  138. if(update_task.length>0){
  139. await quantity_detail_model.bulkWrite(generateUpdateTaks(update_task));
  140. }
  141. doc.ID = uuidV1();
  142. let newrecode = await quantity_detail_model.create(doc);
  143. let data={
  144. doc:newrecode,
  145. resort:true,
  146. update_task:update_task
  147. }
  148. return data;
  149. }
  150. async function doInsertRecode(doc) {
  151. let result={
  152. err:null
  153. }
  154. try{
  155. let query = {
  156. projectID:doc.projectID,
  157. seq : { $gte: doc.seq }
  158. }
  159. if(doc.hasOwnProperty('rationID')){
  160. query.rationID = doc.rationID;
  161. }else {
  162. query.billID = doc.billID;
  163. }
  164. let quantity_detail_List = await getDatailList(doc,{data:{}});
  165. let update_task = getUpdateReferenceTask(quantity_detail_List,doc.seq,1);
  166. await quantity_detail_model.update(query,{$inc:{seq:1}},{multi: true});
  167. if(update_task.length>0){
  168. await quantity_detail_model.bulkWrite(generateUpdateTaks(update_task));
  169. }
  170. let newrecode = await quantity_detail_model.create(doc);
  171. let returndata ={
  172. updateTpye:commonConsts.UT_CREATE,
  173. moduleName:consts.projectConst.QUANTITY_DETAIL,
  174. data:{
  175. doc:newrecode,
  176. resort:true,
  177. update_task:update_task
  178. }
  179. };
  180. result.returndata =returndata
  181. return result;
  182. }catch (error){
  183. console.log(error)
  184. result.err;
  185. return result
  186. }
  187. }
  188. function getUpdateReferenceTask(quantity_detail_List,seq,re) {
  189. let update_task=[];
  190. for(let q of quantity_detail_List){
  191. let need_update =false;
  192. let newReg = q.regex;
  193. let newReferenceIndex;
  194. if(q.referenceIndexs.length>0){
  195. for (let i =0;i< q.referenceIndexs.length;i++){
  196. if(q.referenceIndexs[i]>seq){
  197. newReg = replaceAll('C'+q.referenceIndexs[i],'C'+(q.referenceIndexs[i]+re),newReg);
  198. newReg = replaceAll('c'+q.referenceIndexs[i],'c'+(q.referenceIndexs[i]+re),newReg);
  199. q.referenceIndexs[i] +=re;
  200. need_update = true;
  201. }
  202. }
  203. }
  204. if(need_update){
  205. newReferenceIndex = q.referenceIndexs;
  206. let task ={
  207. query:{
  208. ID:q.ID,
  209. projectID:q.projectID
  210. },
  211. doc:{
  212. regex:newReg,
  213. referenceIndexs:newReferenceIndex
  214. }
  215. }
  216. update_task.push(task);
  217. }
  218. }
  219. return update_task;
  220. }
  221. async function createNormalRecode(doc) {
  222. let result ={};
  223. result.newRecord= await quantity_detail_model.create(doc);
  224. return result;
  225. }
  226. async function insertRecodeWithReg (doc) {
  227. let returnObjec={}
  228. try {
  229. let decimal =await decimal_facade.getProjectDecimal(doc.projectID);
  230. let regex = doc.regex.toUpperCase();
  231. let referenceIndexs = doc.referenceIndexs;
  232. let detailList = await getDatailList(doc,returnObjec);
  233. doc.result =getEvalResult(referenceIndexs,detailList,regex,decimal);
  234. let refreshQuantity =false;
  235. if(doc.refreshQuantity==true){
  236. refreshQuantity = true;
  237. }
  238. delete doc.refreshQuantity;
  239. let newRecord = await quantity_detail_model.create(doc) ;
  240. detailList.push(newRecord);
  241. if(refreshQuantity==true){
  242. let data = {};
  243. data.quantity = await summateResults(doc,detailList,decimal);
  244. data.isFromDetail = 1;
  245. returnObjec.node.data = data;
  246. }
  247. returnObjec.newRecord = newRecord;
  248. return returnObjec;
  249. }catch (error){
  250. console.log(error);
  251. throw new Error('输入的表达式有误,请重新输入!');
  252. }
  253. }
  254. async function doIsSummationUpdate(query,doc) {
  255. let returnObjec={query:query,doc:doc};
  256. let decimal =await decimal_facade.getProjectDecimal(query.projectID);
  257. let refreshQuantity=false;
  258. if(query.refreshQuantity==true){
  259. refreshQuantity=true;
  260. }
  261. delete query.refreshQuantity;
  262. await quantity_detail_model.update(query,doc);
  263. let detailList = await getDatailList(query,returnObjec);
  264. if(refreshQuantity==true){
  265. let data = {};
  266. data.quantity = await summateResults(query,detailList,decimal);
  267. data.isFromDetail = 1;
  268. returnObjec.node.data = data;
  269. }
  270. return returnObjec;
  271. }
  272. async function getDatailList(query,resultObject) {
  273. let detailList = [];
  274. let node={};
  275. if(query.hasOwnProperty('rationID')){
  276. detailList = await quantity_detail_model.find({'projectID':query.projectID,'rationID':query.rationID}).sort('seq').exec();
  277. node.type = consts.projectConst.RATION;
  278. node.ID = query.rationID;
  279. }else {
  280. detailList = await quantity_detail_model.find({'projectID':query.projectID,'billID':query.billID}).sort('seq').exec();
  281. node.type = consts.projectConst.BILLS;
  282. node.ID=query.billID;
  283. }
  284. resultObject.node = node;
  285. return detailList;
  286. }
  287. function updateReferenceRecode(index,detailList,updateTasks) {
  288. for(let d of detailList){
  289. if(_.includes(d.referenceIndexs,index)){
  290. let tResult = getEvalResult(d.referenceIndexs,detailList,d.regex);
  291. let t = {
  292. query:{
  293. ID:d.ID,
  294. projectID:d.projectID
  295. },
  296. doc:{
  297. result:tResult
  298. }
  299. };
  300. d.result = tResult;
  301. updateTasks.push(t);
  302. updateReferenceRecode(d.seq+1,detailList,updateTasks);
  303. }
  304. }
  305. }
  306. async function summateResults (query,detailList,decimal) {
  307. let quantity = 0;
  308. decimal = decimal?decimal:await decimal_facade.getProjectDecimal(query.projectID);
  309. for(let d of detailList){
  310. if(d.isSummation==1){
  311. let result = d.result==null||d.result==undefined?0:d.result;
  312. quantity+=result;
  313. }
  314. }
  315. if(query.hasOwnProperty('rationID')){
  316. quantity = scMathUtil.roundTo(quantity, -decimal.ration.quantity);
  317. await ration_model.update({'ID':query.rationID,'projectID':query.projectID,deleteInfo: null},{quantity:quantity,isFromDetail:1});
  318. }else {
  319. let bill = await bill_model.findOne({'ID':query.billID,'projectID':query.projectID,deleteInfo: null});
  320. decimal = await decimal_facade.getBillsQuantityDecimal(query.projectID,bill.unit);
  321. quantity = scMathUtil.roundTo(quantity, -decimal);
  322. await bill_model.update({'ID':query.billID,'projectID':query.projectID,deleteInfo: null},{quantity:quantity,isFromDetail:1});
  323. }
  324. return quantity
  325. }
  326. function getEvalResult(referenceIndexs,detailList,regex,decimal={}) {
  327. try {
  328. decimal = decimal.quantity_detail?decimal.quantity_detail:4;
  329. for(let i of referenceIndexs){
  330. regex = replaceReference(i,detailList,regex)
  331. }
  332. console.log('replace all C reference -----'+regex);
  333. regex =replaceSqr(regex);
  334. console.log('replace all sqar reference -----'+regex);
  335. return scMathUtil.roundTo(eval(regex), -decimal);
  336. }catch (error){
  337. console.log(error);
  338. throw new Error('输入的表达式有误,请重新输入!');
  339. }
  340. }
  341. function generateUpdateTaks(updateTasks) {
  342. var tasks=[];
  343. for(let u of updateTasks){
  344. let t ={
  345. updateOne:{
  346. filter:u.query,
  347. update: u.doc
  348. }
  349. }
  350. tasks.push(t);
  351. }
  352. return tasks;
  353. }
  354. function replaceReference(index,detailList,str) {
  355. str=str.toUpperCase();
  356. let rstr= detailList[index-1].regex==null?'0':'('+detailList[index-1].regex+')';
  357. str=replaceAll('C'+index,rstr,str);
  358. if(detailList[index-1].referenceIndexs.length>0){
  359. for (let i of detailList[index-1].referenceIndexs){
  360. str =replaceReference(i,detailList,str);
  361. }
  362. }
  363. return str;
  364. }
  365. function replaceAll (FindText, RepText,str) {
  366. let regExp = new RegExp(FindText, "g");
  367. return str.replace(regExp, RepText);
  368. }
  369. function replaceSqr(text) {
  370. var squarRegex = /\([^\^]+\)\^\d+/g;
  371. var sqararr = text.match(squarRegex);
  372. var squarRegex2 = /C[0-9]+\^\d+|[0-9]+([.]{1}[0-9]+){0,1}\^\d+/g; //匹配没有括号的
  373. var sqararr2=text.match(squarRegex2);
  374. if(sqararr){
  375. text=converSqrByArr(sqararr,text);
  376. }
  377. if(sqararr2){
  378. text=converSqrByArr(sqararr2,text);
  379. }
  380. return text;
  381. }
  382. function converSqrByArr (sqararr,text) {
  383. var temp = text;
  384. sqararr.forEach(function (item) {
  385. var arr = item.split('\^');
  386. var y = parseInt(arr[1]);
  387. var x_arr = [];
  388. for (var i = 0; i < y; i++) {
  389. x_arr.push(arr[0]);
  390. }
  391. var temStr = x_arr.join('*');
  392. temp = temp.replace(item, temStr);
  393. });
  394. return temp;
  395. };
  396. function update_quantity_detail(user_id,datas) {
  397. if(datas.updateFunction){
  398. return updateFunctionMap[datas.updateFunction](user_id,datas);
  399. }else {
  400. return normalUpdate(user_id,datas);
  401. }
  402. }
  403. async function deleteRecode(doc) {
  404. let resultObject = {};
  405. let decimal =await decimal_facade.getProjectDecimal(doc.projectID);
  406. let query = {
  407. projectID:doc.projectID,
  408. seq : { $gt: doc.seq }
  409. }
  410. if(doc.hasOwnProperty('rationID')){
  411. query.rationID = doc.rationID;
  412. }else {
  413. query.billID = doc.billID;
  414. }
  415. let quantity_detail_List = await getDatailList(doc,resultObject);
  416. let update_task = getUpdateReferenceTask(quantity_detail_List,doc.seq,-1);
  417. await quantity_detail_model.update(query,{$inc:{seq:-1}},{multi: true});
  418. if(update_task.length>0){
  419. await quantity_detail_model.bulkWrite(generateUpdateTaks(update_task));
  420. }
  421. await quantity_detail_model.deleteOne({ID:doc.ID,projectID:doc.projectID});
  422. _.remove(quantity_detail_List,{ID:doc.ID,projectID:doc.projectID});
  423. let n_data = {};//更新节点信息
  424. n_data.quantity = await summateResults(query,quantity_detail_List,decimal);
  425. n_data.isFromDetail = 1;
  426. resultObject.node.data = n_data;
  427. let r_data ={
  428. doc:doc,
  429. resort:true,
  430. update_task:update_task,
  431. updateTpye:commonConsts.UT_DELETE
  432. };
  433. resultObject.data = r_data;
  434. return resultObject;
  435. }
  436. function getData(projectID, callback) {
  437. quantity_detail_model.find({'projectID':projectID}).sort('seq').exec((err,datas)=>{
  438. if(err){
  439. callback(1, '', null);
  440. }else {
  441. callback(0, consts.projectConst.QUANTITY_DETAIL, datas);
  442. }
  443. })
  444. }
  445. function save (user_id, datas, callback) {
  446. let operations=[];
  447. if(_.isArray(datas)){
  448. for(let i=0;i<datas.length;i++){
  449. operations.push(operationMap[datas[i].updateType](user_id,datas[i]));
  450. }
  451. }else {
  452. operations.push(operationMap[datas.updateType](user_id,datas));
  453. }
  454. async_n.parallel(operations,function (err,results) {
  455. if(err){
  456. callback(err,'');
  457. }else {
  458. if(results.length==1){
  459. callback(null,results[0])
  460. }else {
  461. callback(null,results)
  462. }
  463. }
  464. })
  465. }
  466. function deleteByRation(data) {
  467. return function (callback) {
  468. quantity_detail_model.deleteMany({projectID: data.projectID, rationID: data.ID},(err,result)=>{
  469. commonCallback(callback,result,err);
  470. });
  471. }
  472. }
  473. function deleteByBill(data) {
  474. return function (callback) {
  475. console.log({projectID: data.projectID, billID: data.ID});
  476. quantity_detail_model.deleteMany({projectID: data.projectID, billID: data.ID},(err,result)=>{
  477. commonCallback(callback,result,err);
  478. });
  479. }
  480. }
  481. function quantityEditChecking(doc,type,functions) {
  482. if(doc.updateType == commonConsts.UT_UPDATE&&doc.updateData.hasOwnProperty('isFromDetail')&&doc.updateData.isFromDetail==0) {
  483. if(type=='bills'){
  484. functions.push(deleteByBill(doc.updateData));
  485. }else if(type=='ration'){
  486. functions.push(deleteByRation(doc.updateData));
  487. }
  488. }
  489. }
  490. function commonCallback(callback,result,err) {
  491. if(err){
  492. callback(err,'');
  493. }else {
  494. callback(null,result);
  495. }
  496. }