'use strict'; /** * 决策大屏用户查看权限-数据模型 * * @author ellisran * @date 2021/09/23 * @version */ const accountGroup = require('../const/account_group').group; const paymentConst = require('../const/payment'); const auditConst = require('../const/audit').stage; module.exports = app => { class paymentTenderRpt extends app.BaseService { constructor(ctx) { super(ctx); this.tableName = 'payment_tender_rpt'; } async getList(tid, uid) { const typeValues = []; for (const m in this.ctx.payment.project_setting) { if (this.ctx.payment.project_setting[m].checked) { typeValues.push(this.ctx.payment.project_setting[m].value); } } if (typeValues.length === 0) { return -1; } const sql = 'SELECT * FROM ?? WHERE `tender_id` = ? AND `type` IN (' + this.ctx.helper.getInArrStrSqlFilter(typeValues) + ')' + ' AND (`uid` = ?' + ' OR `id` in (SELECT pda.`tr_id` FROM ?? as pda LEFT JOIN ?? as pd ON pda.`tr_id` = pd.`tr_id` WHERE pd.`status` != ' + auditConst.status.uncheck + ' AND pda.`aid` = ?)' + ' OR `id` in (SELECT pra.`tr_id` FROM ?? as pra LEFT JOIN ?? as pd ON pra.`tr_id` = pd.`tr_id` WHERE pd.`status` != ' + auditConst.status.uncheck + ' AND pd.`status` != ' + auditConst.status.checkNo + ' AND pra.`uid` = ?))' + ' ORDER BY FIELD(`type`, 1, 0), id asc'; const params = [this.tableName, tid, uid, this.ctx.service.paymentDetailAudit.tableName, this.ctx.service.paymentDetail.tableName, uid, this.ctx.service.paymentRptAudit.tableName, this.ctx.service.paymentDetail.tableName, uid]; return await this.db.query(sql, params); } async getProcessList(id) { const typeValues = []; for (const m in this.ctx.payment.project_setting) { if (this.ctx.payment.project_setting[m].checked) { typeValues.push(this.ctx.payment.project_setting[m].value); } } if (typeValues.length === 0) { return -1; } const sql = 'SELECT ptr.*, pa.name as user_name FROM ?? as ptr LEFT JOIN ?? as pa ON ptr.`create_uid` = pa.`id` WHERE ptr.`tender_id` = ? AND ptr.`type` IN (' + this.ctx.helper.getInArrStrSqlFilter(typeValues) + ')'; const params = [this.tableName, this.ctx.service.projectAccount.tableName, id]; return await this.db.query(sql, params); } async checkAndUpdateList(tenderRptList, rptProjectList, formProcess = false) { if (tenderRptList.length > 0) { const transaction = await this.db.beginTransaction(); try { const updateDatas = []; const delDatas = []; const delRptIds = []; const noConstRptList = this._.filter(tenderRptList, { type: 0 }); for (const tr of noConstRptList) { const rptInfo = this._.find(rptProjectList, { ID: tr.rpt_id }); // 判断是否已经新建过报表次 const had_rpt = await this.ctx.service.paymentDetail.hadDetail(tr.id); if (tr.is_del === 0 && !rptInfo) { if (had_rpt) { updateDatas.push({ id: tr.id, is_del: 1, }); tr.is_del = 1; } else { delDatas.push(tr.id); } delRptIds.push(tr.id); } else if (rptInfo && tr.rpt_name !== rptInfo.name) { updateDatas.push({ id: tr.id, rpt_name: rptInfo.name, }); tr.rpt_name = rptInfo.name; } if (rptInfo) rptInfo.had_rpt = had_rpt; } if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas); if (delDatas.length > 0) { this._.remove(tenderRptList, function(item) { return delDatas.indexOf(item.id) !== -1; }); await transaction.delete(this.tableName, { id: delDatas }); } if (delRptIds.length > 0) await this.ctx.service.paymentShenpiAudit.delDataFromtrids(transaction, delRptIds); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } return formProcess ? [tenderRptList, rptProjectList] : tenderRptList; } async setRpt(tid, rpt_list) { const transaction = await this.db.beginTransaction(); try { const originList = await this.getAllDataByCondition({ where: { tender_id: tid, is_del: 0, type: 0 } }); const insertData = []; let deleteData = []; if (originList.length === 0 && rpt_list.length !== 0) { // 添加到tender_rpt for (const rpt of rpt_list) { insertData.push({ tender_id: tid, rpt_id: rpt.id, rpt_name: rpt.name, create_uid: this.ctx.session.sessionUser.accountId, in_time: new Date(), }); } } else if (originList.length !== 0 && rpt_list.length === 0) { // 删除原有 deleteData = this._.map(originList, 'id'); } else if (originList.length !== 0 && rpt_list.length !== 0) { const orginRptIds = this._.map(originList, 'rpt_id'); const newIds = this._.map(rpt_list, 'id'); console.log(orginRptIds, newIds); const insertRptIds = this._.difference(newIds, orginRptIds); const deleteRptIds = this._.difference(orginRptIds, newIds); if (deleteRptIds.length > 0) { for (const id of deleteRptIds) { const orginInfo = this._.find(originList, { rpt_id: id }); deleteData.push(orginInfo.id); } } for (const id of insertRptIds) { const info = this._.find(rpt_list, { id }); insertData.push({ tender_id: tid, rpt_id: id, rpt_name: info.name, create_uid: this.ctx.session.sessionUser.accountId, in_time: new Date(), }); } } if (insertData.length > 0) await transaction.insert(this.tableName, insertData); if (deleteData.length > 0) { await transaction.delete(this.tableName, { id: deleteData }); // 也要删除对应的审批流数据 await this.ctx.service.paymentShenpiAudit.delDataFromtrids(transaction, deleteData); } await transaction.commit(); let tenderRptList = await this.getProcessList(tid); tenderRptList = this._.filter(tenderRptList, { type: 0, is_del: 0 }); return tenderRptList; } catch (err) { await transaction.rollback(); throw err; } } async setStatus(id, sp_status) { const transaction = await this.db.beginTransaction(); try { await transaction.update(this.tableName, { id, sp_status }); await transaction.commit(); return await this.ctx.service.paymentShenpiAudit.getShenpiAudit(id, sp_status); } catch (err) { await transaction.rollback(); throw err; } } async setConstRpt(transaction, tid, uid) { const insertData = []; for (const rpt of paymentConst.const_rpt_list) { insertData.push({ tender_id: tid, create_uid: uid, rpt_id: rpt.rpt_id, rpt_name: rpt.rpt_name, type: 1, in_time: new Date(), }); } if (insertData.length > 0) await transaction.insert(this.tableName, insertData); } async updateRptAudit(trInfo, rpt_audit) { const transaction = await this.db.beginTransaction(); try { // 判断是否存在null if (!trInfo.is_first && this._.findIndex(rpt_audit, { uid: null }) !== -1) { throw '请绑定所有表单角色再提交'; } // 判断是否存在待上报或者退回的详情,有则同步更新 const detailList = await this.ctx.service.paymentDetail.getAllDataByCondition({ where: { tr_id: trInfo.id }, orders: [['id', 'desc']] }); if (!trInfo.is_change && detailList.length > 0 && (detailList[0].status === auditConst.status.uncheck || detailList[0].status === auditConst.status.checkNo)) { if (this._.findIndex(rpt_audit, { uid: null }) !== -1) { throw '未配置好表单角色'; } await this.ctx.service.paymentRptAudit.updateAllAuditList(transaction, detailList[0].id, rpt_audit); let report_json = JSON.parse(detailList[0].report_json); report_json = await this.ctx.service.paymentDetail.clearAllSignatureData(report_json); await transaction.update(this.ctx.service.paymentDetail.tableName, { id: detailList[0].id, report_json: JSON.stringify(report_json), }); } const is_first = this._.findIndex(rpt_audit, { uid: null }) === -1 ? 0 : 1; await transaction.update(this.tableName, { id: trInfo.id, rpt_audit: JSON.stringify(rpt_audit), is_first }); await transaction.commit(); return { is_first }; } catch (err) { await transaction.rollback(); throw err; } } async updateReport(data) { const transaction = await this.db.beginTransaction(); try { const tr_id = data.tr_id; if (!tr_id) throw '参数有误'; const trInfo = await this.getDataById(tr_id); if (!trInfo) throw '表单不存在'; const userInfo = await this.ctx.service.projectAccount.getDataById(data.uid); if (!userInfo) throw '用户不存在'; const updateData = { id: trInfo.id, uid: data.uid, }; await transaction.update(this.tableName, updateData); // 判断是否存在待上报或重新上报的表单详情,有则更改原报人 const result1 = await this.ctx.service.paymentDetail.updateReport(transaction, tr_id, data.uid); // 判断固定审批流或固定终审是否存在上报人,有则移除 const result2 = await this.ctx.service.paymentShenpiAudit.removeAuditByReport(transaction, tr_id, data.uid); await this.ctx.service.subProjPermission.savePaymentPermissionAudits(this.ctx.subProject.id, [data.uid], 'add', transaction); await transaction.commit(); let tenderRptList = await this.getProcessList(this.ctx.paymentTender.id); tenderRptList = this._.filter(tenderRptList, { type: 0, is_del: 0 }); return { list: tenderRptList, updateRows: result2.affectedRows, updateData }; } catch (err) { await transaction.rollback(); throw err; } } } return paymentTenderRpt; };