ration_coe.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * Created by CSL on 2017-06-08.
  3. */
  4. //modified by zhong on 2017-09-25
  5. var rationCoeOprObj = {
  6. sheet: null,
  7. curRation: null,
  8. tempDelArr: [],
  9. cache: {},
  10. setting: {
  11. header:[
  12. {headerName:"编号",headerWidth:50,dataCode:"serialNo", dataType: "Number", hAlign: 'center'},
  13. {headerName:"名称",headerWidth:280,dataCode:"name", dataType: "String", hAlign: 'left'},
  14. {headerName:"内容",headerWidth:150,dataCode:"content", dataType: "String", hAlign: 'left'}
  15. ],
  16. view:{
  17. comboBox:[],
  18. lockColumns:[1,2]
  19. }
  20. },
  21. buildSheet: function(sheet) {
  22. var me = this;
  23. me.sheet = sheet;
  24. sheetCommonObj.initSheet(me.sheet, me.setting, 30);
  25. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  26. me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  27. me.sheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStarting);
  28. me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
  29. //右键
  30. //me.onContextmenuOpr();
  31. },
  32. isDef: function (v) {
  33. return v !== undefined && v !== null;
  34. },
  35. upMove: function (thisObj, preObj, cell) {
  36. let me = this;
  37. let tempNo = thisObj.no;
  38. thisObj.no = preObj.no;
  39. preObj.no = tempNo;
  40. //ajax
  41. me.updateCurRation(function () {
  42. me.showCoeItems(me.curRation.ID);
  43. me.sheet.setActiveCell(cell.row, cell.col);
  44. });
  45. },
  46. downMove: function (thisObj, nextObj, cell) {
  47. let me = this;
  48. let tempNo = thisObj.no;
  49. thisObj.no = nextObj.no;
  50. nextObj.no = tempNo;
  51. me.updateCurRation(function () {
  52. me.showCoeItems(me.curRation.ID);
  53. me.sheet.setActiveCell(cell.row, cell.col);
  54. });
  55. },
  56. onClipboardPasting: function(sender, args) {
  57. var me = rationCoeOprObj;
  58. let rationSection = rationOprObj.getCache();
  59. let rationRow = rationOprObj.workBook.getActiveSheet().getSelections()[0].row;
  60. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  61. if (args.cellRange.colCount != 1 || args.cellRange.col != 0 || !(me.curRation)) {
  62. args.cancel = true;
  63. }
  64. },
  65. onClipboardPasted: function(e, info) {
  66. var me = rationCoeOprObj;
  67. // 修改第一列(编号)
  68. if (info.cellRange.col == 0) {
  69. me.tempDelArr = [];
  70. var coeNos = [];
  71. var items = sheetCommonObj.analyzePasteData({header:[{dataCode: "serialNo"}] }, info);
  72. let curCache = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  73. let isRefresh = false;
  74. for(let i = 0, len = items.length; i < len; i++){
  75. if(!isNaN(items[i].serialNo)){
  76. let row = i + info.cellRange.row;
  77. //update
  78. if(row < curCache.length){
  79. let isExist = false;
  80. for(let j = 0, jLen = curCache.length; j < jLen; j++){
  81. if(items[i].serialNo === curCache[j].serialNo && j !== row){
  82. isExist = true;
  83. break;
  84. }
  85. }
  86. if(!isExist){
  87. me.tempDelArr.push({org: curCache[row], newNo: items[i].serialNo});
  88. coeNos.push(items[i].serialNo);
  89. }
  90. else{
  91. isRefresh = true;
  92. }
  93. }
  94. else{
  95. coeNos.push(items[i].serialNo);
  96. }
  97. }
  98. }
  99. //delete in front
  100. if(me.tempDelArr.length > 0){
  101. for(let i = 0, len = me.tempDelArr.length; i < len; i++){
  102. for(let j = 0; j < curCache.length; j++){
  103. if(me.tempDelArr[i].org.serialNo === curCache[j].serialNo){
  104. curCache.splice(j, 1);
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. me.addCoeItems(coeNos);
  111. if(isRefresh){
  112. me.showCoeItems(me.curRation.ID);
  113. }
  114. } else {
  115. //修改其它列。
  116. }
  117. },
  118. onEditStarting: function (sender, args) {
  119. let me = rationCoeOprObj;
  120. let rationSection = rationOprObj.getCache();
  121. let rationRow = rationOprObj.workBook.getActiveSheet().getSelections()[0].row;
  122. me.curRation = rationRow < rationSection.length ? rationSection[rationRow] : null;
  123. if(!me.curRation || args.col !== 0){
  124. args.cancel = true;
  125. }
  126. },
  127. onEditEnded: function(sender, args){
  128. var me = rationCoeOprObj;
  129. if (args.col == 0 && args.editingText && args.editingText.toString().trim().length > 0 && !isNaN(args.editingText)) { // 编号列
  130. let curCahe = typeof me.cache["_Coe_" + me.curRation.ID] !== 'undefined' ? me.cache["_Coe_" + me.curRation.ID] : [];
  131. me.tempDelArr = [];
  132. //update
  133. if(args.row < curCahe.length && args.editingText != curCahe[args.row].serialNo){
  134. let isExist = false;
  135. for(let i = 0, len = curCahe.length; i < len; i++){
  136. if(args.editingText == curCahe[i].serialNo){
  137. isExist = true;
  138. break;
  139. }
  140. }
  141. if(!isExist){
  142. me.tempDelArr.push({org: curCahe[args.row], newNo: args.editingText});
  143. curCahe.splice(args.row, 1);
  144. me.addCoeItems([args.editingText]);
  145. }
  146. else{
  147. me.showCoeItems(me.curRation.ID);
  148. }
  149. }
  150. //insert
  151. else{
  152. me.addCoeItems([args.editingText]);
  153. }
  154. }
  155. else{
  156. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  157. me.showCoeItems(me.curRation.ID);
  158. }
  159. },
  160. bindRationCoeDel: function () {
  161. let me = rationCoeOprObj;
  162. let workBook = me.sheet.getParent();
  163. workBook.commandManager().register('rationCoeDel', function () {
  164. let sels = me.sheet.getSelections(), isUpdate = false;
  165. let curCahe = me.cache["_Coe_" + me.curRation.ID];
  166. for(let i = 0, len = sels.length; i < len; i ++ ){
  167. if(sels[i].colCount === me.setting.header.length){
  168. if(sels[i].row < curCahe.length){
  169. isUpdate = true;
  170. curCahe.splice(sels[i].row, sels[i].rowCount);
  171. }
  172. }
  173. }
  174. if(isUpdate){
  175. me.updateCurRation(function () {
  176. me.sheet.getParent().focus(true);
  177. });
  178. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  179. me.showCoeItems(me.curRation.ID);
  180. }
  181. });
  182. workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  183. workBook.commandManager().setShortcutKey('rationCoeDel', GC.Spread.Commands.Key.del, false, false, false, false);
  184. },
  185. getRecoveryArr: function (tempDelArr, newArr) {//获得更新的coe不存在,恢复删除的被更新数据
  186. let rst = [];
  187. for(let i = 0, len = tempDelArr.length; i < len; i++){
  188. let isExist = false;
  189. for(let j = 0, jLen = newArr.length; j < jLen; j++){
  190. if(tempDelArr[i].newNo == newArr[j].serialNo){
  191. isExist = true;
  192. break;
  193. }
  194. }
  195. if(!isExist){
  196. rst.push(tempDelArr[i].org);
  197. }
  198. }
  199. return rst;
  200. },
  201. addCoeItems: function(coeNos) {
  202. var me = this;
  203. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  204. var curCache = me.cache["_Coe_" + me.curRation.ID];
  205. var temp = [];
  206. if (curCache) {
  207. for (var i = 0; i < coeNos.length; i++) {
  208. var isExist = false;
  209. for (let obj of curCache) {
  210. if (obj.serialNo == coeNos[i]) {
  211. isExist = true;
  212. break;
  213. };
  214. };
  215. if (!isExist) {
  216. temp.push(coeNos[i]);
  217. };
  218. };
  219. }else{
  220. for (let obj of coeNos){temp.push(obj)};
  221. };
  222. if(temp.length == 0){
  223. me.showCoeItems(me.curRation.ID);
  224. //sheetCommonObj.lockCells(me.sheet, me.setting);
  225. }else{
  226. $.ajax({
  227. type:"POST",
  228. url:"/complementaryRation/api/getCoeItemsByNos",
  229. data: {"data": JSON.stringify({"coeNos": temp})},
  230. dataType:"json",
  231. cache:false,
  232. timeout:5000,
  233. success:function(result){
  234. if (result) {
  235. var rstArr = [];
  236. let newNo = curCache && curCache.length > 0 ? curCache[curCache.length - 1].no + 1 : 1;
  237. for (let obj of result.data){
  238. obj.no = newNo++;
  239. rstArr.push(obj)
  240. }
  241. if (curCache) {
  242. curCache = curCache.concat(rstArr);
  243. }else{
  244. curCache = rstArr;
  245. }
  246. let recoveryArr = me.getRecoveryArr(me.tempDelArr, result.data);
  247. if(recoveryArr.length > 0){
  248. curCache = curCache.concat(recoveryArr);
  249. }
  250. me.cache["_Coe_" + me.curRation.ID] = curCache;
  251. me.updateCurRation(function () {
  252. me.sheet.getParent().focus(true);
  253. });
  254. me.showCoeItems(me.curRation.ID);
  255. };
  256. //sheetCommonObj.lockCells(me.sheet, me.setting);
  257. },
  258. error:function(err){
  259. alert(err);
  260. }
  261. });
  262. };
  263. },
  264. getCoeItems: function(ration, callback) {
  265. var me = this;
  266. me.curRation = ration;
  267. /*if (ration == undefined || ration.rationCoeList == undefined ||
  268. ration.rationCoeList.length == 0){return;};*/
  269. var coeList = ration.rationCoeList;
  270. let coeIDs = [];
  271. for(let i = 0, len = coeList.length; i < len; i++){
  272. coeIDs.push(coeList[i].ID);
  273. }
  274. var curCache = me.cache["_Coe_" + ration.ID];
  275. if (curCache) {
  276. me.showCoeItems(ration.ID);
  277. //sheetCommonObj.lockCells(me.sheet, me.setting);
  278. } else if(!curCache && typeof coeList !== 'undefined' && coeList.length > 0) {
  279. var data = {"coeIDs": coeIDs};
  280. $.ajax({
  281. type:"POST",
  282. url:"/complementaryRation/api/getCoeItemsByIDs",
  283. data: {"data": JSON.stringify(data)},
  284. dataType:"json",
  285. cache:false,
  286. timeout:5000,
  287. success:function(result){
  288. sheetCommonObj.cleanData(me.sheet, me.setting, -1);
  289. if (result.data) {
  290. var tempResult = [];
  291. for (let obj of result.data) {
  292. for(let i = 0, len = coeList.length; i < len; i++){
  293. if(obj.ID === coeList[i].ID){
  294. obj.no = coeList[i].no;
  295. tempResult.push(obj);
  296. break;
  297. }
  298. }
  299. };
  300. me.cache["_Coe_" + ration.ID] = tempResult;
  301. me.showCoeItems(ration.ID);
  302. }
  303. //sheetCommonObj.lockCells(me.sheet, me.setting);
  304. if(callback) callback();
  305. },
  306. error:function(err){
  307. alert(err);
  308. }
  309. });
  310. };
  311. },
  312. showCoeItems: function(rationID) {
  313. var me = this;
  314. var curCache = me.cache["_Coe_" + rationID];
  315. if (curCache) {
  316. curCache.sort(function(a, b) {
  317. var rst = 0;
  318. if (a.no > b.no) rst = 1
  319. else if (a.no < b.no) rst = -1;
  320. return rst;
  321. });
  322. sheetsOprObj.showData(me.sheet, me.setting, curCache);
  323. }
  324. },
  325. updateCurRation: function(callback) {
  326. var me = this, updateArr = [];
  327. if (me.curRation) {
  328. var rst = [];
  329. var curCache = me.cache["_Coe_" + me.curRation.ID];
  330. if (curCache) {
  331. for (let obj of curCache) {
  332. rst.push({ID: obj.ID, no: obj.no});
  333. }
  334. me.curRation.rationCoeList = rst;
  335. updateArr.push(me.curRation);
  336. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  337. if(callback) callback();
  338. });
  339. }
  340. }
  341. },
  342. //更新本节所有定额
  343. updateSectionRation: function (rations, coe, callback) {
  344. let me = this;
  345. let updateArr = [];
  346. for(let i = 0, len = rations.length; i < len; i++){
  347. if(rations[i].ID !== me.curRation.ID){
  348. console.log(`rations[i]`);
  349. console.log(rations[i]);
  350. let rationCoeList = rations[i].rationCoeList;
  351. let isExist = false;
  352. let newNo = 1;
  353. for(let j = 0, jLen = rationCoeList.length; j < jLen; j++){
  354. if(rationCoeList[j].no >= newNo){
  355. newNo = rationCoeList[j].no + 1;
  356. }
  357. if(rationCoeList[j].ID === coe.ID){
  358. isExist = true;
  359. }
  360. }
  361. if(!isExist){
  362. rationCoeList.push({ID: coe.ID, no: newNo});
  363. updateArr.push(rations[i]);
  364. }
  365. }
  366. console.log(rations[i]);
  367. }
  368. if(updateArr.length > 0){
  369. rationOprObj.mixUpdateRequest(updateArr, [], [], function () {
  370. if(callback) callback(updateArr);
  371. });
  372. }
  373. }
  374. }