sub_proj_permission.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class subProjPermission extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @param {String} tableName - 表名
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'sub_project_permission';
  21. this._definePermission();
  22. }
  23. _definePermission() {
  24. this.PermissionConst = {
  25. budget: {
  26. view: { title: '查看', value: 1 },
  27. edit: { title: '编辑', value: 2 },
  28. },
  29. file: {
  30. view: { title: '查看', value: 1 },
  31. upload: { title: '上传/引用', value: 2 },
  32. delete: { title: '删除文件', value: 4 },
  33. filing: { title: '文件类别编辑', value: 3 },
  34. },
  35. manage: {
  36. rela: { title: '关联标段', value: 1 },
  37. },
  38. info: {
  39. view: { title: '查看', value: 1},
  40. edit: { title: '编辑', value: 2 },
  41. },
  42. datacollect: {
  43. view: { title: '查看', value: 1},
  44. },
  45. contract: {
  46. edit: { title: '编辑节点', value: 1 },
  47. add: { title: '添加合同', value: 2 },
  48. node: { title: '授权查看本节点合同', value: 3 },
  49. unit: { title: '授权查看本单位合同', value: 4 },
  50. },
  51. fund_trans: {
  52. view: { title: '查看', value: 1 },
  53. add: { title: '新建划拨', value: 2 },
  54. att: { title: '上传附件', value: 3 },
  55. },
  56. fund_pay: {
  57. view: { title: '查看', value: 1 },
  58. att: { title: '上传附件', value: 3 },
  59. },
  60. };
  61. this.PermissionBlock = [
  62. { key: 'datacollect', name: '决策大屏', field: 'datacollect_permission' },
  63. { key: 'info', name: '项目概况', field: 'info_permission' },
  64. { key: 'contract', name: '合同管理', field: 'contract_permission', hint: ['1、编辑节点:编辑合同管理内页树结构', '2、添加合同:允许添加合同', '3、授权查看本节点合同:授权节点下查看所有人上传的合同', '4、授权查看本单位合同:授权节点下查看本单位人员添加的所有合同'] },
  65. { key: 'file', name: '资料归集', field: 'file_permission' },
  66. { key: 'budget', name: '动态投资', field: 'budget_permission' },
  67. {
  68. key: 'financial', name: '资金监管', children: [
  69. { key: 'fund_trans', name: '资金划拨', field: 'fund_trans_permission' },
  70. { key: 'fund_pay', name: '资金支付', field: 'fund_pay_permission' },
  71. ]
  72. },
  73. ];
  74. for (const p of this.PermissionBlock) {
  75. if (p.children) {
  76. for (const c of p.children) {
  77. c.permission = [];
  78. const pConst = this.PermissionConst[c.key];
  79. if (!pConst) continue;
  80. for (const prop in pConst) {
  81. c.permission.push({ key: prop, ...pConst[prop]});
  82. }
  83. }
  84. } else {
  85. p.permission = [];
  86. const pConst = this.PermissionConst[p.key];
  87. if (!pConst) continue;
  88. for (const prop in pConst) {
  89. p.permission.push({ key: prop, ...pConst[prop]});
  90. }
  91. }
  92. }
  93. }
  94. get adminPermission () {
  95. return {
  96. budget_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.budget, 'value'),
  97. file_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.file, 'value'),
  98. manage_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.manage, 'value'),
  99. filing_type: 'all',
  100. }
  101. }
  102. async showSubTab(uid, type) {
  103. const sql = `SELECT count(*) as count FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?`;
  104. const result = await this.db.queryOne(sql, [uid]);
  105. return result.count;
  106. }
  107. async showBudget(uid) {
  108. return await this.showSubTab(uid, 'budget');
  109. }
  110. async showFile(uid) {
  111. return await this.showSubTab(uid, 'file');
  112. }
  113. parsePermission(data) {
  114. const _ = this.ctx.helper._;
  115. const datas = data instanceof Array ? data : [data];
  116. datas.forEach(x => {
  117. x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : [];
  118. x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : [];
  119. x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : [];
  120. x.filing_type = x.filing_type ? _.map(x.filing_type.split(','), _.toInteger): [];
  121. });
  122. }
  123. async getPermission(subProjectId) {
  124. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  125. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  126. On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]);
  127. this.parsePermission(result);
  128. return result;
  129. }
  130. async getBudgetPermission(subProjectId) {
  131. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  132. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  133. On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]);
  134. this.parsePermission(result);
  135. return result;
  136. }
  137. async getUserPermission(pid, uid) {
  138. const result = await this.getAllDataByCondition({
  139. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  140. });
  141. this.parsePermission(result);
  142. return result;
  143. }
  144. async getBudgetUserPermission(bid) {
  145. const subProj = await this.service.subProject.getDataByCondition({ budget_id: bid });
  146. const result = await this.getDataByCondition({ spid: subProj.id, uid: this.ctx.session.sessionUser.accountId });
  147. if (result) this.parsePermission(result);
  148. return result;
  149. }
  150. async getSubProjectUserPermission(spid, uid) {
  151. const result = await this.getDataByCondition({ spid, uid });
  152. if (result) this.parsePermission(result);
  153. return result;
  154. };
  155. async savePermission(subProjectId, member) {
  156. const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
  157. const dm = [], um = [], im = [];
  158. for (const om of orgMember) {
  159. const nm = member.find(x => { return om.uid === x.uid; });
  160. if (!nm) {
  161. dm.push(om.id);
  162. } else {
  163. um.push({
  164. id: om.id, budget_permission: nm.budget_permission.join(','),
  165. file_permission: nm.file_permission.join(','),
  166. manage_permission: nm.manage_permission.join(',')
  167. });
  168. member.splice(member.indexOf(nm), 1);
  169. }
  170. }
  171. for (const m of member) {
  172. im.push({
  173. id: this.uuid.v4(),
  174. spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid,
  175. budget_permission: m.budget_permission.join(','),
  176. file_permission: m.file_permission.join(','),
  177. manage_permission: m.manage_permission.join(',')
  178. });
  179. }
  180. const conn = await this.db.beginTransaction();
  181. try {
  182. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  183. if (um.length > 0) await conn.updateRows(this.tableName, um);
  184. if (im.length > 0) await conn.insert(this.tableName, im);
  185. await conn.commit();
  186. } catch (err) {
  187. await conn.rollback();
  188. throw err;
  189. }
  190. }
  191. async _addUser(subProject, data) {
  192. const ids = data instanceof Array ? data : [data];
  193. const exists = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  194. if (exists.length > 0) throw '请勿重复选择账号';
  195. const insertData = ids.map(x => {
  196. return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
  197. });
  198. await this.db.insert(this.tableName, insertData);
  199. return insertData;
  200. }
  201. async _delUser(subProject, data) {
  202. const ids = data instanceof Array ? data : [data];
  203. const permissions = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  204. await this.db.delete(this.tableName, { id: permissions.map(x => { return x.id; }) });
  205. return data;
  206. }
  207. async _updateUserPermission(data) {
  208. const datas = data instanceof Array ? data : [data];
  209. const updateData = [];
  210. datas.forEach(x => {
  211. const ud = { id: x.id };
  212. for (const p of this.PermissionBlock) {
  213. if (p.children) {
  214. for (const c of p.children) {
  215. if (data[c.field] !== undefined) ud[c.field] = x[c.field] || '';
  216. }
  217. } else {
  218. if (data[p.field] !== undefined) ud[p.field] = x[p.field] || '';
  219. }
  220. }
  221. updateData.push(ud);
  222. });
  223. await this.db.updateRows(this.tableName, updateData);
  224. return updateData;
  225. }
  226. async updatePermission(subProject, data) {
  227. const result = {};
  228. if (data.add) result.add = await this._addUser(subProject, data.add);
  229. if (data.del) result.del = await this._delUser(subProject, data.del);
  230. if (data.update) result.update = await this._updateUserPermission(data.update);
  231. return result;
  232. }
  233. async getFilingType(subProjectId) {
  234. const permissionConst = {}, prefix = 'f';
  235. for (const p in this.PermissionConst.file) {
  236. const fp = this.PermissionConst.file[p];
  237. permissionConst[prefix + fp.value] = fp.title;
  238. }
  239. const result = await this.db.query(`SELECT spp.id, p.name, p.role, p.company, p.mobile, spp.file_permission, spp.filing_type, spp.create_time
  240. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  241. On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]);
  242. result.forEach(x => {
  243. const filePermission = x.file_permission.split(',');
  244. x.file_permission = filePermission.map(x => {
  245. return permissionConst[prefix + x] || '';
  246. }).join(',');
  247. });
  248. return result;
  249. }
  250. // 资料归集,授权固定分类
  251. async saveFilingType(data) {
  252. const updateData = [];
  253. data.forEach(x => {
  254. updateData.push({ id: x.id, filing_type: x.filing_type });
  255. });
  256. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  257. }
  258. }
  259. return subProjPermission;
  260. };