project_glj.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * 工料机汇总相关
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/15
  6. * @version
  7. */
  8. let projectGLJSpread = null;
  9. let projectGLJSheet = null;
  10. // websocket所需
  11. let host = '';
  12. let socket = null;
  13. let roomId = 0;
  14. // 判定常量
  15. let materialIdList = [];
  16. let canNotChangeTypeId = [];
  17. let GLJTypeConst = [];
  18. // spreadjs载入数据所需
  19. let jsonData = [];
  20. let mixRatioConnectData = [];
  21. let mixRatioMap = {};
  22. // 单价文件相关
  23. let usedUnitPriceInfo = {};
  24. let usedTenderList = [];
  25. let otherFileData = {};
  26. let currentTag = '';
  27. let isChanging = false;
  28. let initPage = false;
  29. let projectGLJView={
  30. currentTag:''
  31. }
  32. $(document).ready(function () {
  33. $('#tab_gongliaoji').on('show.bs.tab', function (e) {
  34. $(e.relatedTarget.hash).removeClass('active');
  35. init();
  36. });
  37. slideResize($("#glj-main"), function () {
  38. projectGLJSpread.sheetObj.spread.refresh();
  39. });
  40. // 单价文件切换弹框
  41. $('#change-dj').on('shown.bs.modal', function () {
  42. // 获取当前建设项数据
  43. let projectName = projectInfoObj.projectInfo.fullFolder !== undefined &&
  44. projectInfoObj.projectInfo.fullFolder.length > 0 ? projectInfoObj.projectInfo.fullFolder[0] : '';
  45. $("#current-project-name").text(projectName);
  46. // 获取切换单价文件相关数据
  47. $.ajax({
  48. url: '/glj/get-project-info',
  49. type: 'post',
  50. data: {project_id: scUrlUtil.GetQueryString('project')},
  51. dataType: 'json',
  52. success: function (response) {
  53. if (response.err === 1) {
  54. alert('数据传输错误!');
  55. return false;
  56. }
  57. let data = response.data;
  58. // 本项目中的单价文件
  59. if (data.self.length > 0) {
  60. let selfFileHtml = '';
  61. for (let tmp of data.self) {
  62. let select = usedUnitPriceInfo === tmp.id ? ' selected="selected"' : '';
  63. selfFileHtml += '<option' + select + ' value="' + tmp.id + '">' + tmp.name + '</option>';
  64. }
  65. $("#self-file").html(selfFileHtml);
  66. }
  67. // 其他建设项目数据
  68. if (data.other.length > 0) {
  69. let otherProjectHtml = '';
  70. let otherFileHtml = '';
  71. for (let tmp of data.other) {
  72. otherProjectHtml += '<option value="' + tmp.ID + '">' + tmp.name + '</option>';
  73. otherFileData[tmp.ID] = tmp.unitPriceList;
  74. if (otherFileHtml !== '') {
  75. continue;
  76. }
  77. for (let unitPrice of tmp.unitPriceList) {
  78. otherFileHtml += '<option value="' + unitPrice.id + '">' + unitPrice.name + '</option>';
  79. }
  80. }
  81. $("#other-project").html(otherProjectHtml);
  82. $("#other-file").html(otherFileHtml);
  83. }
  84. }
  85. });
  86. });
  87. // 单价文件另存为弹框
  88. $("#file-save-as-dialog").on('shown.bs.modal', function () {
  89. // 获取当前建设项数据
  90. $("#save-as-name").val(usedUnitPriceInfo.name + '(复件)');
  91. });
  92. // 单价文件另存为操作
  93. $("#save-as-confirm").click(function () {
  94. let name = $("#save-as-name").val();
  95. if (name === '') {
  96. $("#save-as-tips").text('请填写单价文件名称').show();
  97. return false;
  98. }
  99. if (isChanging) {
  100. return false;
  101. }
  102. $.ajax({
  103. url: '/glj/save-as',
  104. type: 'post',
  105. data: {name: name, project_id: scUrlUtil.GetQueryString('project')},
  106. dataType: 'json',
  107. error: function () {
  108. isChanging = false;
  109. },
  110. beforeSend: function () {
  111. isChanging = true;
  112. },
  113. success: function (response) {
  114. isChanging = false;
  115. if (response.err === 1) {
  116. let msg = response.msg !== undefined && response.msg !== '' ? response.msg : '另存为失败!';
  117. $("#save-as-tips").text(msg).show();
  118. return false;
  119. }
  120. $("#file-save-as-dialog").modal("hide");
  121. }
  122. });
  123. });
  124. // 从其他建设项目中复制 选择建设项目
  125. $("#other-project").change(function () {
  126. let projectId = $(this).val();
  127. if (otherFileData[projectId] === undefined) {
  128. return false;
  129. }
  130. let otherFileHtml = '';
  131. for (let unitPrice of otherFileData[projectId]) {
  132. otherFileHtml += '<option value="' + unitPrice.id + '">' + unitPrice.name + '</option>';
  133. }
  134. $("#other-file").html(otherFileHtml);
  135. });
  136. // 单价文件选项切换
  137. $("input[name='change-type']").change(function () {
  138. let type = $(this).val();
  139. type = parseInt(type);
  140. $("#change-dj .option").hide();
  141. if (type === 0) {
  142. $(".option.select").show();
  143. } else {
  144. $(".option.copy").show();
  145. }
  146. });
  147. // 单价文件切换确认
  148. $("#change-file-confirm").click(function () {
  149. if (isChanging) {
  150. return false;
  151. }
  152. let type = $("input[name='change-type']:checked").val();
  153. type = parseInt(type);
  154. let changeUnitPriceId = 0;
  155. if (type === 0) {
  156. // 从本项目中选择
  157. changeUnitPriceId = $("#self-file").val();
  158. } else {
  159. // 从其他项目中复制
  160. changeUnitPriceId = $("#other-file").val();
  161. }
  162. $('#change-dj').modal("hide");
  163. $.bootstrapLoading.start();
  164. $.ajax({
  165. url: '/glj/change-file',
  166. type: 'post',
  167. data: {project_id: scUrlUtil.GetQueryString('project'), change_id: changeUnitPriceId, type: type},
  168. error: function () {
  169. isChanging = false;
  170. $.bootstrapLoading.end();
  171. },
  172. beforeSend: function () {
  173. isChanging = true;
  174. },
  175. success: function (response) {
  176. isChanging = false;
  177. if (response.err === 1) {
  178. let msg = response.msg !== undefined ? response.msg : '未知错误';
  179. alert(msg);
  180. $.bootstrapLoading.end();
  181. return false;
  182. }
  183. projectObj.project.projectGLJ.loadData(function () {
  184. let projectGLJ = projectObj.project.projectGLJ;
  185. projectGLJ.loadCacheData();
  186. unitPriceFileInit();
  187. gljOprObj.refreshView();
  188. $.bootstrapLoading.end();
  189. });
  190. }
  191. });
  192. });
  193. // 是否主动更改数据
  194. $("#message").on('click', '#load-data', function () {
  195. $("#message").html('正在加载...');
  196. // 重新加载数据到缓存
  197. projectObj.project.projectGLJ.loadData(function () {
  198. projectObj.project.projectGLJ.loadCacheData();
  199. $("#notify").slideUp('fast');
  200. });
  201. });
  202. $('#pop-dj').popover({
  203. placement: "bottom",
  204. html: true,
  205. trigger: "hover | focus",
  206. content: getUsedTenderInfo
  207. }
  208. );
  209. });
  210. /**
  211. * 初始化数据
  212. *
  213. * @return {void|boolean}
  214. */
  215. function init() {
  216. projectObj.project.projectGLJ.loadData(function (data) {
  217. if (initPage==false||jsonData.length <= 0) {
  218. // 赋值
  219. jsonData = data.gljList !== undefined && data.gljList.length > 0 ? data.gljList : [];
  220. jsonData = filterProjectGLJ(jsonData);
  221. jsonData = sortProjectGLJ(jsonData);
  222. mixRatioConnectData = data.mixRatioConnectData !== undefined ? data.mixRatioConnectData : mixRatioConnectData;
  223. mixRatioMap = data.mixRatioMap !== undefined ? data.mixRatioMap : mixRatioMap;
  224. host = data.constData.hostname !== undefined ? data.constData.hostname : '';
  225. materialIdList = data.constData.materialIdList !== undefined ? data.constData.materialIdList : materialIdList;
  226. roomId = data.constData.roomId !== undefined ? data.constData.roomId : roomId;
  227. canNotChangeTypeId = data.constData.ownCompositionTypes !== undefined ?
  228. data.constData.ownCompositionTypes : canNotChangeTypeId;
  229. GLJTypeConst = data.constData.GLJTypeConst !== undefined ? JSON.parse(data.constData.GLJTypeConst) : GLJTypeConst;
  230. // 连接socket服务器
  231. socketInit();
  232. unitPriceFileInit();
  233. setTimeout(spreadInit, 1);
  234. initPage=true;
  235. } else {
  236. projectObj.project.projectGLJ.loadCacheData();
  237. }
  238. });
  239. }
  240. /**
  241. * spreadjs相关初始化
  242. *
  243. * @return {void}
  244. */
  245. function spreadInit() {
  246. projectGLJSpread = new ProjectGLJSpread();
  247. projectGLJSpread.successCallback = successTrigger;
  248. projectGLJSheet = projectGLJSpread.init();
  249. // 绑定单击事件
  250. let lastRow = 0;
  251. projectGLJSheet.bind(GC.Spread.Sheets.Events.CellClick, function (element, info) {
  252. let currentRow = info.row;
  253. if (currentRow === undefined || currentRow === lastRow) {
  254. return;
  255. }
  256. if (currentTag !== 'mix-ratio' && currentTag !== 'machine') {
  257. return;
  258. }
  259. let spread = currentTag === 'mix-ratio' ? mixRatioSpread : machineSpread;
  260. if (spread === null) {
  261. return;
  262. }
  263. let projectGLJId = projectGLJSheet.getActiveDataByField('id');
  264. spread.getRatioData(projectGLJId);
  265. lastRow = currentRow;
  266. });
  267. // 切换tab触发refresh
  268. $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  269. if($(e.target).data('name')==undefined){
  270. return;
  271. }
  272. currentTag = $(e.target).data('name');
  273. if (currentTag === 'ration') {
  274. projectGLJSheet.filterData('unit_price.type', []);
  275. }
  276. });
  277. loadSize("glj-main", function () {
  278. projectGLJSpread.sheetObj.spread.refresh();
  279. });
  280. }
  281. /**
  282. * 单价文件相关初始化
  283. *
  284. * @param {String} name
  285. * @param {Array} data
  286. * @return {void}
  287. */
  288. function unitPriceFileInit() {
  289. let projectGLJ = projectObj.project.projectGLJ;
  290. let data = projectGLJ.datas;
  291. usedTenderList = data.usedTenderList !== undefined ? data.usedTenderList : [];
  292. usedUnitPriceInfo = data.constData.usedUnitPriceInfo !== undefined ?
  293. data.constData.usedUnitPriceInfo : {};
  294. $("#used-name").text(usedUnitPriceInfo.name);
  295. let usedCount = usedTenderList.length <= 0 ? 1 : usedTenderList.length;
  296. $("#used-count").text(usedCount);
  297. }
  298. function getUsedTenderInfo() {
  299. return usedTenderList.join("<br>");
  300. }
  301. /**
  302. * 成功事件
  303. *
  304. * @param {string} field
  305. * @param {object} info
  306. * @return {void}
  307. */
  308. function successTrigger(field, info,id) {
  309. let updateData = {};
  310. switch (field) {
  311. case 'unit_price.market_price':
  312. // 计算价格
  313. updateData = projectGLJSpread.priceCalculate(info);
  314. // 触发websocket通知
  315. socket.emit('dataNotify', JSON.stringify(info));
  316. break;
  317. case 'supply':
  318. // 供货方式更改成功后
  319. projectGLJSpread.changeSupplyType(info);
  320. break;
  321. case 'is_evaluate':
  322. changeIsEvaluate(id);
  323. break;
  324. }
  325. // 重新加载数据到缓存
  326. projectObj.project.projectGLJ.loadData(function () {
  327. projectObj.project.projectGLJ.loadCacheData();
  328. $.bootstrapLoading.end();
  329. });
  330. // 更新定额工料机
  331. gljOprObj.refreshView();
  332. }
  333. //更新是否暂估
  334. function changeIsEvaluate (id){
  335. let projectGLJ = projectObj.project.projectGLJ
  336. let datas = projectGLJ.datas;
  337. let gljList = datas.gljList;
  338. let glj = _.find(gljList, {'id': id});
  339. if(glj){
  340. let con_key = gljOprObj.getIndex(glj,gljKeyArray);
  341. let pratioM =datas.mixRatioConnectData[con_key];//找到父key
  342. let conditions = [];
  343. if(pratioM&&pratioM.length>0){
  344. for(let p_key in pratioM ){
  345. conditions.push(gljOprObj.getConditionByKey(p_key));
  346. }
  347. }
  348. let gljs = projectGLJ.getProjectGLJs(conditions,false);
  349. gljs.push(glj);
  350. let nodes = projectGLJ.getImpactRationNodes(gljs);//取到因为改变工料机价格而受影响的定额
  351. projectObj.project.calcProgram.calcRationsAndSave(nodes);//触发计算程序
  352. }
  353. }
  354. /**
  355. * socket.io相关初始化
  356. *
  357. * @return {void}
  358. */
  359. function socketInit() {
  360. if (socket === null) {
  361. socket = io('http://' + host + ':3300');
  362. socket.on('connect', function () {
  363. socket.emit('join', roomId);
  364. console.log('单价文件同步连接成功');
  365. });
  366. }
  367. // 接收到改变
  368. socket.on('dataChange', function (data) {
  369. data = JSON.parse(data);
  370. if (data.newValue === undefined) {
  371. return false;
  372. }
  373. $("#message").html('市场单位已被修改,<a href="javascript:void(0);" id="load-data">点击加载</a>');
  374. $("#notify").slideDown('fast');
  375. });
  376. }
  377. //过滤消耗量为0的项目工料机
  378. function filterProjectGLJ(jsonData) {
  379. if (jsonData.length > 0) {
  380. // 不显示消耗量为0的数据
  381. let tmpData = [];
  382. for (let data of jsonData) {
  383. if (data.quantity !== 0 && data.quantity !== '0') {
  384. tmpData.push(data);
  385. }
  386. }
  387. jsonData = tmpData;
  388. }
  389. return jsonData;
  390. }
  391. function sortProjectGLJ(jsonData) {
  392. if (jsonData.length > 0) {
  393. jsonData = _.sortByAll(jsonData, [function (item) {
  394. return item.unit_price.type + "";
  395. }, 'code']);
  396. }
  397. return jsonData
  398. }