project.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. };
  17. var me = tools;
  18. tools.doAfterUpdate = function(result){
  19. if (!result) { return }
  20. result.forEach(function(item){
  21. if(_.isArray(item)){
  22. item.forEach(function (e) {
  23. me.eachItem(e);
  24. })
  25. }else {
  26. me.eachItem(item)
  27. }
  28. });
  29. };
  30. tools.doAfterLoad = function(result, callback){
  31. var counter;
  32. result.forEach(function(item){
  33. if (me.modules[item.moduleName]){
  34. me.modules[item.moduleName].loadData(item.data);
  35. } else if (item.moduleName === me.projCounter) {
  36. counter = item.data;
  37. }
  38. });
  39. for (module in counter) {
  40. if (me.modules[module]) {
  41. me.modules[module].setMaxID(counter[module]);
  42. }
  43. }
  44. me._project.loadMainTree();
  45. //me.test(result[0].data[0]);
  46. callback(0);
  47. };
  48. tools.eachItem=function(item){
  49. if (me.modules[item.moduleName]){
  50. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  51. }
  52. };
  53. /*tools.test = function(data){
  54. me._project.beginUpdate('修改名称');
  55. data.name = 'test';
  56. data['updateType'] = 'ut_update';
  57. me._project.push(ModuleNames.bills, [data]);
  58. me._project.endUpdate();
  59. };*/
  60. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  61. var project = function () {
  62. tools._project = this;
  63. this.mainTree = cacheTree.createNew(this);
  64. this.Bills = Bills.createNew(this);
  65. this.Ration = Ration.createNew(this);
  66. this.GLJ = GLJ.createNew(this);
  67. this.ration_glj = ration_glj.createNew(this);
  68. this.ration_coe = ration_coe.createNew(this);
  69. this.ration_ass = ration_ass.createNew(this);
  70. this.quantity_detail = quantity_detail.createNew(this);
  71. this.FeeRate = FeeRate.createNew(this);
  72. this.VolumePrice = VolumePrice.createNew(this);
  73. this.Decimal = {
  74. common: {
  75. quantity: 3,
  76. unitFee: 2,
  77. totalFee: 2
  78. }
  79. };
  80. this.masterField = {ration: 'billsItemID', volumePrice: 'billsItemID'};
  81. };
  82. // prototype用于定义public方法
  83. project.prototype.modify = function (modifyDatas, callback) {
  84. // To Do
  85. };
  86. // prototype用于定义public方法
  87. project.prototype.ID = function () {
  88. return tools._ID;
  89. };
  90. project.prototype.projCounter = function () {
  91. return tools.projCounter;
  92. };
  93. project.prototype.getDecimal = function (fullName) {
  94. let names = fullName.split('.'), decimal = this.Decimal;
  95. for (let name of names) {
  96. if (decimal[name]) {
  97. decimal = decimal[name];
  98. } else {
  99. return null;
  100. }
  101. }
  102. if (Object.prototype.toString.apply(decimal) === '[object Number]') {
  103. return decimal;
  104. } else {
  105. return null;
  106. }
  107. };
  108. project.prototype.loadMainTree = function () {
  109. var that = this;
  110. var loadRationNode = function (rations, cacheNode) {
  111. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  112. for (i = 0; i < br.length; i++) {
  113. newNode = that.mainTree.addNode(cacheNode);
  114. newNode.source = br[i];
  115. newNode.sourceType = that.Ration.getSourceType();
  116. newNode.data = br[i];
  117. }
  118. };
  119. let loadVolumePriceNode = function (cacheNode) {
  120. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  121. for (let v of bv) {
  122. newNode = that.mainTree.addNode(cacheNode);
  123. newNode.source = v;
  124. newNode.sourceType = that.VolumePrice.getSourceType();
  125. newNode.data = v;
  126. }
  127. };
  128. var loadIdTreeNode = function (nodes, parent) {
  129. var newNode, i;
  130. for (i = 0; i < nodes.length; i++) {
  131. newNode = that.mainTree.addNode(parent);
  132. newNode.source = nodes[i];
  133. newNode.sourceType = that.Bills.getSourceType();
  134. newNode.data = nodes[i].data;
  135. if (nodes[i].children.length === 0) {
  136. loadRationNode(that.Ration.datas, newNode);
  137. loadVolumePriceNode(newNode);
  138. } else {
  139. loadIdTreeNode(nodes[i].children, newNode);
  140. }
  141. }
  142. };
  143. loadIdTreeNode(this.Bills.tree.roots, null);
  144. this.mainTree.sortTreeItems();
  145. };
  146. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  147. var parent = node;
  148. while (parent && parent[targetField] !== targetValue) {
  149. parent = parent.parent;
  150. }
  151. return parent;
  152. };
  153. // 提供给各模块调用的统一从后台获取数据的方法
  154. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  155. $.ajax({
  156. type:"POST",
  157. url: url,
  158. data: {'data': JSON.stringify(data)},
  159. dataType: 'json',
  160. cache: false,
  161. timeout: 50000,
  162. success: function(result){
  163. successCallback(result);
  164. },
  165. error: function(jqXHR, textStatus, errorThrown){
  166. alert('error ' + textStatus + " " + errorThrown);
  167. errorCallback();
  168. }
  169. });
  170. };*/
  171. // 所有模块在此从后台获取数据
  172. project.prototype.loadDatas = function (callback){
  173. $.ajax({
  174. type: "POST",
  175. url: '/project/getData',
  176. data: {'data': JSON.stringify({
  177. "project_id": tools._ID,
  178. "user_id": tools._userID
  179. })},
  180. dataType: 'json',
  181. cache: false,
  182. timeout: 50000,
  183. success: function (result) {
  184. if (!result.error) {
  185. tools.doAfterLoad(result.data, callback);
  186. // for test calc
  187. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  188. } else {
  189. alert('error: ' + result.message);
  190. callback(result.error);
  191. }
  192. },
  193. error: function(jqXHR, textStatus, errorThrown){
  194. alert('error ' + textStatus + " " + errorThrown);
  195. }
  196. });
  197. };
  198. project.prototype.beginUpdate = function(operation){
  199. if (tools.updateLock === 0){
  200. tools.operation = operation
  201. }
  202. tools.updateLock += 1;
  203. };
  204. project.prototype.endUpdate = function(){
  205. if (tools.updateLock === 0){
  206. throw "project can not endUpdate before beginUpdate";
  207. }
  208. tools.updateLock -= 1;
  209. if (tools.updateLock === 0) {
  210. $.ajax({
  211. type: "POST",
  212. url: '/project/save',
  213. data: {'data': JSON.stringify({
  214. "project_id": tools._ID,
  215. "user_id": tools._userID,
  216. "date": new Date,
  217. "operation": tools.operation,
  218. "update_data": tools.updateData
  219. })},
  220. dataType: 'json',
  221. cache: false,
  222. timeout: 50000,
  223. success: function (result) {
  224. if (!result.error) {
  225. tools.doAfterUpdate(result.data);
  226. } else {
  227. alert('error: ' + result.message);
  228. }
  229. },
  230. error: function(jqXHR, textStatus, errorThrown){
  231. alert('error ' + textStatus + " " + errorThrown);
  232. }
  233. });
  234. tools.updateData = [];
  235. tools.operation = "";
  236. }
  237. };
  238. project.prototype.push = function(moduleName, data){
  239. if (tools.updateLock === 0){
  240. throw "project can not push data before beginUpdate";
  241. }
  242. var moduleData = {
  243. moduleName: moduleName,
  244. data: data
  245. };
  246. tools.updateData.push(moduleData);
  247. };
  248. project.prototype.pushNow = function (operation, moduleName, data) {
  249. var that = this;
  250. this.beginUpdate(operation);
  251. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  252. moduleName.forEach(function (name, index) {
  253. that.push(moduleName[index], data[index]);
  254. });
  255. } else {
  256. this.push(moduleName, data);
  257. }
  258. this.endUpdate();
  259. }
  260. project.prototype.registerModule = function(moduleName, obj){
  261. if (!tools.modules.hasOwnProperty(moduleName)){
  262. tools.modules[moduleName] = obj;
  263. }
  264. };
  265. return new project();
  266. }
  267. };