'use strict';

/**
 * 标段数据模型
 *
 * @author CaiAoLin
 * @date 2017/11/30
 * @version
 */

module.exports = app => {

    class Tender extends app.BaseService {

        /**
         * 构造函数
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        constructor(ctx) {
            super(ctx);
            this.tableName = 'tender';
            this.status = {
                INIT: 1,
            };
        }

        /**
         * 数据规则
         *
         * @param {String} scene - 场景
         * @return {Object} - 返回数据规则
         */
        rule(scene) {
            let rule = {};
            switch (scene) {
                case 'add':
                    rule = {
                        name: { type: 'string', required: true, min: 2 },
                    };
                    break;
                default:
                    break;
            }

            return rule;
        }

        /**
         * 获取标段列表
         *
         * @return {Array} - 返回标段数据
         */
        async getList() {
            // 获取当前用户信息
            const sessionUser = this.ctx.session.sessionUser;
            this.initSqlBuilder();
            this.sqlBuilder.setAndWhere('project_id', {
                value: sessionUser.projectId,
                operate: '=',
            });
            this.sqlBuilder.setAndWhere('user_id', {
                value: sessionUser.accountId,
                operate: '=',
            });
            this.sqlBuilder.limit = 10;
            this.sqlBuilder.columns = ['id', 'name', 'status'];
            const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);

            return await this.db.query(sql, sqlParam);
        }

        /**
         * 新增标段
         *
         * @param {Object} postData - 表单post过来的数据
         * @return {Boolean} - 返回新增结果
         */
        async add(postData) {
            let result = false;
            this.transaction = await this.db.beginTransaction();
            try {
                // 获取当前用户信息
                const sessionUser = this.ctx.session.sessionUser;
                const insertData = {
                    name: postData.name,
                    status: this.status.INIT,
                    project_id: sessionUser.projectId,
                    user_id: sessionUser.accountId,
                    create_time: postData.create_time,
                };
                const operate = await this.transaction.insert(this.tableName, insertData);
                result = operate.insertId > 0;
                if (!result) {
                    throw '新增标段数据失败';
                }

                // 获取标段项目节点模板
                const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData();
                // 复制模板数据到标段数据表
                this.ctx.service.tenderNode.transaction = this.transaction;
                const copyResult = await this.ctx.service.tenderNode.add(tenderNodeTemplateData, operate.insertId);

                if (!copyResult) {
                    throw '新增标段项目节点失败';
                }
                this.transaction.commit();
            } catch (error) {
                result = false;
                this.transaction.rollback();
            }

            return result;
        }

    }

    return Tender;
};
