/** * Created by Mai on 2017/4/1. */ var Bills = { createNew: function (project) { var billsTreeSetting = { id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true }; // 用户定义private方法 var tools = { coverseTreeUpdateData: function (datas, projectID) { var updateDatas = []; datas.forEach(function (data) { var updateData = {}; data.data.projectID = projectID; if (data.type === idTree.updateType.new) { updateData.updateType = 'ut_create'; updateData.updateData = data.data; } else if (data.type === idTree.updateType.update) { updateData.updateType = 'ut_update'; updateData.updateData = data.data; } else if (data.type === idTree.updateType.delete) { updateData.updateType = 'ut_delete'; updateData.updateData = data.data; } updateDatas.push(updateData); }); return updateDatas; } }; // 所有通过this访问的属性,都不应在此单元外部进行写入操作 var bills = function (proj) { this.project = proj; this.datas = null; this.tree = idTree.createNew(billsTreeSetting); var sourceType = ModuleNames.bills; this.getSourceType = function () { return sourceType; } proj.registerModule(ModuleNames.bills, this); }; // 从后台获取数据 /*bills.prototype.pullData = function (){ this.project.pullData( '/bills/getData', {projectID: this.project.ID}, function(result){ if (result.error ===0){ this.loadDatas(result.data); } else { // to do: ?错误处理需要细化 alert(result.message); } }, function (){}// to do: 错误处理需要细化 ) };*/ // prototype用于定义public方法 bills.prototype.loadData = function (datas) { this.datas = datas; // generate Fees & Flags Index, For View & Calculate this.datas.forEach(function (data) { data.feesIndex = {}; if (data.fees) { data.fees.forEach(function (fee) { data.feesIndex[fee.fieldName] = fee; }); } data.flagsIndex = {}; if (data.flags) { data.flags.forEach(function (flag) { data.flagsIndex[flag.fieldName] = flag; }); } }); // datas load to Tree this.tree.loadDatas(this.datas); }; bills.prototype.setMaxID = function (ID) { this.tree.maxNodeID(ID); }; // 提交数据后的错误处理方法 bills.prototype.doAfterUpdate = function(err, data){ console.log(data) if(data.quantityRefresh){ this.refreshDatas(data,'quantity'); } }; bills.prototype.refreshDatas = function(data,fieldName){ var dataIndex = _.findIndex(this.datas,function(item) { return item.ID ==data.billID; }); this.datas[dataIndex][fieldName] = data[fieldName]; if(fieldName=='quantity'){ this.datas[dataIndex]['isFromDetail']=1 } var controller = projectObj.mainController; var selected = controller.sheet.getSelections(); var col = _.findIndex(BillsGridSetting.cols,function (col) { return col.data.field ==fieldName; }); controller.sheet.getCell(selected[0].row,col).value(data[fieldName]); }; bills.prototype.getCounterData = function (count) { var updateData = {'projectID': this.project.ID()}; if (count) { updateData[this.getSourceType()] = this.tree.maxNodeID() + count; } else { updateData[this.getSourceType()] = this.tree.maxNodeID() + 1; } return updateData; }; bills.prototype.insertBills = function (parentId, nextSiblingId) { var insertData = this.tree.getInsertData(parentId, nextSiblingId); var that = this, newData = null; insertData.forEach(function (data) { if (data.type === idTree.updateType.new) { newData = data.data; } }); this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()], [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]); //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData)); this.datas.push(newData); return this.tree.insert(parentId, nextSiblingId); }; bills.prototype.insertStdBills = function (parentId, nextSiblingId, stdBillsData) { var insertData = this.tree.getInsertData(parentId, nextSiblingId); var newData = null, that = this; insertData.forEach(function (data) { if (data.type === idTree.updateType.new) { data.data.code = stdBillsData.code; data.data.name = stdBillsData.name; data.data.unit = stdBillsData.unit; newData = data.data; } }); this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()], [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]); this.datas.push(newData); return this.tree.insertByData(newData, parentId, nextSiblingId); }; bills.prototype.deleteBills = function (node) { let deleteNode = function (node) { this.project.Ration.deleteByBills([node]); this.project.VolumePrice.deleteByBills([node]); return this.tree.delete(node); } var deleteData = this.tree.getDeleteData(node); var ration_glj =projectObj.project.ration_glj; let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj, ModuleNames.volume_price]; let deleteDatas=[tools.coverseTreeUpdateData(deleteData, this.project.ID()), this.project.Ration.getDeleteDataByBill([node]), ration_glj.getDeleteDataByBills(deleteData), this.project.VolumePrice.getDeleteDataByBills([node]) ]; project.ration_glj.deleteByBills(deleteData); project.quantity_detail.deleteByBills(deleteData); project.pushNow('deleteBills', modules, deleteDatas); return this.tree.delete(node); }; bills.prototype.upMoveBills = function (node) { var upMoveData = node.getUpMoveData(); project.pushNow('upMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(upMoveData, this.project.ID())); return node.upMove(); }; bills.prototype.downMoveBills = function (node) { var downMoveData = node.getDownMoveData(); project.pushNow('downMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(downMoveData, this.project.ID())); return node.downMove(); }; bills.prototype.upLevelBills = function (node) { var upLevelData = node.getUpLevelData(); project.pushNow('upLevelBills', this.getSourceType(), tools.coverseTreeUpdateData(upLevelData, this.project.ID())); return node.upLevel(); }; bills.prototype.downLevelBills = function (node) { var downLevelData = node.getDownLevelData(); project.pushNow('downLevelBills', [this.getSourceType()], [tools.coverseTreeUpdateData(downLevelData, this.project.ID())]); return node.downLevel(); }; bills.prototype.updateField = function (node, field, newValue) { calcFees.setFee(node.data, field, newValue); let updateData = []; let data = {'ID': node.getID(), 'projectID': this.project.ID()}; if (field === 'quantity') { data[field] = newValue; // to do Calculate if (node.data.fees) { data.fees = node.data.fees; } } else if (field === 'feesIndex.common.unitFee') { // to do Calculate if (node.data.fees) { data.fees = node.data.fees; } } else { data[field] = newValue; } updateData.push({'updateType': 'ut_update', 'updateData': data}); this.project.pushNow('updateBills', this.getSourceType(), updateData); }; return new bills(project); } };