project.js 13 KB

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