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: 1},
  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. console.log(response.data);
  117. // 设置数据
  118. self.sheetObj.setData(response.data);
  119. self.specialColumn(response.data);
  120. } else {
  121. self.sheetObj.setData(null);
  122. alert('不存在对应数据');
  123. }
  124. }
  125. });
  126. };
  127. /**
  128. * 设置特殊单元格数据
  129. *
  130. * @param {object} sourceData
  131. * @return {void}
  132. */
  133. CompositionSpread.prototype.specialColumn = function(sourceData) {
  134. let rowCounter = 0;
  135. // 获取市场单价列号
  136. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  137. let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');
  138. let activeSheet = this.sheetObj.getSheet();
  139. for(let data of sourceData) {
  140. // 把消耗量从对象中抽离出来
  141. if (data.ratio_data.consumption !== undefined) {
  142. activeSheet.setValue(rowCounter, consumptionColumn, data.ratio_data.consumption);
  143. activeSheet.setValue(rowCounter, idColumn, data.ratio_data.id);
  144. }
  145. rowCounter++;
  146. }
  147. };
  148. /**
  149. * 更新组成物消耗量或用量
  150. *
  151. * @param {Object} info
  152. * @param {function} callback
  153. * return {void}
  154. */
  155. CompositionSpread.prototype.updateConsumption = function(info, callback) {
  156. // 获取修改的数据
  157. let column = info.col;
  158. let row = info.row;
  159. let field = this.sheetObj.getColumnField(column);
  160. if (field === '') {
  161. return false;
  162. }
  163. // 防止快速同时提交
  164. if (this.isChanging) {
  165. return false;
  166. }
  167. let activeSheet = this.sheetObj.getSheet();
  168. // 校验数据
  169. let value = info.newValue;
  170. if (!this.sheetObj.checkData(column, value)) {
  171. alert('数据格式错误,请重新输入!');
  172. activeSheet.setValue(row, column, info.oldValue);
  173. return false;
  174. }
  175. // 获取id
  176. let idColumn = this.sheetObj.getFieldColumn('mix_ratio_id');
  177. if (idColumn < 0) {
  178. return false;
  179. }
  180. let id = activeSheet.getValue(row, idColumn);
  181. let [parentMarketPrice, parentBasePrice] = this.getCompositionSumPrice('modify', row, info.newValue);
  182. let self = this;
  183. $.ajax({
  184. url: '/glj/update',
  185. type: 'post',
  186. data: {id: id, field: 'mix_ratio.' + field, value: value, market_price: parentMarketPrice, base_price: parentBasePrice},
  187. dataType: 'json',
  188. error: function() {
  189. alert('数据传输有误!');
  190. self.isChanging = false;
  191. activeSheet.setValue(row, column, info.oldValue);
  192. },
  193. beforeSend: function() {
  194. self.isChanging = true;
  195. },
  196. success: function(response) {
  197. self.isChanging = false;
  198. // 修改失败则恢复原值
  199. if (response.err !== 0) {
  200. activeSheet.setValue(row, column, info.oldValue);
  201. alert('更改数据失败!');
  202. } else {
  203. info.parentMarketPrice = parentMarketPrice;
  204. info.parentBasePrice = parentBasePrice;
  205. info.change = info.newValue - info.oldValue;
  206. callback(info);
  207. }
  208. }
  209. });
  210. };
  211. /**
  212. * 获取当前所有组成物累积的市场单价和基价单价
  213. * 用于同步修改父级市场单价和基价单价
  214. *
  215. * @param {String} scene
  216. * @param {Number} affectRow
  217. * @param {Number} newValue
  218. * @return {Array}
  219. */
  220. CompositionSpread.prototype.getCompositionSumPrice = function(scene, affectRow, newValue = 0) {
  221. let activeSheet = this.sheetObj.getSheet();
  222. // 计算父级3个价格
  223. let maxRow = activeSheet.getRowCount();
  224. // 获取对应列的列号
  225. let marketPriceColumn = this.sheetObj.getFieldColumn('unit_price.market_price');
  226. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  227. let basePriceColumn = this.sheetObj.getFieldColumn('unit_price.base_price');
  228. let parentMarketPrice = 0;
  229. let parentBasePrice = 0;
  230. for(let i = 0; i < maxRow; i++) {
  231. // 获取市场单价
  232. let marketPrice = activeSheet.getValue(i, marketPriceColumn);
  233. // 获取基价单价
  234. let basePrice = activeSheet.getValue(i, basePriceColumn);
  235. // 如果是删除则忽略即将被删除的行数据
  236. if (scene === 'delete' && affectRow === i) {
  237. continue;
  238. }
  239. // 获取消耗量(如果是当前修改的行则替换数据)
  240. let consumption = i === affectRow ? newValue : activeSheet.getValue(i, consumptionColumn);
  241. parentMarketPrice += consumption * marketPrice;
  242. parentBasePrice += consumption * basePrice;
  243. }
  244. parentMarketPrice = parentMarketPrice.toDecimal(2);
  245. parentBasePrice = parentBasePrice.toDecimal(2);
  246. return [parentMarketPrice, parentBasePrice]
  247. };
  248. /**
  249. * 删除组成物
  250. *
  251. * @param {Number} id
  252. * @param {Number} row
  253. * @param {function} callback
  254. * @return {void | boolean}
  255. */
  256. CompositionSpread.prototype.deleteComposition = function (id, row, callback) {
  257. id = parseInt(id);
  258. if (isNaN(id) || id <= 0) {
  259. alert('参数错误!');
  260. }
  261. if (isDeleting) {
  262. return false;
  263. }
  264. let activeSheet = this.sheetObj.getSheet();
  265. // 获取当前行的消耗量
  266. let consumptionColumn = this.sheetObj.getFieldColumn('consumption');
  267. let consumption = activeSheet.getValue(row, consumptionColumn);
  268. let self = this;
  269. $.ajax({
  270. url: '/glj/delete-ratio',
  271. type: 'post',
  272. data: {id: id},
  273. dataType: 'json',
  274. error: function() {
  275. isDeleting = false;
  276. alert('服务器繁忙');
  277. },
  278. beforeSend: function() {
  279. isDeleting = true;
  280. },
  281. success: function(response) {
  282. if (response.err === 0) {
  283. // 计算同级的市场单价和基价单价
  284. let [parentMarketPrice, parentBasePrice] = self.getCompositionSumPrice('delete', row);
  285. let info = {
  286. parentMarketPrice: parentMarketPrice,
  287. parentBasePrice: parentBasePrice,
  288. change: -consumption
  289. };
  290. activeSheet.setValue(row, consumptionColumn, 0);
  291. callback(info);
  292. }
  293. }
  294. });
  295. };