123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 'use strict';
- /**
- *
- * 2024/3/21
- * @author Mai
- * @date
- * @version
- */
- const FtType = {
- org: 0,
- add: 1,
- };
- module.exports = app => {
- class FilingTemplateList extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'filing_template_list';
- this.FtType = FtType;
- }
- async getOriginTemplate() {
- return await this.getDataByCondition({ ft_type: FtType.org });
- }
- async getAllTemplate(pid) {
- const sql = `SELECT * FROM ${this.tableName} WHERE ft_type = ${FtType.org} OR project_id = ? ORDER BY create_time asc`;
- return await this.db.query(sql, [pid]);
- }
- async getShareTemplate(pid) {
- const sql = `SELECT ftl.*, p.name as project_name, pa.name as user_name FROM ${this.tableName} ftl
- LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON ftl.user_id = pa.id
- LEFT JOIN ${this.ctx.service.project.tableName} p ON ftl.project_id = p.id
- WHERE ftl.is_share = 1 and ftl.project_id <> ?`;
- return await this.db.query(sql, [pid]);
- }
- /**
- * 保存/新增数据
- *
- * @param {Object} name - 模板名称
- * @param {String} id - 存在则为保存,反之新增
- * @return {boolean} - 操作结果
- */
- async save(name, is_share, id = '') {
- if (id) {
- const updateData = { id };
- if (name !== undefined) updateData.name = name;
- if (is_share !== undefined) updateData.is_share = is_share;
- const result = await this.defaultUpdate(updateData);
- return [result.affectedRows > 0, id];
- } else {
- const conn = await this.db.beginTransaction();
- try {
- const newTemplate = {
- id: this.uuid.v4(), project_id: this.ctx.session.sessionProject.id,
- user_id: this.ctx.session.sessionUser.accountId,
- name: name || '新增XXX模板库', ft_type: FtType.add,
- };
- await conn.insert(this.tableName, newTemplate);
- await this.ctx.service.filingTemplate.initTemplate(conn, newTemplate);
- await conn.commit();
- return [true, newTemplate.id];
- } catch (err) {
- this.ctx.log(err);
- await conn.rollback();
- return [false, ''];
- }
- }
- }
- /**
- * 拷贝模板
- *
- * @param {string} shareId - 拷贝模板的id
- * @return {Boolean} - 操作结果
- */
- async copy(shareId) {
- const shareTemplate = await this.getDataById(shareId);
- if (!shareTemplate.is_share) throw '模板已取消共享';
- const shareTemplateData = await this.ctx.service.filingTemplate.getData(shareId);
- const conn = await this.db.beginTransaction();
- try {
- const newTemplate = {
- id: this.uuid.v4(), project_id: this.ctx.session.sessionProject.id,
- user_id: this.ctx.session.sessionUser.accountId,
- name: shareTemplate.name || '拷贝XXX模板库', ft_type: FtType.add,
- };
- await conn.insert(this.tableName, newTemplate);
- await this.ctx.service.filingTemplate.initTemplate(conn, newTemplate, shareTemplateData);
- await conn.commit();
- return [true, newTemplate.id];
- } catch (err) {
- this.ctx.log(err);
- await conn.rollback();
- return [false, err.stack ? '拷贝模板数据错误' : ''];
- }
- }
- /**
- * 删除模板
- *
- * @param {string} id - 删除的id
- * @return {Boolean} - 返回删除的结果
- */
- async delete(id) {
- const conn = await this.db.beginTransaction();
- try {
- await conn.delete(this.tableName, { id });
- await conn.delete(this.ctx.service.filingTemplate.tableName, { temp_id: id });
- await conn.commit();
- return true;
- } catch (error) {
- this.ctx.log(error);
- await conn.rollback();
- return false;
- }
- }
- async reset(id) {
- const template = await this.getDataById(id);
- const conn = await this.db.beginTransaction();
- try {
- await conn.delete(this.ctx.service.filingTemplate.tableName, { temp_id: id });
- await this.ctx.service.filingTemplate.initTemplate(conn, template);
- await conn.commit();
- } catch(err) {
- await conn.rollback();
- throw err;
- }
- }
- }
- return FilingTemplateList;
- };
|