components.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * Created by Zhong on 2017/10/16.
  3. */
  4. /*
  5. 弹出组成物窗口 组成物表
  6. * */
  7. let componentOprObj = {
  8. treeObj:null,
  9. rootNode: null,//分类树根节点
  10. workBook: null,
  11. selectedList: [],//选中的组成物
  12. setting: {
  13. owner: "components",
  14. header: [
  15. {headerName:"选择", headerWidth: 40, dataCode: "select", hAlign: "center", vAlign: "center"},
  16. {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  17. {headerName:"名称",headerWidth:120,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
  18. {headerName:"规格型号",headerWidth:80,dataCode:"specs", dataType: "String", hAlign: "center", vAlign: "center"},
  19. {headerName:"单位",headerWidth:80,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
  20. {headerName:"单价",headerWidth:80,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
  21. {headerName:"类型",headerWidth:80,dataCode:"gljType", dataType: "String", hAlign: "center", vAlign: "center"}
  22. ]
  23. },
  24. //生成列头(多单价)
  25. initHeaders: function (priceProperties) {
  26. let headers = [
  27. {headerName:"选择", headerWidth: 40, dataCode: "select", hAlign: "center", vAlign: "center"},
  28. {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  29. {headerName:"名称",headerWidth:120,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
  30. {headerName:"规格型号",headerWidth:80,dataCode:"specs", dataType: "String", hAlign: "center", vAlign: "center"},
  31. {headerName:"单位",headerWidth:80,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
  32. {headerName:"类型",headerWidth:80,dataCode:"gljType", dataType: "String", hAlign: "center", vAlign: "center"}
  33. ];
  34. //生成单价列
  35. if(!priceProperties || priceProperties.length === 0){
  36. headers.push({headerName:"定额价",headerWidth:80,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"});
  37. }
  38. else {
  39. for(let priceProp of priceProperties){
  40. let colData = {
  41. headerName: priceProp.price.dataName,
  42. headerWidth: 100,
  43. dataCode: priceProp.price.dataCode,
  44. dataType: 'Number',
  45. formatter: '0.00',
  46. hAlign: 'right',
  47. vAlign: 'center'
  48. };
  49. headers.push(colData);
  50. }
  51. }
  52. let tailHeaders = [
  53. ];
  54. headers = headers.concat(tailHeaders);
  55. return headers;
  56. },
  57. buildSheet: function (container) {
  58. let me = componentOprObj;
  59. //生成人材机组成物表格列头
  60. me.setting.header = me.initHeaders(priceProperties);
  61. //生成人材机组成物列映射
  62. sheetCommonObj.initColMapping(me, me.setting.header);
  63. repositoryGljObj.initPriceCols.call(me, priceProperties, me.colMapping);
  64. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  65. if(priceProperties && priceProperties.length > 0){
  66. me.workBook.getSheet(0).frozenColumnCount(6);
  67. }
  68. me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
  69. me.workBook.getSheet(0).setFormatter(-1, 1, "@", GC.Spread.Sheets.SheetArea.viewport);
  70. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  71. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  72. me.workBook.bind(GC.Spread.Sheets.Events.ButtonClicked, me.onButtonClicked);//复选框点击事件
  73. me.workBook.getSheet(0).getRange(-1, 0, -1, 1).cellType(new GC.Spread.Sheets.CellTypes.CheckBox());
  74. me.componentsBtnOpr($('#componentsConf'));
  75. },
  76. onClipboardPasting: function (sender, args) {
  77. args.cancel = true;
  78. },
  79. onCellEditStart: function (sender, args) {
  80. args.cancel = true;
  81. },
  82. onButtonClicked: function (sender, args) {
  83. let me = componentOprObj, re = repositoryGljObj;
  84. let val = args.sheet.getValue(args.row, args.col);
  85. let thisComponent = me.currentCache[args.row];
  86. thisComponent.isChecked = val;
  87. if(args.sheet.isEditing()){
  88. args.sheet.endEdit(true);
  89. }
  90. else{
  91. //维护选中组成物列表
  92. if(val === true){
  93. let isExist = false;
  94. for(let i = 0, len = me.selectedList.length; i < len; i++){
  95. if(me.selectedList[i].ID === thisComponent.ID){
  96. isExist = true;
  97. break;
  98. }
  99. }
  100. if(!isExist){
  101. me.selectedList.push(thisComponent);
  102. }
  103. }
  104. else if(val === false){
  105. for(let i = 0, len = me.selectedList.length; i < len; i++){
  106. if(me.selectedList[i].ID === thisComponent.ID){
  107. me.selectedList.splice(i, 1);
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. },
  114. setShowGljList: function (gljList, clearChecked) {
  115. let that = repositoryGljObj, me = componentOprObj;
  116. for(let i = 0; i < gljList.length; i++){
  117. if(machineAllowComponent.includes(that.currentGlj.gljType) && machineComponent.includes(gljList[i].gljType) ||
  118. materialAllowComponent.includes(that.currentGlj.gljType) && gljList[i].gljType === 201){
  119. let isExist = false;
  120. for(let j = 0; j < that.currentComponent.length; j++){
  121. if(that.currentComponent[j].ID === gljList[i].ID){
  122. isExist = true;
  123. break;
  124. }
  125. }
  126. if(!isExist){
  127. if(clearChecked){
  128. gljList[i].isChecked = false;
  129. }
  130. }
  131. else {
  132. gljList[i].isChecked = true;
  133. }
  134. me.showGljList.push(gljList[i]);
  135. }
  136. }
  137. },
  138. //初始默认radio
  139. initRadio: function () {
  140. let that = repositoryGljObj, me = componentOprObj;
  141. //初始化组成物列表
  142. me.selectedList = [].concat(that.currentComponent);
  143. //初始为所有工料机,机械类型可添加机械组成物,混凝土,砂浆、配合比可添加普通材料
  144. me.showGljList = [];
  145. me.setShowGljList(that.gljList, true);
  146. that.sortGlj(me.showGljList);
  147. },
  148. getParentCache: function (nodes) {
  149. let me = componentOprObj, rst = [];
  150. for(let i = 0; i < me.showGljList.length; i++){
  151. if(nodes.indexOf(me.showGljList[i].gljClass) !== -1){
  152. rst.push(me.showGljList[i]);
  153. }
  154. }
  155. rst.sort(function (a, b) {
  156. let rst = 0;
  157. if(a.code > b.code) rst = 1;
  158. else if(a.code < b.code)rst = -1;
  159. return rst;
  160. });
  161. return rst;
  162. },
  163. getCache: function() {
  164. let me = componentOprObj, rst = [];
  165. for (let i = 0; i < me.showGljList.length; i++) {
  166. if (me.showGljList[i].gljClass == me.gljCurTypeId) {
  167. rst.push(me.showGljList[i]);
  168. }
  169. }
  170. return rst;
  171. },
  172. showGljItems: function(data, type) {
  173. let me = componentOprObj, re = repositoryGljObj;
  174. if (me.workBook) {
  175. let cacheSection = data;
  176. re.sortGljDeep.call(me, data);
  177. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  178. sheetsOprObj.showData(me, me.workBook.getSheet(0), me.setting, cacheSection, re.distTypeTree);
  179. me.workBook.getSheet(0).setRowCount(cacheSection.length);
  180. cacheSection = null;
  181. }
  182. },
  183. //批量插入组成物
  184. batchUpdateComponent: function () {
  185. $.bootstrapLoading.start();
  186. let me = componentOprObj, that = gljComponentOprObj, re = repositoryGljObj,
  187. updateArr = [],
  188. IDMapping = {};
  189. let formData = new FormData();
  190. //当前工料机的工料机类型
  191. let currentGljType = re.currentGlj.gljType;
  192. let thisTypeGljList = _.filter(re.gljList, function (data) {
  193. return data.gljType == currentGljType;
  194. });
  195. for(let glj of thisTypeGljList){
  196. let gljComponent = glj.component ? glj.component : [];
  197. let newComponent = [];
  198. if(me.insertType === 'batch'){
  199. for(let selectedComponent of me.selectedList){
  200. let isExist = false;
  201. for(let gljPerComponent of gljComponent){
  202. if(selectedComponent.ID === gljPerComponent.ID){
  203. //newComponent.push({ID: selectedComponent.ID, consumeAmt: gljPerComponent.consumeAmt});
  204. isExist = true;
  205. break;
  206. }
  207. }
  208. if(!isExist){
  209. let newComponentObj = {ID: selectedComponent.ID};
  210. that.initConsumeAmt(newComponentObj);
  211. newComponent.push(newComponentObj);
  212. }
  213. }
  214. newComponent = gljComponent.concat(newComponent);
  215. }
  216. else if(me.insertType === 'batchClear'){//去除消耗量为0的组成物
  217. for(let gljPerComponent of gljComponent){
  218. if(hasConsumeAmt(gljPerComponent)){
  219. if(!consumeAmtProperties || consumeAmtProperties.length === 0){
  220. newComponent.push({ID: gljPerComponent.ID, consumeAmt: gljPerComponent.consumeAmt});
  221. }
  222. else {
  223. newComponent.push({ID: gljPerComponent.ID, consumeAmtProperty: gljPerComponent.consumeAmtProperty});
  224. }
  225. }
  226. }
  227. }
  228. function hasConsumeAmt(component){
  229. if(!consumeAmtProperties || consumeAmtProperties.length === 0){
  230. if(component.consumeAmt && component.consumeAmt != 0){
  231. return true;
  232. }
  233. }
  234. else {
  235. if(component.consumeAmtProperty){
  236. for(let field in component.consumeAmtProperty){
  237. if(component.consumeAmtProperty[field] != 0){
  238. return true;
  239. }
  240. }
  241. }
  242. }
  243. return false;
  244. }
  245. IDMapping[glj.ID] = {component : newComponent};
  246. updateArr.push({ID: glj.ID, component: newComponent});
  247. }
  248. formData.append('compressData', LZString.compressToUTF16(JSON.stringify(updateArr)));
  249. $.ajax({
  250. url: 'api/batchUpdateComponent',
  251. type: 'POST',
  252. data: formData,
  253. cache: false,
  254. contentType: false,
  255. processData: false,
  256. success: function(response){
  257. if (response.err === 0) {
  258. //更新缓存数据
  259. for(let glj of thisTypeGljList){
  260. if(glj.ID === re.currentGlj.ID){
  261. re.currentComponent = re.getCurrentComponent(IDMapping[glj.ID]['component']);
  262. sheetCommonObj.cleanData(that.workBook.getSheet(0), that.setting, -1);
  263. sheetsOprObj.showData(that, that.workBook.getSheet(0), that.setting, re.currentComponent);
  264. }
  265. glj.component = IDMapping[glj.ID]['component'];
  266. }
  267. if($('#component').is(':visible')){
  268. $('#component').modal('hide');
  269. }
  270. $.bootstrapLoading.end();
  271. } else {
  272. if($('#component').is(':visible')){
  273. $('#component').modal('hide');
  274. }
  275. $.bootstrapLoading.end();
  276. }
  277. },
  278. error: function(jqXHR){
  279. alert(`与服务器通信发生错误${jqXHR.status} ${jqXHR.statusText}`);
  280. $('#component').modal('hide');
  281. $.bootstrapLoading.end();
  282. }
  283. });
  284. },
  285. //组成物窗口按钮操作
  286. componentsBtnOpr: function (conf) {//确定、取消、关闭按钮
  287. let me = componentOprObj, that = gljComponentOprObj, re = repositoryGljObj;
  288. conf.click(function () {
  289. if(me.insertType === 'single'){
  290. let updateBasePrc = [];
  291. //添加选择添加的组成物
  292. let updateArr = [];
  293. let newComponent = [];
  294. for(let i = 0, len = me.selectedList.length; i < len; i++){
  295. let isExist = false;
  296. for(let j = 0, jLen = re.currentGlj.component.length; j < jLen; j++){
  297. if(me.selectedList[i].ID === re.currentGlj.component[j].ID){
  298. if(!consumeAmtProperties || consumeAmtProperties.length === 0){
  299. let consumeAmt = typeof re.currentGlj.component[j].consumeAmt !== 'undefined' ? re.currentGlj.component[j].consumeAmt : 0;
  300. newComponent.push({ID: me.selectedList[i].ID, consumeAmt: consumeAmt});
  301. }
  302. else{
  303. let consumeAmtProperty = typeof re.currentGlj.component[j].consumeAmtProperty !== 'undefined' ? re.currentGlj.component[j].consumeAmtProperty : {};
  304. newComponent.push({ID: me.selectedList[i].ID, consumeAmt: 0, consumeAmtProperty: consumeAmtProperty});
  305. }
  306. isExist = true;
  307. break;
  308. }
  309. }
  310. if(!isExist){
  311. let obj = {ID: me.selectedList[i].ID};
  312. that.initConsumeAmt(obj);
  313. newComponent.push(obj);
  314. }
  315. }
  316. re.currentGlj.component = newComponent;
  317. updateArr.push(re.currentGlj);
  318. that.updateComponent(updateArr);
  319. $('#component').modal('hide');
  320. }
  321. else {
  322. me.batchUpdateComponent();
  323. }
  324. });
  325. }
  326. };
  327. let componentTypeTreeOprObj = {
  328. onClick: function(event,treeId,treeNode) {
  329. let me = componentOprObj, re = repositoryGljObj, that = gljComponentOprObj, gljTypeId = treeNode.ID;
  330. if(me.gljCurTypeId !== treeNode.ID){
  331. me.gljCurTypeId = treeNode.ID;
  332. if (re.parentNodeIds["_pNodeId_" + treeNode.ID]) {
  333. me.currentOprParent = 1;
  334. me.currentCache = me.getParentCache(re.parentNodeIds["_pNodeId_" + treeNode.ID]);
  335. } else {
  336. me.currentOprParent = 0;
  337. me.currentCache = me.getCache();
  338. }
  339. }
  340. //me.showGljItems(me.showGljList, gljTypeId);
  341. me.showGljItems(me.currentCache, gljTypeId);
  342. }
  343. }