123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /**
- * Created by Mai on 2017/4/1.
- */
- var PROJECT = {
- createNew: function (projectID, userID) {
- // 定义private方法
- var tools = {
- _project: null,
- _ID: projectID,
- _userID: userID,
- updateLock: 0,
- updateData: [],
- operation: '',
- modules: {},
- projCounter: 'projCounter',
- projSetting: 'proj_setting'
- };
- var me = tools;
- tools.doAfterUpdate = function(result){
- if (!result) { return }
- result.forEach(function(item){
- if(_.isArray(item)){
- item.forEach(function (e) {
- me.eachItem(e);
- })
- }else {
- me.eachItem(item)
- }
- });
- };
- tools.doAfterLoad = function(result, callback){
- var counter;
- result.forEach(function(item){
- if (me.modules[item.moduleName]){
- me.modules[item.moduleName].loadData(item.data);
- } else if (item.moduleName === me.projCounter) {
- counter = item.data;
- } else if (item.moduleName === me.projSetting) {
- me._project.projSetting = item.data;
- me._project.projSetting.moduleName = me.projSetting;
- }
- });
- for (module in counter) {
- if (me.modules[module]) {
- me.modules[module].setMaxID(counter[module]);
- }
- }
- me._project.loadMainTree();
- //me.test(result[0].data[0]);
- callback(0);
- };
- tools.eachItem=function(item){
- if (me.modules[item.moduleName]){
- me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
- }
- };
- /*tools.test = function(data){
- me._project.beginUpdate('修改名称');
- data.name = 'test';
- data['updateType'] = 'ut_update';
- me._project.push(ModuleNames.bills, [data]);
- me._project.endUpdate();
- };*/
- // 所有通过this访问的属性,都不应在此单元外部进行写入操作
- var project = function () {
- tools._project = this;
- this.mainTree = cacheTree.createNew(this);
- this.Bills = Bills.createNew(this);
- this.Ration = Ration.createNew(this);
- this.GLJ = GLJ.createNew(this);
- this.ration_glj = ration_glj.createNew(this);
- this.ration_coe = ration_coe.createNew(this);
- this.ration_ass = ration_ass.createNew(this);
- this.quantity_detail = quantity_detail.createNew(this);
- this.FeeRate = FeeRate.createNew(this);
- this.VolumePrice = VolumePrice.createNew(this);
- this.projectGLJ = new projectGLJ(this);
- this.Decimal = {
- common: {
- quantity: 3,
- unitFee: 2,
- totalFee: 2
- }
- };
- this.masterField = {ration: 'billsItemID', volumePrice: 'billsItemID'};
- };
- // prototype用于定义public方法
- project.prototype.modify = function (modifyDatas, callback) {
- // To Do
- };
- // prototype用于定义public方法
- project.prototype.ID = function () {
- return tools._ID;
- };
- project.prototype.projCounter = function () {
- return tools.projCounter;
- };
- project.prototype.getDecimal = function (fullName) {
- let names = fullName.split('.'), decimal = this.Decimal;
- for (let name of names) {
- if (decimal[name]) {
- decimal = decimal[name];
- } else {
- return null;
- }
- }
- if (Object.prototype.toString.apply(decimal) === '[object Number]') {
- return decimal;
- } else {
- return null;
- }
- };
- project.prototype.loadMainTree = function () {
- var that = this;
- var loadRationNode = function (rations, cacheNode) {
- var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
- for (i = 0; i < br.length; i++) {
- newNode = that.mainTree.addNode(cacheNode);
- newNode.source = br[i];
- newNode.sourceType = that.Ration.getSourceType();
- newNode.data = br[i];
- }
- };
- let loadVolumePriceNode = function (cacheNode) {
- let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
- for (let v of bv) {
- newNode = that.mainTree.addNode(cacheNode);
- newNode.source = v;
- newNode.sourceType = that.VolumePrice.getSourceType();
- newNode.data = v;
- }
- };
- var loadIdTreeNode = function (nodes, parent) {
- var newNode, i;
- for (i = 0; i < nodes.length; i++) {
- newNode = that.mainTree.addNode(parent);
- newNode.source = nodes[i];
- newNode.sourceType = that.Bills.getSourceType();
- newNode.data = nodes[i].data;
- if (nodes[i].children.length === 0) {
- loadRationNode(that.Ration.datas, newNode);
- loadVolumePriceNode(newNode);
- } else {
- loadIdTreeNode(nodes[i].children, newNode);
- }
- }
- };
- loadIdTreeNode(this.Bills.tree.roots, null);
- this.mainTree.sortTreeItems();
- };
- project.prototype.getParentTarget = function (node, targetField, targetValue) {
- var parent = node;
- while (parent && parent[targetField] !== targetValue) {
- parent = parent.parent;
- }
- return parent;
- };
- // 提供给各模块调用的统一从后台获取数据的方法
- /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
- $.ajax({
- type:"POST",
- url: url,
- data: {'data': JSON.stringify(data)},
- dataType: 'json',
- cache: false,
- timeout: 50000,
- success: function(result){
- successCallback(result);
- },
- error: function(jqXHR, textStatus, errorThrown){
- alert('error ' + textStatus + " " + errorThrown);
- errorCallback();
- }
- });
- };*/
- // 所有模块在此从后台获取数据
- project.prototype.loadDatas = function (callback){
- $.ajax({
- type: "POST",
- url: '/project/getData',
- data: {'data': JSON.stringify({
- "project_id": tools._ID,
- "user_id": tools._userID
- })},
- dataType: 'json',
- cache: false,
- timeout: 50000,
- success: function (result) {
- if (!result.error) {
- tools.doAfterLoad(result.data, callback);
- // for test calc
- //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
- } else {
- alert('error: ' + result.message);
- callback(result.error);
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- alert('error ' + textStatus + " " + errorThrown);
- }
- });
- };
- project.prototype.beginUpdate = function(operation){
- if (tools.updateLock === 0){
- tools.operation = operation
- }
- tools.updateLock += 1;
- };
- project.prototype.endUpdate = function(){
- if (tools.updateLock === 0){
- throw "project can not endUpdate before beginUpdate";
- }
- tools.updateLock -= 1;
- if (tools.updateLock === 0) {
- $.ajax({
- type: "POST",
- url: '/project/save',
- data: {'data': JSON.stringify({
- "project_id": tools._ID,
- "user_id": tools._userID,
- "date": new Date,
- "operation": tools.operation,
- "update_data": tools.updateData
- })},
- dataType: 'json',
- cache: false,
- timeout: 50000,
- success: function (result) {
- if (!result.error) {
- tools.doAfterUpdate(result.data);
- } else {
- alert('error: ' + result.message);
- }
- },
- error: function(jqXHR, textStatus, errorThrown){
- alert('error ' + textStatus + " " + errorThrown);
- }
- });
- tools.updateData = [];
- tools.operation = "";
- }
- };
- project.prototype.push = function(moduleName, data){
- if (tools.updateLock === 0){
- throw "project can not push data before beginUpdate";
- }
- var moduleData = {
- moduleName: moduleName,
- data: data
- };
- tools.updateData.push(moduleData);
- };
- project.prototype.pushNow = function (operation, moduleName, data) {
- var that = this;
- this.beginUpdate(operation);
- if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
- moduleName.forEach(function (name, index) {
- that.push(moduleName[index], data[index]);
- });
- } else {
- this.push(moduleName, data);
- }
- this.endUpdate();
- };
- project.prototype.registerModule = function(moduleName, obj){
- if (!tools.modules.hasOwnProperty(moduleName)){
- tools.modules[moduleName] = obj;
- }
- };
- return new project();
- }
- };
|