ration_installation.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Created by Zhong on 2018/1/25.
  3. */
  4. let rationInstObj = {
  5. IDMapping: null, //ID - name
  6. feeItem: null, //name - ID
  7. sheet: null,
  8. rationRepId: null,
  9. curRation: null,
  10. cache: [],
  11. setting: {
  12. header:[
  13. {headerName:"费用项",headerWidth:200,dataCode:"feeItem", dataType: "String", hAlign: 'left'},
  14. {headerName:"分册章节",headerWidth:400,dataCode:"section", dataType: "String", hAlign: 'left'}
  15. ],
  16. view:{
  17. comboBox:[],
  18. lockColumns:[]
  19. },
  20. },
  21. buildSheet: function(sheet) {
  22. let me = this;
  23. me.sheet = sheet;
  24. me.sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
  25. me.rationRepId = parseInt(pageOprObj.rationLibId); // 不可靠,有时取不到
  26. if (me.rationRepId == undefined){me.rationRepId = parseInt(getQueryString('repository'))};
  27. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  28. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  29. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  30. me.sheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
  31. me.sheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStarting);
  32. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  33. //右键
  34. // me.onContextmenuOpr();
  35. },
  36. renderFunc: function (sheet, func) {
  37. sheet.suspendPaint();
  38. sheet.suspendEvent();
  39. if(func){
  40. func();
  41. }
  42. sheet.resumePaint();
  43. sheet.resumeEvent();
  44. },
  45. isDef: function (v) {
  46. return v !== undefined && v !== null;
  47. },
  48. initInstallation: function (installationList) {
  49. //建立name - ID 映射, ID - name 映射
  50. this.feeItem = {};
  51. this.IDMapping = {feeItem: {}, section: {}};
  52. installationList.forEach(item => {
  53. this.feeItem[item.feeItem] = {ID: item.ID, section: {}};
  54. this.IDMapping.feeItem[item.ID] = item.feeItem;
  55. item.section.forEach(section => {
  56. this.feeItem[item.feeItem].section[section.name] = section.ID;
  57. this.IDMapping.section[section.ID] = section.name;
  58. });
  59. });
  60. },
  61. getFeeItemCombo: function () {
  62. let feeItemArr = [];
  63. for(let name in this.feeItem){
  64. feeItemArr.push(name);
  65. }
  66. let combo = sheetCommonObj.getDynamicCombo();
  67. combo.items(feeItemArr).itemHeight(10).editable(false);
  68. return combo;
  69. },
  70. getSectionCombo: function (feeItem) {
  71. let sectionObjs = this.feeItem[feeItem]['section'];
  72. let sectionArr = [];
  73. for(let name in sectionObjs){
  74. sectionArr.push(name);
  75. }
  76. let combo = sheetCommonObj.getDynamicCombo();
  77. combo.items(sectionArr).itemHeight(10).editable(false);
  78. return combo;
  79. },
  80. getInstItems: function (ration, callback) {
  81. let me = this, rst = [];
  82. me.curRation = ration;
  83. me.cache = ration.rationInstList;
  84. for(let inst of me.cache){
  85. let data = Object.create(null);
  86. let feeItem = me.IDMapping['feeItem'][inst.feeItemId];
  87. let section = me.IDMapping['section'][inst.sectionId];
  88. data.feeItem = me.isDef(feeItem) ? feeItem : '';
  89. data.section = me.isDef(section) ? section : '';
  90. rst.push(data);
  91. }
  92. me.showInstItems(rst);
  93. if(callback){
  94. callback();
  95. }
  96. },
  97. showInstItems: function (data) {
  98. let me = this;
  99. sheetCommonObj.showData(this.sheet, this.setting, data);
  100. //init combo
  101. this.renderFunc(this.sheet, function () {
  102. let feeIemCombo = me.getFeeItemCombo();
  103. let rowCount = me.sheet.getRowCount();
  104. for(let i = 0, len = rowCount.length; i < len; i++){
  105. me.getRange(i, -1, 1, -1).cellType(null);
  106. }
  107. me.sheet.getRange(-1, 0, -1, 1).cellType(feeIemCombo);
  108. for(let i = 0, len = data.length; i < len; i++){
  109. let sectionCombo = me.getSectionCombo(data[i].feeItem);
  110. me.sheet.getCell(i, 1).cellType(sectionCombo);
  111. }
  112. });
  113. },
  114. onEnterCell: function (sender, args) {
  115. args.sheet.repaint();
  116. },
  117. onEditStarting: function (sender, args) {
  118. let me = rationInstObj;
  119. if(!me.isDef(me.curRation)){
  120. args.cancel = true;
  121. return;
  122. }
  123. if(!me.isDef(me.cache[args.row]) && args.col === 1){
  124. args.cancel = true;
  125. return;
  126. }
  127. },
  128. onEditEnded: function (sender, args) {
  129. let me = rationInstObj;
  130. let v = me.isDef(args.editingText) ? args.editingText.toString().trim() : '';
  131. let field = me.setting.header[args.col]['dataCode'];
  132. let inst = me.cache[args.row];
  133. let toUpdate = false;
  134. //update
  135. if(me.isDef(inst)){
  136. if(field === 'feeItem'){
  137. let feeItemId = me.isDef(me.feeItem[v]) ? me.feeItem[v]['ID'] : null;
  138. if(feeItemId){
  139. inst.feeItemId = feeItemId;
  140. toUpdate = true;
  141. }
  142. else {
  143. me.sheet.setValue(args.row, args.col, me.IDMapping['feeItem'][inst.feeItemId]);
  144. return;
  145. }
  146. }
  147. else{
  148. let sectionId = me.isDef(me.feeItem[me.IDMapping['feeItem'][inst.feeItemId]]) ? me.feeItem[me.IDMapping['feeItem'][inst.feeItemId]]['section'][v] : null;
  149. inst.sectionId = sectionId;
  150. toUpdate = true;
  151. }
  152. }
  153. //insert
  154. else{
  155. let feeItemId = me.isDef(me.feeItem[v]) ? me.feeItem[v]['ID'] : null;
  156. if(feeItemId){
  157. let obj = {feeItemId: feeItemId, sectionId: null};
  158. me.cache.push(obj);
  159. toUpdate = true;
  160. }
  161. }
  162. if(toUpdate){
  163. me.updateRation(me.curRation, function () {
  164. me.getInstItems(me.curRation);
  165. });
  166. }
  167. },
  168. validUpdatePaste: function (rowData, data) {
  169. if(this.isDef(data.feeItem) && this.isDef(data.section) && data.feeItem !== '' && data.section !== ''){
  170. if(!this.isDef(this.feeItem[data.feeItem])){
  171. return false;
  172. }
  173. if(!this.isDef(this.feeItem[data.feeItem]['section'][data.section])){
  174. return false;
  175. }
  176. }
  177. else if(this.isDef(data.feeItem) && data.feeItem !== ''){
  178. if(!this.isDef(this.feeItem[data.feeItem])){
  179. return false;
  180. }
  181. }
  182. else if(this.isDef(data.section) && data.section !== ''){
  183. if(!this.isDef(this.feeItem[this.IDMapping['feeItem'][rowData.feeItemId]]['section'][data.section])){
  184. return false;
  185. }
  186. }
  187. return true;
  188. },
  189. validInsertPaste: function (data) {
  190. if(!this.isDef(data.feeItem) || data.feeItem == ''){
  191. return false;
  192. }
  193. if(!this.isDef(this.feeItem[data.feeItem])){
  194. return false;
  195. }
  196. if(this.isDef(data.section) && data.section !== '' && !this.isDef(this.feeItem[data.feeItem]['section'][data.section])){
  197. return false;
  198. }
  199. return true;
  200. },
  201. toSaveData: function (data, rowData = null) {
  202. let obj = {feeItemId: null, sectionId: null};
  203. if(this.isDef(data.feeItem) && data.feeItem !== ''){
  204. obj.feeItemId = this.isDef(this.feeItem[data.feeItem]) ? this.feeItem[data.feeItem]['ID'] : null;
  205. }
  206. else if(this.isDef(rowData)){
  207. obj.feeItemId = rowData.feeItemId;
  208. }
  209. if(this.isDef(data.section) && data.section !== ''){
  210. obj.sectionId = this.isDef(this.feeItem[this.IDMapping['feeItem'][obj.feeItemId]]['section'][data.section]) ?
  211. this.feeItem[this.IDMapping['feeItem'][obj.feeItemId]]['section'][data.section]: null;
  212. }
  213. return obj;
  214. },
  215. bindRationInstDel: function () {
  216. let me = this;
  217. let workBook = me.sheet.getParent();
  218. workBook.commandManager().register('rationInstDel', function () {
  219. let toUpdate = false;
  220. let sels = me.sheet.getSelections();
  221. for(let i = 0, len = sels.length; i < len; i++){
  222. let sel = sels[i];
  223. //delete
  224. if(sel.colCount === me.setting.header.length){
  225. for(let j = 0, jLen = sel.rowCount; j < jLen; j++){
  226. let row = sel.row + j;
  227. let inst = me.cache[row];
  228. //有数据,删除数据
  229. if(me.isDef(inst)){
  230. if(!toUpdate){
  231. toUpdate = true;
  232. }
  233. }
  234. }
  235. //front delete
  236. me.cache.splice(sel.row, sel.rowCount);
  237. }
  238. }
  239. if(toUpdate && me.curRation){
  240. me.updateRation(me.curRation, function () {
  241. me.getInstItems(me.curRation);
  242. });
  243. }
  244. });
  245. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  246. workBook.commandManager().setShortcutKey('rationInstDel', GC.Spread.Commands.Key.del, false, false, false, false);
  247. },
  248. onClipboardPasting: function (sender, info) {
  249. let me = rationInstObj;
  250. if(!me.isDef(me.curRation)){
  251. info.cancel = true;
  252. }
  253. },
  254. onClipboardPasted: function (sender, info) {
  255. let me = rationInstObj;
  256. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  257. let toUpdate = false;
  258. for(let i = 0, len = items.length; i < len; i++){
  259. let row = info.cellRange.row + i;
  260. //update
  261. let inst = me.cache[row];
  262. if(me.isDef(inst)){
  263. if(me.validUpdatePaste(inst, items[i])){
  264. let updateObj = me.toSaveData(items[i], inst);
  265. for(let attr in updateObj){
  266. inst[attr] = updateObj[attr];
  267. }
  268. toUpdate = true;
  269. }
  270. }
  271. //insert
  272. else{
  273. if(me.validInsertPaste(items[i])){
  274. me.cache.push(me.toSaveData(items[i]));
  275. toUpdate = true;
  276. }
  277. }
  278. }
  279. if(toUpdate){
  280. me.updateRation(me.curRation, function () {
  281. me.getInstItems(me.curRation);
  282. });
  283. }
  284. else {
  285. me.getInstItems(me.curRation);
  286. }
  287. },
  288. updateRation: function (ration,callback) {
  289. rationOprObj.mixUpdateRequest([ration], [], [], function () {
  290. if(callback){
  291. callback();
  292. }
  293. });
  294. }
  295. };