project_controller.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**
  2. * Created by Mai on 2017/6/1.
  3. */
  4. ProjectController = {
  5. /* sc: tree_sheet_controller */
  6. syncDisplayNewNode: function (sc, newNode,callback) {
  7. TREE_SHEET_HELPER.massOperationSheet(sc.sheet, function () {
  8. var sels = sc.sheet.getSelections();
  9. sc.sheet.addRows(newNode.serialNo(), 1);
  10. TREE_SHEET_HELPER.refreshTreeNodeData(sc.setting, sc.sheet, [newNode], false);
  11. sc.setTreeSelected(newNode);
  12. sc.sheet.setSelection(newNode.serialNo(), sels[0].col, 1, 1);
  13. //不显示到中间
  14. //sc.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  15. cbTools.refreshFormulaNodes();
  16. if(callback) callback();
  17. });
  18. },
  19. syncDisplayNewNodes: function (sc, newNodes,withOutSelect=false) {//withOutSelect 不需要自动选中,外面自已处理
  20. TREE_SHEET_HELPER.massOperationSheet(sc.sheet, function () {
  21. var sels = sc.sheet.getSelections();
  22. newNodes.sort(function (a, b) {
  23. let rst = 0;
  24. if(a.serialNo() > b.serialNo()){
  25. rst = 1;
  26. }
  27. else {
  28. rst = -1;
  29. }
  30. return rst;
  31. });
  32. let lastNode = null;
  33. for(let newNode of newNodes){
  34. sc.sheet.addRows(newNode.serialNo(), 1);
  35. TREE_SHEET_HELPER.refreshTreeNodeData(sc.setting, sc.sheet, [newNode], false);
  36. lastNode = newNode
  37. // sc.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  38. }
  39. if(withOutSelect==false&& lastNode){
  40. sc.setTreeSelected(lastNode);
  41. sc.sheet.setSelection(lastNode.serialNo(), sels[0].col, 1, 1);
  42. }
  43. cbTools.refreshFormulaNodes();
  44. });
  45. },
  46. syncDisplayNewRationGljNode:function (sc,newNode) {
  47. TREE_SHEET_HELPER.massOperationSheet(sc.sheet, function () {
  48. sc.sheet.addRows(newNode.serialNo(), 1);
  49. TREE_SHEET_HELPER.refreshTreeNodeData(sc.setting, sc.sheet, [newNode], false);
  50. // sc.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  51. cbTools.refreshFormulaNodes();
  52. });
  53. },
  54. addBillsByData: async function (postData, isSameDepth = false) {
  55. if (!postData || !postData.length) {
  56. return [];
  57. }
  58. await ajaxPost('/bills/insertBills', { postData });
  59. // 插入
  60. const insertData = postData.filter(item => item.updateType === 'create');
  61. const treeData = insertData.map(item => item.updateData);
  62. // 插入清单节点和主树节点
  63. projectObj.project.Bills.datas = projectObj.project.Bills.datas.concat(treeData);
  64. let newNodes;
  65. if (isSameDepth) {
  66. const pre = postData.find(item => item.updateType === 'update');
  67. const preID = pre && pre.updateData.ID || null;
  68. projectObj.project.Bills.tree.multiInsert(treeData, preID);
  69. newNodes = projectObj.project.mainTree.multiInsert(treeData, preID);
  70. } else {
  71. projectObj.project.Bills.tree.insertByDatas(treeData);
  72. newNodes = projectObj.project.mainTree.insertByDatas(treeData);
  73. }
  74. for (const node of newNodes) {
  75. node.source = projectObj.project.Bills.tree.nodes[projectObj.project.Bills.tree.prefix + node.getID()];
  76. node.data = node.source.data;
  77. node.sourceType = projectObj.project.Bills.getSourceType();
  78. }
  79. ProjectController.syncDisplayNewNodes(projectObj.mainController, newNodes, true);
  80. return newNodes;
  81. },
  82. getBillsPostData: function (number, type) {
  83. const project = projectObj.project;
  84. const target = project.getParentTarget(project.mainTree.selected, 'sourceType', project.Bills.getSourceType());
  85. const targetType = target.data.type;
  86. let baseParentID;
  87. let baseNextID;
  88. let updateNode;
  89. const targetIsFXOrBX = targetType === billType.FX || targetType === billType.BX;
  90. const beLastChild = (type === billType.FX && targetType === billType.FB) ||
  91. (type === billType.BILL && target.depth() === 0);
  92. const beNextBrother = (type === billType.FX && targetIsFXOrBX) ||
  93. (type === billType.BILL && target.depth() > 0);
  94. if (beLastChild) {
  95. baseParentID = target.source.getID();
  96. baseNextID = -1;
  97. updateNode = target.source.children[target.source.children.length - 1];
  98. } else if (beNextBrother) {
  99. baseParentID = target.source.getParentID();
  100. baseNextID = target.source.getNextSiblingID();
  101. updateNode = target;
  102. } else {
  103. return [];
  104. }
  105. const insertData = [];
  106. for (let i = 0; i < number; i++) {
  107. const data = {
  108. //type: billType.BILL,
  109. type,
  110. projectID: project.ID(),
  111. ID: uuid.v1(),
  112. ParentID: baseParentID,
  113. };
  114. const pre = insertData[i - 1];
  115. if (pre) {
  116. pre.NextSiblingID = data.ID;
  117. }
  118. if (i === number - 1) {
  119. data.NextSiblingID = baseNextID;
  120. }
  121. insertData.push(data);
  122. }
  123. const postData = insertData.map(item => ({
  124. updateType: 'create',
  125. updateData: item
  126. }));
  127. if (updateNode) {
  128. postData.push({ updateType: 'update', updateData: { ID: updateNode.getID(), NextSiblingID: insertData[0].ID } });
  129. }
  130. return postData;
  131. },
  132. addBills: function (project, sheetController, std) {
  133. if (!project || !sheetController) { return null; }
  134. let target = project.getParentTarget(project.mainTree.selected, 'sourceType', project.Bills.getSourceType());
  135. let newSource = null, newNode = null, parentID, nextSiblingID, nodeParentID, nodeNextSiblingID;
  136. if (target) {
  137. if(target.depth() === 0){
  138. parentID = target.source.getID();
  139. nextSiblingID = project.Bills.tree.setting.rootId;
  140. nodeParentID = target.getID();
  141. nodeNextSiblingID = project.mainTree.rootID();
  142. }
  143. else{
  144. parentID = target.source.getParentID();
  145. nextSiblingID = target.source.getNextSiblingID();
  146. nodeParentID = target.getParentID();
  147. nodeNextSiblingID = target.getNextSiblingID();
  148. }
  149. parentID = target.depth() === 0 ? target.source.getID() : target.source.getParentID();
  150. nextSiblingID = target.depth() === 0 ? project.Bills.tree.setting.rootId : target.source.getNextSiblingID();
  151. if (std) {
  152. let fixedNode = getRootFixedNode(target);
  153. if(!isFlag(fixedNode.data) || (fixedNode.data.flagsIndex.fixed.flag !== fixedFlag.SUB_ENGINERRING && fixedNode.data.flagsIndex.fixed.flag !== fixedFlag.MEASURE)){
  154. return false;
  155. }
  156. //焦点行属于分部分项
  157. if(fixedNode.data.flagsIndex.fixed.flag === fixedFlag.SUB_ENGINERRING){
  158. std.type = billType.FX;
  159. //焦点行是分部分项
  160. let subType = getSubType(target);
  161. if(target.sourceType === project.Bills.getSourceType() && target.data.type === billType.DXFY && subType === billType.FB){
  162. return false;
  163. }
  164. //焦点行是分部
  165. else if(target.sourceType === project.Bills.getSourceType() && target.data.type === billType.FB){
  166. if(!subType || subType === billType.FX){
  167. parentID = target.source.getID();
  168. nextSiblingID = project.Bills.tree.setting.rootId;
  169. nodeParentID = target.getID();
  170. nodeNextSiblingID = project.mainTree.rootID();
  171. }
  172. else{
  173. return false;
  174. }
  175. }
  176. }
  177. else {
  178. std.type = billType.BILL;
  179. }
  180. let newCode = project.Bills.newFormatCode(std.code);
  181. newSource = project.Bills.insertStdBills(parentID, nextSiblingID, std, newCode);
  182. } else {
  183. newSource = project.Bills.insertBills(parentID, nextSiblingID);
  184. }
  185. newNode = project.mainTree.insert(nodeParentID, nodeNextSiblingID, newSource.data.ID);
  186. } else {
  187. alert('不可添加清单');
  188. }
  189. if (newNode) {
  190. newNode.source = newSource;
  191. newNode.sourceType = project.Bills.getSourceType();
  192. newNode.data = newSource.data;
  193. this.syncDisplayNewNode(sheetController, newNode);
  194. return newNode;
  195. }
  196. function getSubType(node){
  197. for(let sub of node.children){
  198. if(sub.sourceType === project.Bills.getSourceType() && sub.data.type === billType.FB){
  199. return billType.FB;
  200. }
  201. else if(sub.sourceType === project.Bills.getSourceType() && sub.data.type === billType.FX){
  202. return billType.FX;
  203. }
  204. }
  205. return null;
  206. }
  207. },
  208. addRootBill:function (project, sheetController) {//添加大项费用
  209. if (!project || !sheetController) { return null; }
  210. this.addSpecialBill(project, sheetController,null, project.mainTree.selected.nextSibling,true,billType.DXFY);
  211. },
  212. addFB:function(project, sheetController,node,ext) {//添加分部
  213. if (!project || !sheetController) { return null; }
  214. let selected =node|| project.mainTree.selected;
  215. if(selected.parent==null&&isFlag(selected.data)&&selected.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING){//选中的是分部分项,则插入做为最后一个子项
  216. return this.addSpecialBill(project, sheetController,selected, null,true,billType.FB,ext);
  217. }
  218. if(selected.parent){
  219. return this.addSpecialBill(project, sheetController,selected.parent, selected.nextSibling,true,billType.FB,ext);
  220. }
  221. },
  222. addFXParent:async function(node,ext){
  223. let datas = [];
  224. //let parent = node.parent.parent;
  225. let newBills = {
  226. 'ID':uuid.v1(),
  227. 'isAdd':1,
  228. 'type':billType.FB,
  229. 'projectID':node.data.projectID
  230. }
  231. if(ext) gljUtil.setProperty(newBills,ext);
  232. if(node.preSibling){//有前兄弟 在当前分项和前兄弟中间新增一行“分部”空行,空行作为分项的父项,原父项变为空行的前兄弟。
  233. let changeParent = false;
  234. newBills.ParentID = node.parent.parent?node.parent.parent.getID():-1;
  235. newBills.NextSiblingID = node.parent.data.NextSiblingID;
  236. datas.push({type:ModuleNames.bills,data:newBills,action:"add",preSiblingID:node.parent.data.ID});
  237. datas.push({type:ModuleNames.bills,data:{'ID':node.parent.getID(),'NextSiblingID':newBills.ID}})
  238. for(let c of node.parent.children){
  239. if(c.data.ID == node.data.ID) changeParent = true
  240. if(changeParent) datas.push({type:ModuleNames.bills,data:{'ID':c.getID(),'ParentID':newBills.ID}})
  241. }
  242. }else{//没有前兄弟,在当前分项和父项中间新增一行“分部”空行,空行作为分项的父项,原父项变为空行的父项。当前分项/补项的所有兄弟都跟着移动。
  243. newBills.ParentID = node.parent.getID();
  244. newBills.NextSiblingID = -1;
  245. datas.push({type:ModuleNames.bills,data:newBills,action:"add"});
  246. for(let c of node.parent.children){
  247. datas.push({type:ModuleNames.bills,data:{'ID':c.getID(),'ParentID':newBills.ID}})
  248. }
  249. }
  250. if(datas.length > 0){
  251. let nodes = await projectObj.project.syncUpdateNodesAndRefresh(datas);
  252. //重新计算
  253. cbTools.refreshFormulaNodes();
  254. projectObj.project.calcProgram.calcNodesAndSave(nodes);
  255. projectObj.mainController.setTreeSelected(projectObj.project.mainTree.getNodeByID(newBills.ID));
  256. }
  257. },
  258. addFX:function(project, sheetController) {//添加分项
  259. if (!project || !sheetController) { return null; }
  260. let selected = project.mainTree.selected;
  261. if(selected.data.type==billType.FB||(selected.parent==null&&isFlag(selected.data)&&selected.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING)){//选中的是分部或者是分部分项工程,则插入做为最后一个子项
  262. return this.addSpecialBill(project, sheetController,selected, null,true,billType.FX);
  263. }
  264. if(selected.parent){
  265. if(selected.data.type==billType.FX||selected.data.type==billType.BX){
  266. return this.addSpecialBill(project, sheetController,selected.parent, selected.nextSibling,true,billType.FX);
  267. }
  268. }
  269. },
  270. addSpecialBill(project,sheetController,parent,nextSibling,isUserAdd,type,ext){
  271. let newSource = null, newNode = null;
  272. let b_nexID = nextSibling==null?-1:nextSibling.source.getID();//主树和清单树,对应的树节点ID不一样
  273. let m_nexID = nextSibling==null?-1:nextSibling.getID();
  274. let b_parent = parent==null?-1:parent.source.getID();
  275. let m_parent = parent==null?-1:parent.getID();
  276. newSource = project.Bills.insertSpecialBill(b_parent, b_nexID,isUserAdd,type,ext);
  277. newNode = project.mainTree.insert(m_parent,m_nexID, newSource.data.ID);
  278. if (newNode) {
  279. newNode.source = newSource;
  280. newNode.sourceType = project.Bills.getSourceType();
  281. newNode.data = newSource.data;
  282. this.syncDisplayNewNode(sheetController, newNode);
  283. return newNode;
  284. }
  285. },
  286. addRation: function (project, sheetController, rationType, std) {
  287. if (!project || !sheetController) { return; }
  288. let selected = project.mainTree.selected, newSource = null, newNode = null;
  289. if (selected === null) { return; }
  290. if (selected.sourceType === project.Bills.getSourceType() && selected.depth() > 0) {
  291. if (selected.source.children.length > 0) {
  292. alert('当前清单已有清单子项,不能套用定额。');
  293. } else if (selected.data.calcBase&&selected.data.calcBase!="") {
  294. alert('当前有基数计算,不能插入定额/量价/人材机。');
  295. } else {
  296. if(selected.data.type === billType.FB){
  297. return;
  298. }
  299. if (std) {
  300. newSource = project.Ration.insertStdRation(selected.source.getID(), null, std);
  301. project.ration_glj.addRationGLJ(newSource,std);
  302. } else {
  303. newSource = project.Ration.insertRation(selected.source.getID(),null, rationType);
  304. }
  305. newNode = project.mainTree.insert(selected.getID(), selected.tree.rootID(), newSource.ID);
  306. }
  307. } else if (selected.sourceType === project.Ration.getSourceType()) {
  308. if (std) {
  309. newSource = project.Ration.insertStdRation(selected.source[project.masterField.ration], selected.source, std);
  310. project.ration_glj.addRationGLJ(newSource,std);
  311. } else {
  312. newSource = project.Ration.insertRation(selected.source[project.masterField.ration], selected.source, rationType);
  313. }
  314. newNode = project.mainTree.insert(selected.getParentID(), selected.getNextSiblingID(), newSource.ID);
  315. };
  316. if (newNode) {
  317. newNode.source = newSource;
  318. newNode.sourceType = project.Ration.getSourceType();
  319. newNode.data = newSource;
  320. this.syncDisplayNewNode(sheetController, newNode);
  321. }
  322. },
  323. replaceRation: function (project, sheetController, std) {
  324. if (!project || !sheetController) { return; }
  325. let selected = project.mainTree.selected, newSource = null, newNode = null;
  326. if (selected === null) { return; }
  327. if ( selected.sourceType === project.Ration.getSourceType()) {
  328. project.Ration.replaceRation(selected.source, std);
  329. project.ration_glj.addRationGLJ(selected.source, std);
  330. sheetController.refreshTreeNode([selected], false);
  331. } else {
  332. alert('当前焦点行不是定额,无法替换。');
  333. }
  334. },
  335. addNewNodes:function (updateData){
  336. let controller = projectObj.mainController;
  337. let Bill = projectObj.project.Bills;
  338. let newAddNode = [];
  339. let newRationNodes = [];
  340. for(let nb of updateData.bills.add){
  341. let newSource = Bill.tree.insertByData(nb, nb.ParentID, -1, true);//按顺序插入的情况下,nextID为-1,树节点
  342. let newNode = projectObj.project.mainTree.insert(nb.ParentID, -1, newSource.data.ID);
  343. newNode.source = newSource;
  344. newNode.sourceType = Bill.getSourceType();
  345. newNode.data = newSource.data;
  346. controller.sheet.addRows(newNode.serialNo(), 1);
  347. // controller.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  348. newAddNode.push(newNode);
  349. Bill.datas.push(nb);
  350. }
  351. for(let nr of updateData.ration.add){
  352. let newNode = projectObj.project.mainTree.insert(nr.billsItemID, -1, nr.ID);
  353. newNode.source = nr;
  354. newNode.sourceType = projectObj.project.Ration.getSourceType();
  355. newNode.data = nr;
  356. controller.sheet.addRows(newNode.serialNo(), 1);
  357. // controller.sheet.showRow(newNode.serialNo(), GC.Spread.Sheets.VerticalPosition.center);
  358. newAddNode.push(newNode);
  359. newRationNodes.push(newNode);
  360. projectObj.project.Ration.datas.push(nr);
  361. }
  362. TREE_SHEET_HELPER.refreshTreeNodeData(controller.setting, controller.sheet, newAddNode, false);
  363. return newRationNodes;
  364. }
  365. /* addVolumePrice: function (project, sheetController) {
  366. if (!project || !sheetController) { return null; }
  367. var selected = project.mainTree.selected;
  368. var newSource = null, newNode = null;
  369. if(selected === null) {
  370. return;
  371. }
  372. if (selected.sourceType === project.Bills.getSourceType() && selected.source.children.length === 0) {
  373. newSource = project.VolumePrice.insertVolumePrice(selected.source.getID());
  374. newNode = project.mainTree.insert(selected.getID(), selected.tree.rootID());
  375. } else if (selected.sourceType === project.Ration.getSourceType() || selected.sourceType === project.VolumePrice.getSourceType()) {
  376. newSource = project.VolumePrice.insertVolumePrice(selected.source[project.masterField.volumePrice], selected.source);
  377. newNode = project.mainTree.insert(selected.getParentID(), selected.getNextSiblingID());
  378. }
  379. if (newNode) {
  380. newNode.source = newSource;
  381. newNode.sourceType = project.VolumePrice.getSourceType();
  382. newNode.data = newSource;
  383. this.syncDisplayNewNode(sheetController, newNode);
  384. }
  385. },*/
  386. /* calculateAll: function (project, sheetController, CalcType) {
  387. this.project.setCalcFlag(CalcType);
  388. let calc = new BillsCalcHelper(project);
  389. calc.calcAll();
  390. sheetController.showTreeData();
  391. project.Bills.updateAll();
  392. calc = null;
  393. }*/
  394. }