/** * Created by chen on 2017/7/20. */ /** * Created by chen on 2017/7/10. */ let mongoose = require('mongoose'); let consts = require('../../main/models/project_consts'); let commonConsts = consts.commonConst; let _=require("lodash"); let async_n = require("async"); let quantity_detail_model = mongoose.model('quantity_detail'); const uuidV1 = require('uuid/v1'); module.exports={ save:save, getData:getData }; let operationMap={ 'ut_create':create_quantity_detail, 'ut_update':update_quantity_detail, 'ut_delete':delete_quantity_detail }; let updateFunctionMap = { 'normalUpdate':normalUpdate, 'updateQuantityRegex':updateQuantityRegex } function create_quantity_detail(user_id,datas) { return function (callback) { let doc = datas.doc; doc.ID = uuidV1(); quantity_detail_model.create(doc,(err,result)=>{ if(err){ callback(err,null); }else { console.log(result); let returndata ={ updateTpye:commonConsts.UT_CREATE, moduleName:consts.projectConst.QUANTITY_DETAIL, data:result } callback(null,returndata) } }); } } function normalUpdate(user_id,datas) { return function(callback) { updateRecored(datas.query,datas.doc,callback); } } function updateQuantityRegex(user_id,datas) { return function(callback){ console.log(datas); doRegexUpdate(datas).then(function (result) { console.log("update"); callback(null,''); }) /* let checkResult =checkingRegex(datas); if(checkResult!=null){ callback(null,{ moduleName:consts.projectConst.QUANTITY_DETAIL, err:{ message:checkResult.message } }); }else { updateRecored(datas.query,datas.doc,callback) }*/ } } async function doRegexUpdate(datas) { if(datas.doc.regex==null){ //update result and bill/ration records to null and refresh }else { try { let regex = datas.doc.regex.toUpperCase(); let referenceIndexs = datas.doc.referenceIndexs; let detailList = await quantity_detail_model.find({'projectID':datas.query.projectID,'rationID':datas.query.rationID}).sort('seq').exec(); let result =getEvalResult(referenceIndexs,detailList,regex); detailList[datas.query.index].result =result; detailList[datas.query.index].regex=datas.doc.regex; let updateTasks =[]; datas.doc.result=result; updateTasks.push({ query:{ ID:datas.query.ID, projectID:datas.query.projectID }, doc:datas.doc }) for(let d of detailList){ if(_.includes(d.referenceIndexs,datas.query.index+1)){ let tResult = getEvalResult(d.referenceIndexs,detailList,d.regex); let t = { query:{ ID:d.ID, projectID:d.projectID }, doc:{ result:tResult } }; updateTasks.push(t); } } let updateEdit = await quantity_detail_model.bulkWrite(generateUpdateTaks(updateTasks)); console.log(updateEdit); return regex; }catch (error){ console.log(error); } } } function getEvalResult(referenceIndexs,detailList,regex) { for(let i of referenceIndexs){ regex = replaceReference(i,detailList,regex) } console.log('replace all C reference -----'+regex); regex =replaceSqr(regex); console.log('replace all sqar reference -----'+regex); return eval(regex); } function generateUpdateTaks(updateTasks) { var tasks=[]; for(let u of updateTasks){ let t ={ updateOne:{ filter:u.query, update: u.doc } } tasks.push(t); } return tasks; } function replaceReference(index,detailList,str) { str=str.toUpperCase(); str=replaceAll('C'+index,'('+detailList[index-1].regex+')',str); if(detailList[index-1].referenceIndexs.length>0){ for (let i of detailList[index-1].referenceIndexs){ str =replaceReference(i,detailList,str); } } return str; } function replaceAll (FindText, RepText,str) { let regExp = new RegExp(FindText, "g"); return str.replace(regExp, RepText); } function replaceSqr(text) { var squarRegex = /\([^\^]+\)\^\d+/g; var sqararr = text.match(squarRegex); var squarRegex2 = /C[0-9]+\^\d+|[0-9]+([.]{1}[0-9]+){0,1}\^\d+/g; //匹配没有括号的 var sqararr2=text.match(squarRegex2); if(sqararr){ text=converSqrByArr(sqararr,text); } if(sqararr2){ text=converSqrByArr(sqararr2,text); } return text; } function converSqrByArr (sqararr,text) { var temp = text; sqararr.forEach(function (item) { var arr = item.split('\^'); var y = parseInt(arr[1]); var x_arr = []; for (var i = 0; i < y; i++) { x_arr.push(arr[0]); } var temStr = x_arr.join('*'); temp = temp.replace(item, temStr); }); return temp; }; function updateRecored(query,doc,callback) { quantity_detail_model.update(query,doc,(err,result)=>{ if(err){ callback(err,''); }else { let returndata ={ moduleName:consts.projectConst.QUANTITY_DETAIL, data:{ updateTpye:commonConsts.UT_UPDATE, query:query, doc:doc } } callback(null,returndata); } }) } function checkingRegex(datas) { try{ if(datas.doc.hasOwnProperty('regex')){ // datas.doc.result=eval(datas.doc.regex); } return null; }catch (error){ console.log(error.message); return error } } function update_quantity_detail(user_id,datas) { if(datas.updateFunction){ return updateFunctionMap[datas.updateFunction](user_id,datas); }else { return normalUpdate(user_id,datas); } } function delete_quantity_detail(user_id,datas) { return function (callback) { callback(null,''); } } function getData(projectID, callback) { quantity_detail_model.find({'projectID':projectID}).sort('seq').exec((err,datas)=>{ if(err){ callback(1, '', null); }else { callback(0, consts.projectConst.QUANTITY_DETAIL, datas); } }) } function save (user_id, datas, callback) { let operations=[]; if(_.isArray(datas)){ for(let i=0;i