project.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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);
  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);
  131. newNode.source = br[i];
  132. newNode.sourceType = that.Ration.getSourceType();
  133. newNode.data = br[i];
  134. if(projectInfoObj.projectInfo.property.displaySetting.disPlayMainMateria==true){
  135. loadRationGLJNode(newNode);
  136. }
  137. }
  138. };
  139. /* let loadVolumePriceNode = function (cacheNode) {
  140. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  141. for (let v of bv) {
  142. newNode = that.mainTree.addNode(cacheNode);
  143. newNode.source = v;
  144. newNode.sourceType = that.VolumePrice.getSourceType();
  145. newNode.data = v;
  146. }
  147. };*/
  148. var loadIdTreeNode = function (nodes, parent) {
  149. var newNode, i;
  150. for (i = 0; i < nodes.length; i++) {
  151. newNode = that.mainTree.addNode(parent);
  152. newNode.source = nodes[i];
  153. newNode.sourceType = that.Bills.getSourceType();
  154. newNode.data = nodes[i].data;
  155. that.FeeRate.loadFeeRateToBill(newNode);
  156. if (nodes[i].children.length === 0) {
  157. loadRationNode(that.Ration.datas, newNode);
  158. // loadVolumePriceNode(newNode);
  159. } else {
  160. loadIdTreeNode(nodes[i].children, newNode);
  161. }
  162. }
  163. };
  164. loadIdTreeNode(this.Bills.tree.roots, null);
  165. this.mainTree.sortTreeItems();
  166. this.mainTree.selected = this.mainTree.firstNode();
  167. };
  168. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  169. var parent = node;
  170. while (parent && parent[targetField] !== targetValue) {
  171. parent = parent.parent;
  172. }
  173. return parent;
  174. };
  175. // 提供给各模块调用的统一从后台获取数据的方法
  176. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  177. $.ajax({
  178. type:"POST",
  179. url: url,
  180. data: {'data': JSON.stringify(data)},
  181. dataType: 'json',
  182. cache: false,
  183. timeout: 50000,
  184. success: function(result){
  185. successCallback(result);
  186. },
  187. error: function(jqXHR, textStatus, errorThrown){
  188. alert('error ' + textStatus + " " + errorThrown);
  189. errorCallback();
  190. }
  191. });
  192. };*/
  193. // 所有模块在此从后台获取数据
  194. project.prototype.loadDatas = function (callback){
  195. $.ajax({
  196. type: "POST",
  197. url: '/project/getData',
  198. data: {'data': JSON.stringify({
  199. "project_id": tools._ID,
  200. "user_id": tools._userID
  201. })},
  202. dataType: 'json',
  203. cache: false,
  204. timeout: 50000,
  205. success: function (result) {
  206. if (!result.error) {
  207. tools.doAfterLoad(result.data, callback);
  208. // for test calc
  209. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  210. } else {
  211. alert('error: ' + result.message);
  212. callback(result.error);
  213. }
  214. },
  215. error: function(jqXHR, textStatus, errorThrown){
  216. alert('error ' + textStatus + " " + errorThrown);
  217. }
  218. });
  219. };
  220. project.prototype.beginUpdate = function(operation){
  221. if (tools.updateLock === 0){
  222. tools.operation = operation
  223. }
  224. tools.updateLock += 1;
  225. };
  226. project.prototype.endUpdate = function(){
  227. if (tools.updateLock === 0){
  228. throw "project can not endUpdate before beginUpdate";
  229. }
  230. tools.updateLock -= 1;
  231. if (tools.updateLock === 0) {
  232. $.ajax({
  233. type: "POST",
  234. url: '/project/save',
  235. data: {'data': JSON.stringify({
  236. "project_id": tools._ID,
  237. "user_id": tools._userID,
  238. "date": new Date,
  239. "operation": tools.operation,
  240. "update_data": tools.updateData
  241. })},
  242. dataType: 'json',
  243. cache: false,
  244. timeout: 50000,
  245. success: function (result) {
  246. if (!result.error) {
  247. tools.doAfterUpdate(result.data);
  248. } else {
  249. alert('error: ' + result.message);
  250. }
  251. },
  252. error: function(jqXHR, textStatus, errorThrown){
  253. alert('error ' + textStatus + " " + errorThrown);
  254. }
  255. });
  256. tools.updateData = [];
  257. tools.operation = "";
  258. }
  259. };
  260. project.prototype.push = function(moduleName, data){
  261. if (tools.updateLock === 0){
  262. throw "project can not push data before beginUpdate";
  263. }
  264. var moduleData = {
  265. moduleName: moduleName,
  266. data: data
  267. };
  268. tools.updateData.push(moduleData);
  269. };
  270. project.prototype.pushNow = function (operation, moduleName, data) {
  271. var that = this;
  272. this.beginUpdate(operation);
  273. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  274. moduleName.forEach(function (name, index) {
  275. that.push(moduleName[index], data[index]);
  276. });
  277. } else {
  278. this.push(moduleName, data);
  279. }
  280. this.endUpdate();
  281. };
  282. project.prototype.registerModule = function(moduleName, obj){
  283. if (!tools.modules.hasOwnProperty(moduleName)){
  284. tools.modules[moduleName] = obj;
  285. }
  286. };
  287. /* project.prototype.setBillsCalcMode = function (calcMode) {
  288. this.property.billsCalcMode = calcMode;
  289. this.initCalcFields();
  290. };*/
  291. /*project.prototype.initCalcFields = function () {
  292. // let settingConst = this.projSetting.settingConst;
  293. if (this.calcFields) {
  294. for (let field of this.calcFields) {
  295. // unitFeeCalcFlag
  296. if (field.type === 'zangu') {
  297. field.unitFeeFlag = converseUnitFeeFlag;
  298. } else {
  299. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationContent) {
  300. if (this.property.billsCalcMode === leafBillGetFeeType.rationContent) {
  301. field.unitFeeFlag = rationContentUnitFeeFlag;
  302. // } else if ( this.projSetting.billsCalcMode === settingConst.billsCalcMode.billsPrice) {
  303. } else if ( this.property.billsCalcMode === leafBillGetFeeType.billsPrice) {
  304. field.unitFeeFlag = billsPriceUnitFeeFlag;
  305. } else {
  306. field.unitFeeFlag = averageQtyUnitFeeFlag;
  307. }
  308. }
  309. // totalFeeCalcFlag
  310. if (field.type === 'common') {
  311. // if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationPriceConverse) {
  312. if (this.property.billsCalcMode === leafBillGetFeeType.rationPriceConverse) {
  313. field.totalFeeFlag = sumTotalFeeFlag;
  314. } else {
  315. field.totalFeeFlag = totalFeeFlag;
  316. }
  317. } else {
  318. field.totalFeeFlag = sumTotalFeeFlag;
  319. }
  320. }
  321. }
  322. }*/
  323. return new project();
  324. }
  325. };