gljComponent.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. * Created by Zhong on 2017/8/15.
  3. */
  4. let gljComponentOprObj = {
  5. workBook: null,
  6. setting: {
  7. owner: "gljComponent",
  8. header:[
  9. {headerName:"编码",headerWidth:50,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
  10. {headerName:"名称",headerWidth:60,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
  11. {headerName:"计量单位",headerWidth:60,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
  12. {headerName:"单价",headerWidth:50,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
  13. {headerName:"消耗量",headerWidth:55,dataCode:"consumeAmt", dataType: "Number", formatter: "0.000", hAlign: "right", vAlign: "center"}
  14. ],
  15. view: {
  16. lockedCells:[1, 2, 3]
  17. }
  18. },
  19. buildSheet: function(container) {
  20. var me = gljComponentOprObj;
  21. me.workBook = sheetCommonObj.buildSheet(container, me.setting, 30, me);
  22. me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
  23. me.workBook.getSheet(0).setFormatter(-1, 0, "@", GC.Spread.Sheets.SheetArea.viewport);
  24. me.workBook.getSheet(0).options.isProtected = true;
  25. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  26. me.gljComponentDelOpr();
  27. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  28. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  29. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  30. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  31. /* me.repositoryGljDelOpr();
  32. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
  33. me.workBook.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
  34. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditStarting, me.onCellEditStart);
  35. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EditEnded, me.onCellEditEnd);
  36. me.workBook.getSheet(0).bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);*/
  37. },
  38. getRowData: function (sheet, row, setting) {
  39. let rst = {};
  40. for(let i = 0; i < setting.header.length; i++){
  41. rst[setting.header[i].dataCode] = sheet.getValue(row, i);
  42. }
  43. return rst;
  44. },
  45. getComponent: function (sheet, rowCount) {
  46. let component = [];
  47. for(let row = 0; row < rowCount; row++){
  48. let obj = {};
  49. obj.consumeAmt = sheet.getValue(row, 4);
  50. obj.ID = sheet.getTag(row, 0);
  51. component.push(obj);
  52. }
  53. return component;
  54. },
  55. gljComponentDelOpr: function () {
  56. let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [], removeArr = [], isUpdate = false;
  57. me.workBook.commandManager().register('gljComponentDel', function () {
  58. let sels = me.workBook.getSheet(0).getSelections();
  59. if(sels.length > 0 && that.currentComponent.length > 0){
  60. let component = that.currentGlj.component;
  61. for(let i = 0; i < sels.length > 0; i++){
  62. if(sels[i].colCount === me.setting.header.length){//可删除
  63. for(let j = 0; j < sels[i].rowCount; j++){
  64. if(sels[i].row + j < that.currentComponent.length){
  65. removeArr.push(that.currentComponent[sels[i].row + j].ID);
  66. }
  67. }
  68. }
  69. else if(sels[i].col === 0){
  70. //编码不可为空
  71. alert("编码不可为空!");
  72. }
  73. else if(sels[i].col === 4){//消耗量修改为0
  74. if(sels[i].row === -1){//全修改
  75. for(let j = 0; j < that.currentComponent.length; j++){
  76. isUpdate = true;
  77. that.currentComponent[j].consumeAmt = 0;
  78. for(let k = 0; k < component.length; k++){
  79. if(component[k].ID === that.currentComponent[j].ID){
  80. component[k].consumeAmt = 0;
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. else{//部分修改
  87. for(let j = 0; j < sels[i].rowCount; j++){
  88. if(sels[i].row + j < that.currentComponent.length){
  89. isUpdate = true;
  90. that.currentComponent[sels[i].row + j].consumeAmt = 0;
  91. for(let k = 0; k < component.length; k++){
  92. if(component[k].ID === that.currentComponent[sels[i].row + j].ID){
  93. component[k].consumeAmt = 0;
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. if(removeArr.length > 0 || isUpdate){
  103. for(let i = 0; i < removeArr.length; i++){
  104. for(let j = 0; j < that.currentComponent.length; j++){
  105. if(that.currentComponent[j].ID === removeArr[i]){
  106. that.currentComponent.splice(j--, 1);
  107. }
  108. }
  109. for(let j = 0; j < component.length; j++){
  110. if(component[j].ID === removeArr[i]){
  111. component.splice(j--, 1);
  112. }
  113. }
  114. }
  115. //重新计算工料机
  116. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  117. that.currentGlj.basePrice = gljBasePrc;
  118. that.reshowGljBasePrc(that.currentGlj);
  119. updateArr.push(that.currentGlj);
  120. console.log(updateArr);
  121. me.updateComponent(updateArr);
  122. }
  123. }
  124. });
  125. me.workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
  126. me.workBook.commandManager().setShortcutKey('gljComponentDel', GC.Spread.Commands.Key.del, false, false, false, false);
  127. },
  128. onCellEditStart: function(sender, args) {
  129. let me = gljComponentOprObj, that = repositoryGljObj;
  130. let rObj = me.getRowData(args.sheet, args.row, me.setting);
  131. me.currentEditingComponent = rObj;
  132. },
  133. onCellEditEnd: function (sender, args) {
  134. let me = gljComponentOprObj, that = repositoryGljObj;
  135. let gljList = that.gljList, updateArr = [], materialComponent = [202, 203, 204], machineComponent = [302, 303];
  136. if(args.editingText !== me.currentEditingComponent.code){
  137. if(args.col === 0 && args.editingText && args.editingText.trim().length > 0){
  138. let component = that.currentGlj.component, hasCode = false;
  139. for(let i = 0; i < gljList.length; i++){
  140. if(gljList[i].code === args.editingText){//有效的组成物
  141. hasCode = true;
  142. if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljList[i].gljType === 201)
  143. || (that.currentGlj.gljType === 3 && machineComponent.indexOf(gljList[i].gljType) !== -1 )){//普通材料
  144. //是否与原有组成物不同
  145. let isExist = false;
  146. for(let j = 0; j < component.length; j++){
  147. if(component[j].ID === gljList[i].ID){
  148. isExist = true;
  149. break;
  150. }
  151. }
  152. if(!isExist){
  153. let rObj = {};
  154. rObj.ID = gljList[i].ID;
  155. //rObj.basePrice = gljList[i].basePrice;
  156. if(typeof that.currentComponent[args.row] !== 'undefined'){
  157. rObj.consumeAmt = that.currentComponent[args.row].consumeAmt;
  158. let index;
  159. for(let j = 0; j < component.length; j++){
  160. if(component[j].ID === that.currentComponent[args.row].ID){
  161. index = j;
  162. break;
  163. }
  164. }
  165. component.splice(index, 1);
  166. component.splice(index, 0, rObj);
  167. updateArr.push(that.currentGlj);
  168. }
  169. else{
  170. rObj.consumeAmt = 0;
  171. component.push(rObj);
  172. updateArr.push(that.currentGlj);
  173. }
  174. break;
  175. }
  176. else{
  177. //已存在
  178. alert("已存在!");
  179. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  180. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  181. }
  182. }
  183. else{
  184. alert("无效!");
  185. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  186. me.currentEditingComponent[me.setting.header[args.col].dataCode]: '');
  187. //无效
  188. }
  189. }
  190. }
  191. if(!hasCode){
  192. alert("不存在,请添加!");
  193. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  194. me.currentEditingComponent[me.setting.header[args.col].dataCode] : '');
  195. //不存在
  196. }
  197. }
  198. else if(args.col === 4 && me.currentEditingComponent.code && args.editingText && args.editingText.trim().length > 0){//消耗量
  199. let consumeAmt = parseFloat(args.editingText);
  200. if(!isNaN(consumeAmt) && consumeAmt !== me.currentEditingComponent.consumeAmt){
  201. let component = that.currentGlj.component;
  202. for(let i = 0; i < component.length; i++){
  203. if(component[i].ID === that.currentComponent[args.row].ID){
  204. component[i].consumeAmt = consumeAmt
  205. }
  206. }
  207. that.currentComponent[args.row].consumeAmt = consumeAmt;
  208. //计算工料机单价
  209. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  210. that.currentGlj.basePrice = gljBasePrc;
  211. that.reshowGljBasePrc(that.currentGlj);
  212. updateArr.push(that.currentGlj);
  213. }
  214. else{
  215. //只能输入数值
  216. args.sheet.setValue(args.row, args.col, me.currentEditingComponent[me.setting.header[args.col].dataCode] ?
  217. me.currentEditingComponent[me.setting.header[args.col].dataCode]: 0);
  218. }
  219. }
  220. else{
  221. args.sheet.setValue(args.row, args.col, '');
  222. }
  223. }
  224. if(updateArr.length > 0){
  225. me.updateComponent(updateArr);
  226. //保存
  227. }
  228. },
  229. onClipboardPasting: function (sender, info) {
  230. },
  231. onClipboardPasted: function (sender, info) {
  232. let me = gljComponentOprObj, that = repositoryGljObj, updateArr = [] ,materialComponent = [202, 203, 204], machineComponent = [302, 303],
  233. component = that.currentGlj.component, newComponent = [], concatComponent = [], isChange = false;
  234. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  235. let gljCache = that.gljList;
  236. if(info.cellRange.col === 0){
  237. for(let i = 0; i < items.length; i++){
  238. for(let j = 0; j < gljCache.length; j++){
  239. if(items[i].code === gljCache[j].code){
  240. if((materialComponent.indexOf(that.currentGlj.gljType) !== -1 && gljCache[j].gljType === 201)
  241. || (that.currentGlj.gljType === 3 && machineComponent.indexOf(gljCache[j].gljType) !== -1 )){
  242. //是否与原有组成物不同
  243. let isExist = false;
  244. for(let k = 0; k < component.length; k++){
  245. if(component[k].ID === gljCache[j].ID){
  246. isExist = true;
  247. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  248. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  249. break;
  250. }
  251. }
  252. if(!isExist){
  253. isChange = true;
  254. let obj = {};
  255. obj.ID = gljCache[j].ID;
  256. if(typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'){//更新
  257. obj.consumeAmt = that.currentComponent[info.cellRange.row + i].consumeAmt;
  258. let index;
  259. for(let k = 0; k < component.length; k++){
  260. if(that.currentComponent[info.cellRange.row + i].ID === component[k].ID){
  261. index = k;
  262. break;
  263. }
  264. }
  265. component.splice(index, 1);
  266. component.splice(index, 0, obj);
  267. }
  268. else{//新增
  269. obj.consumeAmt = 0;
  270. component.push(obj);
  271. }
  272. break;
  273. }
  274. }
  275. else{
  276. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  277. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  278. }
  279. }
  280. else{
  281. me.workBook.getSheet(0).setValue(info.cellRange.row + i, info.cellRange.col,
  282. typeof that.currentComponent[info.cellRange.row + i] !== 'undefined'? that.currentComponent[info.cellRange.row + i].code : '');
  283. }
  284. }
  285. }
  286. if(isChange){
  287. updateArr.push(that.currentGlj);
  288. }
  289. }
  290. else if(info.cellRange.col === 4){
  291. let items = sheetCommonObj.analyzePasteData(me.setting, info);
  292. let row = info.cellRange.row;
  293. for(let i = 0; i < items.length; i++){
  294. if(row + i < that.currentComponent.length){
  295. let currentObj = that.currentComponent[row + i];
  296. if(items[i].consumeAmt.trim().length > 0 && items[i].consumeAmt !== currentObj.consumeAmt){
  297. isChange = true;
  298. currentObj.consumeAmt = items[i].consumeAmt;
  299. for(let j = 0; j < component.length; j++){
  300. if(component[j].ID === currentObj.ID){
  301. component[j].consumeAmt = currentObj.consumeAmt;
  302. break;
  303. }
  304. }
  305. }
  306. else{
  307. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, currentObj.consumeAmt);
  308. }
  309. }
  310. else{
  311. me.workBook.getSheet(0).setValue(row + i, info.cellRange.col, '');
  312. }
  313. }
  314. if(isChange){
  315. //计算工料机单价
  316. let gljBasePrc = me.reCalGljBasePrc(that.currentComponent);
  317. that.currentGlj.basePrice = gljBasePrc;
  318. that.reshowGljBasePrc(that.currentGlj);
  319. updateArr.push(that.currentGlj);
  320. }
  321. }
  322. if(updateArr.length > 0){
  323. me.updateComponent(updateArr);
  324. }
  325. },
  326. updateComponent: function (updateArr) {
  327. let me = gljComponentOprObj, that = repositoryGljObj;
  328. $.ajax({
  329. type: 'post',
  330. url: 'api/updateComponent',
  331. data: {libId: pageOprObj.gljLibId, updateArr: updateArr, oprtor: userAccount},
  332. dataType: 'json',
  333. success: function (result) {
  334. console.log(result);
  335. if(result.data.length > 0){
  336. if(result.data[0]){
  337. that.currentComponent = that.getCurrentComponent(result.data[0].component);
  338. console.log(that.currentComponent);
  339. sheetCommonObj.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
  340. sheetCommonObj.showData(me.workBook.getSheet(0), me.setting, that.currentComponent);
  341. }
  342. }
  343. }
  344. })
  345. },
  346. round: function (v, e) {
  347. var t=1;
  348. for(;e>0;t*=10,e--);
  349. for(;e<0;t/=10,e++);
  350. return Math.round(v*t)/t;
  351. },
  352. reCalGljBasePrc: function (component) {
  353. let me = gljComponentOprObj, gljBasePrc = 0;
  354. for(let i = 0; i < component.length; i++){
  355. gljBasePrc += me.round(component[i].basePrice * component[i].consumeAmt, 2);
  356. }
  357. return gljBasePrc;
  358. }
  359. };