const { DataTypes } = require('sequelize'); const moment = require('moment'); const connect = require('./connect'); /* type: DataTypes.STRING, allowNull: false, defaultValue:'', comment: '名', */ function format(m) { m.href = `/course/${m.id}`; m.updatedMonth = moment(m.updatedAt).format('YYYY-MM'); } const model = connect.define( __filename.slice(__dirname.length + 1).replace('.js', ''), { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, comment: 'id', }, title: { type: DataTypes.STRING, defaultValue: '', comment: '标题', }, desc: { type: DataTypes.STRING, defaultValue: '', comment: '描述', }, detailimg: { type: DataTypes.STRING, defaultValue: '', comment: '详情大图', }, detailvideo: { type: DataTypes.STRING, defaultValue: '', comment: '详情大图', }, tagimg: { type: DataTypes.STRING, defaultValue: '', comment: '标签图', }, teacherId: { type: DataTypes.JSON, defaultValue: [], comment: '讲师', }, recordId: { type: DataTypes.INTEGER, comment: '录播讲师', }, oprice: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0, comment: '原价', }, price: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0, comment: '现价', }, discountmsg: { type: DataTypes.STRING, defaultValue: '', comment: '优惠信息', }, type: { type: DataTypes.TINYINT, defaultValue: 1, comment: '1碳资产开发2碳核算', }, status: { type: DataTypes.TINYINT, defaultValue: 0, comment: '0草稿1已发布2下线', }, publishAt: { type: DataTypes.DATE, comment: '发布时间', }, weight: { type: DataTypes.INTEGER, defaultValue: 100, comment: '权重', }, richText: { type: DataTypes.TEXT, defaultValue: '', comment: '富文本', }, cHours: { type: DataTypes.FLOAT(4, 1), defaultValue: 0, comment: '课程时长', }, cChapter: { type: DataTypes.INTEGER, defaultValue: 0, comment: '文章数', }, }, { comment: '课程', initialAutoIncrement: 10000, hooks: { beforeFind(option) { if (!option.order) { option.order = [ ['weight', 'desc'], ['id', 'desc'], ]; } }, afterFind(m) { m && (Array.isArray(m) ? m.forEach(item => format(item)) : format(m)); }, }, } ); module.exports = model;