123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const audit = require('../const/audit').changeProject;
- // const smsTypeConst = require('../const/sms_type');
- // const SMS = require('../lib/sms');
- // const SmsAliConst = require('../const/sms_alitemplate');
- const wxConst = require('../const/wechat_template');
- const pushType = require('../const/audit').pushType;
- const projectLogConst = require('../const/project_log');
- const codeRuleConst = require('../const/code_rule');
- module.exports = app => {
- class ChangeProject extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change_project';
- }
- async add(tenderId, userId, code, name) {
- const type = userId === this.ctx.tender.data.user_id ? codeRuleConst.ruleType.suggestion : codeRuleConst.ruleType.will;
- const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND `code` = ? AND type = ?';
- const sqlParam = [this.tableName, tenderId, code, audit.status.checked, code, audit.status.checked, type];
- const codeCount = await this.db.queryOne(sql, sqlParam);
- const count = codeCount.count;
- if (count > 0) {
- throw '立项书编号重复';
- }
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const change = {
- tid: tenderId,
- uid: userId,
- status: audit.status.uncheck,
- times: 1,
- type,
- in_time: new Date(),
- code,
- name,
- };
- const operate = await this.transaction.insert(this.tableName, change);
- if (operate.affectedRows <= 0) {
- throw '新建变更令数据失败';
- }
- change.id = operate.insertId;
- // 先找出标段最近存在的变更令审批人的变更令info
- const preChangeInfo = await this.getHaveAuditLastInfo(tenderId, type);
- if (preChangeInfo) {
- // 并把之前存在的变更令审批人添加到zh_change_audit
- const auditResult = await this.ctx.service.changeProjectAudit.copyPreChangeProjectAuditors(this.transaction, preChangeInfo, change);
- if (!auditResult) {
- throw '复制上一次审批流程失败';
- }
- }
- result = change;
- this.transaction.commit();
- } catch (error) {
- console.log(error);
- // 回滚
- await this.transaction.rollback();
- }
- return result;
- }
- async getHaveAuditLastInfo(tenderId, type) {
- const sql = 'SELECT a.* FROM ?? as a LEFT JOIN ?? as b ON a.`id` = b.`cpid` WHERE a.`tid` = ? AND a.type = ? ORDER BY a.`in_time` DESC';
- const sqlParam = [this.tableName, this.ctx.service.changeProjectAudit.tableName, tenderId, type];
- return await this.db.queryOne(sql, sqlParam);
- }
- /**
- * 获取变更立项列表
- * @param {int} tenderId - 标段id
- * @param {int} status - 状态
- * @param {int} hadlimit - 分页
- * @return {object} list - 列表
- */
- async getListByStatus(tenderId, status = 0, hadlimit = 1, sortBy = '', orderBy = '') {
- let sql = '';
- let sqlParam = '';
- if (this.ctx.tender.isTourist && status === 0) {
- sql = 'SELECT a.*, p.name as account_name FROM ?? As a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId];
- } else {
- switch (status) {
- case 0: // 包含你的所有变更立项
- sql =
- 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ? AND ' +
- '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid)) OR a.status = ? )';
- sqlParam = [
- this.tableName,
- this.ctx.service.projectAccount.tableName,
- tenderId,
- this.ctx.session.sessionUser.accountId,
- audit.status.uncheck,
- this.ctx.service.changeProjectAudit.tableName,
- this.ctx.session.sessionUser.accountId,
- audit.status.checked,
- ];
- break;
- case 1: // 待处理(你的)
- sql = 'SELECT a.*, p.name as account_name FROM ?? as a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.id in(SELECT b.cpid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ?))';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.changeProjectAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.back];
- break;
- case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
- sql =
- 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
- // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) AND ' +
- 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
- sqlParam = [
- this.tableName,
- this.ctx.service.projectAccount.tableName,
- // this.ctx.service.changeProjectAudit.tableName,
- this.ctx.session.sessionUser.accountId,
- tenderId,
- audit.status.uncheck,
- audit.status.back,
- ];
- break;
- case 2: // 进行中(所有的)
- case 4: // 终止(所有的)
- sql =
- 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
- 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid))';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeProjectAudit.tableName, this.ctx.session.sessionUser.accountId];
- break;
- case 3: // 已完成(所有的)
- sql = 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.status = ? AND a.tid = ?';
- sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId];
- break;
- default:
- break;
- }
- }
- if (sortBy && orderBy) {
- if (sortBy === 'code') {
- sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
- } else {
- sql += ' ORDER BY a.in_time ' + orderBy;
- }
- } else {
- sql += ' ORDER BY a.in_time DESC';
- }
- if (hadlimit) {
- const limit = this.app.config.pageSize;
- const offset = limit * (this.ctx.page - 1);
- const limitString = offset >= 0 ? offset + ',' + limit : limit;
- sql += ' LIMIT ' + limitString;
- }
- const list = await this.db.query(sql, sqlParam);
- return list;
- }
- /**
- * 获取变更令个数
- * @param {int} tenderId - 标段id
- * @param {int} status - 状态
- * @return {void}
- */
- async getCountByStatus(tenderId, status) {
- if (this.ctx.tender.isTourist && status === 0) {
- const sql5 = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
- const sqlParam5 = [this.tableName, tenderId];
- const result5 = await this.db.query(sql5, sqlParam5);
- return result5[0].count;
- }
- switch (status) {
- case 0: // 包含你的所有变更令
- const sql =
- 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
- '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid)) OR a.status = ? )';
- const sqlParam = [
- this.tableName,
- tenderId,
- this.ctx.session.sessionUser.accountId,
- audit.status.uncheck,
- this.ctx.service.changeProjectAudit.tableName,
- this.ctx.session.sessionUser.accountId,
- audit.status.checked,
- ];
- const result = await this.db.query(sql, sqlParam);
- return result[0].count;
- case 1: // 待处理(你的)
- // return await this.ctx.service.changeAudit.count({
- // tid: tenderId,
- // uid: this.ctx.session.sessionUser.accountId,
- // status: 2,
- // });
- const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE a.id in(SELECT b.cpid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ?))';
- const sqlParam6 = [this.tableName, this.ctx.service.changeProjectAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.back];
- const result6 = await this.db.query(sql6, sqlParam6);
- return result6[0].count;
- case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
- const sql2 =
- 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
- // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) ' +
- 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
- const sqlParam2 = [
- this.tableName,
- // this.ctx.service.changeProjectAudit.tableName,
- this.ctx.session.sessionUser.accountId,
- tenderId,
- audit.status.uncheck,
- audit.status.back,
- ];
- const result2 = await this.db.query(sql2, sqlParam2);
- return result2[0].count;
- case 2: // 进行中(所有的)
- case 4: // 终止(所有的)
- const sql3 =
- 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
- 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid))';
- const sqlParam3 = [this.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeProjectAudit.tableName, this.ctx.session.sessionUser.accountId];
- const result3 = await this.db.query(sql3, sqlParam3);
- return result3[0].count;
- case 3: // 已完成(所有的)
- const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
- const sqlParam4 = [this.tableName, status, tenderId];
- const result4 = await this.db.query(sql4, sqlParam4);
- return result4[0].count;
- default:
- break;
- }
- }
- /**
- * 保存变更信息
- * @param {int} postData - 表单提交的数据
- * @param {int} tenderId - 标段id
- * @return {void}
- */
- async saveInfo(cpId, postData) {
- // 初始化事务
- const transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const updateData = {
- id: cpId,
- };
- updateData[postData.name] = postData.val;
- await transaction.update(this.tableName, updateData);
- await transaction.commit();
- result = true;
- } catch (error) {
- await transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 审批终止
- * @param {int} postData - 表单提交的数据
- * @return {void}
- */
- async approvalStop(postData) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- // 设置审批人终止
- const audit_update = {
- id: postData.audit_id,
- sdesc: postData.sdesc,
- status: 4,
- sin_time: new Date(),
- };
- await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
- // 设置变更令终止
- const change_update = {
- w_code: postData.w_code,
- status: 4,
- cin_time: Date.parse(new Date()) / 1000,
- };
- const options = {
- where: {
- cid: postData.change_id,
- },
- };
- await this.transaction.update(this.tableName, change_update, options);
- await this.transaction.commit();
- result = true;
- } catch (error) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 查询可用的变更令
- * @param { string } cid - 查询的清单
- * @return {Promise<*>} - 可用的变更令列表
- */
- async delete(cid) {
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- const changeInfo = await this.getDataByCondition({ cid });
- // 先删除清单,审批人列表
- await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
- await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
- // 再删除附件和附件文件ni zuo
- const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
- await this.ctx.helper.delFiles(attList);
- await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
- // if (attList.length !== 0) {
- // for (const att of attList) {
- // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
- // }
- // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
- // }
- // 最后删除变更令
- await this.transaction.delete(this.tableName, { cid });
- // 记录删除日志
- await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.change, projectLogConst.status.delete, changeInfo.code);
- await this.transaction.commit();
- result = true;
- } catch (e) {
- await this.transaction.rollback();
- result = false;
- }
- return result;
- }
- /**
- * 判断是否有重名的变更立项
- * @param cpid
- * @param code
- * @param tid
- * @return {Promise<void>}
- */
- async isRepeat(cpId, code, tid, type) {
- const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ? AND `type` = ?';
- const sqlParam = [this.tableName, code, cpId, tid, type];
- const result = await this.db.queryOne(sql, sqlParam);
- return result.count !== 0;
- }
- }
- return ChangeProject;
- };
|