'use strict';

/**
 * 客户信息数据模型
 *
 * @author CaiAoLin
 * @date 2017/11/14
 * @version
 */

module.exports = app => {

    class Customer extends app.BaseService {

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

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

            return rule;
        }

        /**
         * 查询过虑
         *
         * @param {Object} data - 筛选表单中的get数据
         * @return {void}
         */
        searchFilter(data) {
            this.initSqlBuilder();
        }

        /**
         * 新增SSO用户
         *
         * @param {Object} data - 接口返回的数据
         * @return {Number} - 返回新增数据的id
         */
        async addSSOUser(data) {
            let result = 0;
            if (Object.keys(data).length <= 0 || data.useremail === undefined) {
                return result;
            }

            // 先查找是否存在
            const customerData = await this.db.get(this.tableName, { email: data.useremail });
            if (customerData !== null) {
                // 存在则直接返回id
                return customerData.id;
            }
            const insertData = {
                email: data.useremail,
                mobile: data.mobile,
                name: data.username,
            };
            result = await this.db.insert(this.tableName, insertData);

            return result.insertId;
        }

        /**
         * 判断是否需要初始化信息
         *
         * @param {Object} postData - 表单post过来的数据
         * @return {Boolean} - 返回是否需要初始化结果
         */
        async isNeedBoot(postData) {
            if (postData.username === undefined) {
                return false;
            }

            const customer = await this.getDataByCondition({ email: postData.username });
            if (customer === null) {
                return false;
            }

            return customer.company === '';
        }

        /**
         * 初始化用户信息
         *
         * @param {Object} postData - 表单post过来的数据
         * @return {Boolean} - 返回初始化结果
         */
        async boot(postData) {
            const sessionUser = this.ctx.session.sessionUser;

            delete postData._csrf;
            const result = await this.update(postData, { email: sessionUser.account });

            return result;
        }
    }

    return Customer;
};
