project.js 13 KB

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