'use strict'; /** * * * @author Mai * @date * @version */ const profileConst = require('../const/profile'); module.exports = app => { class AccountCert extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'account_cert'; } async addCert(uid) { const result = await this.db.insert(this.tableName, { pid: this.ctx.session.sessionProject.id, uid, create_time: new Date(), }); return { total: await this.count({ uid }), data: await this.getDataById(result.insertId) }; } async updateCert(data) { const updateData = { id: data.id, }; // 判断key是否在常量里,并转换为对应的值 if (!this._.includes(profileConst.cert.postCertConst, data.key)) throw '参数有误'; updateData[profileConst.cert.certConst[data.key]] = data.value ? data.value : null; return await this.db.update(this.tableName, updateData); } async addEduJson(id) { const info = await this.getDataById(id); if (!info) throw '数据有误'; const edu_json = info.edu_json ? JSON.parse(info.edu_json) : []; const newEdu = this._.cloneDeep(profileConst.cert.eduJsonConst); newEdu.id = this.ctx.app.uuid.v4(); edu_json.push(newEdu); info.edu_json = edu_json; const result = await this.db.update(this.tableName, { id: info.id, edu_json: JSON.stringify(edu_json), }); return { data: info, jxData: newEdu }; } async updateEduJson(data) { const info = await this.getDataById(data.id); if (!info) throw '数据有误'; const edu_json = info.edu_json ? JSON.parse(info.edu_json) : []; if (edu_json.length === 0) throw '数据有误'; const updateEdu = this._.find(edu_json, { id: data.jxid }); if (!updateEdu) throw '数据有误'; updateEdu[data.key] = data.value ? data.value : null; return await this.db.update(this.tableName, { id: info.id, edu_json: JSON.stringify(edu_json), }); } async delCert(data) { if (!data.id) throw '参数有误'; const info = await this.getDataById(data.id); if (!info) throw '数据有误1'; if (!data.type) throw '参数有误'; if (data.type === 'cert') { const edu_json = info.edu_json ? JSON.parse(info.edu_json) : []; if (data.jxid) { // 删除继续教育 if (edu_json.length === 0) throw '数据有误'; const delEdu = this._.findIndex(edu_json, { id: data.jxid }); if (delEdu === -1) throw '数据有误2'; if (edu_json[delEdu].file_path) { await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + edu_json[delEdu].file_path); } edu_json.splice(delEdu, 1); // 如果存在文件,需同步移除 return await this.db.update(this.tableName, { id: info.id, edu_json: JSON.stringify(edu_json), }); } // 删除整个证书 // 如果存在文件,需同步移除 // 判断是否已调用,已调用需要先删除再删除这里 const isUsed = await this.ctx.service.tenderCert.count({ cert_id: info.id }); if (isUsed > 0) throw '该证书已被添加到标段从业人员,无法删除'; if (info.file_path) { await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path); if (edu_json.length > 0) { for (const item of edu_json) { if (item.file_path) { await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + item.file_path); } } } } return await this.db.delete(this.tableName, { id: data.id }); } else if (data.type === 'file') { if (data.jxid) { const edu_json = info.edu_json ? JSON.parse(info.edu_json) : []; if (edu_json.length === 0) throw '数据有误'; const eduInfo = this._.find(edu_json, {id: data.jxid}); if (!eduInfo) throw '数据有误2'; // 如果存在文件,需同步移除 if (!eduInfo.file_path) throw '不存在培训证明'; await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + eduInfo.file_path); eduInfo.file_path = null; eduInfo.file_name = null; return await this.db.update(this.tableName, { id: info.id, edu_json: JSON.stringify(edu_json), }); } // 删除证书附件 if (!info.file_path) throw '不存在证书附件'; await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.file_path); return await this.db.update(this.tableName, { id: info.id, file_path: null, file_name: null, }); } throw '参数有误'; } } return AccountCert; };