'use strict'; /** * 版本数据模型 * * @author CaiAoLin * @date 2017/10/25 * @version */ const shenpiConst = require('../const/shenpi'); module.exports = app => { class ShenpiGroup extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'shenpi_group'; } async getGroupList(tid, type, transaction = null) { return transaction ? await transaction.select(this.tableName, { where: { tid, sp_type: type } }) : await this.getAllDataByCondition({ where: { tid, sp_type: type } }); } async getOneGroup(id) { const group = await this.getDataById(id); if (group.change_type) group.change_type = JSON.parse(group.change_type); group.auditGroupList = await this.ctx.service.shenpiAudit.getAuditGroupList(group.tid, group.sp_type, shenpiConst.sp_status.gdspl, group.id); return group; } async saveGroup(tid, data) { const transaction = await this.db.beginTransaction(); try { const sp_type = shenpiConst.sp_type[data.code]; const groupList = await this.getGroupList(tid, sp_type); if (data.groupId) { const info = await this.getDataById(data.groupId); if (!info) { throw '该审批组不存在'; } if (this._.findIndex(groupList, function(item) { return item.id !== data.groupId && item.name === data.name; }) !== -1) { throw '该审批组名称已存在,请更改其它名称'; } const updateData = { id: info.id, name: data.name, }; if (data.code === 'change' && data.change_type) { updateData.change_type = JSON.stringify(data.change_type); } await transaction.update(this.tableName, updateData); } else { const insertData = { tid, sp_type, name: data.name, is_select: 1, change_type: data.code === 'change' && data.change_type ? JSON.stringify(data.change_type) : null, create_time: new Date(), }; const updateGroupData = this._.map(groupList, function(item) { return { id: item.id, is_select: 0, }; }); if (updateGroupData.length > 0) await transaction.updateRows(this.tableName, updateGroupData); const result = await transaction.insert(this.tableName, insertData); data.groupId = result.insertId; // 判断是否存在group_id 为 0的,则自动填写 const auditList = await this.ctx.service.shenpiAudit.getAuditList(tid, sp_type, shenpiConst.sp_status.gdspl); if (auditList && auditList.length > 0) { const updateData = this._.map(auditList, function(item) { return { id: item.id, sp_group: data.groupId, }; }); await transaction.updateRows(this.ctx.service.shenpiAudit.tableName, updateData); } } await transaction.commit(); return { group: await this.getOneGroup(data.groupId) }; } catch (err) { await transaction.rollback(); throw err; } } async changeGroup(data) { const info = await this.getDataById(data.groupId); if (!info) { throw '该审批组不存在'; } const groupList = await this.getGroupList(info.tid, info.sp_type); const updateGroupData = this._.map(groupList, function(item) { return { id: item.id, is_select: item.id === info.id ? 1 : 0, }; }); if (updateGroupData.length > 0) return await this.db.updateRows(this.tableName, updateGroupData); } async deleteGroup(data) { const transaction = await this.db.beginTransaction(); try { const group = await this.getDataById(data.groupId); if (!group) throw '该审批组不存在'; // 移除审批人 await transaction.delete(this.ctx.service.shenpiAudit.tableName, { sp_group: group.id }); await transaction.delete(this.tableName, { id: group.id }); // 第一个审批组设置为is_select=1 const groupList = await this.getGroupList(group.tid, group.sp_type, transaction); if (groupList && groupList.length > 0) { const updateGroupData = this._.map(groupList, function(item) { return { id: item.id, is_select: item.id === groupList[0].id ? 1 : 0, }; }); if (updateGroupData.length > 0) await transaction.updateRows(this.tableName, updateGroupData); } await transaction.commit(); return true; } catch (err) { await transaction.rollback(); throw err; } } async getGroupBySelect(tid, sp_type) { return await this.getDataByCondition({ tid, sp_type, is_select: 1 }); } async getGroupListByChangeType(tid, sp_type, change_type) { const sql = 'select * from ?? where tid= ? and sp_type= ? and JSON_CONTAINS(change_type, json_object(?, true)) order by id asc'; const sqlParam = [this.tableName, tid, sp_type, change_type]; return await this.db.query(sql, sqlParam); } async getSelectGroupByChangeType(tid, sp_type, change_type, group_id = 0) { if (group_id !== 0) { const group = await this.getDataById(group_id); if (group) return group; } // 查找变更选中的审批组id,没有这为默认审批组或null const result = await this.getGroupListByChangeType(tid, sp_type, change_type); if (result && result.length > 0) { const hadSelect = this._.find(result, function(item) { return item.is_select === 1; }); if (hadSelect) { return hadSelect; } return result[0]; } return null; } } return ShenpiGroup; };