'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;
};

