composition_spread.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * 组成物Spread
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/18
  6. * @version
  7. */
  8. /**
  9. * 构造函数
  10. *
  11. * @return {void}
  12. */
  13. function CompositionSpread () {
  14. this.isChanging = false;
  15. this.sheetObj = null;
  16. this.successCallback = null;
  17. this.rightClickTarget = null;
  18. }
  19. /**
  20. * 初始化
  21. *
  22. * @param {String} target
  23. * @return {Object}
  24. */
  25. CompositionSpread.prototype.init = function(target) {
  26. let name = target === 'machine' ? '用量' : '消耗量';
  27. let header = [
  28. {name: '编码', field: 'code', visible: true},
  29. {name: '名称', field: 'name', visible: true},
  30. {name: '单位', field: 'unit', visible: true},
  31. {name: 'ID', field: 'id', visible: false},
  32. {name: '类型', field: 'unit_price.type', visible: false},
  33. {name: '基价单价', field: "unit_price.base_price", visible: true},
  34. {name: '调整基价', field: 'adjust_price', visible: true},
  35. {name: '市场单价', field: "unit_price.market_price", visible: true},
  36. {name: name, field: 'consumption', visible: true, validator: 'number'},
  37. {name: 'CID', field: 'mix_ratio_id', visible: false},
  38. ];
  39. this.sheetObj = new CommonSpreadJs(header);
  40. this.sheetObj.init(target);
  41. // 获取列号
  42. let codeColumn = this.sheetObj.getFieldColumn('code');
  43. let unitColumn = this.sheetObj.getFieldColumn('unit');
  44. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  45. // 居中样式
  46. let centerStyleSetting = {hAlign: 1};
  47. this.sheetObj.setStyle(-1, codeColumn, centerStyleSetting);
  48. this.sheetObj.setStyle(-1, unitColumn, centerStyleSetting);
  49. // 设置可编辑列
  50. this.sheetObj.setColumnEditable(consumptionColumn);
  51. // 绑定事件
  52. let self = this;
  53. this.sheetObj.bind(GC.Spread.Sheets.Events.ValueChanged, function(element, info) {
  54. self.updateConsumption(info, self.successCallback);
  55. });
  56. return this.sheetObj;
  57. };
  58. /**
  59. * 初始化右键
  60. *
  61. * @param {String} target
  62. * @return {void}
  63. */
  64. CompositionSpread.prototype.initRightClick = function(target) {
  65. let activeSheet = this.sheetObj.getSheet();
  66. let self = this;
  67. $.contextMenu({
  68. selector: '#' + target,
  69. build: function ($trigger, e) {
  70. self.rightClickTarget = SheetDataHelper.safeRightClickSelection($trigger, e, self.sheetObj.spread);
  71. return self.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.viewport ||
  72. self.rightClickTarget.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
  73. },
  74. items: {
  75. "deleteMixRatio": {
  76. name: "删除",
  77. icon: 'fa-trash-o',
  78. disabled: function () {
  79. return self.rightClickTarget.row === undefined;
  80. },
  81. callback: function (key, opt) {
  82. let row = self.rightClickTarget.row;
  83. let idColumn = self.sheetObj.getFieldColumn('mix_ratio_id');
  84. let deleteId = activeSheet.getValue(row, idColumn);
  85. self.deleteComposition(deleteId, row, mixRatioSuccess);
  86. }
  87. },
  88. }
  89. });
  90. };
  91. /**
  92. * 获取组成物数据
  93. *
  94. * @param {Number} projectGLJid
  95. * @return {void | boolean}
  96. */
  97. CompositionSpread.prototype.getRatioData = function(projectGLJid) {
  98. projectGLJid = parseInt(projectGLJid);
  99. let self = this;
  100. if (isNaN(projectGLJid) || projectGLJid <= 0) {
  101. this.sheetObj.setData(null);
  102. return false;
  103. }
  104. $.ajax({
  105. url: '/glj/get-ratio',
  106. type: 'post',
  107. data: {id: projectGLJid, project_id: scUrlUtil.GetQueryString('project')},
  108. error: function() {
  109. self.sheetObj.setData(null);
  110. },
  111. beforeSend: function() {
  112. },
  113. success: function(response) {
  114. if (response.err === 0) {
  115. response.data = JSON.parse(response.data);
  116. // 设置数据
  117. self.sheetObj.setData(response.data);
  118. self.specialColumn(response.data);
  119. } else {
  120. self.sheetObj.setData(null);
  121. console.log('不存在对应数据');
  122. }
  123. }
  124. });
  125. };
  126. /**
  127. * 设置特殊单元格数据
  128. *
  129. * @param {object} sourceData
  130. * @return {void}
  131. */
  132. CompositionSpread.prototype.specialColumn = function(sourceData) {
  133. let rowCounter = 0;
  134. // 获取市场单价列号
  135. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  136. let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');
  137. let activeSheet = this.sheetObj.getSheet();
  138. for(let data of sourceData) {
  139. // 把消耗量从对象中抽离出来
  140. if (data.ratio_data.consumption !== undefined) {
  141. activeSheet.setValue(rowCounter, consumptionColumn, data.ratio_data.consumption);
  142. activeSheet.setValue(rowCounter, idColumn, data.ratio_data.id);
  143. }
  144. rowCounter++;
  145. }
  146. };
  147. /**
  148. * 更新组成物消耗量或用量
  149. *
  150. * @param {Object} info
  151. * @param {function} callback
  152. * return {void}
  153. */
  154. CompositionSpread.prototype.updateConsumption = function(info, callback) {
  155. // 获取修改的数据
  156. let column = info.col;
  157. let row = info.row;
  158. let field = this.sheetObj.getColumnField(column);
  159. if (field === '') {
  160. return false;
  161. }
  162. // 防止快速同时提交
  163. if (this.isChanging) {
  164. return false;
  165. }
  166. let activeSheet = this.sheetObj.getSheet();
  167. // 校验数据
  168. let value = info.newValue;
  169. if (!this.sheetObj.checkData(column, value)) {
  170. alert('数据格式错误,请重新输入!');
  171. activeSheet.setValue(row, column, info.oldValue);
  172. return false;
  173. }
  174. // 获取id
  175. let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');
  176. if (idColumn < 0) {
  177. return false;
  178. }
  179. let id = activeSheet.getValue(row, idColumn);
  180. let [parentMarketPrice, parentBasePrice] = this.getCompositionSumPrice('modify', row, info.newValue);
  181. let self = this;
  182. $.ajax({
  183. url: '/glj/update',
  184. type: 'post',
  185. data: {id: id, field: 'mix_ratio.' + field, value: value, market_price: parentMarketPrice, base_price: parentBasePrice},
  186. dataType: 'json',
  187. error: function() {
  188. alert('数据传输有误!');
  189. self.isChanging = false;
  190. activeSheet.setValue(row, column, info.oldValue);
  191. },
  192. beforeSend: function() {
  193. self.isChanging = true;
  194. },
  195. success: function(response) {
  196. self.isChanging = false;
  197. // 修改失败则恢复原值
  198. if (response.err !== 0) {
  199. activeSheet.setValue(row, column, info.oldValue);
  200. alert('更改数据失败!');
  201. } else {
  202. info.parentMarketPrice = parentMarketPrice;
  203. info.parentBasePrice = parentBasePrice;
  204. info.change = info.newValue - info.oldValue;
  205. callback(info);
  206. }
  207. }
  208. });
  209. };
  210. /**
  211. * 获取当前所有组成物累积的市场单价和基价单价
  212. * 用于同步修改父级市场单价和基价单价
  213. *
  214. * @param {String} scene
  215. * @param {Number} affectRow
  216. * @param {Number} newValue
  217. * @return {Array}
  218. */
  219. CompositionSpread.prototype.getCompositionSumPrice = function(scene, affectRow, newValue = 0) {
  220. let activeSheet = this.sheetObj.getSheet();
  221. // 计算父级3个价格
  222. let maxRow = activeSheet.getRowCount();
  223. // 获取对应列的列号
  224. let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');
  225. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  226. let basePriceColumn = this.sheetObj.getFieldColumn('unit_price.base_price');
  227. let parentMarketPrice = 0;
  228. let parentBasePrice = 0;
  229. for(let i = 0; i < maxRow; i++) {
  230. // 获取市场单价
  231. let marketPrice = activeSheet.getValue(i, marketPriceColumn);
  232. // 获取基价单价
  233. let basePrice = activeSheet.getValue(i, basePriceColumn);
  234. // 如果是删除则忽略即将被删除的行数据
  235. if (scene === 'delete' && affectRow === i) {
  236. continue;
  237. }
  238. // 获取消耗量(如果是当前修改的行则替换数据)
  239. let consumption = i === affectRow ? newValue : activeSheet.getValue(i, consumptionColumn);
  240. parentMarketPrice += consumption * marketPrice;
  241. parentBasePrice += consumption * basePrice;
  242. }
  243. parentMarketPrice = parentMarketPrice.toDecimal(2);
  244. parentBasePrice = parentBasePrice.toDecimal(2);
  245. return [parentMarketPrice, parentBasePrice]
  246. };
  247. /**
  248. * 删除组成物
  249. *
  250. * @param {Number} id
  251. * @param {Number} row
  252. * @param {function} callback
  253. * @return {void | boolean}
  254. */
  255. CompositionSpread.prototype.deleteComposition = function (id, row, callback) {
  256. id = parseInt(id);
  257. if (isNaN(id) || id <= 0) {
  258. alert('参数错误!');
  259. }
  260. if (isDeleting) {
  261. return false;
  262. }
  263. let activeSheet = this.sheetObj.getSheet();
  264. // 获取当前行的消耗量
  265. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  266. let consumption = activeSheet.getValue(row, consumptionColumn);
  267. let self = this;
  268. $.ajax({
  269. url: '/glj/delete-ratio',
  270. type: 'post',
  271. data: {id: id},
  272. dataType: 'json',
  273. error: function() {
  274. isDeleting = false;
  275. alert('服务器繁忙');
  276. },
  277. beforeSend: function() {
  278. isDeleting = true;
  279. },
  280. success: function(response) {
  281. if (response.err === 0) {
  282. // 计算同级的市场单价和基价单价
  283. let [parentMarketPrice, parentBasePrice] = self.getCompositionSumPrice('delete', row);
  284. let info = {
  285. parentMarketPrice: parentMarketPrice,
  286. parentBasePrice: parentBasePrice,
  287. change: -consumption
  288. };
  289. activeSheet.setValue(row, consumptionColumn, 0);
  290. callback(info);
  291. }
  292. }
  293. });
  294. };