| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 | '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('', null, 1);            this.transaction = await this.db.beginTransaction();            try {                let sort = 1;                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,                            pid: this.ctx.session.sessionProject.id,                            value: v.value,                            sort,                        });                        v.id = result.insertId;                        sort++;                    } else {                        await this.transaction.update(this.tableName, { id: v.id, value: v.value, sort });                        sort++;                    }                    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;};
 |