account_cert.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.key === 'type') {
  45. const cert_type = this._.find(profileConst.cert.certType, { value: data.value });
  46. updateData.cert_type = cert_type ? cert_type.name : '';
  47. } else if (data.key === 'name') {
  48. const cert_name = this._.find(profileConst.cert.certReg, { value: data.value }) || this._.find(profileConst.cert.certQual, { value: data.value });
  49. updateData.cert_name = cert_name ? cert_name.name : '';
  50. }
  51. if (data.other && this._.includes(profileConst.cert.postCertConst, data.other.key)) {
  52. updateData[data.other.key] = data.other.value ? data.other.value : null;
  53. if (data.other.key === 'name') {
  54. console.log(data.other.value);
  55. const cert_name = this._.find(profileConst.cert.certReg, { value: data.other.value }) || this._.find(profileConst.cert.certQual, { value: data.other.value });
  56. updateData.cert_name = cert_name ? cert_name.name : '';
  57. }
  58. }
  59. return await this.db.update(this.tableName, updateData);
  60. }
  61. async addEduJson(id) {
  62. const info = await this.getDataById(id);
  63. if (!info) throw '数据有误';
  64. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  65. const newEdu = this._.cloneDeep(profileConst.cert.eduJsonConst);
  66. newEdu.id = this.ctx.app.uuid.v4();
  67. edu_json.push(newEdu);
  68. info.edu_json = edu_json;
  69. const result = await this.db.update(this.tableName, {
  70. id: info.id,
  71. edu_json: JSON.stringify(edu_json),
  72. });
  73. return { data: info, jxData: newEdu };
  74. }
  75. async updateEduJson(data) {
  76. const info = await this.getDataById(data.id);
  77. if (!info) throw '数据有误';
  78. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  79. if (edu_json.length === 0) throw '数据有误';
  80. const updateEdu = this._.find(edu_json, { id: data.jxid });
  81. if (!updateEdu) throw '数据有误';
  82. updateEdu[data.key] = data.value ? data.value : null;
  83. return await this.db.update(this.tableName, {
  84. id: info.id,
  85. edu_json: JSON.stringify(edu_json),
  86. });
  87. }
  88. async delCert(data) {
  89. if (!data.id) throw '参数有误';
  90. const info = await this.getDataById(data.id);
  91. if (!info) throw '数据有误1';
  92. if (!data.type) throw '参数有误';
  93. if (data.type === 'cert') {
  94. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  95. if (data.jxid) {
  96. // 删除继续教育
  97. if (edu_json.length === 0) throw '数据有误';
  98. const delEdu = this._.findIndex(edu_json, { id: data.jxid });
  99. if (delEdu === -1) throw '数据有误2';
  100. if (edu_json[delEdu].file_path) {
  101. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + edu_json[delEdu].file_path);
  102. }
  103. edu_json.splice(delEdu, 1);
  104. // 如果存在文件,需同步移除
  105. return await this.db.update(this.tableName, {
  106. id: info.id,
  107. edu_json: JSON.stringify(edu_json),
  108. });
  109. }
  110. // 删除整个证书
  111. // 如果存在文件,需同步移除
  112. // 判断是否已调用,已调用需要先删除再删除这里
  113. const isUsed = await this.ctx.service.tenderCert.count({ cert_id: info.id });
  114. if (isUsed > 0) throw '该证书已被添加到标段从业人员,无法删除';
  115. if (info.file_path) {
  116. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path);
  117. if (edu_json.length > 0) {
  118. for (const item of edu_json) {
  119. if (item.file_path) {
  120. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + item.file_path);
  121. }
  122. }
  123. }
  124. }
  125. return await this.db.delete(this.tableName, { id: data.id });
  126. } else if (data.type === 'file') {
  127. if (data.jxid) {
  128. const edu_json = info.edu_json ? JSON.parse(info.edu_json) : [];
  129. if (edu_json.length === 0) throw '数据有误';
  130. const eduInfo = this._.find(edu_json, {id: data.jxid});
  131. if (!eduInfo) throw '数据有误2';
  132. // 如果存在文件,需同步移除
  133. if (!eduInfo.file_path) throw '不存在培训证明';
  134. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + eduInfo.file_path);
  135. eduInfo.file_path = null;
  136. eduInfo.file_name = null;
  137. return await this.db.update(this.tableName, {
  138. id: info.id,
  139. edu_json: JSON.stringify(edu_json),
  140. });
  141. }
  142. // 删除证书附件
  143. if (!info.file_path) throw '不存在证书附件';
  144. await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path);
  145. return await this.db.update(this.tableName, {
  146. id: info.id,
  147. file_path: null,
  148. file_name: null,
  149. });
  150. }
  151. throw '参数有误';
  152. }
  153. }
  154. return AccountCert;
  155. };