composition_spread.js 11 KB

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