tender_select_multi.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. const TenderSelectMulti = function (setting) {
  2. $('#tsm-title').html(setting.title);
  3. if (setting.type === 'gather' && setting.dataType === 'stage') {
  4. $('#tsm-stage-info').show();
  5. }
  6. if (setting.zoneValid === false) {
  7. $('[stage-type=zone]').hide();
  8. }
  9. $('[name=tsm-source]').click(function() {
  10. $('[name=gather-type]').hide();
  11. const type = this.value;
  12. const gatherBy = $(`#gather-by-${type}`);
  13. if (gatherBy.length > 0) {
  14. $('#tsm-stage-info-detail').show();
  15. $(`#gather-by-${type}`).show();
  16. } else {
  17. $('#tsm-stage-info-detail').hide();
  18. }
  19. });
  20. $('.datepicker-here').datepicker({
  21. autoClose: true,
  22. });
  23. const tsObj = {
  24. setting,
  25. selectSpread: null,
  26. selectSheet: null,
  27. resultSpread: null,
  28. resultSheet: null,
  29. tenderSourceTree: null,
  30. trArray: [],
  31. _rebuildStageSelect: function () {
  32. if (tsObj.setting.type === 'compare') {
  33. const getItems = function (data) {
  34. if (!data) return [];
  35. const items = [];
  36. for (let i = 1; i <= data.stageCount; i++) {
  37. items.push({value: i, text: `第${i}期`});
  38. }
  39. return items;
  40. };
  41. for (let i = 0; i < tsObj.resultSheet.getRowCount(); i++) {
  42. const cellType2 = new spreadNS.CellTypes.ComboBox().itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(getItems(tsObj.trArray[i]));
  43. tsObj.resultSheet.getCell(i, 1).cellType(cellType2);
  44. }
  45. }
  46. },
  47. _initSelected: function () {
  48. for (const node of this.tenderSourceTree.nodes) {
  49. node.selected = this.trArray.findIndex(x => { return node.tid === x.tid; }) >= 0;
  50. }
  51. },
  52. _addTender: function (tender) {
  53. const tr = tsObj.trArray.find(x => { return x.tid === tender.tid; });
  54. const t = { tid: tender.tid, name: tender.name, stageCount: tender.stageCount };
  55. if (!tr) tsObj.trArray.push(t);
  56. return t;
  57. },
  58. _removeTender: function (tender) {
  59. const gri = tsObj.trArray.findIndex(function (x, i, arr) {
  60. return x.tid === tender.tid;
  61. });
  62. if (gri >= 0) tsObj.trArray.splice(gri, 1);
  63. },
  64. reloadResultData: function () {
  65. SpreadJsObj.reLoadSheetData(tsObj.resultSheet);
  66. this._rebuildStageSelect();
  67. },
  68. _getStageSelectHtml: function (valid) {
  69. const html = [];
  70. for (let i = 1; i <= valid; i++) {
  71. html.push(`<option value="${i}">第${i}期</option>`);
  72. }
  73. return html.join('');
  74. },
  75. refreshStageGather: function() {
  76. if (setting.type !== 'gather' || setting.dataType !== 'stage') return;
  77. const minStage = _.min(_.map(this.trArray, 'stageCount'));
  78. $('#gather-stage').html(this._getStageSelectHtml(minStage));
  79. const maxStage = _.max(_.map(this.trArray, 'stageCount'));
  80. $('#gather-stage-begin').html(this._getStageSelectHtml(maxStage));
  81. $('#gather-stage-end').html(this._getStageSelectHtml(maxStage));
  82. },
  83. tsButtonClicked: function (e, info) {
  84. if (!info.sheet.zh_setting) return;
  85. const col = info.sheet.zh_setting.cols[info.col];
  86. if (col.field !== 'selected') return;
  87. const node = SpreadJsObj.getSelectObject(info.sheet);
  88. if (setting.type === 'compare') {
  89. if (node.children && node.children.length > 0) {
  90. toastr.warning('对比标段请直接勾选需要对比的标段');
  91. return;
  92. }
  93. if (!node.selected && tsObj.trArray.length >= 2) {
  94. toastr.warning('仅可选择两个标段进行对比');
  95. return;
  96. }
  97. }
  98. node.selected = !node.selected;
  99. if (node.children && node.children.length > 0) {
  100. const posterity = tsObj.tenderSourceTree.getPosterity(node);
  101. for (const p of posterity) {
  102. p.selected = node.selected;
  103. if (!p.children || p.children.length === 0){
  104. if (p.selected) {
  105. tsObj._addTender(p);
  106. } else {
  107. tsObj._removeTender(p);
  108. }
  109. }
  110. }
  111. SpreadJsObj.reLoadRowData(info.sheet, info.row, posterity.length + 1);
  112. } else {
  113. if (node.selected) {
  114. tsObj._addTender(node);
  115. } else {
  116. tsObj._removeTender(node);
  117. }
  118. SpreadJsObj.reLoadRowData(info.sheet, info.row, 1);
  119. }
  120. tsObj.refreshStageGather();
  121. tsObj.reloadResultData();
  122. },
  123. trEditEnded: function (e, info) {
  124. const data = SpreadJsObj.getSelectObject(info.sheet);
  125. if (!data) return;
  126. const col = info.sheet.zh_setting.cols[info.col];
  127. data[col.field] = info.sheet.getValue(info.row, info.col);
  128. },
  129. loadTenders: function () {
  130. postData(`/sp/${spid}/list/load2`, {type: this.setting.dataType + '-checked' }, data => {
  131. tsObj.tenderSourceTree = Tender2Tree.convert(data.category, data.tenders, data.ledgerAuditConst, data.stageAuditConst);
  132. SpreadJsObj.loadSheetData(tsObj.selectSheet, SpreadJsObj.DataType.Tree, tsObj.tenderSourceTree);
  133. SpreadJsObj.loadSheetData(tsObj.resultSheet, SpreadJsObj.DataType.Data, tsObj.trArray);
  134. });
  135. },
  136. getSelectData: function() {
  137. const selectData = tsObj.trArray;
  138. const checked = $('#tsm-filter-checked')[0].checked;
  139. if (selectData.length === 0) {
  140. toastr.warning('请选择标段');
  141. return;
  142. }
  143. if (this.setting.type === 'compare') {
  144. if (selectData.length !== 2) {
  145. toastr.warning('请选择两个标段进行对比');
  146. return;
  147. }
  148. if (this.setting.dataType === 'stage') {
  149. for (const s of selectData) {
  150. if (!s.stage) {
  151. toastr.warning('请选择标段进行对比的期');
  152. return;
  153. }
  154. s.stageInfo = { type: 'stage', stage: s.stage, checked };
  155. }
  156. }
  157. }
  158. if (this.setting.type === 'gather' && this.setting.dataType === 'stage') {
  159. // todo 检查汇总选项
  160. const stage = { type: $('[name=tsm-source]:checked').val(), checked };
  161. if (stage.type === 'stage') {
  162. stage.stage = _.toInteger($('#gather-stage').val()) || 0;
  163. if (!stage.stage) {
  164. toastr.warning('请选择 汇总期');
  165. return;
  166. }
  167. const validStage = _.min(_.map(tsObj.trArray, 'stageCount'));
  168. if (stage.stage > validStage) {
  169. toastr.warning('选择的期无效,请重新选择');
  170. return;
  171. }
  172. } else if (stage.type === 'month') {
  173. stage.month = $('#gather-month').val();
  174. if (stage.month === '') {
  175. toastr.warning('请选择 汇总年月');
  176. return;
  177. }
  178. } else if (stage.type === 'zone') {
  179. stage.zone = $('#gather-zone').val();
  180. if (stage.zone === '') {
  181. toastr.warning('请选择 汇总周期');
  182. return;
  183. } else if(stage.zone.indexOf(' - ') < 0) {
  184. toastr.warning('请选择 完整汇总周期');
  185. return;
  186. }
  187. } else if (stage.type === 'stage-zone') {
  188. const stageBegin = _.toInteger($('#gather-stage-begin').val()) || 0;
  189. const stageEnd = _.toInteger($('#gather-stage-end').val()) || 0;
  190. const validStage = _.max(_.map(tsObj.trArray, 'stageCount'));
  191. if (!stageBegin || !stageEnd) {
  192. toastr.warning('请选择 汇总开始期与结束期');
  193. return;
  194. }
  195. if (stageEnd <= stageBegin) {
  196. toastr.warning('结束期应大于开始期');
  197. return;
  198. }
  199. if (stageEnd > validStage) {
  200. toastr.warning('选择的期无效,请重新选择');
  201. return;
  202. }
  203. stage.stage_zone = stageBegin + ':' + stageEnd;
  204. } else if (stage.type === 'custom-zone') {
  205. stage.custom_zone = $('#gather-custom-zone').val();
  206. if (stage.custom_zone === '') {
  207. toastr.warning('请选择 汇总周期');
  208. return;
  209. } else if(stage.custom_zone.indexOf(' - ') < 0) {
  210. toastr.warning('请选择 完整汇总周期');
  211. return;
  212. }
  213. }
  214. selectData.forEach(s => { s.stageInfo = stage; });
  215. }
  216. return selectData;
  217. },
  218. initTenderSelect: function () {
  219. if (this.selectSpread) return;
  220. this.selectSpread = SpreadJsObj.createNewSpread($('#tsm-select-spread')[0]);
  221. this.selectSheet = this.selectSpread.getActiveSheet();
  222. SpreadJsObj.initSheet(this.selectSheet, {
  223. cols: [
  224. {title: '选择', field: 'selected', hAlign: 1, width: 40, formatter: '@', cellType: 'checkbox'},
  225. {title: '名称', field: 'name', hAlign: 0, width: 300, formatter: '@', cellType: 'tree'},
  226. {title: '期数', field: 'phase', hAlign: 1, width: 80, formatter: '@'},
  227. {title: '状态', field: 'status', hAlign: 1, width: 80, formatter: '@'}
  228. ],
  229. emptyRows: 0,
  230. headRows: 1,
  231. headRowHeight: [32],
  232. defaultRowHeight: 21,
  233. headerFont: '12px 微软雅黑',
  234. font: '12px 微软雅黑',
  235. headColWidth: [30],
  236. selectedBackColor: '#fffacd',
  237. readOnly: true,
  238. });
  239. this.resultSpread = SpreadJsObj.createNewSpread($('#tsm-result-spread')[0]);
  240. this.resultSheet = this.resultSpread.getActiveSheet();
  241. const resultSpreadSetting = {
  242. cols: [
  243. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 300, formatter: '@', readOnly: true, cellType: 'ellipsisAutoTip'}
  244. ],
  245. emptyRows: 0,
  246. headRows: 1,
  247. headRowHeight: [32],
  248. defaultRowHeight: 21,
  249. headerFont: '12px 微软雅黑',
  250. font: '12px 微软雅黑',
  251. headColWidth: [30],
  252. getColor: function (sheet, data, row, col, defaultColor) {
  253. if (data) {
  254. return data.invalid ? '#ddd' : defaultColor;
  255. } else {
  256. return defaultColor;
  257. }
  258. }
  259. };
  260. if (this.setting.type === 'compare' && this.setting.dataType === 'stage') {
  261. resultSpreadSetting.cols.push({ title: '可选期', colSpan: '1', rowSpan: '1', field: 'stage', hAlign: 0, width: 60 });
  262. }
  263. SpreadJsObj.initSheet(this.resultSheet, resultSpreadSetting);
  264. this.selectSpread.bind(spreadNS.Events.ButtonClicked, tsObj.tsButtonClicked);
  265. if (this.setting.type === 'compare' && this.setting.dataType === 'stage') {
  266. this.resultSpread.bind(spreadNS.Events.EditEnded, tsObj.trEditEnded);
  267. }
  268. $('#tender-select-multi-ok').click(() => {
  269. const selectData = tsObj.getSelectData();
  270. if (!selectData) return;
  271. this.setting.afterSelect(selectData);
  272. $('#tender-select-multi').modal('hide');
  273. });
  274. this.loadTenders();
  275. },
  276. };
  277. $('#tender-select-multi').on('shown.bs.modal', () => {
  278. tsObj.initTenderSelect();
  279. });
  280. const showSelect = function () {
  281. $('#tender-select-multi').modal('show');
  282. };
  283. return { showSelect }
  284. };