project.js 13 KB

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