gljSelect.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /**
  2. * Created by Zhong on 2017/8/25.
  3. */
  4. let gljSelOprObj = {
  5. distTypeTree: null,
  6. parentNodeIds: {},
  7. treeObj:null,
  8. rootNode: null,//分类树根节点
  9. workBook: null,
  10. selectedList: [],//选中的工料机
  11. setting: {
  12. header: [
  13. {headerName:"选择", headerWidth: 40, dataCode: "select", hAlign: "center", vAlign: "center"},
  14. {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  15. {headerName:"名称",headerWidth:120,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
  16. {headerName:"规格型号",headerWidth:80,dataCode:"specs", dataType: "String", hAlign: "center", vAlign: "center"},
  17. {headerName:"单位",headerWidth:80,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
  18. {headerName:"单价",headerWidth:80,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
  19. {headerName:"类型",headerWidth:80,dataCode:"gljType", dataType: "String", hAlign: "center", vAlign: "center"}
  20. ]
  21. },
  22. setProp: function (prop, value, gljList) {
  23. for(let i = 0, len = gljList.length; i < len; i++){
  24. gljList[i][prop] = value;
  25. }
  26. },
  27. sortGlj: function(gljList) {
  28. gljList.sort(function(a, b){
  29. let rst = 0;
  30. if (a.code > b.code) rst = 1
  31. else if (a.code < b.code) rst = -1;
  32. return rst;
  33. });
  34. },
  35. switchToGljId: function (gljList) {
  36. for(let glj of gljList){
  37. glj.gljId = glj.ID;
  38. }
  39. },
  40. /* getSelGljItems: function(gljData) {
  41. this.stdGljList = gljData.stdGljs;
  42. //兼容多单价,计算补充定额价格时,套多单价人材机的时候,默认取第一个价格
  43. for(let sGlj of this.stdGljList){
  44. if(sGlj.priceProperty && typeof sGlj.priceProperty.price1 !== 'undefined'){
  45. sGlj.basePrice = sGlj.priceProperty.price1;
  46. }
  47. }
  48. this.complementaryGljList = gljData.complementaryGljs;
  49. this.switchToGljId(this.stdGljList);
  50. this.switchToGljId(this.complementaryGljList);
  51. this.setProp('type', 'std', this.stdGljList);
  52. this.setProp('type', 'complementary', this.complementaryGljList);
  53. this.sortGlj(this.stdGljList);
  54. this.sortGlj(this.complementaryGljList);
  55. gljAdjOprObj.gljList = this.stdGljList.concat(this.complementaryGljList);
  56. if(priceProperties && priceProperties.length){
  57. const priceField = priceProperties[0].price.dataCode;
  58. for(let glj of gljAdjOprObj.gljList){
  59. glj.basePrice = glj.priceProperty && glj.priceProperty[priceField] ? glj.priceProperty[priceField] : 0;
  60. }
  61. }
  62. }, */
  63. initClassTree: function (type, treeData) {
  64. let me = this;
  65. if (me.treeObj) {
  66. me.treeObj.destroy();
  67. me.parentNodeIds = {};
  68. }
  69. zTreeHelper.createTree(treeData, gljSelTreeOprObj.setting, "selGljTree", me);
  70. let rootNode = me.treeObj.getNodes()[0];
  71. if(rootNode && rootNode.isParent && rootNode.isFirstNode){
  72. me.rootNode = rootNode;
  73. }
  74. if(me.rootNode){
  75. me.treeObj.selectNode(me.rootNode);
  76. if ((me.stdGljList && me.stdGljList.length > 0) ||
  77. (me.complementaryGljList && me.complementaryGljList.length > 0)) {
  78. gljSelTreeOprObj.setting.callback.onClick(null, 'selGljTree', me.rootNode);
  79. }
  80. }
  81. },
  82. /* getGljClassTree: function (mixedTreeData) {
  83. this.treeData = mixedTreeData;
  84. this.initClassTree('std', this.treeData.std);
  85. gljSelOprObj.buildSheet($('#gljSelSheet')[0]);
  86. }, */
  87. buildSheet: function (container) {
  88. let me = gljSelOprObj;
  89. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30);
  90. sheetCommonObj.spreadDefaultStyle(me.workBook);
  91. me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
  92. me.workBook.getSheet(0).setFormatter(-1, 1, "@", GC.Spread.Sheets.SheetArea.viewport);
  93. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  94. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  95. me.workBook.bind(GC.Spread.Sheets.Events.ButtonClicked, me.onButtonClicked);//复选框点击事件
  96. me.bindBtnOpr($('#gljSelY'));
  97. },
  98. setGLJItems: function(stdGLJs, complementaryGLJs) {
  99. this.stdGljList = stdGLJs;
  100. //兼容多单价,计算补充定额价格时,套多单价人材机的时候,默认取第一个价格
  101. for(let sGlj of this.stdGljList){
  102. if(sGlj.priceProperty && typeof sGlj.priceProperty.price1 !== 'undefined'){
  103. sGlj.basePrice = sGlj.priceProperty.price1;
  104. }
  105. }
  106. this.complementaryGljList = complementaryGLJs;
  107. this.switchToGljId(this.stdGljList);
  108. this.switchToGljId(this.complementaryGljList);
  109. this.setProp('type', 'std', this.stdGljList);
  110. this.setProp('type', 'complementary', this.complementaryGljList);
  111. this.sortGlj(this.stdGljList);
  112. this.sortGlj(this.complementaryGljList);
  113. },
  114. initLibOptions: function (libData) {
  115. const html = libData.reduce((acc, lib) => acc += `<option ${lib.isDefault ? 'selected="selected"' : ''} value="${lib.gljLibId}">${lib.name}</option>`, '');
  116. $('#glj-lib-select').html(html);
  117. },
  118. // TODO 改成分页的形式
  119. // 初始化选择页面
  120. initView: async function (gljLibId, isInitial) {
  121. try {
  122. $.bootstrapLoading.start();
  123. let { libData, treeData, distTypeTree, stdGLJ, complementaryGLJs } = await this.getViewData(gljLibId);
  124. if (isInitial) {
  125. this.initLibOptions(libData);
  126. $('#gljSearchKeyword').val('');
  127. setTimeout(() => $('#selGlj').modal('show'), 200);
  128. }
  129. if (!this.workBook) {
  130. this.buildSheet($('#gljSelSheet')[0]);
  131. }
  132. this.distTypeTree = distTypeTree;
  133. if (gljLibId === 0) {
  134. stdGLJ = pageOprObj.mixedGLJData.stdGljs;
  135. complementaryGLJs = pageOprObj.mixedGLJData.complementaryGljs;
  136. }
  137. this.setGLJItems(stdGLJ, complementaryGLJs);
  138. this.selectedList = [].concat(rationGLJOprObj.cache['_GLJ_' + rationGLJOprObj.currentRationItem.ID]);
  139. this.showGljList = [];
  140. this.setShowGljList(this.stdGljList, true);
  141. if (treeData.std.length) {
  142. this.initClassTree('std', treeData.std);
  143. } else if (treeData.comple.length) {
  144. this.initClassTree('comple', treeData.comple);
  145. } else {
  146. throw '没有有效的分类树。';
  147. }
  148. this.filterDatasAndShow();
  149. } catch (err) {
  150. console.log(err);
  151. alert(err);
  152. } finally {
  153. $.bootstrapLoading.end();
  154. }
  155. },
  156. getViewData: async function (gljLibId) {
  157. const url = `/rationGlj/getGLJData/${commonConstants.COMPILATION}/${gljLibId}/true`;
  158. const data = await ajaxGet(url);
  159. return data.datas;
  160. },
  161. onClipboardPasting: function (sender, args) {
  162. args.cancel = true;
  163. },
  164. onCellEditStart: function (sender, args) {
  165. args.cancel = true;
  166. },
  167. onButtonClicked: function (sender, args) {
  168. let me = gljSelOprObj;
  169. let val = args.sheet.getValue(args.row, args.col);
  170. let thisGlj = me.currentCache[args.row];
  171. thisGlj.isChecked = val;
  172. if(args.sheet.isEditing()){
  173. args.sheet.endEdit(true);
  174. }
  175. else{
  176. //设置选中
  177. if(val === true){
  178. let isExist = false;
  179. for(let i = 0, len = me.selectedList.length; i < len; i++){
  180. if(me.selectedList[i].gljId === thisGlj.gljId){
  181. isExist = true;
  182. break;
  183. }
  184. }
  185. if(!isExist){
  186. thisGlj.consumeAmt = 0;
  187. me.selectedList.push(_.cloneDeep(thisGlj));
  188. }
  189. }
  190. else if(val === false){
  191. for(let i = 0, len = me.selectedList.length; i < len; i++){
  192. if(me.selectedList[i].gljId === thisGlj.gljId){
  193. me.selectedList.splice(i, 1);
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. },
  200. setShowGljList: function (gljList, clearChecked) {
  201. //初始为所有工料机
  202. let me = this;
  203. let curRationGlj = rationGLJOprObj.cache['_GLJ_' + rationGLJOprObj.currentRationItem.ID];
  204. for(let i = 0; i < gljList.length; i++){
  205. //去除与已添加的组成物重复的条目
  206. let isExist = false;
  207. for(let j = 0; j < curRationGlj.length; j++){
  208. if(curRationGlj[j].gljId === gljList[i].gljId){
  209. isExist = true;
  210. break;
  211. }
  212. }
  213. if(!isExist){
  214. if(clearChecked){
  215. gljList[i].isChecked = false;
  216. }
  217. }
  218. else {
  219. gljList[i].isChecked = true;
  220. }
  221. me.showGljList.push(gljList[i]);
  222. }
  223. },
  224. //初始默认radio
  225. /* initRadio: function () {
  226. let me = gljSelOprObj;
  227. $('#gljSearchKeyword').val('');//恢复搜索文本
  228. me.selectedList = [].concat(rationGLJOprObj.cache['_GLJ_' + rationGLJOprObj.currentRationItem.ID]);
  229. //默认radio所有工料机
  230. if(typeof $("input[name='glj']:checked")[0] !== 'undefined'){
  231. $("input[name='glj']:checked")[0].checked = false;
  232. }
  233. $("input[value = 'stdGljs']")[0].checked = true;
  234. me.radiosSelected = 'stdGljs';
  235. //初始为标准工料机
  236. me.showGljList = [];
  237. if(me.radiosSelected === 'stdGljs'){
  238. me.setShowGljList(me.stdGljList, true);
  239. //me.setShowGljList(me.complementaryGljList, true);
  240. me.sortGlj(me.showGljList);
  241. }
  242. }, */
  243. filterDatasAndShow: function () {
  244. const gljLib = $('#glj-lib-select').val();
  245. let me = gljSelOprObj;
  246. //选择改变,数据重新筛选显示
  247. me.showGljList = [];
  248. const cptLibReg = new RegExp(commonConstants.COMPLEMENTARY_LIB);
  249. if (cptLibReg.test(gljLib)) {
  250. me.setShowGljList(me.complementaryGljList);
  251. } else {
  252. me.setShowGljList(me.stdGljList);
  253. }
  254. //搜索匹配
  255. let searchStr = $('#gljSearchKeyword').val();
  256. if(searchStr && searchStr.trim() != ''){
  257. let reg = new RegExp(searchStr);
  258. me.showGljList = _.filter(me.showGljList, function (data) {
  259. return reg.test(data.code) || reg.test(data.name);
  260. });
  261. }
  262. //me.sortGlj(me.showGljList);
  263. //重新显示
  264. me.showGljItems(me.showGljList, me.gljCurTypeId);
  265. if (me.currentOprParent = 1) {
  266. if(me.parentNodeIds["_pNodeId_" + me.gljCurTypeId]){
  267. me.currentCache = me.getParentCache(me.parentNodeIds["_pNodeId_" + me.gljCurTypeId]);
  268. }
  269. else{
  270. me.currentCache = [];
  271. }
  272. } else {
  273. me.currentCache = me.getCache();
  274. }
  275. },
  276. //监听radios选择事件
  277. /* radiosChange: function () {
  278. let me = gljSelOprObj;
  279. $('.glj-radio').change(function () {
  280. if($(this).val() === 'stdGljs') {
  281. me.initClassTree('std', me.treeData.std);
  282. } else {
  283. me.initClassTree('comple', me.treeData.comple);
  284. }
  285. me.filterDatasAndShow();
  286. });
  287. }, */
  288. getParentCache: function (nodes) {
  289. let me = gljSelOprObj, rst = [];
  290. for(let i = 0; i < me.showGljList.length; i++){
  291. if(nodes.indexOf(me.showGljList[i].gljClass) !== -1){
  292. rst.push(me.showGljList[i]);
  293. }
  294. }
  295. rst.sort(function (a, b) {
  296. let rst = 0;
  297. if(a.code > b.code) rst = 1;
  298. else if(a.code < b.code)rst = -1;
  299. return rst;
  300. });
  301. return rst;
  302. },
  303. getCache: function() {
  304. let me = gljSelOprObj, rst = [];
  305. for (let i = 0; i < me.showGljList.length; i++) {
  306. if (me.showGljList[i].gljClass == me.gljCurTypeId) {
  307. rst.push(me.showGljList[i]);
  308. }
  309. }
  310. return rst;
  311. },
  312. showGljItems: function(data, type) {
  313. let me = gljSelOprObj;
  314. if (me.workBook) {
  315. let cacheSection = [];
  316. let pArr = me.parentNodeIds["_pNodeId_" + type];
  317. for (let i = 0; i < data.length; i++) {
  318. if (pArr && pArr.indexOf(data[i].gljClass) >= 0) {
  319. cacheSection.push(data[i]);
  320. } else if (type == data[i].gljClass) {
  321. cacheSection.push(data[i]);
  322. }
  323. }
  324. // sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  325. sheetsOprObj.showDataForGljSel(me.workBook.getSheet(0), me.setting, cacheSection, rationGLJOprObj.distTypeTree);
  326. me.workBook.getSheet(0).setRowCount(cacheSection.length);
  327. cacheSection = null;
  328. }
  329. },
  330. //组成物窗口按钮操作
  331. bindBtnOpr: function (conf) {//确定、取消、关闭按钮
  332. let me = gljSelOprObj, that = rationGLJOprObj;
  333. conf.click(function () {
  334. that.cache['_GLJ_' + that.currentRationItem.ID] = me.selectedList;
  335. that.updateRationItem(function () {
  336. that.sheet.getParent().focus();
  337. sheetCommonObj.cleanData(that.sheet, that.setting, -1);
  338. that.showGljItems(that.currentRationItem.ID);
  339. $('#selGlj').modal('hide');
  340. });
  341. });
  342. }
  343. };
  344. let gljSelTreeOprObj = {
  345. setting: {
  346. view: {
  347. expandSpeed: "",
  348. selectedMulti: false
  349. },
  350. edit: {
  351. enable: false,
  352. editNameSelectAll: true,
  353. showRemoveBtn: true,
  354. showRenameBtn: true,
  355. removeTitle: "删除节点",
  356. renameTitle: "更改名称"
  357. },
  358. data: {
  359. keep: {
  360. parent:true,
  361. leaf:true
  362. },
  363. key: {
  364. children: "items",
  365. name: "Name"
  366. },
  367. simpleData: {
  368. enable: false,
  369. idKey: "ID",
  370. pIdKey: "ParentID",
  371. rootPId: -1
  372. }
  373. },
  374. callback:{
  375. onClick: function(event,treeId,treeNode) {
  376. let me = gljSelOprObj, gljTypeId = treeNode.ID;
  377. if(me.gljCurTypeId !== treeNode.ID){
  378. me.gljCurTypeId = treeNode.ID;
  379. if (me.parentNodeIds["_pNodeId_" + treeNode.ID]) {
  380. me.currentOprParent = 1;
  381. me.currentCache = me.getParentCache(me.parentNodeIds["_pNodeId_" + treeNode.ID]);
  382. } else {
  383. me.currentCache = me.getCache();
  384. }
  385. }
  386. me.showGljItems(me.showGljList, gljTypeId);
  387. }
  388. }
  389. }
  390. };
  391. $(document).ready(function () {
  392. $('#glj-lib-select').change(function () {
  393. const gljLibId = $(this).val();
  394. gljSelOprObj.initView(gljLibId, false);
  395. });
  396. $('#gljSearchKeyword').on('keyup', _.debounce(function () {
  397. gljSelOprObj.filterDatasAndShow();
  398. }, 300));
  399. $('#selGlj').on('shown.bs.modal', function () {
  400. if (gljSelOprObj.workBook) {
  401. gljSelOprObj.workBook.refresh();
  402. }
  403. });
  404. });