| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- module.exports = app => {
- class LedgerTemplate extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'ledger_template';
- }
- async getAllTemplate(pid) {
- // const result = await this.getAllDataByCondition({
- // columns: ['id', 'create_user_id', 'user_name', 'create_time', 'name', 'source', 'is_share'],
- // where: { pid },
- // orders: [['create_time', 'ASC']],
- // });
- const sql = `SELECT id, create_user_id, user_name, create_time, name, source, is_share FROM ${this.tableName} WHERE pid = ? AND (create_user_id = ? OR is_share) ORDER BY create_time ASC`;
- const result = await this.db.query(sql, [pid, this.ctx.session.sessionUser.accountId]);
- return result;
- }
- async getTemplateData(id) {
- const template = await this.getDataById(id);
- if (!template) throw '模板不存在';
- return JSON.parse(template.content);
- }
- async checkTemplateEdit(id) {
- const template = await this.getDataById(id);
- if (!template) throw '编辑的模板不存在';
- if (template.pid !== this.ctx.session.sessionProject.id) throw '模板不属于当前项目,请刷新后重试';
- if (template.create_user_id !== this.ctx.session.sessionUser.accountId) throw '您无权修改该模板';
- return template;
- }
- async _addTemplate(data) {
- const user = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
- const addData = {
- id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id,
- create_user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
- name: data.name || '', source: data.source || '', content: JSON.stringify(data.content),
- is_share: data.is_share ? 1 :0,
- };
- const result = await this.db.insert(this.tableName, addData);
- return addData;
- }
- async _delTemplate(id) {
- const template = await this.checkTemplateEdit(id);
- await this.deleteById(id);
- return id;
- }
- async _updateTemplate(data) {
- const template = await this.checkTemplateEdit(data.id);
- const updateData = { id: template.id };
- if (data.name !== undefined) updateData.name = data.name || '';
- if (data.is_share !== undefined) updateData.is_share = data.is_share ? 1 : 0;
- const result = await this.db.update(this.tableName, updateData);
- if (result.affectedRows === 1) return updateData;
- }
- async saveTemplate(data) {
- const result = {};
- if (data.add) result.add = await this._addTemplate(data.add);
- if (data.del) result.del = await this._delTemplate(data.del);
- if (data.update) result.update = await this._updateTemplate(data.update);
- return result;
- }
- }
- return LedgerTemplate;
- };
|