cs_tools.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. 'use strict';
  2. /**
  3. * cs_errorList:错误列表
  4. * 使用范围:
  5. * 台账分解(原报)、台账修订(原报)、计量台账(所有角色)
  6. *
  7. * posSearch & billsSearch:台账搜索相关
  8. * 使用范围:
  9. * 0号台账:台账分解、台账审批、台账修订、部位台账;
  10. * 期计量:计量台账、部位台账
  11. *
  12. * 所有工具均基于spreadjs,请放在gc.spread.sheets.all.10.0.1.min.js/spreadjs_zh.js之后
  13. *
  14. * @author Mai
  15. * @date
  16. * @version
  17. */
  18. const showSideTools = function (show) {
  19. const left = $('#left-view'), right = $('#right-view'), parent = left.parent();
  20. if (show) {
  21. right.show();
  22. autoFlashHeight();
  23. /**
  24. * right.show()后, parent被撑开成2倍left.height, 导致parent.width减少了10px
  25. * 第一次left.width调整后,parent的缩回left.height, 此时parent.width又增加了10px
  26. * 故需要通过最终的parent.width再计算一次left.width
  27. *
  28. * Q: 为什么不通过先计算left.width的宽度,以避免计算两次left.width?
  29. * A: 右侧工具栏不一定显示,当右侧工具栏显示过一次后,就必须使用parent和right来计算left.width
  30. *
  31. */
  32. //left.css('width', parent.width() - right.outerWidth());
  33. //left.css('width', parent.width() - right.outerWidth());
  34. const percent = 100 - right.outerWidth() /parent.width() * 100;
  35. left.css('width', percent + '%');
  36. } else {
  37. left.width(parent.width());
  38. right.hide();
  39. }
  40. };
  41. (function($){
  42. /**
  43. * 错误列表
  44. * @param setting
  45. * {
  46. * tabSelector: 'a[content=#error-list]',
  47. * selector: '#error-list',
  48. * relaSpread: ledgerSpread,
  49. * storeKey: 'ledger-error-' + tenderId,
  50. * }
  51. * @returns {{spread: *}}
  52. */
  53. $.cs_errorList = function (setting) {
  54. if (!setting.selector || !setting.relaSpread) return;
  55. if (!setting.spreadSetting) {
  56. setting.spreadSetting = {
  57. cols: [
  58. {title: '行号', field: 'serialNo', width: 80, formatter: '@'},
  59. {title: '清单编号', field: 'b_code', width: 150, formatter: '@'},
  60. {title: '清单名称', field: 'name', width: 230, formatter: '@'},
  61. ],
  62. emptyRows: 0,
  63. headRows: 1,
  64. headRowHeight: [32],
  65. defaultRowHeight: 21,
  66. headerFont: '12px 微软雅黑',
  67. font: '12px 微软雅黑',
  68. selectedBackColor: '#fffacd',
  69. readOnly: true,
  70. };
  71. }
  72. const resultId = setting.id + '-spread';
  73. const obj = $(setting.selector);
  74. obj.html(
  75. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  76. ' </div>'
  77. );
  78. autoFlashHeight();
  79. const spread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  80. const sheet = spread.getActiveSheet();
  81. SpreadJsObj.initSheet(sheet, setting.spreadSetting);
  82. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, spread);
  83. spread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  84. const sheet = info.sheet;
  85. const data = sheet.zh_data;
  86. if (!data) { return }
  87. const curBills = data[info.row];
  88. if (!curBills) { return }
  89. SpreadJsObj.locateTreeNode(setting.relaSpread.getActiveSheet(), curBills.ledger_id, true);
  90. console.log(curBills);
  91. if (setting.afterLocated) {
  92. setting.afterLocated();
  93. }
  94. });
  95. const loadErrorData = function (data, his = false) {
  96. SpreadJsObj.loadSheetData(sheet, SpreadJsObj.DataType.Data, data);
  97. if (!his && setting.storeKey) {
  98. setLocalCache(setting.storeKey, JSON.stringify(data));
  99. }
  100. $(setting.tabSelector).show();
  101. };
  102. if (setting.storeKey) {
  103. const storeStr = getLocalCache(setting.storeKey);
  104. const storeData = storeStr ? JSON.parse(storeStr) : [];
  105. if (storeData.length > 0) loadErrorData(storeData, true);
  106. }
  107. const showErrorList = function () {
  108. const tab = $(setting.tabSelector), tabPanel = $(tab.attr('content'));
  109. $('a', '#side-menu').removeClass('active');
  110. tab.addClass('active');
  111. $('.tab-content .tab-pane').removeClass('active');
  112. tabPanel.addClass('active');
  113. showSideTools(true);
  114. spread.refresh();
  115. if (setting.afterShow) setting.afterShow();
  116. };
  117. return {spread: spread, loadErrorData: loadErrorData, show: showErrorList};
  118. };
  119. $.posSearch = function (setting) {
  120. if (!setting.selector || !setting.searchSpread) return;
  121. const searchHtml =
  122. ' <div class="ml-2">\n' +
  123. ' <div class="input-group input-group-sm">\n' +
  124. ' <input type="text" class="form-control" placeholder="输入名称查找" id="pos-keyword">\n' +
  125. ' <div class="input-group-append">\n' +
  126. ' <span class="input-group-text" id="pos-search-hint">结果:0</span>\n' +
  127. ' </div>\n' +
  128. ' <div class="input-group-append" >\n' +
  129. ' <button class="btn btn-outline-secondary" type="button" title="上一个" id="search-pre-pos"><i class="fa fa-angle-double-left"></i></button>\n' +
  130. ' <button class="btn btn-outline-secondary" type="button" title="下一个" id="search-next-pos"><i class="fa fa-angle-double-right"></i></button>\n' +
  131. ' </div>\n' +
  132. ' </div>\n' +
  133. ' </div>\n';
  134. $(setting.selector).html(searchHtml);
  135. const sheet = setting.searchSpread.getActiveSheet();
  136. const searchObj = (function () {
  137. let resultArr = [];
  138. const search = function (keyword) {
  139. if (keyword && keyword !== '') {
  140. resultArr = [];
  141. const sortData = sheet.zh_data;
  142. if (sortData) {
  143. for (let i = 0, iLength = sortData.length; i < iLength; i++) {
  144. const sd = sortData[i];
  145. if (sd.name && sd.name.indexOf(keyword) > -1) {
  146. resultArr.push({index: i, data: sd});
  147. }
  148. }
  149. }
  150. } else {
  151. resultArr = [];
  152. }
  153. $('#pos-search-hint').html('结果:' + resultArr.length);
  154. };
  155. const searchAndLocate = function (keyword) {
  156. search(keyword);
  157. if (resultArr.length > 0) {
  158. const sel = sheet.getSelections()[0];
  159. const curRow = sel ? sel.row : 0;
  160. const pos = resultArr[0];
  161. if (pos.index !== curRow) {
  162. sheet.setSelection(pos.index, sel ? sel.col : 0, 1, 1);
  163. sheet.getParent().focus();
  164. sheet.showRow(pos.index, spreadNS.VerticalPosition.center);
  165. }
  166. }
  167. };
  168. const locateNext = function () {
  169. if (resultArr.length > 0) {
  170. const sel = sheet.getSelections()[0];
  171. const curRow = sel ? sel.row : 0;
  172. let next = _.find(resultArr, function (d) {
  173. return d.index > curRow;
  174. });
  175. if (!next) next = resultArr[0];
  176. if (next.index !== curRow) {
  177. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  178. sheet.getParent().focus();
  179. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  180. }
  181. }
  182. };
  183. const locatePre = function () {
  184. if (resultArr.length > 0) {
  185. const sel = sheet.getSelections()[0];
  186. const curRow = sel ? sel.row : 0;
  187. let next = _.findLast(resultArr, function (d) {
  188. return d.index < curRow;
  189. });
  190. if (!next) next = resultArr[resultArr.length - 1];
  191. if (next.index !== curRow) {
  192. sheet.setSelection(next.index, sel ? sel.col : 0, 1, 1);
  193. sheet.getParent().focus();
  194. sheet.showRow(next.index, spreadNS.VerticalPosition.center);
  195. }
  196. }
  197. };
  198. return {search, searchAndLocate, locateNext, locatePre};
  199. })();
  200. // $('#pos-keyword').bind('input propertychange', function () {
  201. // posSearch.search(this.value);
  202. // });
  203. $('#pos-keyword').bind('keydown', function(e){
  204. if (e.keyCode == 13) searchObj.searchAndLocate(this.value);
  205. });
  206. $('#search-pre-pos').click(function () {
  207. searchObj.locatePre();
  208. });
  209. $('#search-next-pos').click(function () {
  210. searchObj.locateNext();
  211. });
  212. return searchObj;
  213. };
  214. $.billsSearch = function (setting) {
  215. if (!setting.selector || !setting.searchSpread || !setting.resultSpreadSetting) return;
  216. if (!setting.searchRangeStr) setting.searchRangeStr = '项目节编号/清单编号/名称';
  217. const resultId = setting.id + '-search-result';
  218. const obj = $(setting.selector);
  219. obj.html(
  220. ' <div class="sjs-bar">\n' +
  221. ' <div class="input-group input-group-sm pb-1">\n' +
  222. ' <div class="input-group-prepend">\n' +
  223. (setting.searchOver ? ' <div class="input-group-text"><input type="radio" name="searchType" id="over"> 超计</div>\n' : '') +
  224. (setting.searchEmpty ? ' <div class="input-group-text"><input type="radio" name="searchType" id="empty"> 漏计</div>\n' : '') +
  225. ' </div>' +
  226. ' <input id="searchKeyword" type="text" class="form-control" placeholder="可查找 ' + setting.searchRangeStr + '" aria-label="Recipient\'s username" aria-describedby="button-addon2">\n' +
  227. ' <div class="input-group-append">\n' +
  228. ' <button class="btn btn-outline-secondary" type="button"">搜索</button>\n' +
  229. ' </div>\n' +
  230. ' </div>\n' +
  231. ' </div>\n' +
  232. ' <div id="' + resultId + '" class="sjs-sh">\n' +
  233. ' </div>'
  234. );
  235. autoFlashHeight();
  236. const resultSpread = SpreadJsObj.createNewSpread($('#' + resultId)[0]);
  237. SpreadJsObj.initSheet(resultSpread.getActiveSheet(), setting.resultSpreadSetting);
  238. SpreadJsObj.forbiddenSpreadContextMenu('#' + resultId, resultSpread);
  239. const searchSheet = setting.searchSpread.getActiveSheet();
  240. let searchResult = [];
  241. const searchBills = function () {
  242. const keyword = $('#searchKeyword', obj).val();
  243. searchResult = [];
  244. const sortData = SpreadJsObj.getSortData(searchSheet);
  245. for (const node of sortData) {
  246. if ((node.code && node.code.indexOf(keyword) > -1) ||
  247. node.b_code && node.b_code.indexOf(keyword) > -1 ||
  248. node.name && node.name.indexOf(keyword) > -1) {
  249. const data = JSON.parse(JSON.stringify(node));
  250. data.visible = true;
  251. searchResult.push(data);
  252. }
  253. }
  254. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  255. };
  256. const calculateCompletePercent = function (searchResult) {
  257. if (!searchResult) return;
  258. for (const sr of searchResult) {
  259. const base = ZhCalc.add(sr.total_price, sr.end_qc_tp);
  260. sr.complete_percent = base !== 0 ? ZhCalc.mul(ZhCalc.div(sr.end_gather_tp, base), 100, 2) : 0;
  261. }
  262. };
  263. const searchOver = function () {
  264. searchResult = [];
  265. const sortData = SpreadJsObj.getSortData(searchSheet);
  266. for (const node of sortData) {
  267. if (node.children && node.children.length > 0) continue;
  268. if (setting.checkOver && setting.checkOver(node)) {
  269. const data = JSON.parse(JSON.stringify(node));
  270. data.visible = true;
  271. searchResult.push(data);
  272. }
  273. }
  274. calculateCompletePercent(searchResult);
  275. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  276. };
  277. const searchEmpty = function () {
  278. searchResult = [];
  279. const sortData = SpreadJsObj.getSortData(searchSheet);
  280. for (const node of sortData) {
  281. if (node.children && node.children.length > 0) continue;
  282. if (setting.checkEmpty && setting.checkEmpty(node)) {
  283. const data = JSON.parse(JSON.stringify(node));
  284. data.visible = true;
  285. searchResult.push(data);
  286. }
  287. }
  288. calculateCompletePercent(searchResult);
  289. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  290. };
  291. const searchLess = function () {
  292. searchResult = [];
  293. const sortData = SpreadJsObj.getSortData(searchSheet);
  294. for (const node of sortData) {
  295. if (node.children && node.children.length > 0) continue;
  296. if (setting.checkLess && setting.checkLess(node)) {
  297. const data = JSON.parse(JSON.stringify(node));
  298. data.visible = true;
  299. searchResult.push(data);
  300. }
  301. }
  302. calculateCompletePercent(searchResult);
  303. SpreadJsObj.loadSheetData(resultSpread.getActiveSheet(), 'data', searchResult);
  304. };
  305. $('input', obj).bind('keydown', function (e) {
  306. if (e.keyCode == 13) searchBills();
  307. });
  308. $('button', obj).bind('click', () => {searchBills()});
  309. $('#over', obj).bind('change', () => {searchOver()});
  310. $('#empty', obj).bind('change', () => {searchLess()});
  311. resultSpread.getActiveSheet().bind(spreadNS.Events.CellDoubleClick, function (e, info) {
  312. const sheet = info.sheet;
  313. const data = sheet.zh_data;
  314. if (!data) { return }
  315. const curBills = data[info.row];
  316. if (!curBills) { return }
  317. SpreadJsObj.locateTreeNode(searchSheet, curBills.ledger_id, true);
  318. if (setting.afterLocated) {
  319. setting.afterLocated();
  320. }
  321. });
  322. return {spread: resultSpread};
  323. };
  324. })(jQuery);