12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2018/6/12
- * @version
- */
- const audit = require('../const/audit').revise;
- module.exports = app => {
- class LedgerRevise extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'ledger_revise';
- }
- /**
- * 获取标段下,修订(分页,且按时间倒序)
- * @param {Number}tid - 标段id
- * @returns {Promise<*>} - ledger_change下所有数据,并关联 project_account(读取提交人名称、单位、公司)
- */
- async getReviseList (tid) {
- const sql = 'SELECT *, pa.name As user_name, pa.role As user_role, pa.company As user_company' +
- ' FROM ' + this.tableName + ' As lc' +
- ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +
- ' WHERE lc.tid = ?' +
- ' ORDER BY lc.in_time DESC' +
- ' LIMIT ?, ?';
- const Len = this.app.config.pageSize;
- const sqlParam = [tid, (this.ctx.page - 1) * Len, Len];
- return await this.db.query(sql, sqlParam);
- }
- /**
- * 获取新增修订的序号
- * @param {Number}tid - 标段id
- * @returns {Promise<number>}
- */
- async getNewOrder(tid) {
- const sql = 'SELECT Max(`corder`) As max_order FROM ' + this.tableName + ' Where `tid` = ? and `valid`';
- const sqlParam = [tid];
- const result = await this.db.queryOne(sql, sqlParam);
- return result.max_order || 0;
- // if (result && result.max_order) {
- // return result.max_order;
- // } else {
- // return 0;
- // }
- }
- /**
- * 新增修订
- * @param {Number}tid - 标段id
- * @param {Number}uid - 提交人id
- * @returns {Promise<void>}
- */
- async add(tid, uid) {
- if (!tid && !uid) {
- throw '数据错误';
- }
- const maxOrder = await this.getNewOrder(tid);
- const data = {
- id: this.uuid.v4(), tid: tid, uid: uid,
- corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck,
- };
- const transaction = await this.db.beginTransaction();
- try {
- const result = await transaction.insert(this.tableName, data);
- if (result.affectedRows !== 1) {
- throw '新增台账修订失败';
- }
- // todo 拷贝数据(清单、部位明细)
- await transaction.commit();
- } catch(err) {
- await transaction.rollback();
- throw err;
- }
- }
- }
- return LedgerRevise;
- };
|