当前位置:   article > 正文

基于Vue+Nodejs实现宿舍管理系统_基于vue和node的宿舍管理系统

基于vue和node的宿舍管理系统

作者主页:编程指南针

作者简介:Java领域优质创作者、CSDN博客专家 、CSDN内容合伙人、掘金特邀作者、阿里云博客专家、51CTO特邀作者、多年架构师设计经验、腾讯课堂常驻讲师

主要内容:Java项目、Python项目、前端项目、人工智能与大数据、简历模板、学习资料、面试题库、技术互助

收藏点赞不迷路  关注作者有好处

文末获取源码 

项目编号:BS-QD-002

主要需求:

  • 学生信息录入、修改、删除、查询
  • 宿舍管理评分
  • 学生早起率、懒床率
  • 学生宿舍打扫频率
  • 学生晚归情况
  • 楼层管理

考虑到实用性,该系统需要拆分为两大子系统,一个是学生端系统,一个是后台管理端系统。学生端系统主要提供给学生使用,负责一些宿舍记录及个人信息记录的基本操作;后台管理模块则是主要负责对所有学生信息的整理,提供宿舍管理、楼层管理、数据查看等权限,提供给宿舍管理员使用的。

学生登陆

学生系统拥有以下功能:

  • 创建账户
  • 分配宿舍
  • 填写个人信息
  • 修改个人信息
  • 起床打卡(用于统计懒床率)
  • 归宿登记(用于统计晚归情况)
  • 打扫记录(用于统计宿舍打扫频率)
  • 查看宿日常数据

管理员登陆

管理系统拥有以下功能:

  • 楼层管理
  • 宿舍评价
  • 宿舍信息管理
  • 学生信息查看
  • 保洁人员管理
  • 统计学生早起率
  • 统计学生宿舍打扫频率
  • 统计学生晚归

超级管理员在享有上述管理员同等权限的同时额外拥有如下功能:

  • 创建管理员
  • 创建宿舍楼
  • 为宿舍楼分配管理员
  • 为宿舍楼分配保洁人员

前端:

  • Vue 作为基础框架
  • vue-router 控制路由(hash 模式)
  • vuex 状态管理
  • axios 接入数据
  • Vue-element-admin 作为基础框架

后台(Nodejs):

  • Koa 作为基础框架
  • koa-router —— 服务端路由控制
  • koa-static —— 读取静态文件
  • koa-jwt —— JWT 登录校验
  • koa-body —— http body 数据处理
  • koa-compress —— Gzip 压缩
  • koa-cors —— CORS 解决跨域问题
  • sequelize —— ORM

数据库:

  • MySQL

数据库设计一览:

下面展示一下系统的部分功能:

仪表盘概揽:选择不同的宿舍楼查看相关信息

管理员管理:

宿舍楼管理

楼层管理

宿舍信息

宿舍入住学生信息

查看学生起床记录

查看学生归宿信息

查看学生宿舍打扫信息

查看个人信息

学生注册

注册后登陆系统

入住宿舍

起床打卡

归宿记录

打扫记录

后端工程:

前端工程

