components.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. that.currentGlj.gljType === 302 && gljList[i].gljType === 201){//机械组成物,应可选择无组成物的普通材料
  120. let isExist = false;
  121. for(let j = 0; j < that.currentComponent.length; j++){
  122. if(that.currentComponent[j].ID === gljList[i].ID){
  123. isExist = true;
  124. break;
  125. }
  126. }
  127. if(!isExist){
  128. if(clearChecked){
  129. gljList[i].isChecked = false;
  130. }
  131. }
  132. else {
  133. gljList[i].isChecked = true;
  134. }
  135. me.showGljList.push(gljList[i]);
  136. }
  137. }
  138. },
  139. //初始默认radio
  140. initRadio: function () {
  141. let that = repositoryGljObj, me = componentOprObj;
  142. //初始化组成物列表
  143. me.selectedList = [].concat(that.currentComponent);
  144. //初始为所有工料机,机械类型可添加机械组成物,混凝土,砂浆、配合比可添加普通材料
  145. me.showGljList = [];
  146. me.setShowGljList(that.gljList, true);
  147. that.sortGlj(me.showGljList);
  148. },
  149. getParentCache: function (nodes) {
  150. let me = componentOprObj, rst = [];
  151. for(let i = 0; i < me.showGljList.length; i++){
  152. if(nodes.indexOf(me.showGljList[i].gljClass) !== -1){
  153. rst.push(me.showGljList[i]);
  154. }
  155. }
  156. rst.sort(function (a, b) {
  157. let rst = 0;
  158. if(a.code > b.code) rst = 1;
  159. else if(a.code < b.code)rst = -1;
  160. return rst;
  161. });
  162. return rst;
  163. },
  164. getCache: function() {
  165. let me = componentOprObj, rst = [];
  166. for (let i = 0; i < me.showGljList.length; i++) {
  167. if (me.showGljList[i].gljClass == me.gljCurTypeId) {
  168. rst.push(me.showGljList[i]);
  169. }
  170. }
  171. return rst;
  172. },
  173. showGljItems: function(data, type) {
  174. let me = componentOprObj, re = repositoryGljObj;
  175. if (me.workBook) {
  176. let cacheSection = data;
  177. re.sortGljDeep.call(me, data);
  178. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  179. sheetsOprObj.showData(me, me.workBook.getSheet(0), me.setting, cacheSection, re.distTypeTree);
  180. me.workBook.getSheet(0).setRowCount(cacheSection.length);
  181. cacheSection = null;
  182. }
  183. },
  184. //批量插入组成物
  185. batchUpdateComponent: function () {
  186. $.bootstrapLoading.start();
  187. let me = componentOprObj, that = gljComponentOprObj, re = repositoryGljObj,
  188. updateArr = [],
  189. IDMapping = {};
  190. let formData = new FormData();
  191. //当前工料机的工料机类型
  192. let currentGljType = re.currentGlj.gljType;
  193. let thisTypeGljList = _.filter(re.gljList, function (data) {
  194. return data.gljType == currentGljType;
  195. });
  196. for(let glj of thisTypeGljList){
  197. let gljComponent = glj.component ? glj.component : [];
  198. let newComponent = [];
  199. if(me.insertType === 'batch'){
  200. for(let selectedComponent of me.selectedList){
  201. let isExist = false;
  202. for(let gljPerComponent of gljComponent){
  203. if(selectedComponent.ID === gljPerComponent.ID){
  204. //newComponent.push({ID: selectedComponent.ID, consumeAmt: gljPerComponent.consumeAmt});
  205. isExist = true;
  206. break;
  207. }
  208. }
  209. if(!isExist){
  210. let newComponentObj = {ID: selectedComponent.ID};
  211. that.initConsumeAmt(newComponentObj);
  212. newComponent.push(newComponentObj);
  213. }
  214. }
  215. newComponent = newComponent.concat(gljComponent);
  216. }
  217. else if(me.insertType === 'batchClear'){//去除消耗量为0的组成物
  218. for(let gljPerComponent of gljComponent){
  219. if(hasConsumeAmt(gljPerComponent)){
  220. if(!consumeAmtProperties || consumeAmtProperties.length === 0){
  221. newComponent.push({ID: gljPerComponent.ID, consumeAmt: gljPerComponent.consumeAmt});
  222. }
  223. else {
  224. newComponent.push({ID: gljPerComponent.ID, consumeAmtProperty: gljPerComponent.consumeAmtProperty});
  225. }
  226. }
  227. }
  228. }
  229. function hasConsumeAmt(component){
  230. if(!consumeAmtProperties || consumeAmtProperties.length === 0){
  231. if(component.consumeAmt && component.consumeAmt != 0){
  232. return true;
  233. }
  234. }
  235. else {
  236. if(component.consumeAmtProperty){
  237. for(let field in component.consumeAmtProperty){
  238. if(component.consumeAmtProperty[field] != 0){
  239. return true;
  240. }
  241. }
  242. }
  243. }
  244. return false;
  245. }
  246. IDMapping[glj.ID] = {component : newComponent};
  247. updateArr.push({ID: glj.ID, component: newComponent});
  248. }
  249. formData.append('compressData', LZString.compressToUTF16(JSON.stringify(updateArr)));
  250. $.ajax({
  251. url: 'api/batchUpdateComponent',
  252. type: 'POST',
  253. data: formData,
  254. cache: false,
  255. contentType: false,
  256. processData: false,
  257. success: function(response){
  258. if (response.err === 0) {
  259. //更新缓存数据
  260. for(let glj of thisTypeGljList){
  261. if(glj.ID === re.currentGlj.ID){
  262. re.currentComponent = re.getCurrentComponent(IDMapping[glj.ID]['component']);
  263. sheetCommonObj.cleanData(that.workBook.getSheet(0), that.setting, -1);
  264. sheetsOprObj.showData(that, that.workBook.getSheet(0), that.setting, re.currentComponent);
  265. }
  266. glj.component = IDMapping[glj.ID]['component'];
  267. }
  268. if($('#component').is(':visible')){
  269. $('#component').modal('hide');
  270. }
  271. $.bootstrapLoading.end();
  272. } else {
  273. if($('#component').is(':visible')){
  274. $('#component').modal('hide');
  275. }
  276. $.bootstrapLoading.end();
  277. }
  278. },
  279. error: function(jqXHR){
  280. alert(`与服务器通信发生错误${jqXHR.status} ${jqXHR.statusText}`);
  281. $('#component').modal('hide');
  282. $.bootstrapLoading.end();
  283. }
  284. });
  285. },
  286. //组成物窗口按钮操作
  287. componentsBtnOpr: function (conf) {//确定、取消、关闭按钮
  288. let me = componentOprObj, that = gljComponentOprObj, re = repositoryGljObj;
  289. conf.click(function () {
  290. if(me.insertType === 'single'){
  291. let updateBasePrc = [];
  292. //添加选择添加的组成物
  293. let updateArr = [];
  294. let newComponent = [];
  295. for(let i = 0, len = me.selectedList.length; i < len; i++){
  296. let isExist = false;
  297. for(let j = 0, jLen = re.currentGlj.component.length; j < jLen; j++){
  298. if(me.selectedList[i].ID === re.currentGlj.component[j].ID){
  299. if(!consumeAmtProperties || consumeAmtProperties.length === 0){
  300. let consumeAmt = typeof re.currentGlj.component[j].consumeAmt !== 'undefined' ? re.currentGlj.component[j].consumeAmt : 0;
  301. newComponent.push({ID: me.selectedList[i].ID, consumeAmt: consumeAmt});
  302. }
  303. else{
  304. let consumeAmtProperty = typeof re.currentGlj.component[j].consumeAmtProperty !== 'undefined' ? re.currentGlj.component[j].consumeAmtProperty : {};
  305. newComponent.push({ID: me.selectedList[i].ID, consumeAmt: 0, consumeAmtProperty: consumeAmtProperty});
  306. }
  307. isExist = true;
  308. break;
  309. }
  310. }
  311. if(!isExist){
  312. let obj = {ID: me.selectedList[i].ID};
  313. that.initConsumeAmt(obj);
  314. newComponent.push(obj);
  315. }
  316. }
  317. re.currentGlj.component = newComponent;
  318. updateArr.push(re.currentGlj);
  319. that.updateComponent(updateArr);
  320. $('#component').modal('hide');
  321. }
  322. else {
  323. me.batchUpdateComponent();
  324. }
  325. });
  326. }
  327. };
  328. let componentTypeTreeOprObj = {
  329. onClick: function(event,treeId,treeNode) {
  330. let me = componentOprObj, re = repositoryGljObj, that = gljComponentOprObj, gljTypeId = treeNode.ID;
  331. if(me.gljCurTypeId !== treeNode.ID){
  332. me.gljCurTypeId = treeNode.ID;
  333. if (re.parentNodeIds["_pNodeId_" + treeNode.ID]) {
  334. me.currentOprParent = 1;
  335. me.currentCache = me.getParentCache(re.parentNodeIds["_pNodeId_" + treeNode.ID]);
  336. } else {
  337. me.currentOprParent = 0;
  338. me.currentCache = me.getCache();
  339. }
  340. }
  341. //me.showGljItems(me.showGljList, gljTypeId);
  342. me.showGljItems(me.currentCache, gljTypeId);
  343. }
  344. }