account_cert.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 getAllCertByPid(pid, WithoutNullName = 0) {
  23. const list = await this.getAllDataByCondition({ where: { pid }, orders: [['create_time', 'desc']] });
  24. if (WithoutNullName) {
  25. return this._.filter(list, l => l.name !== null && l.name !== '');
  26. }
  27. return list;
  28. }
  29. async addCert(uid) {
  30. const result = await this.db.insert(this.tableName, {
  31. pid: this.ctx.session.sessionProject.id,
  32. uid,
  33. create_time: new Date(),
  34. });
  35. return { total: await this.count({ uid }), data: await this.getDataById(result.insertId) };
  36. }
  37. async updateCert(data) {
  38. const updateData = {
  39. id: data.id,
  40. };
  41. // 判断key是否在常量里,并转换为对应的值
  42. if (!this._.includes(profileConst.cert.postCertConst, data.key)) throw '参数有误';
  43. updateData[data.key] = data.value ? data.value : null;
  44. if (data.other && this._.includes(profileConst.cert.postCertConst, data.other.key)) {
  45. updateData[data.other.key] = data.other.value ? data.other.value : null;
  46. }
  47. return await this.db.update(this.tableName, updateData);
  48. }
  49. async addEduJson(id) {
  50. const info = await this.getDataById(id);
  51. if (!info) throw '数据有误';
  52. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  53. const newEdu = this._.cloneDeep(profileConst.cert.eduJsonConst);
  54. newEdu.id = this.ctx.app.uuid.v4();
  55. edu_json.push(newEdu);
  56. info.edu_json = edu_json;
  57. const result = await this.db.update(this.tableName, {
  58. id: info.id,
  59. edu_json: JSON.stringify(edu_json),
  60. });
  61. return { data: info, jxData: newEdu };
  62. }
  63. async updateEduJson(data) {
  64. const info = await this.getDataById(data.id);
  65. if (!info) throw '数据有误';
  66. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  67. if (edu_json.length === 0) throw '数据有误';
  68. const updateEdu = this._.find(edu_json, { id: data.jxid });
  69. if (!updateEdu) throw '数据有误';
  70. updateEdu[data.key] = data.value ? data.value : null;
  71. return await this.db.update(this.tableName, {
  72. id: info.id,
  73. edu_json: JSON.stringify(edu_json),
  74. });
  75. }
  76. async delCert(data) {
  77. if (!data.id) throw '参数有误';
  78. const info = await this.getDataById(data.id);
  79. if (!info) throw '数据有误1';
  80. if (!data.type) throw '参数有误';
  81. if (data.type === 'cert') {
  82. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  83. if (data.jxid) {
  84. // 删除继续教育
  85. if (edu_json.length === 0) throw '数据有误';
  86. const delEdu = this._.findIndex(edu_json, { id: data.jxid });
  87. if (delEdu === -1) throw '数据有误2';
  88. if (edu_json[delEdu].file_path) {
  89. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + edu_json[delEdu].file_path);
  90. }
  91. edu_json.splice(delEdu, 1);
  92. // 如果存在文件,需同步移除
  93. return await this.db.update(this.tableName, {
  94. id: info.id,
  95. edu_json: JSON.stringify(edu_json),
  96. });
  97. }
  98. // 删除整个证书
  99. // 如果存在文件,需同步移除
  100. // 判断是否已调用,已调用需要先删除再删除这里
  101. const isUsed = await this.ctx.service.tenderCert.count({ cert_id: info.id });
  102. if (isUsed > 0) throw '该证书已被添加到标段从业人员,无法删除';
  103. if (info.file_path) {
  104. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path);
  105. if (edu_json.length > 0) {
  106. for (const item of edu_json) {
  107. if (item.file_path) {
  108. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + item.file_path);
  109. }
  110. }
  111. }
  112. }
  113. return await this.db.delete(this.tableName, { id: data.id });
  114. } else if (data.type === 'file') {
  115. if (data.jxid) {
  116. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  117. if (edu_json.length === 0) throw '数据有误';
  118. const eduInfo = this._.find(edu_json, {id: data.jxid});
  119. if (!eduInfo) throw '数据有误2';
  120. // 如果存在文件,需同步移除
  121. if (!eduInfo.file_path) throw '不存在培训证明';
  122. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + eduInfo.file_path);
  123. eduInfo.file_path = null;
  124. eduInfo.file_name = null;
  125. return await this.db.update(this.tableName, {
  126. id: info.id,
  127. edu_json: JSON.stringify(edu_json),
  128. });
  129. }
  130. // 删除证书附件
  131. if (!info.file_path) throw '不存在证书附件';
  132. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path);
  133. return await this.db.update(this.tableName, {
  134. id: info.id,
  135. file_path: null,
  136. file_name: null,
  137. });
  138. }
  139. throw '参数有误';
  140. }
  141. }
  142. return AccountCert;
  143. };