123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/8/14
- * @version
- */
- const audit = require('../const/audit');
- module.exports = app => {
- class Change extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'change';
- }
- /**
- * 查找数据
- *
- * @param {Object} data - 筛选表单中的get数据
- * @return {void}
- */
- searchFilter(data) {
- this.initSqlBuilder();
- // this.sqlBuilder.columns = ['id', 'username', 'real_name', 'create_time', 'last_login', 'login_ip',
- // 'group_id', 'token', 'can_login'];
- data.type = parseInt(data.status);
- if (data.keyword !== undefined) {
- switch (data.type) {
- // 用户名
- case 1:
- this.sqlBuilder.setAndWhere('username', {
- value: this.db.escape(data.keyword + '%'),
- operate: 'like',
- });
- break;
- // 姓名
- case 2:
- this.sqlBuilder.setAndWhere('real_name', {
- value: this.db.escape(data.keyword + '%'),
- operate: 'like',
- });
- break;
- // 联系电话
- case 3:
- this.sqlBuilder.setAndWhere('telephone', {
- value: this.db.escape(data.keyword + '%'),
- operate: 'like',
- });
- break;
- default:
- break;
- }
- }
- // 办事处筛选
- if (data.office !== undefined && data.office !== '') {
- this.sqlBuilder.setAndWhere('office', {
- value: this.db.escape(data.office),
- operate: '=',
- });
- }
- }
- async add(tenderId, userId, code, name) {
- const count = await this.count({tid: tenderId, code: code});
- if (count > 0) {
- throw '变更令号重复';
- }
- // 初始化事务
- this.transaction = await this.db.beginTransaction();
- let result = false;
- try {
- let cid = this.uuid.v4();
- const change = {
- cid: cid,
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.uncheck,
- times: 1,
- valid: true,
- in_time: new Date(),
- code: code,
- name: name,
- };
- const operate = await this.transaction.insert(this.tableName, change);
- if (operate.affectedRows <= 0) {
- throw '新建变更令数据失败';
- }
- // 把提交人信息添加到zh_change_audit
- this.ctx.service.changeAudit.transaction = this.transaction;
- const userInfo = await this.ctx.service.projectAccount.getDataById(userId);
- const changeaudit = {
- tid: tenderId,
- cid: cid,
- uid: userId,
- name: userInfo.name,
- jobs: userInfo.role,
- company: userInfo.company,
- times: 1,
- usite: 0,
- usort: 0,
- status: 2
- };
- await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit);
- result = change;
- this.transaction.commit();
- } catch (error) {
- console.log(error);
- // 回滚
- await this.transaction.rollback();
- }
- return result;
- }
- async pendingDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checking,
- });
- }
- async uncheckDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.uncheck,
- });
- }
- async checkingDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checking,
- });
- }
- async checkedDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checked,
- });
- }
- async checkNoDatas(tenderId, userId) {
- return await this.getAllDataByCondition({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checkNo,
- });
- }
- async checkNoCount(tenderId, userId) {
- return await this.count({
- tid: tenderId,
- uid: userId,
- status: audit.flow.status.checkNo,
- });
- }
- /**
- * 获取变更令列表
- * @param tenderId 标段id
- * @param userId 创建者id
- * @param status 状态
- * @return {Promise.<void>}
- */
- async getListByStatus(tenderId, status = 0) {
- let sql = '';
- let sqlParam = '';
- switch (status) {
- case 0:
- // 包含你的所有变更令
- sql = 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
- '(a.uid = ? OR (a.status != 1 AND a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid)))';
- sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
- this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
- break;
- case 1:
- // 待处理(你的)
- sql = 'SELECT * FROM ?? WHERE tid = ? AND uid = ? AND status = ?';
- sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId, status];
- break;
- case 2:
- // 待上报(所有的)PS:取未上报和退回的变更令
- sql = 'SELECT a.* FROM ?? AS a WHERE ' +
- 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
- '(a.status = 1 OR a.status = 5) AND a.tid = ?';
- sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
- this.ctx.session.sessionUser.accountId, tenderId];
- break;
- case 3:
- // 进行中(所有的)
- case 4:
- // 已完成(所有的)
- case 5:
- // 终止(所有的)
- sql = 'SELECT a.* FROM ?? AS a WHERE ' +
- 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
- 'a.status = ? AND a.tid = ?';
- sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
- this.ctx.session.sessionUser.accountId, status, tenderId];
- break;
- default:
- break;
- }
- 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 tenderId 标段id
- * @param userId 创建者id
- * @param status 状态
- * @return {Promise.<*>}
- */
- async getCountByStatus(tenderId, status) {
- switch (status) {
- case 0:
- // 包含你的所有变更令
- let sql = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
- '(a.uid = ? OR a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid))';
- let sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
- this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
- let result = await this.db.query(sql, sqlParam);
- return result[0]['count'];
- break;
- case 1:
- // 待处理(你的)
- return await this.count({
- tid: tenderId,
- uid: this.ctx.session.sessionUser.accountId,
- status: status,
- });
- break;
- case 2:
- // 待上报(所有的)PS:取未上报和退回的变更令
- let sql2 = 'SELECT count(*) AS count FROM ?? AS a WHERE a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND (a.status = 1 OR a.status = 5) AND a.tid = ?';
- let sqlParam2 = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, tenderId];
- let result2 = await this.db.query(sql2, sqlParam2);
- return result2[0]['count'];
- break;
- case 3:
- // 进行中(所有的)
- case 4:
- // 已完成(所有的)
- case 5:
- // 终止(所有的)
- let sql3 = 'SELECT count(*) AS count FROM ?? AS a WHERE a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND a.status = ? AND a.tid = ?';
- let sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, status, tenderId];
- let result3 = await this.db.query(sql3, sqlParam3);
- return result3[0]['count'];
- break;
- default:
- break;
- }
- }
- }
- return Change;
- };
|