'use strict'; /** * 主体成员表模型(subject_members) * * 一行 = 某主体下的一个成员: * - `name`:姓名(必填,创建成员时先只填姓名也行) * - `userId`:绑定的账号(可为空 —— 先建成员,之后用「扫码绑定」绑微信/账号) * - `tempUserId`:扫码绑定时**自动建给成员的账号**(被已有账号继承时删除) * - `role`:1 管理员 / 2 成员 * - `wechatOpenid` / `boundAt`:扫码绑定微信后记录 * * 同一主体下同一账号只能有一条(DB 唯一索引 `subject_id + user_id`)。 */ module.exports = (app) => { const { BIGINT, STRING, TINYINT, DATE } = app.Sequelize; const SubjectMember = app.model.define('subject_member', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, subjectId: { type: BIGINT.UNSIGNED, allowNull: false, comment: '主体 subjects.id' }, userId: { type: BIGINT.UNSIGNED, comment: '绑定账号 users.id(未绑定为空)' }, tempUserId: { type: BIGINT.UNSIGNED, comment: '自动建给成员的临时账号 users.id(被已有账号继承时删除)' }, name: { type: STRING(32), allowNull: false, comment: '成员姓名' }, role: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 2, comment: '角色:1管理员 2成员' }, wechatOpenid: { type: STRING(64), comment: '绑定的微信 openid' }, boundAt: { type: DATE, comment: '绑定账号/微信时间' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0禁用' }, }, { tableName: 'subject_members', paranoid: true, comment: '主体成员表', }); SubjectMember.associate = function() { app.model.SubjectMember.belongsTo(app.model.Subject, { foreignKey: 'subject_id', as: 'subject' }); app.model.SubjectMember.belongsTo(app.model.User, { foreignKey: 'user_id', as: 'user' }); }; return SubjectMember; };