'use strict';

/**
 *
 *
 * @author Mai
 * @date 2018/10/15
 * @version
 */

module.exports = app => {
    class CategoryValue extends app.BaseService {
        /**
         * 构造函数
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        constructor(ctx) {
            super(ctx);
            this.tableName = 'category_value';
        }

        /**
         * 新增分类值
         *
         * @param categoryId - 分类Id
         * @param value - 值
         * @returns {Promise<void>}
         * @private
         */
        async _addValue(categoryId, value) {
            const count = await this.count({cid: categoryId, value: value});
            if (count > 0) {
                throw '该分类下存在相同的值';
            }

            const newValue = {
                cid: categoryId,
                value: value,
            };
            const result = await this.transaction.insert(this.tableName, newValue);
            if (result.affectedRows !== 1) {
                throw '提交数据失败';
            } else {
                category.id = result.insertId;
            }

            category.value = category.value && category.value !== '' ? JSON.parse(category.value) : [];
            return category;
        }

        async _deleteAndUpdateValue(orgValue, newValue) {
            if (!orgValue || orgValue.length === 0) { return }
            for (const ov of orgValue) {
                const nv = _.find(newValue, function (v) {
                    return v.id === orgValue.id;
                });
                if (!nv) {
                    await this.transaction.delete(this.tableName, {id: orgValue.id});
                } else if (nv.value !== ov.value) {
                    await this.transaction.update(this.tableName, {id: orgValue.id, value: nv.value});
                }
            }
        }

        async setCategoryValue(cid, value) {
            const tenders = await this.ctx.service.tender.getList();

            this.transaction = await this.db.beginTransaction();
            try {
                for (const v of value) {
                    if (v.delete) {
                        await this.transaction.delete(this.tableName, {id: v.id});
                    } else if (v.new) {
                        const result = await this.transaction.insert(this.tableName, {
                            cid: cid,
                            pid: this.ctx.session.sessionProject.id,
                            value: v.value,
                        });
                        v.id = result.insertId;
                    } else {
                        await this.transaction.update(this.tableName, {id: v.id, value: v.value});
                    }
                    if (!v.newTenders) { continue }
                    for (const nt of v.newTenders) {
                        const tender = this._.find(tenders, function (t) { return t.id === nt; });
                        let cate = this._.find(tender.category, function (c) { return c.cid === cid; });
                        if (!cate) {
                            cate = {cid: cid};
                            tender.category.push(cate);
                        }
                        cate.value = v.id;
                        await this.transaction.update(this.service.tender.tableName, {
                            id: tender.id,
                            category: JSON.stringify(tender.category),
                        });
                    }
                }
                await this.transaction.commit();
            } catch(error) {
                await this.transaction.rollback();
                throw error;
            }
        }

    }

    return CategoryValue;
};