composition.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * 组成物相关数据
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/10/27
  6. * @version
  7. */
  8. function Composition() {
  9. this.datas = null;
  10. this.isLoading = false;
  11. }
  12. /**
  13. * 加载数据
  14. *
  15. * @param {function} callback
  16. * @return {boolean}
  17. */
  18. Composition.prototype.loadData = function (callback = null) {
  19. let self = this;
  20. if (self.isLoading) {
  21. return false;
  22. }
  23. /* // 加载组成物数据
  24. $.ajax({
  25. url: '/glj/get-composition',
  26. type: 'post',
  27. dataType: 'json',
  28. data: {project_id: scUrlUtil.GetQueryString('project')},
  29. error: function() {
  30. // alert('数据传输错误');
  31. },
  32. beforeSend: function() {
  33. self.isLoading = true;
  34. },
  35. success: function(response) {
  36. self.isLoading = false;
  37. if (response.err === 1) {
  38. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '读取组成物数据失败!';
  39. alert(msg);
  40. return false;
  41. }
  42. self.datas = response.data;
  43. // 回调函数
  44. if (callback !== null) {
  45. callback(response.data);
  46. }
  47. // 存入缓存
  48. projectObj.project.composition = self;
  49. }
  50. });*/
  51. };
  52. /**
  53. * 获取对应code的组成物数据
  54. *
  55. * @param {String} code
  56. * @param {Number} gljType
  57. * @return {Array}
  58. */
  59. Composition.prototype.getCompositionByCode = function(code, gljType = 0) {
  60. let result = [];
  61. if (code === '') {
  62. return result;
  63. }
  64. if (gljType === 0) {
  65. return this.datas[code] !== undefined ? this.datas[code] : result;
  66. }
  67. for(let composition of this.datas[code]) {
  68. if (composition.glj_type === gljType) {
  69. result.push(composition);
  70. }
  71. }
  72. return result;
  73. };
  74. Composition.prototype.getCompositionByGLJ = function (glj) {
  75. let mixRatioMap = projectObj.project.projectGLJ.datas.mixRatioMap;
  76. let projectGljs = projectObj.project.projectGLJ.datas.gljList;
  77. let connect_index = gljOprObj.getIndex(glj,gljKeyArray);
  78. let gljList=[];
  79. if(mixRatioMap[connect_index]){ //说明是有组成物的类型
  80. gljList = gljOprObj.getMixRationShowDatas(mixRatioMap[connect_index], projectGljs);
  81. }
  82. return gljList;
  83. };
  84. Composition.prototype.deleteComposition = function (updateData,recode,pid,callback) {
  85. let me = this;
  86. $.bootstrapLoading.start();
  87. CommonAjax.specialPost( '/glj/delete-ratio',{id: updateData.id},function (result){
  88. me.updateConsumptionInCache(pid,recode,updateData,"delete");
  89. if(callback){
  90. callback();
  91. }
  92. $.bootstrapLoading.end();
  93. })
  94. };
  95. Composition.prototype.updateConsumptionInCache = function (pid,recode,updateData,operation='modify') {
  96. let decimal = getDecimal("glj.quantity");
  97. let parentGlj = projectObj.project.projectGLJ.getByID(pid);//得到父工料机的数据
  98. let ratioData = recode;
  99. let con_key = gljOprObj.getIndex(ratioData,gljKeyArray);
  100. let mglj = projectObj.project.projectGLJ.getByConKey(con_key);//取组成物对应的工料机;
  101. if(mglj && parentGlj){
  102. let newValue = scMathUtil.roundForObj(updateData.value,decimal);
  103. let oldValue = scMathUtil.roundForObj(ratioData.consumption,decimal);
  104. let changValue = scMathUtil.roundForObj(newValue-oldValue,decimal);
  105. //乘以父工料机的消耗量得到该组成物总消耗量的改变量
  106. changValue = operationWithRound(parentGlj.quantity,changValue,"glj.quantity","*");
  107. let oldQuantity = scMathUtil.roundForObj(mglj.quantity,decimal);
  108. mglj.quantity = scMathUtil.roundForObj(oldQuantity+changValue,decimal);
  109. recode.consumption = newValue;
  110. projectObj.project.projectGLJ.quantityChangeMap[mglj.id] = mglj.quantity;
  111. let p_key = gljOprObj.getIndex(parentGlj,gljKeyArray);
  112. let m_list = projectObj.project.projectGLJ.datas.mixRatioMap[p_key];
  113. if(operation == 'delete'){//如果是删除
  114. _.remove(parentGlj.ratio_data,{"id":updateData.id});
  115. _.remove(m_list,{"id":updateData.id});
  116. }else {
  117. let subData = _.find(parentGlj.ratio_data,{"id":updateData.id});
  118. if(subData){
  119. subData.consumption = updateData.value;
  120. }
  121. //更新组成物map里的数据
  122. let m_ratioData = _.find(m_list,{"id":updateData.id});
  123. if(m_ratioData){
  124. m_ratioData.consumption = updateData.value;
  125. }
  126. }
  127. // 设置父级3个价格
  128. parentGlj.unit_price.market_price = updateData.market_price;
  129. parentGlj.unit_price.base_price = updateData.base_price;
  130. return mglj.id;
  131. }
  132. }
  133. Composition.prototype.updateConsumption = function (updateData,recode,pid,callback){
  134. let me = this;
  135. $.bootstrapLoading.start();
  136. CommonAjax.specialPost( '/glj/update',updateData,function (result) {
  137. //更新缓存
  138. let sid = me.updateConsumptionInCache(pid,recode,updateData);
  139. if(callback){
  140. callback(sid);
  141. }
  142. $.bootstrapLoading.end();
  143. })
  144. };