index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. let unitPriceObj = {
  2. unitPriceMap:{},
  3. setUntiPriceMap:function(){
  4. for(let u of unitPriceList){
  5. this.unitPriceMap[gljUtil.getIndex(u)] = u;
  6. }
  7. },
  8. mainSpread:null,
  9. mainSetting:{
  10. header: [
  11. {headerName: "编号", headerWidth: 80, dataCode: "code", dataType: "String"},
  12. {headerName: "名称", headerWidth: 160, dataCode: "name", dataType: "String"},
  13. {headerName: "单位", headerWidth: 45, dataCode: "unit", hAlign: "center", dataType: "String"},
  14. {headerName: "规格", headerWidth: 120, dataCode: "specs", hAlign: "left", dataType: "String"},
  15. {headerName: "类型", headerWidth: 45, dataCode: "short_name", hAlign: "center", dataType: "String"},
  16. {headerName: "定额价", headerWidth: 70, dataCode: "basePrice", hAlign: "right", dataType: "Number",validator:"number"},//decimalField:'glj.unitPrice',
  17. {headerName: "预算价", headerWidth: 70, dataCode: "marketPrice", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:"glj.unitPrice"
  18. {headerName: "是否新增", headerWidth: 50, dataCode: "is_add", hAlign: "center", dataType: "String",cellType:'checkBox'}
  19. ],
  20. view: {
  21. lockColumns: ["code","name","specs","unit","short_name","basePrice","is_add"],
  22. colHeaderHeight:36
  23. },
  24. getStyle:function (data,row,activeRow) {
  25. if(row == activeRow){//选中黄色显示
  26. return {backColor:"#FFFACD"};
  27. }
  28. return null;
  29. }
  30. },
  31. subSpread:null,
  32. subSetting:{
  33. header:[
  34. {headerName: "编号", headerWidth: 80, dataCode: "code", dataType: "String"},
  35. {headerName: "名称", headerWidth: 240, dataCode: "name", dataType: "String"},
  36. {headerName: "规格", headerWidth: 190, dataCode: "specs", dataType: "String"},
  37. {headerName: "单位", headerWidth: 45, dataCode: "unit", hAlign: "center", dataType: "String"},
  38. {headerName: "类型", headerWidth: 45, dataCode: "short_name", hAlign: "center", dataType: "String"},
  39. {headerName: "定额价", headerWidth: 80, dataCode: "basePrice", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:'glj.unitPrice'
  40. {headerName: "预算价", headerWidth: 80, dataCode: "marketPrice", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:"glj.unitPrice"
  41. {headerName: "消耗量", headerWidth: 80, dataCode: "consumption", hAlign: "right", dataType: "Number",validator:"number",tofix:3}
  42. ],
  43. view: {
  44. lockColumns: [0,1,2,3,4,5,6,7],
  45. rowHeaderWidth: 25
  46. }
  47. },
  48. initMainSpread:function () {
  49. if(this.mainSpread) return this.mainSpread.refresh();
  50. this.mainSpread = SheetDataHelper.createNewSpread($("#mainSpread")[0]);
  51. sheetCommonObj.spreadDefaultStyle(this.mainSpread);
  52. this.mainSheet = this.mainSpread.getSheet(0);
  53. sheetCommonObj.initSheet(this.mainSheet, this.mainSetting, 30);
  54. this.mainSheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onMainSelectionChange);
  55. this.mainSpread.bind(GC.Spread.Sheets.Events.RangeChanged, this.onMainRangeChange);
  56. this.mainSheet.bind(GC.Spread.Sheets.Events.ValueChanged, this.onMainValueChange);
  57. // this.sheet.bind(GC.Spread.Sheets.Events.EditStarting,this.onElectrovalenceEditStarting);
  58. /* if(projectReadOnly){
  59. disableSpread(this.spread);
  60. } */
  61. },
  62. showMainDatas:function(){
  63. unitPriceList = gljUtil.sortProjectGLJ(unitPriceList);
  64. this.setData(unitPriceList);
  65. let sel = this.mainSheet.getSelections()[0];
  66. let oldData = sel.row<unitPriceList.length?unitPriceList[sel.row]:"";
  67. sheetCommonObj.showData(this.mainSheet, this.mainSetting,unitPriceList);
  68. this.mainSheet.setRowCount(unitPriceList.length);
  69. sel.row = oldData?_.findIndex(unitPriceList,{'id':oldData.id}):sel.row ;
  70. this.mainSheet.setSelection(sel.row==-1?0:sel.row,sel.col,sel.rowCount,sel.colCount);
  71. },
  72. getShortNameByType : function (type) {
  73. return gljTypeMap["typeId" + type]?gljTypeMap["typeId" + type].shortName:'';
  74. },
  75. setData:function(list){
  76. for(let l of list){
  77. l.bgColour = "white";
  78. l.basePrice = this.getPrice('base_price',l);
  79. l.marketPrice = this.getPrice('market_price',l);
  80. l.short_name = this.getShortNameByType(l.type);
  81. if(l.base_price == l.market_price){//如果定额价等于市场价时,改底色。 优先度低于有组成物时的底色
  82. l.bgColour = "#C4CAFB";
  83. }
  84. let lindex = gljUtil.getIndex(l);
  85. if (gljUtil.notEditType.indexOf(l.type) >= 0 && mixRatioMap[lindex] && mixRatioMap[lindex].length>0) {//有组成物时
  86. l.bgColour = "#E0E0E0";
  87. }
  88. if(gljUtil.isConcreteType(l.type)) l.bgColour = "#E0E0E0";//混凝土、砂浆、配合比的底色显示为 灰色#E0E0E0,灰色底色提醒用户不可修改。
  89. }
  90. },
  91. getSelectedUnitPrice:function () {
  92. let me = this,data = null;
  93. let sheet = me.mainSpread.getActiveSheet();
  94. let sel = sheet.getSelections()[0];
  95. let srow = sel.row == -1||sel.row == ""?0:sel.row;
  96. if(unitPriceList.length>srow){
  97. data = unitPriceList[srow];
  98. }
  99. return data;
  100. },
  101. onMainSelectionChange:function(sender,args){
  102. let me = unitPriceObj;
  103. let newSel = args.newSelections[0];
  104. let oldSel = args.oldSelections?args.oldSelections[0]:{};
  105. args.sheet.suspendPaint();
  106. args.sheet.suspendEvent();
  107. if(newSel.row != oldSel.row){
  108. let style = me.getSelStyle(true,{});
  109. args.sheet.setStyle(newSel.row, -1, style);
  110. let orow = oldSel.row==''||oldSel.row==-1?0:oldSel.row;
  111. if(unitPriceList[orow]){
  112. let tstyle = me.getSelStyle(false,{},unitPriceList[orow].bgColour);
  113. args.sheet.setStyle(orow, -1, tstyle);
  114. me.showSubDatas();
  115. }
  116. }else{
  117. args.sheet.repaint();
  118. }
  119. args.sheet.resumeEvent();
  120. args.sheet.resumePaint();
  121. },
  122. onMainValueChange:function(e,info){
  123. let me = unitPriceObj;
  124. let value = info.newValue;
  125. if(info.newValue === undefined){
  126. return;
  127. }
  128. if(value && !sheetCommonObj.checkData(info.col,me.mainSetting,value)) {
  129. alert('输入的数据类型不对,请重新输入!');
  130. return me.showMainDatas();
  131. }
  132. me.batchUpdateUnitPrice([{row:info.row,col:info.col,value:value}]);
  133. },
  134. onMainRangeChange:function(sender,info){
  135. let me = unitPriceObj;
  136. let canChange = true;
  137. for(let c of info.changedCells){
  138. let value= info.sheet.getCell(c.row, c.col).text();
  139. changeInfo.push({row:c.row,col:c.col,value:value});
  140. if (!sheetCommonObj.checkData(c.col,me.mainSetting,value)) {
  141. alert('输入的数据类型不对,请重新输入!');
  142. canChange = false;
  143. break;
  144. }
  145. }
  146. if(canChange == false) return me.showMainDatas();
  147. me.batchUpdateUnitPrice(changeInfo);
  148. },
  149. batchUpdateUnitPrice:async function(changeInfo){
  150. let me = unitPriceObj;
  151. let updateData = [];
  152. let newValueMap = {};
  153. let refreshList = [];
  154. try {
  155. for(let ci of changeInfo){
  156. let dataCode = me.mainSetting.header[ci.col].dataCode;
  157. let recode = unitPriceList[ci.row];
  158. if(dataCode=='basePrice'||dataCode=='marketPrice'){
  159. let editField = dataCode === 'basePrice'?"base_price":"market_price";
  160. let newValue = ci.value;
  161. if(recode && recode[editField]!=newValue){
  162. newValue= scMathUtil.roundForObj(ci.value,3);
  163. updateData.push({unit_price: recode, field: editField, newval: newValue});
  164. newValueMap[recode.id]={field:editField,value:newValue};
  165. refreshList.push(recode);
  166. }
  167. }
  168. }
  169. if(updateData.length > 0){
  170. $.bootstrapLoading.start();
  171. let result = await ajaxPost("/glj/batchUpdatePrices",updateData);
  172. for(let r of refreshList){
  173. r[newValueMap[r.id].field] = newValueMap[r.id].value;
  174. }
  175. for(let r of result){
  176. let pdata = r.updateOne.filter;
  177. let set = r.updateOne.update.$set;
  178. for(let skey in set){
  179. let pindex = gljUtil.getIndex(pdata);
  180. me.unitPriceMap[pindex][skey] = set[skey];
  181. }
  182. }
  183. }
  184. } catch (error) {
  185. alert(error);
  186. }
  187. $.bootstrapLoading.end();
  188. me.showMainDatas();
  189. },
  190. initSubSpread:function () {
  191. if(this.subSpread) return this.subSpread.refresh();
  192. this.subSpread = SheetDataHelper.createNewSpread($("#subSpread")[0]);
  193. sheetCommonObj.spreadDefaultStyle(this.subSpread);
  194. this.subSheet = this.subSpread.getSheet(0);
  195. sheetCommonObj.initSheet(this.subSheet, this.subSetting, 30);
  196. // this.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onElectrovalenceSelectionChange);
  197. // this.sheet.bind(GC.Spread.Sheets.Events.ValueChanged, this.onElectrovalenceValueChange);
  198. // this.sheet.bind(GC.Spread.Sheets.Events.EditStarting,this.onElectrovalenceEditStarting);
  199. /* if(projectReadOnly){
  200. disableSpread(this.spread);
  201. } */
  202. },
  203. showSubDatas:function(){
  204. let parentData = this.getSelectedUnitPrice();
  205. this.mixRatioList = mixRatioMap[gljUtil.getIndex(parentData)];
  206. this.mixRatioList = this.mixRatioList?this.mixRatioList:[];
  207. this.setMixRatioData(this.mixRatioList);
  208. let sel = this.subSheet.getSelections()[0];
  209. this.subSheet.setRowCount(0);
  210. sheetCommonObj.showData(this.subSheet, this.subSetting,this.mixRatioList);
  211. this.subSheet.setRowCount(this.mixRatioList.length);
  212. this.subSheet.setSelection(sel.row==-1?0:sel.row,sel.col,sel.rowCount,sel.colCount);
  213. },
  214. setMixRatioData:function(mixRatioList){
  215. for(let m of mixRatioList){
  216. m.short_name = this.getShortNameByType(m.type);
  217. let mu = this.unitPriceMap[gljUtil.getIndex(m)];
  218. if(mu){
  219. m.basePrice = this.getPrice("base_price",mu);
  220. m.marketPrice = this.getPrice("market_price",mu);
  221. }else{
  222. console.log("组成物的单价信息未找到---"+m.code);
  223. }
  224. }
  225. },
  226. getPrice:function(pricefield,unitprice){
  227. let quantity = 3;
  228. let unitPriceHasMix = 2;
  229. let unitPrice = 3;
  230. let process_decimal = 6;
  231. let uIndex = gljUtil.getIndex(unitprice);
  232. if(mixRatioMap[uIndex] && mixRatioMap[uIndex].length > 0){
  233. let total = unitprice[pricefield];
  234. if(pricefield == "market_price"){
  235. total = 0;
  236. for(let m of mixRatioMap[uIndex]){
  237. let mu = unitPriceObj.unitPriceMap[gljUtil.getIndex(m)];
  238. let price_m = unitPriceObj.getPrice(pricefield,mu);
  239. let temP = scMathUtil.roundForObj(price_m * scMathUtil.roundForObj(m.consumption,quantity),process_decimal);
  240. total = scMathUtil.roundForObj(temP+total,process_decimal);
  241. }
  242. }
  243. return scMathUtil.roundForObj(unitprice[pricefield],unitPriceHasMix);
  244. }else{
  245. return scMathUtil.roundForObj(unitprice[pricefield],unitPrice);
  246. }
  247. },
  248. getSelStyle: function (selected,settingStyle,rcolor) {
  249. let style = new GC.Spread.Sheets.Style();
  250. if(settingStyle){
  251. for(let key in settingStyle){
  252. style[key] = settingStyle[key];
  253. }
  254. }
  255. style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  256. style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  257. style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  258. style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
  259. let selectedColor = "#FFFACD",
  260. recColor = rcolor?rcolor:'White';
  261. style.backColor = selected ? selectedColor : recColor;
  262. return style;
  263. },
  264. }
  265. function initPageHeight(){
  266. let headerHeight = $(".header").height();
  267. $(".main-data-top").height($(window).height()*0.6-headerHeight);
  268. $(".main-data-bottom").height($(window).height()-headerHeight-$(".main-data-top").height()-$(".nav-item").height());
  269. }
  270. function initPage(){
  271. $('[data-toggle="tooltip"]').tooltip({html: true});
  272. initPageHeight();
  273. unitPriceObj.initMainSpread();
  274. unitPriceObj.initSubSpread();
  275. unitPriceObj.showMainDatas();
  276. }
  277. unitPriceObj.setUntiPriceMap();
  278. initPage();
  279. $(window).resize(initPage);