sub_project.js 35 KB

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