course.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const { DataTypes } = require('sequelize');
  2. const moment = require('moment');
  3. const connect = require('./connect');
  4. /*
  5. type: DataTypes.STRING,
  6. allowNull: false,
  7. defaultValue:'',
  8. comment: '名',
  9. */
  10. function format(m) {
  11. m.href = `/course/${m.id}`;
  12. m.updatedMonth = moment(m.updatedAt).format('YYYY-MM');
  13. }
  14. const model = connect.define(
  15. __filename.slice(__dirname.length + 1).replace('.js', ''),
  16. {
  17. id: {
  18. type: DataTypes.INTEGER,
  19. autoIncrement: true,
  20. primaryKey: true,
  21. comment: 'id',
  22. },
  23. title: {
  24. type: DataTypes.STRING,
  25. defaultValue: '',
  26. comment: '标题',
  27. },
  28. desc: {
  29. type: DataTypes.STRING,
  30. defaultValue: '',
  31. comment: '描述',
  32. },
  33. detailimg: {
  34. type: DataTypes.STRING,
  35. defaultValue: '',
  36. comment: '详情大图',
  37. },
  38. detailvideo: {
  39. type: DataTypes.STRING,
  40. defaultValue: '',
  41. comment: '详情大图',
  42. },
  43. tagimg: {
  44. type: DataTypes.STRING,
  45. defaultValue: '',
  46. comment: '标签图',
  47. },
  48. teacherId: {
  49. type: DataTypes.JSON,
  50. defaultValue: [],
  51. comment: '讲师',
  52. },
  53. recordId: {
  54. type: DataTypes.INTEGER,
  55. comment: '录播讲师',
  56. },
  57. oprice: {
  58. type: DataTypes.DECIMAL(10, 2),
  59. defaultValue: 0,
  60. comment: '原价',
  61. },
  62. price: {
  63. type: DataTypes.DECIMAL(10, 2),
  64. defaultValue: 0,
  65. comment: '现价',
  66. },
  67. discountmsg: {
  68. type: DataTypes.STRING,
  69. defaultValue: '',
  70. comment: '优惠信息',
  71. },
  72. type: {
  73. type: DataTypes.TINYINT,
  74. defaultValue: 1,
  75. comment: '1碳资产开发2碳核算',
  76. },
  77. status: {
  78. type: DataTypes.TINYINT,
  79. defaultValue: 0,
  80. comment: '0草稿1已发布2下线',
  81. },
  82. publishAt: {
  83. type: DataTypes.DATE,
  84. comment: '发布时间',
  85. },
  86. weight: {
  87. type: DataTypes.INTEGER,
  88. defaultValue: 100,
  89. comment: '权重',
  90. },
  91. richText: {
  92. type: DataTypes.TEXT,
  93. defaultValue: '',
  94. comment: '富文本',
  95. },
  96. cHours: {
  97. type: DataTypes.FLOAT(4, 1),
  98. defaultValue: 0,
  99. comment: '课程时长',
  100. },
  101. cChapter: {
  102. type: DataTypes.INTEGER,
  103. defaultValue: 0,
  104. comment: '文章数',
  105. },
  106. },
  107. {
  108. comment: '课程',
  109. initialAutoIncrement: 10000,
  110. hooks: {
  111. beforeFind(option) {
  112. if (!option.order) {
  113. option.order = [
  114. ['weight', 'desc'],
  115. ['id', 'desc'],
  116. ];
  117. }
  118. },
  119. afterFind(m) {
  120. m && (Array.isArray(m) ? m.forEach(item => format(item)) : format(m));
  121. },
  122. },
  123. }
  124. );
  125. module.exports = model;