部门核心代码:

  1. const { Building } = require("../model")
  2. module.exports = {
  3. getStudents: async function(buildingId) {
  4. const FloorController = require("./floor_controller")
  5. let users = []
  6. const building = await Building.findOne({ where: { id: buildingId } })
  7. const floors = await building.getFloors()
  8. for (let floor of floors) {
  9. const floorId = floor.id
  10. users = [...users, ...(await FloorController.getStudents(floorId))]
  11. }
  12. return users
  13. },
  14. delBuilding: async function(id) {
  15. const { setStudentRoomNull } = require("./user_controller")
  16. const students = await this.getStudents(id)
  17. students.forEach(student => {
  18. setStudentRoomNull(student.id)
  19. })
  20. return await Building.destroy({ where: { id } })
  21. }
  22. }

  1. const _ = require("lodash")
  2. const { User } = require("../model")
  3. module.exports = {
  4. async getEvaluatesInfo(evaluates) {
  5. const cpEvaluates = _.cloneDeep(evaluates)
  6. for (let evaluate of cpEvaluates) {
  7. const creator = await evaluate.getUser()
  8. evaluate.dataValues.userName = creator.name
  9. }
  10. return cpEvaluates
  11. }
  12. }
  1. const { Floor } = require("../model")
  2. module.exports = {
  3. getStudents: async function(floorId) {
  4. const { getStudentInfo } = require("./user_controller")
  5. let users = []
  6. const floor = await Floor.findOne({ where: { id: floorId } })
  7. const rooms = await floor.getRooms()
  8. for (let room of rooms) {
  9. const roomUsers = await room.getUsers()
  10. for (let user of roomUsers) {
  11. users.push(await getStudentInfo(user.id))
  12. }
  13. }
  14. return users
  15. }
  16. }

  1. module.exports = {
  2. UserController: require("./user_controller"),
  3. RoomController: require("./room_controller"),
  4. FloorController: require("./floor_controller"),
  5. BuildingController: require("./building_controller"),
  6. EvaluateController: require("./evaluate_controller"),
  7. RecordController: require("./record_controller")
  8. }

  1. const {
  2. User,
  3. GetupRecord,
  4. CleanRecord,
  5. BackRecord,
  6. Room,
  7. Floor,
  8. Building
  9. } = require("../model")
  10. const { Op } = require("sequelize")
  11. const moment = require("moment")
  12. const _ = require("lodash")
  13. const getupEarlyPoint = 8
  14. const backEarlyPoint = 22
  15. module.exports = {
  16. // getup 相关
  17. async addGetupRecord(userId) {
  18. const user = await User.findOne({ where: { id: userId } })
  19. const todyRecord = await GetupRecord.findOne({
  20. where: {
  21. userId: user.id,
  22. roomId: user.roomId,
  23. createdAt: {
  24. [Op.gt]: moment()
  25. .startOf("day")
  26. .toDate(),
  27. [Op.lt]: moment()
  28. .endOf("day")
  29. .toDate()
  30. }
  31. }
  32. })
  33. if (todyRecord) {
  34. throw new Error("当天已经有记录,记录失败!")
  35. }
  36. return await GetupRecord.create({ userId: user.id, roomId: user.roomId })
  37. },
  38. async getUserGetupRecords(userId, days, pure = false) {
  39. days = parseInt(days)
  40. const user = await User.findOne({ where: { id: userId } })
  41. const roomId = user.roomId
  42. const room = await Room.findOne({ where: { id: roomId } })
  43. // 获取最近 days 天的记录
  44. const startTime = moment()
  45. .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
  46. .startOf("day")
  47. .toDate()
  48. const allRecords = []
  49. for (let i = 0; i < days; i++) {
  50. const todayStart = moment(startTime)
  51. .add(i, "days")
  52. .toDate()
  53. const todayEnd = moment(todayStart)
  54. .endOf("day")
  55. .toDate()
  56. let record = await GetupRecord.findOne({
  57. where: {
  58. userId,
  59. roomId,
  60. createdAt: {
  61. [Op.gt]: todayStart,
  62. [Op.lt]: todayEnd
  63. }
  64. },
  65. attributes: { exclude: ["updatedAt", "deletedAt"] }
  66. })
  67. if (record) {
  68. // 如果当天有记录就推入结果
  69. record = record.toJSON()
  70. record.time = moment(record.createdAt).format("HH:mm")
  71. } else if (!record && !pure) {
  72. // 如果获取的是全部数据且当前天无数据
  73. // 就建立一条空记录
  74. record = GetupRecord.build({
  75. id: "fake" + i,
  76. roomId,
  77. userId,
  78. createdAt: todayStart
  79. }).toJSON()
  80. record.time = null
  81. } else {
  82. continue
  83. }
  84. record.date = moment(record.createdAt).format("YYYY-MM-DD")
  85. record.userName = user.name
  86. record.roomNumber = room.number
  87. allRecords.push(record)
  88. }
  89. return allRecords.reverse()
  90. },
  91. async getRoomGetupRecords(roomId, days, pure = false) {
  92. days = parseInt(days)
  93. const room = await Room.findOne({ where: { id: roomId } })
  94. const users = await room.getUsers()
  95. const records = {}
  96. for (let user of users) {
  97. records[user.name] = await this.getUserGetupRecords(user.id, days, pure)
  98. }
  99. return records
  100. },
  101. async getGetupRecordLineCharData(roomId) {
  102. const room = await Room.findOne({ where: { id: roomId } })
  103. const users = await room.getUsers()
  104. const data = { columns: ["周期"], rows: [] }
  105. const dataCount = 5 // 获取的记录条数
  106. const dataStep = 7 // 每条记录相隔的条数
  107. // 初始化记录值
  108. for (let i = 0; i < dataCount; i++) {
  109. data.rows.push({ 周期: `最近${(i + 1) * dataStep}天` })
  110. }
  111. // 遍历当前宿舍的用户
  112. for (let user of users) {
  113. data.columns.push(user.name)
  114. for (let i = 0; i < dataCount; i++) {
  115. const days = (i + 1) * dataStep
  116. // 获取某学生最近 days 天的早起记录
  117. const records = await this.getUserGetupRecords(user.id, days, true)
  118. let earlyTimes = 0
  119. records.forEach(record => {
  120. // 统计这些记录中有几天是早起的
  121. const timeHour = parseInt(moment(record.createdAt).format("HH"))
  122. if (timeHour < getupEarlyPoint) {
  123. earlyTimes++
  124. }
  125. })
  126. // 计算早起率
  127. const probability = (earlyTimes / days).toFixed(4)
  128. data.rows[i][user.name] = probability
  129. }
  130. }
  131. return data
  132. },
  133. async getGetupTableData({
  134. current,
  135. step,
  136. buildingId,
  137. floorId,
  138. roomId,
  139. userId,
  140. startTime,
  141. endTime
  142. }) {
  143. // 初始化时间
  144. startTime = startTime
  145. ? moment(startTime)
  146. .startOf("day")
  147. .toDate()
  148. : moment(0).toDate()
  149. endTime = endTime
  150. ? moment(endTime)
  151. .endOf("day")
  152. .toDate()
  153. : moment()
  154. .endOf("day")
  155. .toDate()
  156. console.log("endTime: ", endTime)
  157. // 开始分情况获取数据
  158. let result
  159. if (userId) {
  160. result = await GetupRecord.findAndCountAll({
  161. where: {
  162. userId: userId,
  163. createdAt: {
  164. [Op.gt]: startTime,
  165. [Op.lt]: endTime
  166. }
  167. },
  168. limit: step,
  169. offset: step * (current - 1),
  170. order: [["createdAt", "DESC"]]
  171. })
  172. } else if (roomId) {
  173. result = await GetupRecord.findAndCountAll({
  174. where: {
  175. roomId: roomId,
  176. createdAt: {
  177. [Op.gt]: startTime,
  178. [Op.lt]: endTime
  179. }
  180. },
  181. limit: step,
  182. offset: step * (current - 1),
  183. order: [["createdAt", "DESC"]]
  184. })
  185. } else if (floorId) {
  186. result = await GetupRecord.findAndCountAll({
  187. where: {
  188. createdAt: {
  189. [Op.gt]: startTime,
  190. [Op.lt]: endTime
  191. }
  192. },
  193. include: [
  194. {
  195. model: Room,
  196. where: { floorId }
  197. }
  198. ],
  199. limit: step,
  200. offset: step * (current - 1),
  201. order: [["createdAt", "DESC"]]
  202. })
  203. } else if (buildingId) {
  204. result = await GetupRecord.findAndCountAll({
  205. where: {
  206. createdAt: {
  207. [Op.gt]: startTime,
  208. [Op.lt]: endTime
  209. }
  210. },
  211. include: [
  212. {
  213. model: Room,
  214. where: { buildingId }
  215. }
  216. ],
  217. limit: step,
  218. offset: step * (current - 1),
  219. order: [["createdAt", "DESC"]]
  220. })
  221. } else {
  222. result = await GetupRecord.findAndCountAll({
  223. where: {
  224. createdAt: {
  225. [Op.gt]: startTime,
  226. [Op.lt]: endTime
  227. }
  228. },
  229. limit: step,
  230. offset: step * (current - 1),
  231. order: [["createdAt", "DESC"]]
  232. })
  233. }
  234. const getStudentInfo = require("./user_controller").getStudentInfo
  235. let rows = []
  236. for (let record of result.rows) {
  237. record = record.toJSON()
  238. delete record.room
  239. const userInfo = await getStudentInfo(record.userId)
  240. record = Object.assign(userInfo, record)
  241. record.time = moment(record.createdAt).format("HH:mm")
  242. record.date = moment(record.createdAt).format("YYYY-MM-DD")
  243. if (parseInt(moment(record.createdAt).format("HH")) < getupEarlyPoint) {
  244. record.early = true
  245. } else {
  246. record.early = false
  247. }
  248. rows.push(record)
  249. }
  250. result.rows = rows
  251. return result
  252. },
  253. // back 相关
  254. async addBackRecord(userId) {
  255. const user = await User.findOne({ where: { id: userId } })
  256. const todyRecord = await BackRecord.findOne({
  257. where: {
  258. userId: user.id,
  259. roomId: user.roomId,
  260. createdAt: {
  261. [Op.gt]: moment()
  262. .startOf("day")
  263. .toDate(),
  264. [Op.lt]: moment()
  265. .endOf("day")
  266. .toDate()
  267. }
  268. }
  269. })
  270. if (todyRecord) {
  271. throw new Error("当天已经有记录,记录失败!")
  272. }
  273. return await BackRecord.create({ userId: user.id, roomId: user.roomId })
  274. },
  275. async getUserBackRecords(userId, days, pure = false) {
  276. days = parseInt(days)
  277. const user = await User.findOne({ where: { id: userId } })
  278. const roomId = user.roomId
  279. const room = await Room.findOne({ where: { id: roomId } })
  280. // 获取最近 days 天的记录
  281. const startTime = moment()
  282. .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
  283. .startOf("day")
  284. .toDate()
  285. const allRecords = []
  286. for (let i = 0; i < days; i++) {
  287. const todayStart = moment(startTime)
  288. .add(i, "days")
  289. .toDate()
  290. const todayEnd = moment(todayStart)
  291. .endOf("day")
  292. .toDate()
  293. let record = await BackRecord.findOne({
  294. where: {
  295. userId,
  296. roomId,
  297. createdAt: {
  298. [Op.gt]: todayStart,
  299. [Op.lt]: todayEnd
  300. }
  301. },
  302. attributes: { exclude: ["updatedAt", "deletedAt"] }
  303. })
  304. if (record) {
  305. // 如果当天有记录就推入结果
  306. record = record.toJSON()
  307. record.time = moment(record.createdAt).format("HH:mm")
  308. } else if (!record && !pure) {
  309. // 如果获取的是全部数据且当前天无数据
  310. // 就建立一条空记录
  311. record = BackRecord.build({
  312. id: "fake" + i,
  313. roomId,
  314. userId,
  315. createdAt: todayStart
  316. }).toJSON()
  317. record.time = null
  318. } else {
  319. continue
  320. }
  321. record.date = moment(record.createdAt).format("YYYY-MM-DD")
  322. record.userName = user.name
  323. record.roomNumber = room.number
  324. allRecords.push(record)
  325. }
  326. return allRecords.reverse()
  327. },
  328. async getRoomBackRecords(roomId, days, pure = false) {
  329. days = parseInt(days)
  330. const room = await Room.findOne({ where: { id: roomId } })
  331. const users = await room.getUsers()
  332. const records = {}
  333. for (let user of users) {
  334. records[user.name] = await this.getUserBackRecords(user.id, days, pure)
  335. }
  336. return records
  337. },
  338. async getBackRecordLineCharData(roomId) {
  339. const room = await Room.findOne({ where: { id: roomId } })
  340. const users = await room.getUsers()
  341. const data = { columns: ["周期"], rows: [] }
  342. const dataCount = 5 // 获取的记录条数
  343. const dataStep = 7 // 每条记录相隔的条数
  344. // 初始化记录值
  345. for (let i = 0; i < dataCount; i++) {
  346. data.rows.push({ 周期: `最近${(i + 1) * dataStep}天` })
  347. }
  348. // 遍历当前宿舍的用户
  349. for (let user of users) {
  350. data.columns.push(user.name)
  351. for (let i = 0; i < dataCount; i++) {
  352. const days = (i + 1) * dataStep
  353. // 获取某学生最近 days 天的归宿记录
  354. const records = await this.getUserBackRecords(user.id, days, true)
  355. let earlyTimes = 0
  356. records.forEach(record => {
  357. // 统计这些记录中有几天是早归的
  358. const timeHour = parseInt(moment(record.createdAt).format("HH"))
  359. if (timeHour < backEarlyPoint) {
  360. earlyTimes++
  361. }
  362. })
  363. // 计算早起率
  364. const probability = (earlyTimes / days).toFixed(4)
  365. data.rows[i][user.name] = probability
  366. }
  367. }
  368. return data
  369. },
  370. async getBackTableData({
  371. current,
  372. step,
  373. buildingId,
  374. floorId,
  375. roomId,
  376. userId,
  377. startTime,
  378. endTime
  379. }) {
  380. // 初始化时间
  381. startTime = startTime
  382. ? moment(startTime)
  383. .startOf("day")
  384. .toDate()
  385. : moment(0).toDate()
  386. endTime = endTime
  387. ? moment(endTime)
  388. .endOf("day")
  389. .toDate()
  390. : moment()
  391. .endOf("day")
  392. .toDate()
  393. // 开始分情况获取数据
  394. let result
  395. if (userId) {
  396. result = await BackRecord.findAndCountAll({
  397. where: {
  398. userId: userId,
  399. createdAt: {
  400. [Op.gt]: startTime,
  401. [Op.lt]: endTime
  402. }
  403. },
  404. limit: step,
  405. offset: step * (current - 1),
  406. order: [["createdAt", "DESC"]]
  407. })
  408. } else if (roomId) {
  409. result = await BackRecord.findAndCountAll({
  410. where: {
  411. roomId: roomId,
  412. createdAt: {
  413. [Op.gt]: startTime,
  414. [Op.lt]: endTime
  415. }
  416. },
  417. limit: step,
  418. offset: step * (current - 1),
  419. order: [["createdAt", "DESC"]]
  420. })
  421. } else if (floorId) {
  422. result = await BackRecord.findAndCountAll({
  423. where: {
  424. createdAt: {
  425. [Op.gt]: startTime,
  426. [Op.lt]: endTime
  427. }
  428. },
  429. include: [
  430. {
  431. model: Room,
  432. where: { floorId }
  433. }
  434. ],
  435. limit: step,
  436. offset: step * (current - 1),
  437. order: [["createdAt", "DESC"]]
  438. })
  439. } else if (buildingId) {
  440. result = await BackRecord.findAndCountAll({
  441. where: {
  442. createdAt: {
  443. [Op.gt]: startTime,
  444. [Op.lt]: endTime
  445. }
  446. },
  447. include: [
  448. {
  449. model: Room,
  450. where: { buildingId }
  451. }
  452. ],
  453. limit: step,
  454. offset: step * (current - 1),
  455. order: [["createdAt", "DESC"]]
  456. })
  457. } else {
  458. result = await BackRecord.findAndCountAll({
  459. where: {
  460. createdAt: {
  461. [Op.gt]: startTime,
  462. [Op.lt]: endTime
  463. }
  464. },
  465. limit: step,
  466. offset: step * (current - 1),
  467. order: [["createdAt", "DESC"]]
  468. })
  469. }
  470. const getStudentInfo = require("./user_controller").getStudentInfo
  471. let rows = []
  472. for (let record of result.rows) {
  473. record = record.toJSON()
  474. delete record.room
  475. const userInfo = await getStudentInfo(record.userId)
  476. record = Object.assign(userInfo, record)
  477. record.time = moment(record.createdAt).format("HH:mm")
  478. record.date = moment(record.createdAt).format("YYYY-MM-DD")
  479. if (parseInt(moment(record.createdAt).format("HH")) < backEarlyPoint) {
  480. record.early = true
  481. } else {
  482. record.early = false
  483. }
  484. rows.push(record)
  485. }
  486. result.rows = rows
  487. return result
  488. },
  489. // clean 相关
  490. async addCleanRecord(userId) {
  491. const user = await User.findOne({ where: { id: userId } })
  492. const todyRecord = await CleanRecord.findOne({
  493. where: {
  494. roomId: user.roomId,
  495. createdAt: {
  496. [Op.gt]: moment()
  497. .startOf("day")
  498. .toDate(),
  499. [Op.lt]: moment()
  500. .endOf("day")
  501. .toDate()
  502. }
  503. }
  504. })
  505. if (todyRecord) {
  506. throw new Error("当天已经有清扫记录,记录失败")
  507. }
  508. return await CleanRecord.create({
  509. userId: user.id,
  510. roomId: user.roomId
  511. })
  512. },
  513. async getUserCleanRecords(userId, days) {
  514. // 获取打扫记录不会自动补全每一天的信息
  515. days = parseInt(days)
  516. const user = await User.findOne({ where: { id: userId } })
  517. const roomId = user.roomId
  518. const room = await Room.findOne({ where: { id: roomId } })
  519. // 获取最近 days 天的记录
  520. const startTime = moment()
  521. .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
  522. .startOf("day")
  523. .toDate()
  524. const todayEnd = moment()
  525. .endOf("day")
  526. .toDate()
  527. const records = await CleanRecord.findAll({
  528. where: {
  529. userId,
  530. roomId,
  531. createdAt: {
  532. [Op.gt]: startTime,
  533. [Op.lt]: todayEnd
  534. }
  535. },
  536. attributes: { exclude: ["updatedAt", "deletedAt"] },
  537. order: [["createdAt", "DESC"]]
  538. })
  539. const allRecords = []
  540. records.forEach(record => {
  541. record = record.toJSON()
  542. record.time = moment(record.createdAt).format("HH:mm")
  543. record.date = moment(record.createdAt).format("YYYY-MM-DD")
  544. record.userName = user.name
  545. record.roomNumber = room.number
  546. allRecords.push(record)
  547. })
  548. return allRecords
  549. },
  550. async getRoomCleanRecords(roomId, days) {
  551. const room = await Room.findOne({ where: { id: roomId } })
  552. const startTime = moment()
  553. .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
  554. .startOf("day")
  555. .toDate()
  556. const todayEnd = moment()
  557. .endOf("day")
  558. .toDate()
  559. const records = await room.getCleanRecords({
  560. where: {
  561. createdAt: {
  562. [Op.gt]: startTime,
  563. [Op.lt]: todayEnd
  564. }
  565. },
  566. attributes: { exclude: ["updatedAt", "deletedAt"] },
  567. order: [["createdAt", "DESC"]]
  568. })
  569. const allRecords = []
  570. for (let record of records) {
  571. const user = await record.getUser()
  572. record = record.toJSON()
  573. record.date = moment(record.createdAt).format("YYYY-MM-DD")
  574. record.time = moment(record.createdAt).format("HH:mm")
  575. record.userName = user.name
  576. record.roomNumber = room.number
  577. allRecords.push(record)
  578. }
  579. return allRecords
  580. },
  581. async getCleanTableData({
  582. current,
  583. step,
  584. buildingId,
  585. floorId,
  586. roomId,
  587. userId,
  588. startTime,
  589. endTime
  590. }) {
  591. // 初始化时间
  592. startTime = startTime
  593. ? moment(startTime)
  594. .startOf("day")
  595. .toDate()
  596. : moment(0).toDate()
  597. endTime = endTime
  598. ? moment(endTime)
  599. .endOf("day")
  600. .toDate()
  601. : moment()
  602. .endOf("day")
  603. .toDate()
  604. // 开始分情况获取数据
  605. let result
  606. if (userId) {
  607. result = await CleanRecord.findAndCountAll({
  608. where: {
  609. userId: userId,
  610. createdAt: {
  611. [Op.gt]: startTime,
  612. [Op.lt]: endTime
  613. }
  614. },
  615. limit: step,
  616. offset: step * (current - 1),
  617. order: [["createdAt", "DESC"]]
  618. })
  619. } else if (roomId) {
  620. result = await CleanRecord.findAndCountAll({
  621. where: {
  622. roomId: roomId,
  623. createdAt: {
  624. [Op.gt]: startTime,
  625. [Op.lt]: endTime
  626. }
  627. },
  628. limit: step,
  629. offset: step * (current - 1),
  630. order: [["createdAt", "DESC"]]
  631. })
  632. } else if (floorId) {
  633. result = await CleanRecord.findAndCountAll({
  634. where: {
  635. createdAt: {
  636. [Op.gt]: startTime,
  637. [Op.lt]: endTime
  638. }
  639. },
  640. include: [
  641. {
  642. model: Room,
  643. where: { floorId }
  644. }
  645. ],
  646. limit: step,
  647. offset: step * (current - 1),
  648. order: [["createdAt", "DESC"]]
  649. })
  650. } else if (buildingId) {
  651. result = await CleanRecord.findAndCountAll({
  652. where: {
  653. createdAt: {
  654. [Op.gt]: startTime,
  655. [Op.lt]: endTime
  656. }
  657. },
  658. include: [
  659. {
  660. model: Room,
  661. where: { buildingId }
  662. }
  663. ],
  664. limit: step,
  665. offset: step * (current - 1),
  666. order: [["createdAt", "DESC"]]
  667. })
  668. } else {
  669. result = await CleanRecord.findAndCountAll({
  670. where: {
  671. createdAt: {
  672. [Op.gt]: startTime,
  673. [Op.lt]: endTime
  674. }
  675. },
  676. limit: step,
  677. offset: step * (current - 1),
  678. order: [["createdAt", "DESC"]]
  679. })
  680. }
  681. const getStudentInfo = require("./user_controller").getStudentInfo
  682. let rows = []
  683. for (let record of result.rows) {
  684. record = record.toJSON()
  685. delete record.room
  686. const userInfo = await getStudentInfo(record.userId)
  687. record = Object.assign(userInfo, record)
  688. record.time = moment(record.createdAt).format("HH:mm")
  689. record.date = moment(record.createdAt).format("YYYY-MM-DD")
  690. record.early = null
  691. rows.push(record)
  692. }
  693. result.rows = rows
  694. return result
  695. },
  696. // 通用
  697. async getUserProbability(type, userId) {
  698. const user = await User.findById(userId)
  699. const startTime = user.checkTime
  700. let records = []
  701. let allRecords = []
  702. switch (type) {
  703. case "getup":
  704. allRecords = await user.getGetupRecords({
  705. where: {
  706. createdAt: { [Op.gt]: startTime }
  707. }
  708. })
  709. allRecords.forEach(record => {
  710. let hour = parseInt(moment(record.createdAt).format("HH"))
  711. if (hour < getupEarlyPoint) {
  712. records.push(record)
  713. }
  714. })
  715. break
  716. case "back":
  717. allRecords = await user.getBackRecords({
  718. where: {
  719. createdAt: { [Op.gt]: startTime }
  720. }
  721. })
  722. allRecords.forEach(record => {
  723. let hour = parseInt(moment(record.createdAt).format("HH"))
  724. if (hour < backEarlyPoint) {
  725. records.push(record)
  726. }
  727. })
  728. break
  729. case "clean":
  730. records = await user.getCleanRecords({
  731. where: {
  732. createdAt: { [Op.gt]: startTime }
  733. }
  734. })
  735. break
  736. default:
  737. throw new Error("参数传入错误")
  738. }
  739. // 计算从入住到现在有几天了
  740. const days = Math.abs(moment(startTime).diff(moment(), "days"))
  741. return (records.length / (days + 1)).toFixed(4)
  742. }
  743. }

  1. const { User } = require("../model")
  2. const _ = require("lodash")
  3. const RecordController = require("./record_controller")
  4. module.exports = {
  5. /**
  6. * 获取学生用户的完整信息
  7. * @param {Number} userId
  8. */
  9. async getStudentInfo(userId) {
  10. const student = await User.findOne({
  11. where: { id: userId },
  12. attributes: { exclude: ["password", "deletedAt"] }
  13. })
  14. const room = await student.getRoom()
  15. const floor = await room.getFloor()
  16. const building = await floor.getBuilding()
  17. const getupProb = await RecordController.getUserProbability("getup", userId)
  18. const backProb = await RecordController.getUserProbability("back", userId)
  19. const cleanProb = await RecordController.getUserProbability("clean", userId)
  20. const info = Object.assign(student.dataValues, {
  21. roomNumber: room.number,
  22. floorId: floor.id,
  23. floorLayer: floor.layer,
  24. buildingId: building.id,
  25. buildingName: building.name,
  26. getupProb,
  27. backProb,
  28. cleanProb
  29. })
  30. return info
  31. },
  32. /**
  33. * 获取学生用户们的完整信息
  34. * @param {Array} users
  35. */
  36. async getStudentsInfo(users) {
  37. const cloneUsers = _.cloneDeep(users)
  38. for (let user of cloneUsers) {
  39. delete user.dataValues.password
  40. delete user.dataValues.deletedAt
  41. const room = await user.getRoom()
  42. const floor = await room.getFloor()
  43. const building = await floor.getBuilding()
  44. Object.assign(user.dataValues, {
  45. roomNumber: room.number,
  46. floorId: floor.id,
  47. floorLayer: floor.layer,
  48. buildingId: building.id,
  49. buildingName: building.name
  50. })
  51. }
  52. return cloneUsers
  53. },
  54. async setStudentRoomNull(id) {
  55. const student = await User.findOne({ where: { id, role: "student" } })
  56. const result = await student.update({ roomId: null })
  57. return result
  58. }
  59. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/166777
推荐阅读
相关标签
  

闽ICP备14008679号