sub_project_permission_financial.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. const doComplete = async function(spid) {
  20. try {
  21. const filter = spid ? ` where spid = '${spid}'` : '';
  22. const audits = await querySql('Select * From zh_financial_audit' + filter);
  23. for (const a of audits) {
  24. const existSubProj = await querySql('SELECT * FROM zh_sub_project where id = ? and is_folder = 0 and is_delete = 0;', [a.spid]);
  25. if (existSubProj.length === 0) {
  26. continue;
  27. } else {
  28. const existSubProjPermission = await querySql('SELECT * FROM zh_sub_project_permission WHERE spid = ? and uid = ?', [existSubProj[0].id, a.uid]);
  29. const fund_trans_permission = [];
  30. if (a.permission_transfer_show) {
  31. fund_trans_permission.push(1);
  32. }
  33. if (a.permission_transfer_add) {
  34. fund_trans_permission.push(2);
  35. }
  36. if (a.permission_transfer_file) {
  37. fund_trans_permission.push(3);
  38. }
  39. const fund_pay_permission = [];
  40. if (a.permission_pay_show) {
  41. fund_pay_permission.push(1);
  42. }
  43. if (a.permission_pay_file) {
  44. fund_pay_permission.push(3);
  45. }
  46. if (fund_trans_permission.length > 0 || fund_pay_permission.length > 0) {
  47. console.log('Copy sub_project_permission: ' + a.uid + ' ' + a.spid + ' ');
  48. if (existSubProjPermission.length === 0) {
  49. 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(',') : '' };
  50. const [spSql, spSqlParam] = getInsertSql('zh_sub_project_permission', sp_permission);
  51. await querySql(spSql, spSqlParam);
  52. } else {
  53. 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]);
  54. }
  55. }
  56. }
  57. console.log('END Update;');
  58. }
  59. } catch (err) {
  60. console.log(err);
  61. }
  62. BaseUtil.closePool();
  63. };
  64. const spid = process.argv[3];
  65. doComplete(spid);