tender_select_multi.js 12 KB

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