project.js 14 KB

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