sub_project.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const rootId = '-1';
  10. const imType = require('../const/tender').imType;
  11. const defaultFunRela = {
  12. banOver: true,
  13. hintOver: true,
  14. banMinusChangeBills: true,
  15. minusNoValue: true,
  16. lockPayExpr: false,
  17. showMinusCol: true,
  18. imType: imType.zl.value,
  19. needGcl: false,
  20. };
  21. const funSet = require('../const/fun_set');
  22. const defaultFunSet = funSet.defaultInfo;
  23. const pageShowConst = require('../const/sp_page_show').defaultSetting;
  24. class DragTree {
  25. /**
  26. * 构造函数
  27. */
  28. constructor(setting) {
  29. // 无索引
  30. this.datas = [];
  31. // 以key为索引indexedDB
  32. this.items = {};
  33. // 以排序为索引
  34. this.nodes = [];
  35. // 根节点
  36. this.children = [];
  37. // 树设置
  38. this.setting = setting;
  39. if (!this.setting.itemsPre) this.setting.itemsPre = 'id_';
  40. }
  41. /**
  42. * 树结构根据显示排序
  43. */
  44. sortTreeNode(isResort) {
  45. const self = this;
  46. const addSortNodes = function (nodes) {
  47. if (!nodes) { return }
  48. for (let i = 0; i < nodes.length; i++) {
  49. self.nodes.push(nodes[i]);
  50. nodes[i].index = self.nodes.length - 1;
  51. if (!isResort) {
  52. nodes[i].children = self.getChildren(nodes[i]);
  53. } else {
  54. nodes[i].children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; })
  55. }
  56. addSortNodes(nodes[i].children);
  57. }
  58. };
  59. this.nodes = [];
  60. if (!isResort) {
  61. this.children = this.getChildren();
  62. } else {
  63. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  64. }
  65. addSortNodes(this.children);
  66. }
  67. /**
  68. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  69. * @param datas
  70. */
  71. loadDatas(datas) {
  72. const self = this;
  73. // 清空旧数据
  74. this.items = {};
  75. this.nodes = [];
  76. this.datas = [];
  77. this.children = [];
  78. // 加载全部数据
  79. datas.sort(function (a, b) {
  80. return a[self.setting.level] - b[self.setting.level];
  81. });
  82. for (const data of datas) {
  83. const keyName = this.setting.itemsPre + data[this.setting.id];
  84. if (this.items[keyName]) continue;
  85. const item = JSON.parse(JSON.stringify(data));
  86. item.children = [];
  87. item.expanded = true;
  88. item.visible = true;
  89. if (item[this.setting.pid] === this.setting.rootId) {
  90. this.children.push(item);
  91. } else {
  92. const parent = this.getParent(item);
  93. if (!parent) continue;
  94. parent.children.push(item);
  95. }
  96. this.items[keyName] = item;
  97. this.datas.push(item);
  98. }
  99. this.children.sort((a, b) => { return a[self.setting.order] - b[self.setting.order]; });
  100. this.sortTreeNode(true);
  101. }
  102. getItemsByIndex(index) {
  103. return this.nodes[index];
  104. }
  105. getItems(id) {
  106. return this.items[this.setting.itemsPre + id];
  107. };
  108. getParent(node) {
  109. return this.getItems(node[this.setting.pid]);
  110. };
  111. getChildren(node) {
  112. const setting = this.setting;
  113. const pid = node ? node[setting.id] : setting.rootId;
  114. const children = this.datas.filter(function (x) {
  115. return x[setting.pid] === pid;
  116. });
  117. children.sort((a, b) => { return a[setting.order] - b[setting.order]; });
  118. return children;
  119. };
  120. isLastSibling(node) {
  121. const siblings = this.getChildren(this.getParent(node));
  122. return (siblings && siblings.length > 0) ? node[this.setting.order] === siblings[siblings.length - 1][this.setting.order] : false;
  123. };
  124. recursiveFun(children, fun) {
  125. if (!fun) return;
  126. if (!children || children.length === 0) return;
  127. for (const c of children) {
  128. this.recursiveFun(c.children, fun);
  129. fun(c);
  130. }
  131. }
  132. }
  133. module.exports = app => {
  134. class SubProject extends app.BaseService {
  135. /**
  136. * 构造函数
  137. *
  138. * @param {Object} ctx - egg全局变量
  139. * @param {String} tableName - 表名
  140. * @return {void}
  141. */
  142. constructor(ctx) {
  143. super(ctx);
  144. this.tableName = 'sub_project';
  145. // const fileType = [{ key: 'file', value: 1, name: '资料归集'}, { key: 'info_progress', value: 2, name: '项目概况-阶段进度'}]
  146. this.FileReferenceType = { file: 1, info_progress: 2};
  147. }
  148. /**
  149. * 数据规则
  150. *
  151. * @param {String} scene - 场景
  152. * @return {Object} - 返回数据规则
  153. */
  154. rule(scene) {
  155. let rule = {};
  156. switch (scene) {
  157. case 'fun':
  158. rule = {
  159. imType: {type: 'enum', values: [imType.tz.value, imType.zl.value, imType.bb.value, imType.bw.value], required: true},
  160. banOver: {type: 'bool', required: true,},
  161. hintOver: {type: 'bool', required: true,},
  162. banMinusChangeBills: {type: 'bool', required: true,},
  163. minusNoValue: {type: 'bool', required: true,},
  164. lockPayExpr: {type: 'bool', required: true,},
  165. showMinusCol: {type: 'bool', required: true,},
  166. };
  167. break;
  168. default:
  169. break;
  170. }
  171. return rule;
  172. }
  173. _filterEmptyFolder(data) {
  174. data.sort((a, b) => { return b.tree_level - a.tree_level});
  175. const result = [];
  176. for (const d of data) {
  177. if (!d.is_folder) result.push(d);
  178. if (result.find(x => { return x.tree_pid === d.id; })) result.push(d);
  179. }
  180. return result;
  181. }
  182. async getSubProject(pid, uid, admin, filterFolder = false) {
  183. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  184. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  185. result = result.filter(x => {
  186. if (x.is_folder) return !filterFolder;
  187. const pb = permission.find(y => { return x.id === y.spid});
  188. if (admin) {
  189. x.tp_cache = pb && pb.tp_cache ? JSON.parse(pb.tp_cache) : {};
  190. return true;
  191. }
  192. if (!pb) return false;
  193. x.user_permission = pb;
  194. x.tp_cache = pb.tp_cache ? JSON.parse(pb.tp_cache) : {};
  195. // 只要项目下添加了账号,就允许看到项目
  196. return true;
  197. // return x.user_permission.budget_permission.length > 0 || x.user_permission.file_permission.length > 0 || x.user_permission.manage_permission.length > 0;
  198. });
  199. return admin ? result : this._filterEmptyFolder(result);
  200. }
  201. async getSubProjectTreeNodes(pid, uid, admin, filterFolder = false) {
  202. const subProjects = await this.getSubProject(pid, uid, admin, filterFolder);
  203. const subProjectsTree = new DragTree({ id: 'id', pid: 'tree_pid', level: 'tree_level', order: 'tree_order', rootId: '-1' });
  204. subProjectsTree.loadDatas(subProjects);
  205. const result = subProjectsTree.nodes.map(x => {
  206. return {
  207. id: x.id, tree_pid: x.tree_pid, tree_level: x.tree_level, is_folder: x.is_folder, name: x.name,
  208. is_last_sibling: subProjectsTree.isLastSibling(x), has_children: x.children && x.children.length > 0,
  209. };
  210. });
  211. return result;
  212. }
  213. async getBudgetProject(pid, uid, admin) {
  214. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  215. const adminPermission = this.ctx.service.subProjPermission.adminPermission;
  216. const permission = admin ? [] : await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  217. result = result.filter(x => {
  218. if (!x.is_folder && !x.budget_id) return false;
  219. if (x.is_folder) return true;
  220. if (admin) {
  221. x.permission = adminPermission.budget_permission;
  222. x.manage_permission = adminPermission.manage_permission;
  223. return true;
  224. } else {
  225. const pb = permission.find(y => { return x.id === y.spid});
  226. if (!pb) return false;
  227. x.permission = pb.budget_permission;
  228. x.manage_permission = pb.manage_permission;
  229. return x.permission.length > 0;
  230. }
  231. });
  232. return this._filterEmptyFolder(result);
  233. }
  234. async getFileProject(pid, uid, admin) {
  235. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  236. const adminPermission = this.ctx.service.subProjPermission.adminPermission;
  237. const permission = await this.ctx.service.subProjPermission.getUserPermission(pid, uid);
  238. result = result.filter(x => {
  239. if (!x.is_folder && !x.management) return false;
  240. if (x.is_folder) return true;
  241. if (admin) {
  242. x.permission = adminPermission.file_permission;
  243. x.manage_permission = adminPermission.manage_permission;
  244. return true;
  245. } else {
  246. const pb = permission.find(y => { return x.id === y.spid});
  247. if (!pb) return false;
  248. x.permission = pb.file_permission;
  249. x.manage_permission = pb.manage_permission;
  250. return x.permission.length > 0;
  251. }
  252. });
  253. return this._filterEmptyFolder(result);
  254. }
  255. async getLastChild(tree_pid) {
  256. const result = await this.getAllDataByCondition({ where: { tree_pid, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'desc']], limit: 1, offset: 0 });
  257. return result[0];
  258. }
  259. async getPosterityData(id){
  260. const result = [];
  261. let cur = await this.getAllDataByCondition({ where: { tree_pid: id, project_id: this.ctx.session.sessionProject.id } });
  262. let iLevel = 1;
  263. while (cur.length > 0 && iLevel < 6) {
  264. result.push(...cur);
  265. cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} });
  266. iLevel += 1;
  267. }
  268. return result;
  269. }
  270. async getStepNode(node, step) {
  271. const tree_order = [];
  272. while(step) {
  273. tree_order.push(node.tree_order + step);
  274. if (step > 0) {
  275. step = step - 1;
  276. } else {
  277. step = step + 1;
  278. }
  279. }
  280. return await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, tree_order, project_id: this.ctx.session.sessionProject.id }, orders: [['tree_order', 'asc']]});
  281. }
  282. async addFolder(data) {
  283. const parent = await this.getDataById(data.tree_pid);
  284. if (parent && !parent.is_folder) throw '添加数据结构错误';
  285. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  286. const conn = await this.db.beginTransaction();
  287. try {
  288. // 获取当前用户信息
  289. const sessionUser = this.ctx.session.sessionUser;
  290. // 获取当前项目信息
  291. const sessionProject = this.ctx.session.sessionProject;
  292. const insertData = {
  293. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  294. tree_pid: data.tree_pid,
  295. tree_level: parent ? parent.tree_level + 1 : 1,
  296. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  297. name: data.name, is_folder: 1,
  298. };
  299. const operate = await conn.insert(this.tableName, insertData);
  300. if (operate.affectedRows === 0) throw '新增文件夹失败';
  301. await conn.commit();
  302. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  303. } catch (error) {
  304. await conn.rollback();
  305. throw error;
  306. }
  307. }
  308. async addSubProject(data) {
  309. const parent = await this.getDataById(data.tree_pid);
  310. if (parent && !parent.is_folder) throw '添加数据结构错误';
  311. const lastChild = await this.getLastChild(parent ? parent.id : rootId);
  312. const conn = await this.db.beginTransaction();
  313. try {
  314. // 获取当前用户信息
  315. const sessionUser = this.ctx.session.sessionUser;
  316. // 获取当前项目信息
  317. const sessionProject = this.ctx.session.sessionProject;
  318. const insertData = {
  319. id: this.uuid.v4(), project_id: sessionProject.id, user_id: sessionUser.accountId,
  320. tree_pid: data.tree_pid,
  321. tree_level: parent ? parent.tree_level + 1 : 1,
  322. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  323. name: data.name, is_folder: 0,
  324. };
  325. const operate = await conn.insert(this.tableName, insertData);
  326. // todo 根据节点新增时的其他操作
  327. if (operate.affectedRows === 0) throw '新增文件夹失败';
  328. await conn.commit();
  329. return await this.getSubProject(sessionProject.id, sessionUser.accountId, sessionUser.is_admin);
  330. } catch (error) {
  331. await conn.rollback();
  332. throw error;
  333. }
  334. }
  335. async dragTo(data) {
  336. const dragNode = await this.getDataById(data.drag_id);
  337. const dropNode = await this.getDataById(data.drop_id);
  338. if (!dragNode || !dropNode || !dropNode.is_folder) throw '拖拽数据结构错误';
  339. const lastChild = await this.getLastChild(dropNode.id);
  340. const posterity = await this.getPosterityData(dragNode.id);
  341. const conn = await this.db.beginTransaction();
  342. try {
  343. const updateData = {
  344. id: dragNode.id, tree_pid: dropNode.id, tree_level: dropNode.tree_level + 1,
  345. tree_order: lastChild ? lastChild.tree_order + 1 : 1,
  346. };
  347. await conn.update(this.tableName, updateData);
  348. if (dragNode.tree_level !== dropNode.tree_level + 1 && posterity.length > 0) {
  349. const posterityUpdateData = posterity.map(x => {
  350. return { id: x.id, tree_level: dropNode.tree_level + 1 - dragNode.tree_level + x.tree_level }
  351. });
  352. await conn.updateRows(this.tableName, posterityUpdateData);
  353. }
  354. // 升级原来的后项的order
  355. await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [dragNode.tree_pid, dragNode.tree_order]);
  356. await conn.commit();
  357. } catch (error) {
  358. await conn.rollback();
  359. throw error;
  360. }
  361. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  362. }
  363. async _siblingMove(node, step) {
  364. const stepNode = await this.getStepNode(node, step);
  365. const conn = await this.db.beginTransaction();
  366. try {
  367. const updateData = [];
  368. updateData.push({ id: node.id, tree_order: node.tree_order + step });
  369. for (const sn of stepNode) {
  370. updateData.push({ id: node.id, tree_order: step > 0 ? sn.tree_order - 1 : sn.tree_order + 1 });
  371. }
  372. await conn.updateRows(this.tableName, updateData);
  373. await conn.commit();
  374. } catch (error) {
  375. await conn.rollback();
  376. throw error;
  377. }
  378. }
  379. async _siblingMoveForce(node, step) {
  380. const sibling = await this.getAllDataByCondition({ where: { tree_pid: node.tree_pid, project_id: this.ctx.session.sessionProject.id, is_delete: 0 }, orders: [['tree_order', 'asc']] });
  381. const nodeIndex = sibling.findIndex(x => { return x.id === node.id });
  382. if (nodeIndex + step < 0) throw '移动数据结构错误';
  383. if (nodeIndex + step > sibling.length - 1) throw '移动数据结构错误';
  384. const conn = await this.db.beginTransaction();
  385. try {
  386. const updateData = [];
  387. updateData.push({ id: node.id, tree_order: sibling[nodeIndex + step].tree_order });
  388. while(step) {
  389. const stepNode = sibling[nodeIndex + step];
  390. if (step > 0) {
  391. updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step - 1].tree_order });
  392. step = step - 1;
  393. } else {
  394. updateData.push({ id: stepNode.id, tree_order: sibling[nodeIndex + step + 1].tree_order});
  395. step = step + 1;
  396. }
  397. }
  398. await conn.updateRows(this.tableName, updateData);
  399. await conn.commit();
  400. } catch (error) {
  401. await conn.rollback();
  402. throw error;
  403. }
  404. }
  405. async _topMove(node) {
  406. const lastChild = await this.getLastChild(rootId);
  407. const posterity = await this.getPosterityData(node.id);
  408. const conn = await this.db.beginTransaction();
  409. try {
  410. const updateData = { id: node.id, tree_pid: rootId, tree_level: 1, tree_order: lastChild ? lastChild.tree_order + 1 : 1 };
  411. await conn.update(this.tableName, updateData);
  412. if (node.tree_level !== 1 && posterity.length > 0) {
  413. const posterityUpdateData = posterity.map(x => {
  414. return { id: x.id, tree_level: x.tree_level - node.tree_level + 1 }
  415. });
  416. await conn.updateRows(this.tableName, posterityUpdateData);
  417. }
  418. // 升级原来的后项的order
  419. await conn.query(`UPDATE ${this.tableName} SET tree_order = tree_order-1 WHERE tree_pid = ? AND tree_order > ?`, [node.tree_pid, node.tree_order]);
  420. await conn.commit();
  421. } catch (error) {
  422. await conn.rollback();
  423. throw error;
  424. }
  425. }
  426. async move(data) {
  427. const node = await this.getDataById(data.id);
  428. if (!node) throw '移动数据结构错误';
  429. switch(data.type) {
  430. case 'up': await this._siblingMoveForce(node, -1); break;
  431. case 'down': await this._siblingMoveForce(node, 1); break;
  432. case 'top': await this._topMove(node); break;
  433. default: throw '未知移动类型';
  434. }
  435. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  436. }
  437. async del(id) {
  438. const node = await this.getDataById(id);
  439. if (!node) throw '删除的数据不存在';
  440. const posterity = await this.getPosterityData(node.id);
  441. const updateData = [
  442. { id: node.id, is_delete: 1 },
  443. ];
  444. posterity.forEach(x => {
  445. updateData.push({ id: x.id, is_delete: 1});
  446. });
  447. await this.db.updateRows(this.tableName, updateData);
  448. return await this.getSubProject(this.ctx.session.sessionProject.id, this.ctx.session.sessionUser.accountId, this.ctx.session.sessionUser.is_admin);
  449. }
  450. async save(data) {
  451. const result = await this.db.update(this.tableName, data);
  452. if (result.affectedRows > 0) {
  453. return data;
  454. } else {
  455. throw '更新数据失败';
  456. }
  457. }
  458. async setBudgetStd(data) {
  459. const subProject = await this.getDataById(data.id);
  460. const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
  461. if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
  462. const conn = await this.db.beginTransaction();
  463. try {
  464. const budget_id = await this.ctx.service.budget.add(conn, {
  465. pid: subProject.project_id, user_id: subProject.user_id, rela_tender: subProject.rela_tender
  466. }, budgetStd);
  467. const updateData = { id: data.id, std_id: budgetStd.id, std_name: budgetStd.name, budget_id };
  468. await conn.update(this.tableName, updateData);
  469. await conn.commit();
  470. return updateData;
  471. } catch (error) {
  472. await conn.rollback();
  473. throw error;
  474. }
  475. }
  476. async setRelaTender(data) {
  477. const subProject = await this.getDataById(data.id);
  478. const orgRelaTenderId = subProject.rela_tender.split(',');
  479. const conn = await this.db.beginTransaction();
  480. try {
  481. await conn.update(this.tableName, data);
  482. await conn.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender: data.rela_tender });
  483. const relaTenderId = data.rela_tender.split(',');
  484. const removeTenderId = orgRelaTenderId.filter(x => { return relaTenderId.indexOf(x) < 0});
  485. const addTenderId = relaTenderId.filter(x => { return orgRelaTenderId.indexOf(x) < 0});
  486. if (removeTenderId.length > 0) await conn.update(this.ctx.service.tender.tableName, { spid: '' }, { where: { id: removeTenderId }});
  487. if (addTenderId.length > 0) await conn.update(this.ctx.service.tender.tableName, { spid: data.id }, { where: { id: addTenderId }});
  488. await conn.commit();
  489. return data;
  490. } catch (error) {
  491. await conn.rollback();
  492. throw error;
  493. }
  494. }
  495. async addRelaTender(transaction, spid, tid) {
  496. if (!transaction) throw '未定义事务';
  497. const subProject = await this.getDataById(spid);
  498. if (!subProject) throw '所属项目不存在';
  499. const rela = subProject.rela_tender.split(',');
  500. if (rela.indexOf(tid + '') >= 0) return;
  501. rela.push(tid + '');
  502. const rela_tender = rela.join(',');
  503. await transaction.update(this.tableName, { id: spid, rela_tender});
  504. await transaction.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender});
  505. }
  506. async removeRelaTender(transaction, spid, tid) {
  507. if (!transaction) throw '未定义事务';
  508. const subProject = await this.getDataById(spid);
  509. if (!subProject) throw '所属项目不存在';
  510. const rela = subProject.rela_tender.split(',');
  511. if (rela.indexOf(tid + '') < 0) return;
  512. const rela_tender = rela.filter(x => { return x === tid + ''}).join(',');
  513. await transaction.update(this.tableName, { id: spid, rela_tender});
  514. await transaction.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender});
  515. }
  516. async setManagement(data) {
  517. const subProject = await this.getDataById(data.id);
  518. if (subProject.management === data.management) return data;
  519. const users = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: subProject.project_id, company: data.management }});
  520. const orgMember = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  521. const dm = [], um = [], im = [];
  522. const template = await this.ctx.service.filingTemplateList.getDataById(data.filingTemplate);
  523. if (!template) throw '选择的文件类别不存在';
  524. const templateFiling = await this.ctx.service.filingTemplate.getAllDataByCondition({
  525. where: { temp_id: template.id, is_fixed: 1 },
  526. });
  527. const filing_type = this.ctx.service.filing.analysisFilingType(templateFiling).map(x => { return x.value; }).join(','), file_permission = '1,2';
  528. for (const u of users) {
  529. const nm = orgMember.find(x => { return u.id === x.uid; });
  530. if (nm) {
  531. if (!nm.file_permission) um.push({ id: nm.id, file_permission, filing_type });
  532. } else {
  533. im.push({ id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: u.id, file_permission, filing_type });
  534. }
  535. }
  536. const conn = await this.db.beginTransaction();
  537. try {
  538. await conn.update(this.tableName, { id: subProject.id, management: data.management, filing_template_id: template.id, filing_template_name: template.name });
  539. await this.ctx.service.filing.initFiling(subProject.id, data.filingTemplate, conn);
  540. if (dm.length > 0) await conn.delete(this.ctx.service.subProjPermission.tableName, { id: dm });
  541. if (um.length > 0) await conn.updateRows(this.ctx.service.subProjPermission.tableName, um);
  542. if (im.length > 0) await conn.insert(this.ctx.service.subProjPermission.tableName, im);
  543. await conn.commit();
  544. return data;
  545. } catch (error) {
  546. await conn.rollback();
  547. throw error;
  548. }
  549. }
  550. async refreshManagementPermission(data) {
  551. const subProject = await this.getDataById(data.id);
  552. const users = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: subProject.project_id, company: subProject.management }});
  553. const orgMember = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
  554. const dm = [], um = [], im = [];
  555. const filing_type = this.ctx.service.filing.allFilingType.join(','), file_permission = '1,2';
  556. for (const u of users) {
  557. const nm = orgMember.find(x => { return u.id === x.uid; });
  558. if (nm) {
  559. if (!nm.file_permission) um.push({ id: nm.id, file_permission, filing_type });
  560. } else {
  561. im.push({ id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: u.id, file_permission, filing_type });
  562. }
  563. }
  564. const conn = await this.db.beginTransaction();
  565. try {
  566. if (dm.length > 0) await conn.delete(this.ctx.service.subProjPermission.tableName, { id: dm });
  567. if (um.length > 0) await conn.updateRows(this.ctx.service.subProjPermission.tableName, um);
  568. if (im.length > 0) await conn.insert(this.ctx.service.subProjPermission.tableName, im);
  569. await conn.commit();
  570. return { dm: dm.length, um: um.length, im: im.length };
  571. } catch (error) {
  572. await conn.rollback();
  573. throw error;
  574. }
  575. }
  576. // 合同管理获取项目列表
  577. async getSubProjectByContract(pid, uid, admin, filterFolder = false) {
  578. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  579. if (admin) return this._filterEmptyFolder(result);
  580. const permission = await this.ctx.service.contractAudit.getAllDataByCondition({ where: { uid } });
  581. result = result.filter(x => {
  582. if (x.is_folder) return !filterFolder;
  583. const pb = permission.find(y => { return x.id === y.spid; });
  584. if (!pb) return false;
  585. return true;
  586. });
  587. return this._filterEmptyFolder(result);
  588. }
  589. async getSubProjectByTender(pid, tenders, filterFolder = false) {
  590. if (tenders.length === 0) return [];
  591. const spids = this._.uniq(this._.map(tenders, 'spid'));
  592. let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  593. result = result.filter(x => {
  594. if (x.is_folder) return !filterFolder;
  595. if (!x.rela_tender) return false;
  596. return this._.includes(spids, x.id);
  597. });
  598. return this._filterEmptyFolder(result);
  599. }
  600. // 合同管理获取项目列表
  601. // async getSubProjectByFinancial(pid, uid, admin, filterFolder = false) {
  602. // let result = await this.getAllDataByCondition({ where: { project_id: pid, is_delete: 0 } });
  603. // if (admin) return this._filterEmptyFolder(result);
  604. //
  605. // const permission = await this.ctx.service.financialAudit.getAllDataByCondition({ where: { uid } });
  606. // result = result.filter(x => {
  607. // if (x.is_folder) return !filterFolder;
  608. // const pb = permission.find(y => { return x.id === y.spid; });
  609. // if (!pb) return false;
  610. // return true;
  611. // });
  612. // return this._filterEmptyFolder(result);
  613. // }
  614. async getFileReference(subProject, file_type) {
  615. if (file_type) {
  616. return await this.db.query(`SELECT id, name FROM zh_file_reference_list WHERE file_type = ?`, [file_type]);
  617. } else {
  618. return await this.db.query(`SELECT id, name FROM zh_file_reference_list`);
  619. }
  620. };
  621. getPageShow(page_show) {
  622. const info = page_show ? JSON.parse(page_show) : {};
  623. for (const pi in pageShowConst) {
  624. info[pi] = info[pi] === undefined ? pageShowConst[pi] : parseInt(info[pi]);
  625. this.ctx.helper._.defaults(info[pi], pageShowConst[pi]);
  626. }
  627. return info;
  628. }
  629. async updatePageshow(id) {
  630. const result = await this.db.update(this.tableName, {
  631. id, page_show: JSON.stringify(this.ctx.subProject.page_show),
  632. });
  633. return result.affectedRows === 1;
  634. }
  635. /**
  636. * 功能设置
  637. * @param id
  638. * @returns {Promise<null>}
  639. */
  640. getFunRela(subProject) {
  641. const result = subProject.fun_rela ? JSON.parse(subProject.fun_rela) : {};
  642. this.ctx.helper._.defaults(result, defaultFunRela);
  643. return result;
  644. }
  645. async updateFunRela(id, data) {
  646. const result = await this.db.update(this.tableName, {
  647. id: id, fun_rela: JSON.stringify({
  648. banOver: data.banOver, hintOver: data.hintOver, banMinusChangeBills: data.banMinusChangeBills,
  649. imType: data.imType, needGcl: data.needGcl, minusNoValue: data.minusNoValue,
  650. lockPayExpr: data.lockPayExpr, showMinusCol: data.showMinusCol,
  651. }),
  652. });
  653. return result.affectedRows === 1;
  654. }
  655. getFunSet(fun_set = null) {
  656. const result = fun_set ? JSON.parse(fun_set) : {};
  657. this.ctx.helper._.defaults(result, defaultFunSet);
  658. return result;
  659. }
  660. async updateFunSet(id, funSet) {
  661. const result = await this.db.update(this.tableName, {
  662. id, fun_set: JSON.stringify(funSet),
  663. });
  664. return result.affectedRows === 1;
  665. }
  666. async saveCommonJson(id, field, datas) {
  667. const subProject = await this.getDataById(id);
  668. subProject.common_json = subProject.common_json ? JSON.parse(subProject.common_json) : {};
  669. const updateData = {
  670. id,
  671. };
  672. subProject.common_json[field] = datas;
  673. updateData.common_json = JSON.stringify(subProject.common_json);
  674. const result = await this.db.update(this.tableName, updateData);
  675. return result.affectedRows === 1;
  676. }
  677. async updateCommonJsonDaping06(subProject, projectData) {
  678. const categoryData = await this.ctx.service.category.getAllCategory(subProject);
  679. const projCommonJson = projectData.common_json ? JSON.parse(projectData.common_json) : null;
  680. const projDaping06Set = projCommonJson && projCommonJson.daPing06_set ? projCommonJson.daPing06_set : null;
  681. if (projDaping06Set) {
  682. const subProjDaping06Set = {};
  683. const orgCategoryData = await this.ctx.service.category.getOrgAllCategory(projectData.id);
  684. const orgCb = this._.find(orgCategoryData, { id: projDaping06Set.cb }) || null;
  685. const orgSr = this._.find(orgCategoryData, { id: projDaping06Set.sr }) || null;
  686. if (orgCb) {
  687. const newCb = this._.find(categoryData, { name: orgCb.name });
  688. if (newCb) {
  689. subProjDaping06Set.cb = newCb.id;
  690. const orgCbv = this._.find(orgCb.value, { id: projDaping06Set.cb_value }) || null;
  691. if (orgCbv && orgCbv.value) {
  692. const newCbv = this._.find(newCb.value, { value: orgCbv.value });
  693. if (newCbv) {
  694. subProjDaping06Set.cb_value = newCbv.id;
  695. }
  696. }
  697. }
  698. }
  699. if (orgSr) {
  700. const newSr = this._.find(categoryData, { name: orgSr.name });
  701. if (newSr) {
  702. subProjDaping06Set.sr = newSr.id;
  703. const orgSrv = this._.find(orgSr.value, { id: projDaping06Set.sr_value }) || null;
  704. if (orgSrv && orgSrv.value) {
  705. const newSrv = this._.find(newSr.value, { value: orgSrv.value });
  706. if (newSrv) {
  707. subProjDaping06Set.sr_value = newSrv.id;
  708. }
  709. }
  710. }
  711. }
  712. const newCbShow = [];
  713. const orgGlCategory = orgCategoryData.find(item => item.name === '管理类别');
  714. for (const d of projDaping06Set.cb_show) {
  715. const org = orgGlCategory && orgGlCategory.value ? this._.find(orgGlCategory.value, { id: d }) || null : null;
  716. if (org && org.value) {
  717. const newGlCategory = categoryData.find(item => item.name === '管理类别');
  718. const newOrg = newGlCategory && newGlCategory.value ? this._.find(newGlCategory.value, { value: org.value }) : '';
  719. if (newOrg) {
  720. newCbShow.push(newOrg.id);
  721. }
  722. }
  723. }
  724. subProjDaping06Set.cb_show = newCbShow;
  725. await this.saveCommonJson(subProject.id, 'daPing06_set', subProjDaping06Set);
  726. return JSON.stringify({ daPing06_set: subProjDaping06Set });
  727. }
  728. }
  729. async refreshTpCache(spid, user_id, is_admin) {
  730. const tp_cache = {};
  731. let permission = await this.ctx.service.subProjPermission.getDataByCondition({ spid: spid, uid: user_id });
  732. if (!permission && is_admin) {
  733. await this.db.insert(this.ctx.service.subProjPermission.tableName, {
  734. id: this.uuid.v4(), spid, pid: this.ctx.session.sessionProject.id, uid: user_id
  735. });
  736. permission = await this.ctx.service.subProjPermission.getDataByCondition({ spid: spid, uid: user_id });
  737. }
  738. if (!permission) return tp_cache;
  739. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  740. const userPermission = accountInfo !== undefined && accountInfo.permission !== ''
  741. ? JSON.parse(accountInfo.permission) : null;
  742. const subProject = await this.getDataById(spid);
  743. subProject.page_show = this.getPageShow(subProject.page_show);
  744. const tenders = await this.ctx.service.tender.getList('', userPermission, this.ctx.session.sessionUser.is_admin, '', subProject);
  745. for (const t of tenders) {
  746. await this.ctx.service.tenderCache.loadTenderCache(t, this.ctx.session.sessionUser.accountId);
  747. tp_cache.contract_price = this.ctx.helper.add(tp_cache.contract_price, t.contract_price);
  748. if (t.ledger_tp) tp_cache.ledger_tp = this.ctx.helper.add(tp_cache.ledger_tp, t.ledger_tp.total_price);
  749. tp_cache.advance_tp = this.ctx.helper.add(tp_cache.advance_tp, t.advance_tp);
  750. tp_cache.change_tp = this.ctx.helper.add(tp_cache.change_tp, t.change_tp);
  751. if (!t.stage_tp) continue;
  752. tp_cache.contract_tp = this.ctx.helper.add(tp_cache.contract_tp, t.stage_tp.contract_tp);
  753. tp_cache.qc_tp = this.ctx.helper.add(tp_cache.qc_tp, t.stage_tp.qc_tp);
  754. tp_cache.positive_qc_tp = this.ctx.helper.add(tp_cache.positive_qc_tp, t.stage_tp.positive_qc_tp);
  755. tp_cache.negative_qc_tp = this.ctx.helper.add(tp_cache.negative_qc_tp, t.stage_tp.negative_qc_tp);
  756. tp_cache.yf_tp = this.ctx.helper.add(tp_cache.yf_tp, t.stage_tp.yf_tp);
  757. tp_cache.sf_tp = this.ctx.helper.add(tp_cache.sf_tp, t.stage_tp.sf_tp);
  758. tp_cache.pre_contract_tp = this.ctx.helper.add(tp_cache.pre_contract_tp, t.stage_tp.pre_contract_tp);
  759. tp_cache.pre_qc_tp = this.ctx.helper.add(tp_cache.pre_qc_tp, t.stage_tp.pre_qc_tp);
  760. tp_cache.pre_positive_qc_tp = this.ctx.helper.add(tp_cache.pre_positive_qc_tp, t.stage_tp.pre_positive_qc_tp);
  761. tp_cache.pre_positive_qc_tp = this.ctx.helper.add(tp_cache.pre_positive_qc_tp, t.stage_tp.pre_positive_qc_tp);
  762. tp_cache.pre_yf_tp = this.ctx.helper.add(tp_cache.pre_yf_tp, t.stage_tp.pre_yf_tp);
  763. tp_cache.pre_sf_tp = this.ctx.helper.add(tp_cache.pre_sf_tp, t.stage_tp.pre_sf_tp);
  764. tp_cache.contract_pc_tp = this.ctx.helper.add(tp_cache.contract_pc_tp, t.stage_tp.contract_pc_tp);
  765. tp_cache.qc_pc_tp = this.ctx.helper.add(tp_cache.qc_pc_tp, t.stage_tp.qc_pc_tp);
  766. tp_cache.pc_tp = this.ctx.helper.add(tp_cache.pc_tp, t.stage_tp.pc_tp);
  767. }
  768. await this.db.update(this.ctx.service.subProjPermission.tableName, { id: permission.id, tp_cache: JSON.stringify(tp_cache)});
  769. return tp_cache;
  770. }
  771. }
  772. return SubProject;
  773. };