'use strict'; /** * 决策大屏用户查看权限-数据模型 * * @author ellisran * @date 2021/09/23 * @version */ const accountGroup = require('../const/account_group').group; const paymentConst = require('../const/payment'); module.exports = app => { class paymentTenderRpt extends app.BaseService { constructor(ctx) { super(ctx); this.tableName = 'payment_tender_rpt'; } async getProcessList(id) { const sql = 'SELECT ptr.*, pa.name as user_name FROM ?? as ptr LEFT JOIN ?? as pa ON ptr.`uid` = pa.`id` WHERE ptr.`tender_id` = ?'; const params = [this.tableName, this.ctx.service.projectAccount.tableName, id]; return await this.db.query(sql, params); } async checkAndUpdateList(tenderRptList, rptProjectList) { if (tenderRptList.length > 0) { const updateDatas = []; const delDatas = []; for (const tr of tenderRptList) { 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); } } else if (rptInfo && tr.rpt_name !== rptInfo.name) { updateDatas.push({ id: tr.id, rpt_name: rptInfo.name, }); tr.rpt_name = rptInfo.name; } rptInfo.had_rpt = had_rpt; } if (updateDatas.length > 0) await this.db.updateRows(this.tableName, updateDatas); if (delDatas.length > 0) { this._.remove(tenderRptList, function(item) { return delDatas.indexOf(item.id) !== -1; }); await this.db.delete(this.tableName, { id: delDatas }); } } return [tenderRptList, rptProjectList]; } async setRpt(tid, rpt_list) { const transaction = await this.db.beginTransaction(); try { const originList = await this.getAllDataByCondition({ where: { tender_id: tid, is_del: 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, 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); } } console.log(insertRptIds, deleteData); for (const id of insertRptIds) { const info = this._.find(rpt_list, { id }); insertData.push({ tender_id: tid, rpt_id: id, rpt_name: info.name, 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 transaction.commit(); return await this.getProcessList(tid); } 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; } } } return paymentTenderRpt; };