| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | 'use strict';/** * Created by EllisRan on 2020/3/3. */const BaseService = require('../base/base_service');module.exports = app => {    class ConstructionUnit extends BaseService {        /**         * 构造函数         *         * @param {Object} ctx - egg全局变量         * @return {void}         */        constructor(ctx) {            super(ctx);            this.tableName = 'construction_unit';            this.dataId = 'id';        }        /**         * 数据验证规则         *         * @param {String} scene - 场景         * @return {Object} - 返回数据         */        rule(scene) {            let rule = {};            switch (scene) {                case 'add':                    rule = {                        name: { type: 'string', required: true },                        type: { type: 'string', required: true },                    };                    break;                default:                    break;            }            return rule;        }        /**         * 修改单位数据         *         * @param {Object} data - post过来的数据         * @return {Boolean} - 返回修改结果         */        async save(data) {            if (data._csrf_j !== undefined) {                delete data._csrf_j;            }            const id = data.id !== undefined ? parseInt(data.id) : 0;            const transaction = await this.db.beginTransaction();            try {                if (id > 0) {                    // 修改操作时                    delete data.create_time;                    data.id = id;                } else {                    // 重名检测                    const accountData = await this.db.select(this.tableName, {                        where: {                            name: data.name,                            pid: data.pid,                        },                    });                    if (accountData.length > 0) {                        throw '已存在对应的单位名';                    }                    data.create_time = new Date();                }                // 并修改用户的用户组及单位名称                const updateRow = {};                if (data.name) updateRow.company = data.name;                if (data.type) updateRow.account_group = data.type;                if (!this._.isEmpty(updateRow)) {                    const info = id !== 0 ? await this.getDataById(id) : null;                    await this.ctx.service.projectAccount.update(updateRow, {                        project_id: data.pid,                        company: info ? info.name : data.name,                    }, transaction);                    // egg 自带的update方法无法使用UPDATE zh_project_account SET company = ? WHERE project_id = ? AND company = ? ,未知原因                    // await transaction.update(this.ctx.service.projectAccount.tableName, updateRow,                    //     {                    //         where: {                    //             project_id: data.pid,                    //             company: info ? info.name : data.name,                    //         },                    //     }                    // );                }                const operate = id === 0 ? await transaction.insert(this.tableName, data) :                    await transaction.update(this.tableName, data);                transaction.commit();                const result = operate.affectedRows > 0;                return result;            } catch (e) {                transaction.rollback();            }        }        async del(id) {            // 先删除签章oss地址            const info = await this.getDataById(id);            if (info.sign_path) {                await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.sign_path);            }            // 删除单位            return await this.db.delete(this.tableName, { id });        }    }    return ConstructionUnit;};
 |