material_controller.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Created by zhang on 2018/9/12.
  3. */
  4. let MaterialController = {
  5. //规则相关映射
  6. rule1: function (node) {
  7. let itemText = node.data.itemCharacterText;
  8. let name = this.getKeyString(itemText,"材质及规格:");
  9. if(name){
  10. return{name:name,specs:""}//规格型号变成空的
  11. }
  12. return null;
  13. },
  14. rule2:function (node) {
  15. let itemText = node.data.itemCharacterText;
  16. let name = this.getKeyString(itemText,"混凝土种类:");
  17. let specs = this.getKeyString(itemText,"混凝土强度等级:");
  18. if(name||specs){
  19. let doc ={};
  20. if(name) doc["name"] = name;
  21. if(specs) doc["specs"] = specs;
  22. return doc;
  23. }
  24. return null
  25. },
  26. replaceMaterial:function(nodes){
  27. let me = this;
  28. this.getMaterial(nodes,function (result) {
  29. if(!_.isEmpty(result)){
  30. me.startReplace(nodes,result);
  31. }
  32. });
  33. },
  34. startReplace:function (nodes,result) {//其实应该是批量修改工料机属性,与替换工料机不同
  35. let me = this,updateData = [];
  36. for(let n of nodes){
  37. let code = n.data.code;
  38. if(code.length >= 9){
  39. let r_list = this.eachNode(n,result[code.substr(0,9)]);
  40. if(r_list.length > 0) updateData.push(...r_list);
  41. }
  42. }
  43. me.postReplace(updateData);
  44. },
  45. postReplace:function (updateData) {
  46. let me = this;
  47. if(updateData.length == 0) return;
  48. $.bootstrapLoading.start();
  49. CommonAjax.post("/material/replace",updateData,function(result){
  50. $.bootstrapLoading.end();
  51. me.updateCacheAfterReplace(result)
  52. })
  53. },
  54. updateCacheAfterReplace:function (result){
  55. let nodes = [];
  56. for(let data of result){
  57. let node = projectObj.project.ration_glj.refreshRationNode(data.name,data.adjustState,data.rationID);//刷新定额名称和子目调整状态
  58. if(node) nodes.push(node);
  59. if(data.ration_gljs.length > 0) this.refreshRationGLJ(data.ration_gljs);
  60. }
  61. if(nodes.length >0){
  62. projectObj.project.projectGLJ.loadData(function () {
  63. projectObj.project.calcProgram.calcNodesAndSave(nodes,function(){
  64. installationFeeObj.calcInstallationFee();
  65. });
  66. gljOprObj.refreshView();
  67. })
  68. }
  69. },
  70. refreshRationGLJ:function(ration_gljs){
  71. let ration_glj_model = projectObj.project.ration_glj;
  72. for(let rg of ration_gljs){
  73. let glj = ration_glj_model.refreshByID(rg.ID,rg.doc);
  74. ration_glj_model.refreshTreeNodeIfNeeded(glj);//刷新造价书上的树节点(如果需要)
  75. }
  76. },
  77. eachNode:function (node,item) {
  78. let replaceDatas =[];
  79. if(item && item.bills.rule){
  80. let replace_property = this["rule"+item.bills.rule](node);//按清单设置的规则获取要修改的属性
  81. if(replace_property){
  82. replaceDatas = this.getReplaceData(node.data.ID,item.materialMap,replace_property);
  83. }
  84. }
  85. return replaceDatas;
  86. },
  87. getReplaceData:function (billsItemID,materialMap,replace_property) {
  88. let list = [];
  89. let replace_glj_list = this.findMatchRationGLJ(billsItemID,materialMap);//取出需要替换的工料机和对替换的材料内容
  90. for(let r of replace_glj_list){
  91. let doc = this.getDoc(r.glj,r.material,replace_property);//获取要修改的字段
  92. list.push({glj:gljOprObj.setGLJPrice(r.glj),doc:doc});
  93. }
  94. return list;
  95. },
  96. getDoc:function (glj,material,replace_property) {//取定额工料机中需要修改的内容
  97. let doc = {},keyList = ['name','specs','type','unit'];
  98. for(let key of keyList){
  99. if(glj[key] != material[key]) doc[key] = material[key];
  100. }
  101. for(let rkey in replace_property){
  102. doc[rkey] = replace_property[rkey]
  103. }
  104. return doc;
  105. },
  106. findMatchRationGLJ:function (billsItemID,materialMap) {//查找清单下匹配的需要替换的定额工料机
  107. let replaceList=[];
  108. for(let g of projectObj.project.ration_glj.datas){
  109. if(g.billsItemID == billsItemID && materialMap[g.original_code]) {
  110. replaceList.push({glj:g,material:materialMap[g.original_code]});
  111. }
  112. }
  113. return replaceList;
  114. },
  115. getMaterial:function (nodes,callback) {
  116. let data = [],materialMap= null;
  117. for(let n of nodes){
  118. let code = n.data.code;
  119. if(code.length >= 9 && n.data.billsLibId){
  120. let billCode = code.substr(0,9);
  121. data.push({code:billCode,billsLibId:n.data.billsLibId});
  122. }
  123. }
  124. if(data.length>0){
  125. //data 按编码去重
  126. let dataMap = _.indexBy(data,'code');
  127. CommonAjax.post("/material/getMaterial",dataMap,function (result) {
  128. callback(result);
  129. })
  130. }else {
  131. callback(null);
  132. }
  133. },
  134. getKeyString:function (itemText,matchStr) {//截取关键数据
  135. itemText = itemText.replace(/:/g, ":");//中文字符转换为英文字符
  136. itemText = itemText.replace(matchStr,"@$@");//用特殊符号取代关键字,方便截取
  137. let index = itemText.indexOf("@$@");
  138. if(index == -1) return null;
  139. let temString = itemText.substr(index+3);
  140. let strArray = temString.split(/\n/);
  141. let keyString = this.trim(strArray[0]);
  142. return keyString===""?null:keyString;
  143. },
  144. trim:function (str) {
  145. return str.replace(/(^\s*)|(\s*$)/g, "");
  146. },
  147. //--------------2018-10-08新加替换表格-----------------
  148. setting:{
  149. header:[
  150. {headerName: "项目特征", headerWidth: 120, dataCode: "character", dataType: "String"},
  151. {headerName: "内容", headerWidth: 240, dataCode: "context", dataType: "String"},
  152. {headerName: "名称", headerWidth: 50, dataCode: "name", dataType: "String",hAlign: "center",cellType:"replaceButton"},
  153. {headerName: "规格", headerWidth: 50, dataCode: "specs", dataType: "String",hAlign: "center",cellType:"replaceButton"},
  154. ],
  155. view: {
  156. lockColumns: [0,1,2,3],
  157. rowHeaderWidth:25
  158. }
  159. },
  160. spread:null,
  161. sheet:null,
  162. datas:[],
  163. showReplaceDiv:function (node) {
  164. $("#subSpread").addClass("ration_glj_spread");
  165. $("#replaceM").show();
  166. if(node) this.showReplaceSpread(node);//如果不是只刷新页面
  167. refreshSubSpread();
  168. },
  169. hideReplaceDiv:function(){
  170. $("#subSpread").removeClass("ration_glj_spread");
  171. $("#replaceM").hide();
  172. refreshSubSpread();
  173. },
  174. showReplaceSpread:function(node){
  175. this.initSpread();
  176. this.showData(node);
  177. },
  178. initSpread:function () {
  179. if(!this.spread){
  180. this.spread = SheetDataHelper.createNewSpread($("#replaceM")[0]);
  181. sheetCommonObj.spreadDefaultStyle(this.spread);
  182. this.initSheet();
  183. }else {
  184. this.spread.refresh();
  185. }
  186. },
  187. initSheet:function(){
  188. this.sheet = this.spread .getSheet(0);
  189. sheetCommonObj.initSheet( this.sheet, this.setting);
  190. this.spread.bind(GC.Spread.Sheets.Events.ButtonClicked, this.onReplaceButtonClick);
  191. this.sheet.name('materialReplace');
  192. },
  193. showData:function(node){
  194. this.datas = [];
  195. let selected = node?node:projectObj.project.mainTree.selected;
  196. let parent = selected.parent;
  197. if(selected&&parent){
  198. this.getItems(parent.data.itemCharacterText);
  199. }
  200. sheetCommonObj.showData(this.sheet, this.setting,this.datas);
  201. this.sheet.setRowCount(this.datas.length);
  202. },
  203. getItems:function(characterText){
  204. if(!characterText) return;
  205. let strArray = characterText.split(/\n/);
  206. for(let tem of strArray){
  207. tem = tem.replace(/:/g, ":");//中文字符转换为英文字符
  208. if(tem.indexOf(":") != -1){//有:号才提取
  209. let proArr = tem.split(":");
  210. //如果内容为空的话也不提取
  211. let context = this.trim(proArr[1]);
  212. if(context!="") this.datas.push({character:proArr[0],context:context,name:"换",specs:"换"})
  213. }
  214. }
  215. },
  216. onReplaceButtonClick:function(e, args){
  217. //项目只读
  218. if(projectReadOnly) {
  219. return;
  220. }
  221. let me = MaterialController;
  222. let sheet = args.sheet, row = args.row, col = args.col;
  223. let dataCode = me.setting.header[col].dataCode, item = me.datas[row], doc = {};
  224. if(dataCode && item){
  225. doc[dataCode] = item.context;
  226. me.replaceByClick(doc);
  227. }
  228. },
  229. replaceByClick:function (tem) {
  230. let ration_glj = gljOprObj.getSelectedRationGlj();
  231. let doc = {};
  232. if(ration_glj && !ration_glj.isMixRatio){//有选中并且不是组成物
  233. if(gljOprObj.isExtraType(ration_glj.type)) return; //如果是“企业管理费”“利润”“一般风险费”类型,不能替换
  234. for(let key in tem){//检查属性是否做了改变,如果没有改变不用提交替换
  235. if(ration_glj[key] != tem[key]) doc[key] = tem[key]
  236. }
  237. if(!_.isEmpty(doc)) this.postReplace([{glj:ration_glj,doc:doc}]);
  238. }
  239. }
  240. };