gljSelect.js 16 KB

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