project.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var PROJECT = {
  5. createNew: function (projectID, userID) {
  6. // 定义private方法
  7. var tools = {
  8. _project: null,
  9. _ID: projectID,
  10. _userID: userID,
  11. updateLock: 0,
  12. updateData: [],
  13. operation: '',
  14. modules: {},
  15. projCounter: 'projCounter',
  16. projSetting: 'proj_setting'
  17. };
  18. var me = tools;
  19. tools.doAfterUpdate = function(result){
  20. if (!result) { return }
  21. result.forEach(function(item){
  22. if(_.isArray(item)){
  23. item.forEach(function (e) {
  24. me.eachItem(e);
  25. })
  26. }else {
  27. me.eachItem(item)
  28. }
  29. });
  30. };
  31. tools.doAfterLoad = function(result, callback){
  32. var counter;
  33. result.forEach(function(item){
  34. if (me.modules[item.moduleName]){
  35. me.modules[item.moduleName].loadData(item.data);
  36. } else if (item.moduleName === me.projCounter) {
  37. counter = item.data;
  38. } else if (item.moduleName === me.projSetting) {
  39. me._project.projSetting = item.data;
  40. me._project.projSetting.moduleName = me.projSetting;
  41. }else if(item.moduleName === ModuleNames.projectGLJ){
  42. me._project.projectGLJ.loadToCache(item.data);
  43. }
  44. });
  45. for (module in counter) {
  46. if (me.modules[module]) {
  47. me.modules[module].setMaxID(counter[module]);
  48. }
  49. }
  50. me._project.loadMainTree();
  51. //me.test(result[0].data[0]);
  52. callback(0);
  53. };
  54. tools.eachItem=function(item){
  55. if (me.modules[item.moduleName]){
  56. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  57. }
  58. };
  59. /*tools.test = function(data){
  60. me._project.beginUpdate('修改名称');
  61. data.name = 'test';
  62. data['updateType'] = 'ut_update';
  63. me._project.push(ModuleNames.bills, [data]);
  64. me._project.endUpdate();
  65. };*/
  66. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  67. var project = function () {
  68. tools._project = this;
  69. this.mainTree = cacheTree.createNew(this);
  70. this.Bills = Bills.createNew(this);
  71. this.Ration = Ration.createNew(this);
  72. this.GLJ = GLJ.createNew(this);
  73. this.ration_glj = ration_glj.createNew(this);
  74. this.ration_coe = ration_coe.createNew(this);
  75. this.ration_ass = ration_ass.createNew(this);
  76. this.quantity_detail = quantity_detail.createNew(this);
  77. this.FeeRate = FeeRate.createNew(this);
  78. // this.VolumePrice = VolumePrice.createNew(this);
  79. this.projectGLJ = new ProjectGLJ();
  80. // this.projectGLJ.loadData();
  81. this.composition = new Composition();
  82. this.composition.loadData();
  83. this.labourCoe = new LabourCoe(this);
  84. this.calcProgram = new CalcProgram(this);
  85. this.calcBase = calcBase;
  86. // this.masterField = {ration: 'billsItemID', volumePrice: 'billsItemID'};
  87. this.masterField = {ration: 'billsItemID'};
  88. };
  89. // prototype用于定义public方法
  90. project.prototype.modify = function (modifyDatas, callback) {
  91. // To Do
  92. };
  93. // prototype用于定义public方法
  94. project.prototype.ID = function () {
  95. return tools._ID;
  96. };
  97. project.prototype.projCounter = function () {
  98. return tools.projCounter;
  99. };
  100. project.prototype.getDecimal = function (fullName) {
  101. let names = fullName.split('.'), decimal = this.Decimal;
  102. for (let name of names) {
  103. if (decimal[name]) {
  104. decimal = decimal[name];
  105. } else {
  106. return null;
  107. }
  108. }
  109. if (Object.prototype.toString.apply(decimal) === '[object Number]') {
  110. return decimal;
  111. } else {
  112. return null;
  113. }
  114. };
  115. project.prototype.loadMainTree = function () {
  116. var that = this;
  117. let loadRationGLJNode = function (cacheNode) {
  118. var newNode, bj = that.ration_glj.getMainAndEquGLJ(cacheNode.source.ID), i;
  119. for(i=0;i<bj.length;i++){
  120. that.ration_glj.transferToNodeData(bj[i]);
  121. newNode = that.mainTree.addNode(cacheNode, null, bj[i].ID);
  122. newNode.source = bj[i];
  123. newNode.sourceType = that.ration_glj.getSourceType();
  124. newNode.data = bj[i];
  125. }
  126. };
  127. var loadRationNode = function (rations, cacheNode) {
  128. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  129. for (i = 0; i < br.length; i++) {
  130. newNode = that.mainTree.addNode(cacheNode, null, br[i].ID);
  131. if(br[i].type==rationType.gljRation){
  132. br[i]= that.ration_glj.combineRationAndGLJ(br[i]);
  133. }
  134. newNode.source = br[i];
  135. newNode.sourceType = that.Ration.getSourceType();
  136. newNode.data = br[i];
  137. if(projectInfoObj.projectInfo.property.displaySetting !== undefined &&
  138. projectInfoObj.projectInfo.property.displaySetting.disPlayMainMaterial==true){
  139. loadRationGLJNode(newNode);
  140. }
  141. }
  142. };
  143. /* let loadVolumePriceNode = function (cacheNode) {
  144. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  145. for (let v of bv) {
  146. newNode = that.mainTree.addNode(cacheNode);
  147. newNode.source = v;
  148. newNode.sourceType = that.VolumePrice.getSourceType();
  149. newNode.data = v;
  150. }
  151. };*/
  152. var loadIdTreeNode = function (nodes, parent) {
  153. var newNode, i;
  154. for (i = 0; i < nodes.length; i++) {
  155. newNode = that.mainTree.addNode(parent, null, nodes[i].data.ID);
  156. newNode.source = nodes[i];
  157. newNode.sourceType = that.Bills.getSourceType();
  158. newNode.data = nodes[i].data;
  159. that.FeeRate.loadFeeRateToBill(newNode);
  160. if (nodes[i].children.length === 0) {
  161. loadRationNode(that.Ration.datas, newNode);
  162. // loadVolumePriceNode(newNode);
  163. } else {
  164. loadIdTreeNode(nodes[i].children, newNode);
  165. }
  166. }
  167. };
  168. loadIdTreeNode(this.Bills.tree.roots, null);
  169. this.mainTree.sortTreeItems();
  170. this.mainTree.selected = this.mainTree.firstNode();
  171. };
  172. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  173. var parent = node;
  174. while (parent && parent[targetField] !== targetValue) {
  175. parent = parent.parent;
  176. }
  177. return parent;
  178. };
  179. // 提供给各模块调用的统一从后台获取数据的方法
  180. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  181. $.ajax({
  182. type:"POST",
  183. url: url,
  184. data: {'data': JSON.stringify(data)},
  185. dataType: 'json',
  186. cache: false,
  187. timeout: 50000,
  188. success: function(result){
  189. successCallback(result);
  190. },
  191. error: function(jqXHR, textStatus, errorThrown){
  192. alert('error ' + textStatus + " " + errorThrown);
  193. errorCallback();
  194. }
  195. });
  196. };*/
  197. // 所有模块在此从后台获取数据
  198. project.prototype.loadDatas = function (callback){
  199. $.ajax({
  200. type: "POST",
  201. url: '/project/getData',
  202. data: {'data': JSON.stringify({
  203. "project_id": tools._ID,
  204. "user_id": tools._userID
  205. })},
  206. dataType: 'json',
  207. cache: false,
  208. timeout: 50000,
  209. success: function (result) {
  210. if (!result.error) {
  211. tools.doAfterLoad(result.data, callback);
  212. // for test calc
  213. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  214. } else {
  215. alert('error: ' + result.message);
  216. callback(result.error);
  217. }
  218. },
  219. error: function(jqXHR, textStatus, errorThrown){
  220. alert('error ' + textStatus + " " + errorThrown);
  221. }
  222. });
  223. };
  224. project.prototype.beginUpdate = function(operation){
  225. if (tools.updateLock === 0){
  226. tools.operation = operation
  227. }
  228. tools.updateLock += 1;
  229. };
  230. project.prototype.endUpdate = function(){
  231. if (tools.updateLock === 0){
  232. throw "project can not endUpdate before beginUpdate";
  233. }
  234. tools.updateLock -= 1;
  235. if (tools.updateLock === 0) {
  236. $.ajax({
  237. type: "POST",
  238. url: '/project/save',
  239. data: {'data': JSON.stringify({
  240. "project_id": tools._ID,
  241. "user_id": tools._userID,
  242. "date": new Date,
  243. "operation": tools.operation,
  244. "update_data": tools.updateData
  245. })},
  246. dataType: 'json',
  247. cache: false,
  248. timeout: 50000,
  249. success: function (result) {
  250. if (!result.error) {
  251. tools.doAfterUpdate(result.data);
  252. } else {
  253. alert('error: ' + result.message);
  254. }
  255. },
  256. error: function(jqXHR, textStatus, errorThrown){
  257. alert('error ' + textStatus + " " + errorThrown);
  258. }
  259. });
  260. tools.updateData = [];
  261. tools.operation = "";
  262. }
  263. };
  264. project.prototype.push = function(moduleName, data){
  265. if (tools.updateLock === 0){
  266. throw "project can not push data before beginUpdate";
  267. }
  268. var moduleData = {
  269. moduleName: moduleName,
  270. data: data
  271. };
  272. tools.updateData.push(moduleData);
  273. };
  274. project.prototype.pushNow = function (operation, moduleName, data) {
  275. var that = this;
  276. this.beginUpdate(operation);
  277. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  278. moduleName.forEach(function (name, index) {
  279. if(name != 'projCounter'){
  280. that.push(moduleName[index], data[index]);
  281. }
  282. });
  283. } else {
  284. this.push(moduleName, data);
  285. }
  286. this.endUpdate();
  287. };
  288. project.prototype.registerModule = function(moduleName, obj){
  289. if (!tools.modules.hasOwnProperty(moduleName)){
  290. tools.modules[moduleName] = obj;
  291. }
  292. };
  293. /* project.prototype.setBillsCalcMode = function (calcMode) {
  294. this.property.billsCalcMode = calcMode;
  295. this.initCalcFields();
  296. };*/
  297. /*project.prototype.initCalcFields = function () {
  298. // let settingConst = this.projSetting.settingConst;
  299. if (this.calcFields) {
  300. for (let field of this.calcFields) {
  301. // unitFeeCalcFlag
  302. if (field.type === 'zangu') {
  303. field.unitFeeFlag = converseUnitFeeFlag;
  304. } else {
  305. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationContent) {
  306. if (this.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  307. field.unitFeeFlag = rationContentUnitFeeFlag;
  308. // } else if ( this.projSetting.billsCalcMode === settingConst.billsCalcMode.billsPrice) {
  309. } else if ( this.property.billsCalcMode === leafBillGetFeeType.billsPrice) {
  310. field.unitFeeFlag = billsPriceUnitFeeFlag;
  311. } else {
  312. field.unitFeeFlag = averageQtyUnitFeeFlag;
  313. }
  314. }
  315. // totalFeeCalcFlag
  316. if (field.type === 'common') {
  317. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationPriceConverse) {
  318. if (this.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse) {
  319. field.totalFeeFlag = sumTotalFeeFlag;
  320. } else {
  321. field.totalFeeFlag = totalFeeFlag;
  322. }
  323. } else {
  324. field.totalFeeFlag = sumTotalFeeFlag;
  325. }
  326. }
  327. }
  328. }*/
  329. return new project();
  330. }
  331. };