sub_project_permission.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. 修改脚本,请现在指定spid测试
  3. 例如: node db_script/sub_project local 3fcee213-92c9-41de-8f5a-b896c986311a
  4. 没有意外再全部执行
  5. 所有修改均应考虑脚本二次执行时的兼容,应检查是否已执行过,避免在生产环境运行时出现问题后需要二次执行,参见自定义分类脚本
  6. */
  7. const BaseUtil = require('./baseUtils');
  8. const querySql = BaseUtil.querySql;
  9. const uuid = require('node-uuid');
  10. const getInsertSql = function (tableName, data) {
  11. const column = [], query = [], value = [];
  12. for (const prop in data) {
  13. column.push(prop);
  14. query.push('?');
  15. value.push(data[prop]);
  16. }
  17. return [`INSERT INTO ${tableName} (${column.join(',')}) VALUES (${query.join(',')})`, value];
  18. };
  19. console.log('Copy sub_project_permission properties: fund_trans, fund_pay, contract');
  20. const doComplete = async function(spid) {
  21. try {
  22. const filter = spid ? ` where spid = '${spid}'` : '';
  23. const audits = await querySql('Select * From zh_financial_audit' + filter);
  24. for (const a of audits) {
  25. const existSubProj = await querySql('SELECT * FROM zh_sub_project where id = ? and is_folder = 0 and is_delete = 0;', [a.spid]);
  26. if (existSubProj.length === 0) {
  27. continue;
  28. } else {
  29. const existSubProjPermission = await querySql('SELECT * FROM zh_sub_project_permission WHERE spid = ? and uid = ?', [existSubProj[0].id, a.uid]);
  30. const fund_trans_permission = [];
  31. if (a.permission_transfer_show) {
  32. fund_trans_permission.push(1);
  33. }
  34. if (a.permission_transfer_add) {
  35. fund_trans_permission.push(2);
  36. }
  37. if (a.permission_transfer_file) {
  38. fund_trans_permission.push(3);
  39. }
  40. const fund_pay_permission = [];
  41. if (a.permission_pay_show) {
  42. fund_pay_permission.push(1);
  43. }
  44. if (a.permission_pay_file) {
  45. fund_pay_permission.push(3);
  46. }
  47. if (fund_trans_permission.length > 0 || fund_pay_permission.length > 0) {
  48. console.log('Copy sub_project_permission_financial: ' + a.uid + ' ' + a.spid + ' ');
  49. if (existSubProjPermission.length === 0) {
  50. const sp_permission = { id: uuid.v4(), spid: existSubProj[0].id, pid: existSubProj[0].project_id, uid: a.uid, fund_trans_permission: fund_trans_permission.length > 0 ? fund_trans_permission.join(',') : '', fund_pay_permission: fund_pay_permission.length > 0 ? fund_pay_permission.join(',') : '' };
  51. const [spSql, spSqlParam] = getInsertSql('zh_sub_project_permission', sp_permission);
  52. await querySql(spSql, spSqlParam);
  53. } else {
  54. await querySql('Update zh_sub_project_permission SET fund_trans_permission = ?, fund_pay_permission = ? WHERE id = ?; ', [fund_trans_permission.length > 0 ? fund_trans_permission.join(',') : '', fund_pay_permission.length > 0 ? fund_pay_permission.join(',') : '', existSubProjPermission[0].id]);
  55. }
  56. }
  57. }
  58. }
  59. const contractAudits = await querySql(`Select * From zh_contract_audit ${filter ? filter + ' and' : 'where'} tid is NULL`);
  60. for (const a of contractAudits) {
  61. const existSubProj = await querySql('SELECT * FROM zh_sub_project where id = ? and is_folder = 0 and is_delete = 0;', [a.spid]);
  62. if (existSubProj.length === 0) {
  63. continue;
  64. } else {
  65. const existSubProjPermission = await querySql('SELECT * FROM zh_sub_project_permission WHERE spid = ? and uid = ?', [existSubProj[0].id, a.uid]);
  66. const contract_permission = [];
  67. if (a.permission_add) {
  68. contract_permission.push(2);
  69. }
  70. if (a.permission_edit) {
  71. contract_permission.push(1);
  72. }
  73. if (a.permission_show_node) {
  74. contract_permission.push(3);
  75. }
  76. if (a.permission_show_unit) {
  77. contract_permission.push(4);
  78. }
  79. if (!a.permission_show_node && !a.permission_show_unit) {
  80. contract_permission.push(5);
  81. }
  82. if (contract_permission > 0) {
  83. console.log('Copy sub_project_permission_contract: ' + a.uid + ' ' + a.spid + ' ');
  84. if (existSubProjPermission.length === 0) {
  85. const sp_permission = { id: uuid.v4(), spid: existSubProj[0].id, pid: existSubProj[0].project_id, uid: a.uid, contract_permission: contract_permission.length > 0 ? contract_permission.join(',') : '' };
  86. const [spSql, spSqlParam] = getInsertSql('zh_sub_project_permission', sp_permission);
  87. await querySql(spSql, spSqlParam);
  88. } else {
  89. await querySql('Update zh_sub_project_permission SET contract_permission = ? WHERE id = ?; ', [contract_permission.length > 0 ? contract_permission.join(',') : '', existSubProjPermission[0].id]);
  90. }
  91. }
  92. }
  93. }
  94. console.log('END Update;');
  95. } catch (err) {
  96. console.log(err);
  97. }
  98. BaseUtil.closePool();
  99. };
  100. const spid = process.argv[3];
  101. doComplete(spid);