tender_select_multi.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. if (selectData.length === 0) {
  139. toastr.warning('请选择标段');
  140. return;
  141. }
  142. if (this.setting.type === 'compare') {
  143. if (selectData.length !== 2) {
  144. toastr.warning('请选择两个标段进行对比');
  145. return;
  146. }
  147. if (this.setting.dataType === 'stage') {
  148. for (const s of selectData) {
  149. if (!s.stage) {
  150. toastr.warning('请选择标段进行对比的期');
  151. return;
  152. }
  153. s.stageInfo = { type: 'stage', stage: s.stage };
  154. }
  155. }
  156. }
  157. if (this.setting.type === 'gather' && this.setting.dataType === 'stage') {
  158. // todo 检查汇总选项
  159. const stage = { type: $('[name=tsm-source]:checked').val() };
  160. if (stage.type === 'stage') {
  161. stage.stage = _.toInteger($('#gather-stage').val()) || 0;
  162. if (!stage.stage) {
  163. toastr.warning('请选择 汇总期');
  164. return;
  165. }
  166. const validStage = _.min(_.map(tsObj.trArray, 'stageCount'));
  167. if (stage.stage > validStage) {
  168. toastr.warning('选择的期无效,请重新选择');
  169. return;
  170. }
  171. } else if (stage.type === 'month') {
  172. stage.month = $('#gather-month').val();
  173. if (stage.month === '') {
  174. toastr.warning('请选择 汇总年月');
  175. return;
  176. }
  177. } else if (stage.type === 'zone') {
  178. stage.zone = $('#gather-zone').val();
  179. if (stage.zone === '') {
  180. toastr.warning('请选择 汇总周期');
  181. return;
  182. } else if(stage.zone.indexOf(' - ') < 0) {
  183. toastr.warning('请选择 完整汇总周期');
  184. return;
  185. }
  186. } else if (stage.type === 'stage-zone') {
  187. const stageBegin = _.toInteger($('#gather-stage-begin').val()) || 0;
  188. const stageEnd = _.toInteger($('#gather-stage-end').val()) || 0;
  189. const validStage = _.max(_.map(tsObj.trArray, 'stageCount'));
  190. if (!stageBegin || !stageEnd) {
  191. toastr.warning('请选择 汇总开始期与结束期');
  192. return;
  193. }
  194. if (stageEnd <= stageBegin) {
  195. toastr.warning('结束期应大于开始期');
  196. return;
  197. }
  198. if (stageEnd > validStage) {
  199. toastr.warning('选择的期无效,请重新选择');
  200. return;
  201. }
  202. stage.stage_zone = stageBegin + ':' + stageEnd;
  203. } else if (stage.type === 'custom-zone') {
  204. stage.custom_zone = $('#gather-custom-zone').val();
  205. if (stage.custom_zone === '') {
  206. toastr.warning('请选择 汇总周期');
  207. return;
  208. } else if(stage.custom_zone.indexOf(' - ') < 0) {
  209. toastr.warning('请选择 完整汇总周期');
  210. return;
  211. }
  212. }
  213. selectData.forEach(s => { s.stageInfo = stage; });
  214. }
  215. return selectData;
  216. },
  217. initTenderSelect: function () {
  218. if (this.selectSpread) return;
  219. this.selectSpread = SpreadJsObj.createNewSpread($('#tsm-select-spread')[0]);
  220. this.selectSheet = this.selectSpread.getActiveSheet();
  221. SpreadJsObj.initSheet(this.selectSheet, {
  222. cols: [
  223. {title: '选择', field: 'selected', hAlign: 1, width: 40, formatter: '@', cellType: 'checkbox'},
  224. {title: '名称', field: 'name', hAlign: 0, width: 300, formatter: '@', cellType: 'tree'},
  225. {title: '期数', field: 'phase', hAlign: 1, width: 80, formatter: '@'},
  226. {title: '状态', field: 'status', hAlign: 1, width: 80, formatter: '@'}
  227. ],
  228. emptyRows: 0,
  229. headRows: 1,
  230. headRowHeight: [32],
  231. defaultRowHeight: 21,
  232. headerFont: '12px 微软雅黑',
  233. font: '12px 微软雅黑',
  234. headColWidth: [30],
  235. selectedBackColor: '#fffacd',
  236. readOnly: true,
  237. });
  238. this.resultSpread = SpreadJsObj.createNewSpread($('#tsm-result-spread')[0]);
  239. this.resultSheet = this.resultSpread.getActiveSheet();
  240. const resultSpreadSetting = {
  241. cols: [
  242. {title: '名称', colSpan: '1', rowSpan: '1', field: 'name', hAlign: 0, width: 300, formatter: '@', readOnly: true, cellType: 'ellipsisAutoTip'}
  243. ],
  244. emptyRows: 0,
  245. headRows: 1,
  246. headRowHeight: [32],
  247. defaultRowHeight: 21,
  248. headerFont: '12px 微软雅黑',
  249. font: '12px 微软雅黑',
  250. headColWidth: [30],
  251. getColor: function (sheet, data, row, col, defaultColor) {
  252. if (data) {
  253. return data.invalid ? '#ddd' : defaultColor;
  254. } else {
  255. return defaultColor;
  256. }
  257. }
  258. };
  259. if (this.setting.type === 'compare' && this.setting.dataType === 'stage') {
  260. resultSpreadSetting.cols.push({ title: '可选期', colSpan: '1', rowSpan: '1', field: 'stage', hAlign: 0, width: 60 });
  261. }
  262. SpreadJsObj.initSheet(this.resultSheet, resultSpreadSetting);
  263. this.selectSpread.bind(spreadNS.Events.ButtonClicked, tsObj.tsButtonClicked);
  264. if (this.setting.type === 'compare' && this.setting.dataType === 'stage') {
  265. this.resultSpread.bind(spreadNS.Events.EditEnded, tsObj.trEditEnded);
  266. }
  267. $('#tender-select-multi-ok').click(() => {
  268. const selectData = tsObj.getSelectData();
  269. if (!selectData) return;
  270. this.setting.afterSelect(selectData);
  271. $('#tender-select-multi').modal('hide');
  272. });
  273. this.loadTenders();
  274. },
  275. };
  276. $('#tender-select-multi').on('shown.bs.modal', () => {
  277. tsObj.initTenderSelect();
  278. });
  279. const showSelect = function () {
  280. $('#tender-select-multi').modal('show');
  281. };
  282. return { showSelect }
  283. };