block_controller.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /**
  2. * Created by zhang on 2018/5/10.
  3. */
  4. let BlockController = {
  5. datas:[],
  6. copyBtnDisable:function (selected) {
  7. if(this.isDXFYorMainEq(selected)){
  8. return true;
  9. }
  10. if(selected.sourceType == ModuleNames.ration && selected.data.type == rationType.install){
  11. return true;
  12. }
  13. return false;
  14. },
  15. pasteBtnDisable:function (selected) {
  16. let blockData = this.getBlockData();
  17. let Bills = projectObj.project.Bills;
  18. if(this.isDXFYorMainEq(selected)){
  19. return true;
  20. }
  21. if(blockData == null){
  22. return true;
  23. }
  24. if(blockData.firstNodeType != blockType.RATION && blockData.isFBFX != Bills.isFBFX(selected)){//除了复制定额外,焦点行和复制的块不是来自同一个地方(分部分项工其它)
  25. return true;
  26. }
  27. if(selected.sourceType == Bills.getSourceType() && selected.data.type == billType.FX && blockData.firstNodeType == blockType.FB){//焦点行是分项,复制块的第一层是分部,则无效。
  28. return true;
  29. }
  30. if(blockData.firstNodeType == blockType.RATION && selected.sourceType == Bills.getSourceType()){ //复制的是定额/量价/工料机、焦点行是分部/清单父项则无效
  31. if( selected.data.type == billType.FB ){
  32. return true;
  33. }
  34. if(MainTreeCol.readOnly.billsParent(selected)){
  35. return true;
  36. }
  37. }
  38. if(blockData.firstNodeType != blockType.RATION && selected.sourceType == ModuleNames.ration){
  39. return true;
  40. }
  41. return false;
  42. },
  43. isDXFYorMainEq:function (node) {//焦点行是大项费用或定额下的主材设备
  44. if(node.sourceType == ModuleNames.bills && node.data.type == billType.DXFY){//焦点行是大项费用则无效;
  45. return true;
  46. }
  47. if(node.sourceType == ModuleNames.ration_glj){// 焦点行是定额下的主材设备则无效;
  48. return true;
  49. }
  50. return false;
  51. },
  52. getBlockData:function () {
  53. let blockString =getLocalCache('project_block');
  54. if(blockString !=null){
  55. return JSON.parse(blockString);
  56. }
  57. return null;
  58. },
  59. copyBlock:function (selected) {
  60. let startTime = +new Date();
  61. let blockOjb = {
  62. compilationID:projectInfoObj.projectInfo.compilation
  63. };
  64. if(selected.sourceType == projectObj.project.Bills.getSourceType()){
  65. blockOjb.firstNodeType = selected.data.type;
  66. }else if(selected.sourceType == projectObj.project.Ration.getSourceType()){
  67. blockOjb.firstNodeType = blockType.RATION;
  68. }
  69. blockOjb.isFBFX = projectObj.project.Bills.isFBFX(selected);
  70. let ration_glj_Map = _.groupBy(projectObj.project.ration_glj.datas, 'rationID');
  71. blockOjb.data = this.getNodeDatas(selected,ration_glj_Map);
  72. let getDataTime = +new Date();
  73. console.log(`复制所用时间——${getDataTime - startTime}`);
  74. blockOjb.copyTime = +new Date();//设置复制时间,可以用来做过期处理
  75. setLocalCache('project_block',JSON.stringify(blockOjb));
  76. let endTime = +new Date();
  77. console.log(`复制所用时间——${endTime - startTime}`);
  78. },
  79. getNodeDatas : function (node,ration_glj_Map) {
  80. let data = node.data;
  81. data.sourceType = node.sourceType;
  82. if(node.sourceType == projectObj.project.Bills.getSourceType()){
  83. data.quantity_details = projectObj.project.quantity_detail.getDetailByBillID(data.ID);
  84. }
  85. if(node.sourceType == projectObj.project.Ration.getSourceType()){
  86. if(node.data.type == rationType.install){ //如果是生成的安装增加费,则跳过
  87. return null;
  88. }
  89. let gljList = ration_glj_Map[data.ID]?ration_glj_Map[data.ID]:[];//gljOprObj.filterGljByRation(node.data, datas);
  90. gljList = gljOprObj.combineWithProjectGlj(gljList,false);
  91. data.ration_gljs = gljList;
  92. data.ration_coes = projectObj.project.ration_coe.getCoeByRationID(data.ID);
  93. data.ration_installations = projectObj.project.ration_installation.getInstallationByRationID(data.ID);
  94. data.quantity_details = projectObj.project.quantity_detail.getDetailByRationID(data.ID);
  95. }else if(node.children.length>0){
  96. let children = [];
  97. for(let c of node.children){
  98. let tem_data = this.getNodeDatas(c,ration_glj_Map);
  99. tem_data?children.push(tem_data):'';
  100. }
  101. data.children = children;
  102. }
  103. return data;
  104. },
  105. pasteBlock:function (selected) {
  106. let blockData = this.getBlockData();
  107. let position = this.initOptionDiv(selected,blockData);
  108. if(position == null){
  109. return;
  110. }
  111. this.confirmPaste(blockData,selected,position);
  112. //do paste
  113. },
  114. initOptionDiv:function (selected,blockData) {
  115. let Bills = projectObj.project.Bills;
  116. if( blockData.firstNodeType == blockType.RATION){//如果剪切板中是复制的定额块
  117. if(selected.sourceType == ModuleNames.ration && selected.data.type != rationType.install ){//焦点行是定额/量价/工料机,则粘贴为后项
  118. return "next";
  119. }
  120. if(isFXorBX(selected)||(selected.sourceType === Bills.getSourceType()&& selected.source.children.length == 0)){//焦点行是分项/叶子清单,且无基数计算,则粘贴到分项/叶子清单下
  121. if(selected.data.calcBase ==null||selected.data.calcBase ==undefined|| selected.data.calcBase == ""){//无基数计算
  122. return "sub";
  123. }
  124. }
  125. }
  126. //设置选项的初始值
  127. setRadioProp('next_node',{checked:true,disabled:false});
  128. setRadioProp('pre_node',{checked:false,disabled:false});
  129. if(Bills.isFBFX(selected)){// (2.1)、焦点行属于“分部分项工程”:
  130. //复制块的第一层、焦点行的类型都是“分部”
  131. if(blockData.firstNodeType == blockType.FB && isFB(selected)){
  132. //默认为当前行的后项,可选前项、子项。
  133. setRadioProp('sub_node',{checked:false,disabled:false});
  134. }
  135. //复制块的第一层、焦点行的类型都是“分项”或补项
  136. if(blockIsFXorBX(blockData.firstNodeType) && isFXorBX(selected)){
  137. //默认为当前行的后项,可选前项,子项灰显不可选。
  138. setRadioProp('sub_node',{checked:false,disabled:true});
  139. }
  140. //复制块的第一层是分项,焦点行是分部,且分部下无子项或者子项是分项
  141. if(blockIsFXorBX(blockData.firstNodeType)&& isFB(selected)){
  142. if(selected.children.length == 0 || isFXorBX(selected.children[0])){
  143. return 'sub';//不弹出选择窗口,直接粘贴为子项。
  144. }
  145. }
  146. }else {//焦点行不属于“分部分项工程”
  147. if(blockData.firstNodeType == blockType.BILL &&selected.sourceType == Bills.getSourceType()){//如果剪切板中是从“分部分项工程”以外复制的清单块
  148. if(selected.children.length==0 || selected.children[0].sourceType == Bills.getSourceType()){//焦点行没有子项,或者子项是清单,判断是否有基数计算
  149. if(selected.data.calcBase && selected.data.calcBase!=""){//如果焦点行有基数计算,则子项灰显不可选。
  150. setRadioProp('sub_node',{checked:false,disabled:true});
  151. }else{//如果焦点行没有基数计算,则子项可选。
  152. setRadioProp('sub_node',{checked:false,disabled:false});
  153. }
  154. }else {
  155. setRadioProp('sub_node',{checked:false,disabled:true});
  156. }
  157. }
  158. }
  159. $("#pastePosition").modal({show:true});
  160. return null;
  161. function setRadioProp(id,options) {
  162. $("#"+id).prop("checked",options.checked);
  163. $("#"+id).prop("disabled",options.disabled);
  164. }
  165. function isFB(selected) {
  166. return selected.sourceType == Bills.getSourceType() && selected.data.type == billType.FB;
  167. }
  168. function isFXorBX(selected) {//是分项或者补项
  169. if(selected.sourceType == Bills.getSourceType()){
  170. return selected.data.type == billType.FX || selected.data.type == billType.BX;
  171. }
  172. return false;
  173. }
  174. function blockIsFXorBX(type) {
  175. return type == blockType.FX||blockType.BX;
  176. }
  177. },
  178. /**
  179. * @param blockData
  180. * @param selected
  181. * @param position next/pre/sub
  182. */
  183. confirmPaste:function (blockData,selected,position) {
  184. let Bills = projectObj.project.Bills;
  185. let parent = null,next = null,pre = null;
  186. let updateData = [],billUpdate = null;
  187. let billsIDMap = {};//用来做新旧ID映射
  188. if(blockData.compilationID != projectInfoObj.projectInfo.compilation){//如果编办不一样,不能复制
  189. alert("编办不一致,不能粘贴!");
  190. return;
  191. }
  192. if(position == 'next'){ //插入为选中节点的后项,即选中节点为前项
  193. parent = selected.parent;
  194. next = selected.nextSibling;
  195. pre = selected;
  196. }
  197. if(position == 'pre'){//插入为选中节点的前项,即选中节点为后项
  198. parent = selected.parent;
  199. next = selected;
  200. pre = selected.preSibling;
  201. }
  202. if(position == 'sub'){//即选中节点为父项
  203. parent = selected;
  204. pre = selected.lastChild();
  205. }
  206. let parentID = parent?parent.getID():-1;
  207. let nextID = next?next.getID():-1;
  208. if(blockData.firstNodeType == blockType.RATION){ //复制的是定额,需根据新的父项信息和工程量明细计算工程量
  209. blockData.data.billsItemID = parentID;
  210. let billsQuantity = scMathUtil.roundForObj(parent.data.quantity,getDecimal("quantity",parent));
  211. this.calcRationQuantityAndContain(billsQuantity,blockData.data);
  212. //如果粘贴位置不属于分部分项工程,或者不是安装工程,则把安装增加费内容置空
  213. if(!Bills.isFBFX(parent)|| projectInfoObj.projectInfo.property.engineering!=engineeringType.BUILD_IN){
  214. blockData.data.ration_installations = [];
  215. }
  216. //计算序列号
  217. this.calcRationSerialNo(parentID,blockData,updateData,pre,next);
  218. }else {
  219. blockData.data.ParentID = parentID;
  220. blockData.data.NextSiblingID=nextID;
  221. if(pre){
  222. billUpdate = {type:blockData.data.sourceType,query:{ID:pre.getID()},doc:{NextSiblingID:blockData.data.ID}};
  223. }
  224. }
  225. let dataMap = this.preparePasteData(blockData.data,billsIDMap);
  226. if(billUpdate){
  227. billUpdate.doc.NextSiblingID = billsIDMap[billUpdate.doc.NextSiblingID];
  228. updateData.push(billUpdate);
  229. }
  230. dataMap.updateData = updateData;
  231. console.log(dataMap);
  232. $.bootstrapLoading.start();
  233. CommonAjax.post('/bills/pasteBlock',dataMap,function (data) {
  234. $.bootstrapLoading.end();
  235. })
  236. // delete fees / feesIndex /__v
  237. },
  238. preparePasteData : function (data,billsIDMap) {
  239. let me = this;
  240. me.datas = _.cloneDeep(projectObj.project.Bills.datas);
  241. let bills = [],rations=[],ration_gljs = [],ration_coes = [],quantity_details = [],ration_installations = [];
  242. eachData(data);
  243. for(let b of bills){//更新ID
  244. billsIDMap[b.ParentID]?b.ParentID = billsIDMap[b.ParentID]:'';
  245. billsIDMap[b.NextSiblingID]?b.NextSiblingID = billsIDMap[b.NextSiblingID]:'';
  246. }
  247. return {bills:bills,rations:rations,ration_gljs:ration_gljs,ration_coes:ration_coes,quantity_details:quantity_details,ration_installations:ration_installations};
  248. function eachData(data) {
  249. if(data.sourceType == 'bills'){
  250. let tem_b = createBillsData(data);
  251. bills.push(tem_b);
  252. for(let d of data.quantity_details){
  253. quantity_details.push(createQuantityDetails(d,tem_b,'bills'));
  254. }
  255. if(data.children && data.children.length>0){
  256. for(let c of data.children){
  257. eachData(c);
  258. }
  259. }
  260. }
  261. if( data.sourceType == 'ration'){
  262. let tem_r = createRationData(data);
  263. rations.push(tem_r);
  264. for(let d of data.quantity_details){
  265. quantity_details.push(createQuantityDetails(d,tem_r, 'ration'));
  266. }
  267. for(let g of data.ration_gljs){
  268. let tem_rg = createSubList(g,tem_r);
  269. tem_rg.billsItemID = tem_r.billsItemID;
  270. ration_gljs.push(tem_rg);
  271. }
  272. for(let o of data.ration_coes){
  273. ration_coes.push(createSubList(o,tem_r));
  274. }
  275. for(let ri of data.ration_installations){
  276. ration_installations.push(createSubList(ri,tem_r));
  277. }
  278. }
  279. }
  280. function createSubList(subData,rationData) {
  281. let tem_sub = _.cloneDeep(subData);
  282. delete tem_sub._id;
  283. tem_sub.ID = uuid.v1();
  284. tem_sub.projectID = projectObj.project.ID();
  285. tem_sub.rationID = rationData.ID;
  286. return tem_sub;
  287. }
  288. function createQuantityDetails(detailData,pdata,type) {
  289. let tem_detail = _.cloneDeep(detailData);
  290. tem_detail.ID = uuid.v1();
  291. tem_detail.projectID = projectObj.project.ID();
  292. if(type == 'bills'){
  293. tem_detail.billID = pdata.ID;
  294. }
  295. if(type == 'ration'){
  296. tem_detail.rationID = pdata.ID;
  297. }
  298. return tem_detail;
  299. }
  300. function createRationData(rationData) {
  301. let tem_ration = _.cloneDeep(rationData);
  302. //删除旧数据
  303. delete tem_ration.fees;
  304. delete tem_ration.feesIndex;
  305. delete tem_ration.ration_gljs;
  306. delete tem_ration.ration_coes;
  307. delete tem_ration.ration_installations;
  308. delete tem_ration.quantity_details;
  309. delete tem_ration.__v;
  310. delete tem_ration.sourceType;
  311. tem_ration.projectID = projectObj.project.ID();
  312. tem_ration.ID = uuid.v1();
  313. billsIDMap[tem_ration.billsItemID]?tem_ration.billsItemID = billsIDMap[tem_ration.billsItemID]:'';
  314. let firstLibID = rationLibObj.getFirstStdRationLibID();
  315. if(firstLibID){
  316. tem_ration.prefix = projectObj.project.Ration.getRationPrefix(firstLibID,tem_ration);
  317. }
  318. return tem_ration;
  319. }
  320. function createBillsData(billsData) { //ID、重新生成code
  321. let temData = _.cloneDeep(billsData);
  322. //删除旧数据
  323. delete temData.fees;
  324. delete temData.feesIndex;
  325. delete temData.children;
  326. delete temData.quantity_details;
  327. delete temData.__v;
  328. delete temData.sourceType;
  329. temData.projectID = projectObj.project.ID();
  330. let newID = uuid.v1(); //新的清单ID
  331. billsIDMap[temData.ID] = newID;
  332. temData.ID = newID; //新的清单ID
  333. if(temData.billsLibId && temData.billsLibId!="" && temData.code.length == 12){//是从清单库来的
  334. let value = temData.code.substr(0,9);
  335. if (value&&value.length === 9 && /^[\d]+$/.test(value)) {
  336. temData.code = projectObj.project.Bills.newFormatCode(value);
  337. }
  338. }
  339. me.datas.push(temData);//为了下一个清单的std code 一样
  340. return temData;
  341. }
  342. },
  343. calcRationSerialNo:function (billsItemID,blockData,updateData,pre,next) {
  344. let br = projectObj.project.Ration.getBillsSortRation(billsItemID);
  345. if(next == null){ //没有下一树节点,即为最后节点
  346. blockData.data.serialNo = br.length > 0 ? br[br.length - 1].serialNo + 1 : 1;
  347. }else {//有下一节点
  348. let startIndex =0;
  349. if(pre){
  350. startIndex = br.indexOf(pre.data)+1;
  351. }
  352. blockData.data.serialNo = br[startIndex].serialNo;
  353. for(let i = startIndex;i < br.length; i++){
  354. let br_serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
  355. updateData.push({type:blockData.data.sourceType,query:{ID:br[i].ID},doc:{serialNo:br_serialNo}});//更新兄弟节点序列号
  356. }
  357. }
  358. },
  359. calcRationQuantityAndContain : function (billsQuantity,ration) {//计算定额工程量和含量
  360. let EXPString = ration.quantityEXP+"";
  361. if(EXPString.indexOf("QDL") != -1){
  362. if(EXPString=="QDL"){//定额的工程量是直接通过清单量填进来的;
  363. let times = parseInt(ration.unit);
  364. if(isNaN(times)){
  365. times = 1;
  366. }
  367. ration.quantity = scMathUtil.roundForObj(billsQuantity / times,getDecimal("ration.quantity"));
  368. billsQuantity?ration.contain = scMathUtil.roundForObj(ration.quantity/billsQuantity,getDecimal("process")):ration.contain = 0;
  369. } else {//如果定额的工程量是通过计算出来的,则应该重新计算。
  370. let tem_contain = scMathUtil.roundForObj(ration.contain,getDecimal("process"));
  371. let tem_quantity = scMathUtil.roundForObj(billsQuantity*tem_contain,getDecimal("ration.quantity")); //this.autoTransformQuantity(tem_quantity,rationNode);
  372. ration.quantity = tem_quantity;
  373. }
  374. }else {//GCLMXHJ
  375. let tem_contain=0;
  376. if(billsQuantity&&billsQuantity!=0){
  377. let children_quantity = scMathUtil.roundForObj(ration.quantity,getDecimal("ration.quantity"));
  378. // children_quantity = scMathUtil.roundForObj(this.reverseQuantity(children_quantity,rationNode),getDecimal("quantity",rationNode)); 原先是要反算的,现在改成不用反算了
  379. tem_contain =scMathUtil.roundForObj(children_quantity/billsQuantity,getDecimal("process"));
  380. }
  381. ration.contain = tem_contain;
  382. }
  383. },
  384. newFormatCode : function (stdCode, filterCode) {
  385. let matchs = this.sameStdCode(stdCode, filterCode);
  386. let format = function (Number) {
  387. let s = Number + '';
  388. while (s.length < 3) {
  389. s = '0' + s;
  390. }
  391. return s;
  392. };
  393. for (let i = 0; i <= matchs.length; i++) {
  394. let formatCode = stdCode + format(i+1);
  395. if (matchs.indexOf(formatCode) === -1) {
  396. return formatCode;
  397. }
  398. }
  399. },
  400. sameStdCode : function (stdCode, filterCode) {
  401. let reg = new RegExp('^' + stdCode), matchs= [];
  402. for (let data of this.datas) {
  403. if (data.code && data.code.length === 12 && reg.test(data.code) && data.code !== filterCode) {
  404. matchs.push(data.code);
  405. }
  406. }
  407. return matchs;
  408. },
  409. removeBlock:function () {
  410. removeLocalCache('project_block');
  411. }
  412. };