account_cert.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const profileConst = require('../const/profile');
  10. module.exports = app => {
  11. class AccountCert extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'account_cert';
  21. }
  22. async addCert(uid) {
  23. const result = await this.db.insert(this.tableName, {
  24. pid: this.ctx.session.sessionProject.id,
  25. uid,
  26. create_time: new Date(),
  27. });
  28. return { total: await this.count({ uid }), data: await this.getDataById(result.insertId) };
  29. }
  30. async updateCert(data) {
  31. const updateData = {
  32. id: data.id,
  33. };
  34. // 判断key是否在常量里,并转换为对应的值
  35. if (!this._.includes(profileConst.cert.postCertConst, data.key)) throw '参数有误';
  36. updateData[profileConst.cert.certConst[data.key]] = data.value ? data.value : null;
  37. return await this.db.update(this.tableName, updateData);
  38. }
  39. async addEduJson(id) {
  40. const info = await this.getDataById(id);
  41. if (!info) throw '数据有误';
  42. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  43. const newEdu = this._.cloneDeep(profileConst.cert.eduJsonConst);
  44. newEdu.id = this.ctx.app.uuid.v4();
  45. edu_json.push(newEdu);
  46. info.edu_json = edu_json;
  47. const result = await this.db.update(this.tableName, {
  48. id: info.id,
  49. edu_json: JSON.stringify(edu_json),
  50. });
  51. return { data: info, jxData: newEdu };
  52. }
  53. async updateEduJson(data) {
  54. const info = await this.getDataById(data.id);
  55. if (!info) throw '数据有误';
  56. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  57. if (edu_json.length === 0) throw '数据有误';
  58. const updateEdu = this._.find(edu_json, { id: data.jxid });
  59. if (!updateEdu) throw '数据有误';
  60. updateEdu[data.key] = data.value ? data.value : null;
  61. return await this.db.update(this.tableName, {
  62. id: info.id,
  63. edu_json: JSON.stringify(edu_json),
  64. });
  65. }
  66. async delCert(data) {
  67. if (!data.id) throw '参数有误';
  68. const info = await this.getDataById(data.id);
  69. if (!info) throw '数据有误1';
  70. if (!data.type) throw '参数有误';
  71. if (data.type === 'cert') {
  72. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  73. if (data.jxid) {
  74. // 删除继续教育
  75. if (edu_json.length === 0) throw '数据有误';
  76. const delEdu = this._.findIndex(edu_json, { id: data.jxid });
  77. if (delEdu === -1) throw '数据有误2';
  78. if (edu_json[delEdu].file_path) {
  79. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + edu_json[delEdu].file_path);
  80. }
  81. edu_json.splice(delEdu, 1);
  82. // 如果存在文件,需同步移除
  83. return await this.db.update(this.tableName, {
  84. id: info.id,
  85. edu_json: JSON.stringify(edu_json),
  86. });
  87. }
  88. // 删除整个证书
  89. // 如果存在文件,需同步移除
  90. // 判断是否已调用,已调用需要先删除再删除这里
  91. const isUsed = await this.ctx.service.tenderCert.count({ cert_id: info.id });
  92. if (isUsed > 0) throw '该证书已被添加到标段从业人员,无法删除';
  93. if (info.file_path) {
  94. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path);
  95. if (edu_json.length > 0) {
  96. for (const item of edu_json) {
  97. if (item.file_path) {
  98. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + item.file_path);
  99. }
  100. }
  101. }
  102. }
  103. return await this.db.delete(this.tableName, { id: data.id });
  104. } else if (data.type === 'file') {
  105. if (data.jxid) {
  106. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  107. if (edu_json.length === 0) throw '数据有误';
  108. const eduInfo = this._.find(edu_json, {id: data.jxid});
  109. if (!eduInfo) throw '数据有误2';
  110. // 如果存在文件,需同步移除
  111. if (!eduInfo.file_path) throw '不存在培训证明';
  112. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + eduInfo.file_path);
  113. eduInfo.file_path = null;
  114. eduInfo.file_name = null;
  115. return await this.db.update(this.tableName, {
  116. id: info.id,
  117. edu_json: JSON.stringify(edu_json),
  118. });
  119. }
  120. // 删除证书附件
  121. if (!info.file_path) throw '不存在证书附件';
  122. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path);
  123. return await this.db.update(this.tableName, {
  124. id: info.id,
  125. file_path: null,
  126. file_name: null,
  127. });
  128. }
  129. throw '参数有误';
  130. }
  131. }
  132. return AccountCert;
  133. };