123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 'use strict';
- /**
- * 中间计量数据模型
- *
- * @author Mai
- * @date 2018/6/27
- * @version
- */
- const auditConst = require('../const/audit').flow;
- const moment = require('moment');
- module.exports = app => {
- class Measure extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'measure';
- }
- /**
- * 新增中间计量
- * @param code - 编号
- * @param date - 年月
- * @param stage - 所属期
- * @returns {Promise<void>}
- */
- async add(tenderId, code, userId, date, stage) {
- const count = await this.ctx.service.measure.count({
- tender_id: tenderId, code: code,
- });
- if (count === 0) {
- const newData = {
- tender_id: tenderId,
- mid: this.uuid.v4(),
- code: code,
- in_time: new Date(),
- user_id: userId,
- status: auditConst.status.uncheck,
- valid: true,
- };
- const result = await this.ctx.service.measure.db.insert(this.tableName, newData);
- if (result.affectedRow === 0) {
- throw '新增中间计量失败';
- }
- } else {
- throw '您输入的编号已存在';
- }
- }
- async invalid(mid, userId) {
- const measure = this.getDataByCondition({mid: mid});
- if (measure.user_id === userId) {
- const result = await this.update({valid: false}, {mid: mid});
- return result.affectedRow === 0;
- } else {
- throw '数据错误';
- }
- }
- async _getAssistData(m) {
- m.in_time_str = moment(m.in_time_str).format('YYYYMM');
- if (m.times > 1 && m.status === auditConst.status.checkNo) {
- m.curAuditor = await this.ctx.service.measureAudit.getAuditor(m.mid, m.user_id, m.times);
- } else {
- m.curAuditor = await this.ctx.service.measureAudit.getCurAuditor(m.mid, m.times);
- }
- m.user = await this.ctx.service.projectAccount.getAccountInfoById(m.user_id);
- }
- async joinMeasure(tenderId, audit_id) {
- const sql = 'SELECT M.* FROM ?? As M, ?? As MA ' +
- ' WHERE M.`tender_id` = ? and MA.`audit_id` = ? and M.times = MA.times and MA.`order` > 0 and M.valid = true';
- const sqlParam = [this.tableName, this.ctx.service.measureAudit.tableName, tenderId, audit_id];
- const measures = await this.db.query(sql, sqlParam);
- for (const m of measures) {
- await this._getAssistData(m);
- }
- return measures;
- }
- async measureList(condition) {
- const measures = await this.getAllDataByCondition({where: condition});
- for (const m of measures) {
- await this._getAssistData(m);
- }
- return measures;
- }
- async measureData (condition) {
- const measure = await this.getDataByCondition(condition);
- await this._getAssistData(measure);
- return measure;
- }
- }
- return Measure;
- };
|