StatJson.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var StatJson = new Class({
  2. options : {
  3. },
  4. initialize: function ( context, options ) {
  5. this.setOptions(options);
  6. this.context = context;
  7. },
  8. load : function(){
  9. if( this.context.statJson ){
  10. this.json = JSON.parse(this.context.statJson);
  11. }else{
  12. this.json = {
  13. total : {
  14. publishedCount : 0,
  15. errorCount : 0
  16. },
  17. batch: {}
  18. }
  19. }
  20. },
  21. sumit : function(){
  22. this.context.statJson = JSON.stringify(this.json);
  23. },
  24. addBatch : function( batchName, isSetCurrent ){
  25. if( !this.json.batch[batchName] ){
  26. this.json.batch[batchName] = {
  27. publishedCount : 0,
  28. errorCount : 0
  29. };
  30. }
  31. if( isSetCurrent )this.currentBatch = this.json.batch[batchName];
  32. },
  33. deleteBatch : function( batchName ){
  34. var json = this.json;
  35. if( json.batch[batchName] ){
  36. var batch = json.batch[batchName];
  37. if( batch.publishedCount ){
  38. json.total.publishedCount = json.total.publishedCount - batch.publishedCount;
  39. }
  40. if( batch.errorCount ){
  41. json.total.errorCount = json.total.errorCount - batch.errorCount;
  42. }
  43. delete this.json.batch[batchName];
  44. }
  45. },
  46. addData: function( cmsDocData ){
  47. var d = cmsDocData;
  48. var totalJson = this.json.total;
  49. var batchJson = this.currentBatch;
  50. if( d.docStatus == "published" ){
  51. totalJson.publishedCount++;
  52. batchJson.publishedCount++;
  53. this.addCount( totalJson, d );
  54. this.addCount( batchJson, d );
  55. }else if( d.docStatus == "error" ){
  56. totalJson.errorCount++;
  57. batchJson.errorCount++;
  58. }
  59. },
  60. addCount : function( json, d ){
  61. if( d.city ){
  62. var cityJson = json[ d.city ];
  63. if( !cityJson ){
  64. cityJson = json[ d.city ] = { publishedCount : 0 };
  65. }
  66. cityJson.publishedCount ++;
  67. if( d.county ){
  68. var countyJson = cityJson[ d.county ];
  69. if( !countyJson ){
  70. countyJson = cityJson[ d.county ] = { publishedCount : 0 };
  71. }
  72. countyJson.publishedCount ++;
  73. if( d.brach ) {
  74. var branchJson = countyJson[d.brach];
  75. if (!branchJson) {
  76. branchJson = countyJson[d.brach] = {publishedCount: 0};
  77. }
  78. branchJson.publishedCount++;
  79. }
  80. }
  81. }
  82. },
  83. reduceCount : function( json, d ){
  84. if( d.city ){
  85. var cityJson = json[ d.city ];
  86. if( !cityJson ){
  87. cityJson = json[ d.city ] = { publishedCount : 0 };
  88. }
  89. cityJson.publishedCount --;
  90. if( d.county ){
  91. var countyJson = cityJson[ d.county ];
  92. if( !countyJson ){
  93. countyJson = cityJson[ d.county ] = { publishedCount : 0 };
  94. }
  95. countyJson.publishedCount --;
  96. if( d.brach ) {
  97. var branchJson = countyJson[d.brach];
  98. if (!branchJson) {
  99. branchJson = countyJson[d.brach] = {publishedCount: 0};
  100. }
  101. branchJson.publishedCount--;
  102. }
  103. }
  104. }
  105. },
  106. getCity : function(){
  107. var totalJson = this.json.total;
  108. var city = [];
  109. for( var key in totalJson ){
  110. if( key != "publishedCount" && key != "errorCount" ){
  111. if( totalJson[key].publishedCount > 0 ){
  112. city.push(key);
  113. }
  114. }
  115. }
  116. return city;
  117. },
  118. changeData : function( cmsDocData, oldData ){
  119. var d = cmsDocData;
  120. //var oldData = {
  121. // status : "error",
  122. // city : "",
  123. // county : "",
  124. // branch : ""
  125. //};
  126. var batchJson;
  127. if( d.$importBatchName && this.json.batch[d.$importBatchName]) {
  128. batchJson = this.json.batch[d.$importBatchName];
  129. }
  130. var totalJson = this.json.total;
  131. if( oldData.status == "error" ){
  132. totalJson.errorCount--;
  133. if( batchJson )batchJson.errorCount--;
  134. }
  135. if( oldData.status == "published" ){
  136. totalJson.publishedCount--;
  137. this.reduceCount( totalJson, oldData );
  138. if( batchJson ){
  139. batchJson.publishedCount--;
  140. this.reduceCount( batchJson, oldData );
  141. }
  142. }
  143. if( d.docStatus == "error"){
  144. totalJson.errorCount++;
  145. if( batchJson )batchJson.errorCount++;
  146. }
  147. if( d.docStatus == "published"){
  148. totalJson.publishedCount++;
  149. this.addCount( totalJson, d );
  150. if( batchJson ){
  151. batchJson.publishedCount++;
  152. this.addCount( batchJson, d );
  153. }
  154. }
  155. }
  156. });