project.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.masterField = {ration: 'billsItemID', volumePrice: 'billsItemID'};
  86. this.masterField = {ration: 'billsItemID'};
  87. };
  88. // prototype用于定义public方法
  89. project.prototype.modify = function (modifyDatas, callback) {
  90. // To Do
  91. };
  92. // prototype用于定义public方法
  93. project.prototype.ID = function () {
  94. return tools._ID;
  95. };
  96. project.prototype.projCounter = function () {
  97. return tools.projCounter;
  98. };
  99. project.prototype.getDecimal = function (fullName) {
  100. let names = fullName.split('.'), decimal = this.Decimal;
  101. for (let name of names) {
  102. if (decimal[name]) {
  103. decimal = decimal[name];
  104. } else {
  105. return null;
  106. }
  107. }
  108. if (Object.prototype.toString.apply(decimal) === '[object Number]') {
  109. return decimal;
  110. } else {
  111. return null;
  112. }
  113. };
  114. project.prototype.loadMainTree = function () {
  115. var that = this;
  116. var loadRationNode = function (rations, cacheNode) {
  117. var newNode, br = that.Ration.getBillsSortRation(cacheNode.source.getID()), i;
  118. for (i = 0; i < br.length; i++) {
  119. newNode = that.mainTree.addNode(cacheNode);
  120. newNode.source = br[i];
  121. newNode.sourceType = that.Ration.getSourceType();
  122. newNode.data = br[i];
  123. }
  124. };
  125. /* let loadVolumePriceNode = function (cacheNode) {
  126. let newNode = null, bv = that.VolumePrice.getBillsSortVolumePrice(cacheNode.source.getID());
  127. for (let v of bv) {
  128. newNode = that.mainTree.addNode(cacheNode);
  129. newNode.source = v;
  130. newNode.sourceType = that.VolumePrice.getSourceType();
  131. newNode.data = v;
  132. }
  133. };*/
  134. var loadIdTreeNode = function (nodes, parent) {
  135. var newNode, i;
  136. for (i = 0; i < nodes.length; i++) {
  137. newNode = that.mainTree.addNode(parent);
  138. newNode.source = nodes[i];
  139. newNode.sourceType = that.Bills.getSourceType();
  140. newNode.data = nodes[i].data;
  141. that.FeeRate.loadFeeRateToBill(newNode);
  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. this.mainTree.selected = this.mainTree.firstNode();
  153. };
  154. project.prototype.getParentTarget = function (node, targetField, targetValue) {
  155. var parent = node;
  156. while (parent && parent[targetField] !== targetValue) {
  157. parent = parent.parent;
  158. }
  159. return parent;
  160. };
  161. // 提供给各模块调用的统一从后台获取数据的方法
  162. /*project.prototype.pullData = function (url, data, successCallback, errorCallback) {
  163. $.ajax({
  164. type:"POST",
  165. url: url,
  166. data: {'data': JSON.stringify(data)},
  167. dataType: 'json',
  168. cache: false,
  169. timeout: 50000,
  170. success: function(result){
  171. successCallback(result);
  172. },
  173. error: function(jqXHR, textStatus, errorThrown){
  174. alert('error ' + textStatus + " " + errorThrown);
  175. errorCallback();
  176. }
  177. });
  178. };*/
  179. // 所有模块在此从后台获取数据
  180. project.prototype.loadDatas = function (callback){
  181. $.ajax({
  182. type: "POST",
  183. url: '/project/getData',
  184. data: {'data': JSON.stringify({
  185. "project_id": tools._ID,
  186. "user_id": tools._userID
  187. })},
  188. dataType: 'json',
  189. cache: false,
  190. timeout: 50000,
  191. success: function (result) {
  192. if (!result.error) {
  193. tools.doAfterLoad(result.data, callback);
  194. // for test calc
  195. //tools.doAfterLoad([{moduleName: 'bills', data: BillsData}, {'moduleName': 'ration', data: DrawingData}], callback);
  196. } else {
  197. alert('error: ' + result.message);
  198. callback(result.error);
  199. }
  200. },
  201. error: function(jqXHR, textStatus, errorThrown){
  202. alert('error ' + textStatus + " " + errorThrown);
  203. }
  204. });
  205. };
  206. project.prototype.beginUpdate = function(operation){
  207. if (tools.updateLock === 0){
  208. tools.operation = operation
  209. }
  210. tools.updateLock += 1;
  211. };
  212. project.prototype.endUpdate = function(){
  213. if (tools.updateLock === 0){
  214. throw "project can not endUpdate before beginUpdate";
  215. }
  216. tools.updateLock -= 1;
  217. if (tools.updateLock === 0) {
  218. $.ajax({
  219. type: "POST",
  220. url: '/project/save',
  221. data: {'data': JSON.stringify({
  222. "project_id": tools._ID,
  223. "user_id": tools._userID,
  224. "date": new Date,
  225. "operation": tools.operation,
  226. "update_data": tools.updateData
  227. })},
  228. dataType: 'json',
  229. cache: false,
  230. timeout: 50000,
  231. success: function (result) {
  232. if (!result.error) {
  233. tools.doAfterUpdate(result.data);
  234. } else {
  235. alert('error: ' + result.message);
  236. }
  237. },
  238. error: function(jqXHR, textStatus, errorThrown){
  239. alert('error ' + textStatus + " " + errorThrown);
  240. }
  241. });
  242. tools.updateData = [];
  243. tools.operation = "";
  244. }
  245. };
  246. project.prototype.push = function(moduleName, data){
  247. if (tools.updateLock === 0){
  248. throw "project can not push data before beginUpdate";
  249. }
  250. var moduleData = {
  251. moduleName: moduleName,
  252. data: data
  253. };
  254. tools.updateData.push(moduleData);
  255. };
  256. project.prototype.pushNow = function (operation, moduleName, data) {
  257. var that = this;
  258. this.beginUpdate(operation);
  259. if (Object.prototype.toString.apply(moduleName) === "[object Array]" && Object.prototype.toString.apply(data) === "[object Array]") {
  260. moduleName.forEach(function (name, index) {
  261. that.push(moduleName[index], data[index]);
  262. });
  263. } else {
  264. this.push(moduleName, data);
  265. }
  266. this.endUpdate();
  267. };
  268. project.prototype.registerModule = function(moduleName, obj){
  269. if (!tools.modules.hasOwnProperty(moduleName)){
  270. tools.modules[moduleName] = obj;
  271. }
  272. };
  273. project.prototype.setBillsCalcMode = function (calcMode) {
  274. this.projSetting.billsCalcMode = calcMode;
  275. this.initCalcFields();
  276. };
  277. project.prototype.initCalcFields = function () {
  278. let settingConst = this.projSetting.settingConst;
  279. if (this.calcFields) {
  280. for (let field of this.calcFields) {
  281. // unitFeeCalcFlag
  282. if (field.type === 'zangu') {
  283. field.unitFeeFlag = converseUnitFeeFlag;
  284. } else {
  285. if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationContent) {
  286. field.unitFeeFlag = rationContentUnitFeeFlag;
  287. } else if ( this.projSetting.billsCalcMode === settingConst.billsCalcMode.billsPrice) {
  288. field.unitFeeFlag = billsPriceUnitFeeFlag;
  289. } else {
  290. field.unitFeeFlag = averageQtyUnitFeeFlag;
  291. }
  292. }
  293. // totalFeeCalcFlag
  294. if (field.type === 'common') {
  295. if (this.projSetting.billsCalcMode === settingConst.billsCalcMode.rationPriceConverse) {
  296. field.totalFeeFlag = sumTotalFeeFlag;
  297. } else {
  298. field.totalFeeFlag = totalFeeFlag;
  299. }
  300. } else {
  301. field.totalFeeFlag = sumTotalFeeFlag;
  302. }
  303. }
  304. }
  305. }
  306. return new project();
  307. }
  308. };