123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const audit = require('../const/audit');
- const pushType = require('../const/audit').pushType;
- module.exports = app => {
- class ChangeAudit extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change_audit';
- }
- /**
- * 获取最后一位审批人
- * @param {int} cid - 变更令id
- * @param {int} times - 次数
- * @param {int} site - 位置
- * @param {int} status - 状态
- * @return {void}
- */
- async getLastUser(cid, times, site = 1, status = 1) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('cid', {
- value: this.db.escape(cid),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('times', {
- value: times,
- operate: '=',
- });
- if (status === 1) {
- this.sqlBuilder.setAndWhere('status', {
- value: 1,
- operate: '!=',
- });
- }
- if (site === 0) {
- this.sqlBuilder.setAndWhere('usite', {
- value: site,
- operate: '=',
- });
- }
- const u_sort = [['usort', 'DESC']];
- this.sqlBuilder.orderBy = u_sort;
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const data = await this.db.queryOne(sql, sqlParam);
- return data;
- }
- /**
- * 获取最后一位退回的审批人
- * @param {int} cid - 变更令id
- * @param {int} times - 次数
- * @return {void}
- */
- async getLastBackUser(cid, times) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('cid', {
- value: this.db.escape(cid),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('times', {
- value: times,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('status', {
- value: 6,
- operate: '=',
- });
- const u_sort = [['usort', 'DESC']];
- this.sqlBuilder.orderBy = u_sort;
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const data = await this.db.queryOne(sql, sqlParam);
- return data;
- }
- /**
- * 获取当前审批人查看info时的状态
- * @param {Object} change - 变更令数据
- * @return {void}
- */
- async getStatusByChange(change) {
- const statusConst = audit.flow.status;
- const auditStatusConst = audit.flow.auditStatus;
- const uid = this.ctx.session.sessionUser.accountId;
- const changeAuditInfo = await this.getAllDataByCondition({ where: { cid: change.cid, times: change.times, uid }, orders: [['id', 'desc']], limit: 1, offset: 0 });
- if (!change.status === statusConst.checked && (changeAuditInfo === null || changeAuditInfo[0] === undefined)) {
- // 无权限查看此变更令
- return 0;
- }
- if (change.status === statusConst.uncheck && uid === change.uid) {
- // 待上报
- return 1;
- } else if (change.status === statusConst.back && uid === change.uid) {
- // 待重新上报
- return 2;
- } else if (change.status === statusConst.back && uid !== change.uid) {
- // 被退回但你不是原报人
- return 3;
- } else if (change.status === statusConst.checked) {
- // 已完成
- return 4;
- } else if (change.status === statusConst.checkNo) {
- // 已终止
- return 5;
- } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo[0].status === auditStatusConst.checking) {
- // 待你审批
- return 6;
- } else if ((change.status === statusConst.checking || change.status === statusConst.backnew) && changeAuditInfo[0].status !== auditStatusConst.checking) {
- // 审批中但你未到你审批或你已审批
- return 7;
- }
- // 无权限查看此变更令
- return 0;
- }
- /**
- * 根据用户查看此变更状态获取审批人列表
- * @param {Object} change - 变更令
- * @param {int} status - 状态
- * @return {object} list - 列表
- */
- async getListByStatus(change, status) {
- let sql = '';
- let sqlParam = '';
- switch (status) {
- case 1:// 待上报
- case 2:// 待重新上报
- sql = 'SELECT * FROM ?? WHERE ' +
- 'cid = ? AND times = ? GROUP BY usite';
- sqlParam = [this.tableName, change.cid,
- change.times];
- break;
- case 3: // 被退回但你不是原报人
- case 4:// 已完成
- case 5:// 已终止
- case 7:// 审批中但你未到你审批或你已审批
- // 获取完整的审批顺序
- sql = 'SELECT * FROM ?? WHERE ' +
- 'cid = ? ORDER BY usort';
- sqlParam = [this.tableName, change.cid];
- break;
- case 6: // 审批中
- sql = 'SELECT * FROM (SELECT MAX(usort) as ust FROM ?? ' +
- 'WHERE cid = ? and times = ? GROUP BY usite ) as b ' +
- 'JOIN ?? as a ON a.usort = b.ust WHERE cid = ? and times = ? ORDER BY usite';
- sqlParam = [this.tableName, change.cid, change.times, this.tableName, change.cid, change.times];
- // sql = 'SELECT * FROM ?? WHERE ' +
- // 'cid = ? AND times = ? ORDER BY usort';
- // sqlParam = [this.tableName, change.cid, change.times];
- break;
- default:// 无权限查看此变更令
- break;
- }
- const list = await this.db.query(sql, sqlParam);
- return list;
- }
- /**
- * 删除变更令审批人列表
- * @param {Object} transaction - 事务
- * @param {Object} cid - 变更令id
- * @param {int} times - 次数
- * @return {object} 返回结果
- */
- async deleteAuditData(transaction, cid, times) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('cid', {
- value: this.db.escape(cid),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('times', {
- value: times,
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('usite', {
- value: 0,
- operate: '!=',
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'delete');
- const result = await transaction.query(sql, sqlParam);
- return result;
- }
- /**
- * 获取不重复列表
- * @param {Object} cid - 变更令id
- * @param {int} times - 次数
- * @return {object} 返回结果
- */
- async getListGroupByTimes(cid, times) {
- const sql = 'SELECT * FROM ?? where id in (SELECT MAX(id) FROM ?? WHERE ' +
- 'cid = ? AND times = ? GROUP BY usite)';
- const sqlParam = [this.tableName, this.tableName, cid, times];
- const list = await this.db.query(sql, sqlParam);
- return list;
- }
- async getListOrderByTimes(cid, times) {
- const sql = 'SELECT * FROM ?? WHERE ' +
- 'cid = ? AND times = ? ORDER BY usort';
- const sqlParam = [this.tableName, cid, times];
- const list = await this.db.query(sql, sqlParam);
- return list;
- }
- /**
- * 获取比sort大的审批人列表
- * @param {Object} cid - 变更令id
- * @param {int} usort - 排序
- * @return {object} 返回结果
- */
- async getNextAuditList(cid, usort) {
- const sql = 'SELECT * FROM ?? WHERE ' +
- 'cid = ? AND usort > ?';
- const sqlParam = [this.tableName, cid, usort];
- const list = await this.db.query(sql, sqlParam);
- return list;
- }
- /**
- * 获取除当前次数的审批人列表
- * @param {Object} cid - 变更令id
- * @param {int} times - 次数
- * @return {object} 返回结果
- */
- async getListByBack(cid, times) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('cid', {
- value: this.db.escape(cid),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('times', {
- value: times,
- operate: '!=',
- });
- this.sqlBuilder.orderBy = [['usort', 'ASC']];
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const result = await this.db.query(sql, sqlParam);
- return result;
- }
- /**
- * 获取 审核人 待审批的() 变更令列表
- * @param uid
- * @return {Promise<void>}
- */
- async getAuditChange(uid) {
- const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
- ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, ' +
- ' t.`name`, t.`type`, t.`user_id` ' +
- ' FROM ?? AS ca, ?? AS c, ?? As t ' +
- ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ?' +
- ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id`';
- const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, uid, 2, audit.flow.status.uncheck];
- const changes = await this.db.query(sql, sqlParam);
- for (const c of changes) {
- if (c.cstatus === audit.flow.status.back) {
- const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
- ' WHERE ca.cid = ? and ca.times = ? and ca.status = ? and ca.uid = pa.id';
- const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times - 1, c.cstatus];
- c.pre = await this.db.queryOne(preSql, preSqlParam);
- } else {
- const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
- ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
- const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
- c.pre = await this.db.queryOne(preSql, preSqlParam);
- }
- }
- return changes;
- }
- /**
- * 获取 某时间后 审批进度更新的 变更令
- * @param {Number} pid - 查询标段
- * @param {Number} uid - 查询人
- * @param {Date} time - 查询时间
- * @return {Promise<*>}
- */
- async getNoticeChange(pid, uid, time) {
- // const sql = 'SELECT * FROM (SELECT t.`id`, t.`name`, t.`type`, t.`user_id`, ' +
- // ' ca.`cid`, c.`code` As `c_code`, c.name As `c_name`, ' +
- // ' ca.`uid`, ca.`sin_time` As `cu_time`, ca.`status` As `cu_status`, ca.`name` As `cu_name`, ca.`jobs` As `cu_jobs`, ca.company As `cu_company`' +
- // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `uid` = ? GROUP BY `tid`)) As t' +
- // ' LEFT JOIN ?? As c ON c.`tid` = t.`id`' +
- // ' LEFT JOIN ?? As ca ON ca.`cid` = c.`cid`' +
- // ' WHERE t.`project_id` = ? and `ca`.`sin_time` > ? and `ca`.`usite` != 0 and `ca`.`status` != ?' +
- // ' ORDER By ca.`sin_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
- // ' ORDER BY new_t.`cu_time`';
- // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.change.tableName, this.tableName, pid, time, audit.flow.status.checking];
- // return await this.db.query(sql, sqlParam);
- let notice = await this.db.select('zh_notice', {
- where: { pid, type: pushType.change, uid, is_read: 0 },
- });
- notice = notice.map(v => {
- const extra = JSON.parse(v.content)
- delete v.content
- return { ...v, ...extra }
- })
- return notice;
- }
- async getAllAuditors(tenderId) {
- const sql = 'SELECT ca.uid, ca.tid FROM ' + this.tableName + ' ca' +
- ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ca.tid = t.id' +
- ' WHERE t.id = ?' +
- ' GROUP BY ca.uid';
- const sqlParam = [tenderId];
- return this.db.query(sql, sqlParam);
- }
- /**
- * 取待审批变更列表(wap用)
- *
- * @param auditorId
- * @return {Promise<*>}
- */
- async getAuditChangeByWap(uid) {
- const sql = 'SELECT ca.`uid`, ca.`times`, ca.`usite`, ca.`usort`, ca.`tid`, ca.`cid`, ca.`sin_time`, ca.`name` As `caname`, ' +
- ' c.`code` As `ccode`, c.`name` As `cname`, c.`status` As `cstatus`, c.`quality`, c.`total_price`,' +
- ' t.`name`, t.`type`, t.`user_id`, ' +
- ' ti.`deal_info`, ti.`decimal` ' +
- ' FROM ?? AS ca, ?? AS c, ?? As t, ?? AS ti ' +
- ' WHERE ca.`uid` = ? and ca.`status` = ? and c.`status` != ? and c.`status` != ?' +
- ' and ca.`cid` = c.`cid` and ca.`tid` = t.`id` and ti.`tid` = t.`id`';
- const sqlParam = [this.tableName, this.ctx.service.change.tableName, this.ctx.service.tender.tableName, this.ctx.service.tenderInfo.tableName, uid, audit.flow.auditStatus.checking, audit.flow.status.uncheck, audit.flow.status.back];
- const changes = await this.db.query(sql, sqlParam);
- for (const c of changes) {
- const preSql = 'SELECT pa.`id`, pa.`account`, pa.`account_group`, pa.`name`, pa.`company`, pa.`role`, pa.`telephone` FROM ?? As ca, ?? As pa ' +
- ' WHERE ca.cid = ? and ca.times = ? and ca.usort = ? and ca.uid = pa.id';
- const preSqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, c.cid, c.times, c.usort - 1];
- c.pre = await this.db.queryOne(preSql, preSqlParam);
- }
- return await this.db.query(sql, sqlParam);
- }
- }
- return ChangeAudit;
- };
|