project.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. }
  42. });
  43. for (module in counter) {
  44. if (me.modules[module]) {
  45. me.modules[module].setMaxID(counter[module]);
  46. }
  47. }
  48. me._project.loadMainTree();
  49. //me.test(result[0].data[0]);
  50. callback(0);
  51. };
  52. tools.eachItem=function(item){
  53. if (me.modules[item.moduleName]){
  54. me.modules[item.moduleName].doAfterUpdate(item.err, item.data);
  55. }
  56. };
  57. /*tools.test = function(data){
  58. me._project.beginUpdate('修改名称');
  59. data.name = 'test';
  60. data['updateType'] = 'ut_update';
  61. me._project.push(ModuleNames.bills, [data]);
  62. me._project.endUpdate();
  63. };*/
  64. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  65. var project = function () {
  66. tools._project = this;
  67. this.mainTree = cacheTree.createNew(this);
  68. this.Bills = Bills.createNew(this);
  69. this.Ration = Ration.createNew(this);
  70. this.GLJ = GLJ.createNew(this);
  71. this.ration_glj = ration_glj.createNew(this);
  72. this.ration_coe = ration_coe.createNew(this);
  73. this.ration_ass = ration_ass.createNew(this);
  74. this.quantity_detail = quantity_detail.createNew(this);
  75. this.FeeRate = FeeRate.createNew(this);
  76. this.VolumePrice = VolumePrice.createNew(this);
  77. this.projectGLJ = new ProjectGLJ();
  78. this.projectGLJ.loadData();
  79. this.Decimal = {
  80. common: {
  81. quantity: 3,
  82. unitFee: 2,
  83. totalFee: 2
  84. }
  85. };
  86. this.LabourCoe = new LabourCoe(this);
  87. this.masterField = {ration: 'billsItemID', volumePrice: '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. var loadRationNode = function (rations, cacheNode) {
  118. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  119. for (i = 0; i < br.length; i++) {
  120. newNode = that.mainTree.addNode(cacheNode);
  121. newNode.source = br[i];
  122. newNode.sourceType = that.Ration.getSourceType();
  123. newNode.data = br[i];
  124. }
  125. };
  126. let loadVolumePriceNode = function (cacheNode) {
  127. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  128. for (let v of bv) {
  129. newNode = that.mainTree.addNode(cacheNode);
  130. newNode.source = v;
  131. newNode.sourceType = that.VolumePrice.getSourceType();
  132. newNode.data = v;
  133. }
  134. };
  135. var loadIdTreeNode = function (nodes, parent) {
  136. var newNode, i;
  137. for (i = 0; i < nodes.length; i++) {
  138. newNode = that.mainTree.addNode(parent);
  139. newNode.source = nodes[i];
  140. newNode.sourceType = that.Bills.getSourceType();
  141. newNode.data = nodes[i].data;
  142. if (nodes[i].children.length === 0) {
  143. loadRationNode(that.Ration.datas, newNode);
  144. loadVolumePriceNode(newNode);
  145. } else {
  146. loadIdTreeNode(nodes[i].children, newNode);
  147. }
  148. }
  149. };
  150. loadIdTreeNode(this.Bills.tree.roots, null);
  151. this.mainTree.sortTreeItems();
  152. };
  153. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  154. var parent = node;
  155. while (parent && parent[targetField] !== targetValue) {
  156. parent = parent.parent;
  157. }
  158. return parent;
  159. };
  160. // 提供给各模块调用的统一从后台获取数据的方法
  161. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  162. $.ajax({
  163. type:"POST",
  164. url: url,
  165. data: {'data': JSON.stringify(data)},
  166. dataType: 'json',
  167. cache: false,
  168. timeout: 50000,
  169. success: function(result){
  170. successCallback(result);
  171. },
  172. error: function(jqXHR, textStatus, errorThrown){
  173. alert('error ' + textStatus + " " + errorThrown);
  174. errorCallback();
  175. }
  176. });
  177. };*/
  178. // 所有模块在此从后台获取数据
  179. project.prototype.loadDatas = function (callback){
  180. $.ajax({
  181. type: "POST",
  182. url: '/project/getData',
  183. data: {'data': JSON.stringify({
  184. "project_id": tools._ID,
  185. "user_id": tools._userID
  186. })},
  187. dataType: 'json',
  188. cache: false,
  189. timeout: 50000,
  190. success: function (result) {
  191. if (!result.error) {
  192. tools.doAfterLoad(result.data, callback);
  193. // for test calc
  194. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  195. } else {
  196. alert('error: ' + result.message);
  197. callback(result.error);
  198. }
  199. },
  200. error: function(jqXHR, textStatus, errorThrown){
  201. alert('error ' + textStatus + " " + errorThrown);
  202. }
  203. });
  204. };
  205. project.prototype.beginUpdate = function(operation){
  206. if (tools.updateLock === 0){
  207. tools.operation = operation
  208. }
  209. tools.updateLock += 1;
  210. };
  211. project.prototype.endUpdate = function(){
  212. if (tools.updateLock === 0){
  213. throw "project can not endUpdate before beginUpdate";
  214. }
  215. tools.updateLock -= 1;
  216. if (tools.updateLock === 0) {
  217. $.ajax({
  218. type: "POST",
  219. url: '/project/save',
  220. data: {'data': JSON.stringify({
  221. "project_id": tools._ID,
  222. "user_id": tools._userID,
  223. "date": new Date,
  224. "operation": tools.operation,
  225. "update_data": tools.updateData
  226. })},
  227. dataType: 'json',
  228. cache: false,
  229. timeout: 50000,
  230. success: function (result) {
  231. if (!result.error) {
  232. tools.doAfterUpdate(result.data);
  233. } else {
  234. alert('error: ' + result.message);
  235. }
  236. },
  237. error: function(jqXHR, textStatus, errorThrown){
  238. alert('error ' + textStatus + " " + errorThrown);
  239. }
  240. });
  241. tools.updateData = [];
  242. tools.operation = "";
  243. }
  244. };
  245. project.prototype.push = function(moduleName, data){
  246. if (tools.updateLock === 0){
  247. throw "project can not push data before beginUpdate";
  248. }
  249. var moduleData = {
  250. moduleName: moduleName,
  251. data: data
  252. };
  253. tools.updateData.push(moduleData);
  254. };
  255. project.prototype.pushNow = function (operation, moduleName, data) {
  256. var that = this;
  257. this.beginUpdate(operation);
  258. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  259. moduleName.forEach(function (name, index) {
  260. that.push(moduleName[index], data[index]);
  261. });
  262. } else {
  263. this.push(moduleName, data);
  264. }
  265. this.endUpdate();
  266. };
  267. project.prototype.registerModule = function(moduleName, obj){
  268. if (!tools.modules.hasOwnProperty(moduleName)){
  269. tools.modules[moduleName] = obj;
  270. }
  271. };
  272. project.prototype.setCalcFlag = function (calcFlag) {
  273. this.calcFlag = calcFlag;
  274. if (this.calcFields) {
  275. for (let field of this.calcFields) {
  276. // unitFeeCalcFlag
  277. if (this.calcFlag === rationContent) {
  278. field.unitFeeFlag = rationContentUnitFeeFlag;
  279. } else if ( this.calcFlag === billsPrice) {
  280. field.unitFeeFlag = billsPriceUnitFeeFlag;
  281. } else {
  282. field.unitFeeFlag = averageQtyUnitFeeFlag;
  283. }
  284. // totalFeeCalcFlag
  285. if (field.type === 'common') {
  286. if (this.calcFlag === rationPriceConverse) {
  287. field.totalFeeFlag = sumTotalFeeFlag;
  288. } else {
  289. field.totalFeeFlag = totalFeeFlag;
  290. }
  291. } else {
  292. field.totalFeeFlag = sumTotalFeeFlag;
  293. }
  294. }
  295. }
  296. }
  297. return new project();
  298. }
  299. };