專案編號: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
資料庫設計一覽:

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

儀表盤概攬:選擇不同的宿舍樓查看相關資訊


管理員管理:

宿舍樓管理

樓層管理

宿舍資訊


宿舍入住學生資訊

查看學生起床記錄

查看學生歸宿資訊

查看學生宿舍打掃資訊

查看個人資訊

學生注冊

注冊后登陸系統

入住宿舍

起床打卡

歸宿記錄

打掃記錄

后端工程:

前端工程

部門核心代碼:
const { Building } = require("../model")
module.exports = {
getStudents: async function(buildingId) {
const FloorController = require("./floor_controller")
let users = []
const building = await Building.findOne({ where: { id: buildingId } })
const floors = await building.getFloors()
for (let floor of floors) {
const floorId = floor.id
users = [...users, ...(await FloorController.getStudents(floorId))]
}
return users
},
delBuilding: async function(id) {
const { setStudentRoomNull } = require("./user_controller")
const students = await this.getStudents(id)
students.forEach(student => {
setStudentRoomNull(student.id)
})
return await Building.destroy({ where: { id } })
}
}
const _ = require("lodash")
const { User } = require("../model")
module.exports = {
async getEvaluatesInfo(evaluates) {
const cpEvaluates = _.cloneDeep(evaluates)
for (let evaluate of cpEvaluates) {
const creator = await evaluate.getUser()
evaluate.dataValues.userName = creator.name
}
return cpEvaluates
}
}
const { Floor } = require("../model")
module.exports = {
getStudents: async function(floorId) {
const { getStudentInfo } = require("./user_controller")
let users = []
const floor = await Floor.findOne({ where: { id: floorId } })
const rooms = await floor.getRooms()
for (let room of rooms) {
const roomUsers = await room.getUsers()
for (let user of roomUsers) {
users.push(await getStudentInfo(user.id))
}
}
return users
}
}
module.exports = {
UserController: require("./user_controller"),
RoomController: require("./room_controller"),
FloorController: require("./floor_controller"),
BuildingController: require("./building_controller"),
EvaluateController: require("./evaluate_controller"),
RecordController: require("./record_controller")
}
const {
User,
GetupRecord,
CleanRecord,
BackRecord,
Room,
Floor,
Building
} = require("../model")
const { Op } = require("sequelize")
const moment = require("moment")
const _ = require("lodash")
const getupEarlyPoint = 8
const backEarlyPoint = 22
module.exports = {
// getup 相關
async addGetupRecord(userId) {
const user = await User.findOne({ where: { id: userId } })
const todyRecord = await GetupRecord.findOne({
where: {
userId: user.id,
roomId: user.roomId,
createdAt: {
[Op.gt]: moment()
.startOf("day")
.toDate(),
[Op.lt]: moment()
.endOf("day")
.toDate()
}
}
})
if (todyRecord) {
throw new Error("當天已經有記錄,記錄失敗!")
}
return await GetupRecord.create({ userId: user.id, roomId: user.roomId })
},
async getUserGetupRecords(userId, days, pure = false) {
days = parseInt(days)
const user = await User.findOne({ where: { id: userId } })
const roomId = user.roomId
const room = await Room.findOne({ where: { id: roomId } })
// 獲取最近 days 天的記錄
const startTime = moment()
.subtract(days - 1 /* -1 代表查詢天數包括今天 */, "days")
.startOf("day")
.toDate()
const allRecords = []
for (let i = 0; i < days; i++) {
const todayStart = moment(startTime)
.add(i, "days")
.toDate()
const todayEnd = moment(todayStart)
.endOf("day")
.toDate()
let record = await GetupRecord.findOne({
where: {
userId,
roomId,
createdAt: {
[Op.gt]: todayStart,
[Op.lt]: todayEnd
}
},
attributes: { exclude: ["updatedAt", "deletedAt"] }
})
if (record) {
// 如果當天有記錄就推入結果
record = record.toJSON()
record.time = moment(record.createdAt).format("HH:mm")
} else if (!record && !pure) {
// 如果獲取的是全部資料且當前天無資料
// 就建立一條空記錄
record = GetupRecord.build({
id: "fake" + i,
roomId,
userId,
createdAt: todayStart
}).toJSON()
record.time = null
} else {
continue
}
record.date = moment(record.createdAt).format("YYYY-MM-DD")
record.userName = user.name
record.roomNumber = room.number
allRecords.push(record)
}
return allRecords.reverse()
},
async getRoomGetupRecords(roomId, days, pure = false) {
days = parseInt(days)
const room = await Room.findOne({ where: { id: roomId } })
const users = await room.getUsers()
const records = {}
for (let user of users) {
records[user.name] = await this.getUserGetupRecords(user.id, days, pure)
}
return records
},
async getGetupRecordLineCharData(roomId) {
const room = await Room.findOne({ where: { id: roomId } })
const users = await room.getUsers()
const data = { columns: ["周期"], rows: [] }
const dataCount = 5 // 獲取的記錄條數
const dataStep = 7 // 每條記錄相隔的條數
// 初始化記錄值
for (let i = 0; i < dataCount; i++) {
data.rows.push({ 周期: `最近${(i + 1) * dataStep}天` })
}
// 遍歷當前宿舍的用戶
for (let user of users) {
data.columns.push(user.name)
for (let i = 0; i < dataCount; i++) {
const days = (i + 1) * dataStep
// 獲取某學生最近 days 天的早起記錄
const records = await this.getUserGetupRecords(user.id, days, true)
let earlyTimes = 0
records.forEach(record => {
// 統計這些記錄中有幾天是早起的
const timeHour = parseInt(moment(record.createdAt).format("HH"))
if (timeHour < getupEarlyPoint) {
earlyTimes++
}
})
// 計算早起率
const probability = (earlyTimes / days).toFixed(4)
data.rows[i][user.name] = probability
}
}
return data
},
async getGetupTableData({
current,
step,
buildingId,
floorId,
roomId,
userId,
startTime,
endTime
}) {
// 初始化時間
startTime = startTime
? moment(startTime)
.startOf("day")
.toDate()
: moment(0).toDate()
endTime = endTime
? moment(endTime)
.endOf("day")
.toDate()
: moment()
.endOf("day")
.toDate()
console.log("endTime: ", endTime)
// 開始分情況獲取資料
let result
if (userId) {
result = await GetupRecord.findAndCountAll({
where: {
userId: userId,
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (roomId) {
result = await GetupRecord.findAndCountAll({
where: {
roomId: roomId,
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (floorId) {
result = await GetupRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
include: [
{
model: Room,
where: { floorId }
}
],
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (buildingId) {
result = await GetupRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
include: [
{
model: Room,
where: { buildingId }
}
],
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else {
result = await GetupRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
}
const getStudentInfo = require("./user_controller").getStudentInfo
let rows = []
for (let record of result.rows) {
record = record.toJSON()
delete record.room
const userInfo = await getStudentInfo(record.userId)
record = Object.assign(userInfo, record)
record.time = moment(record.createdAt).format("HH:mm")
record.date = moment(record.createdAt).format("YYYY-MM-DD")
if (parseInt(moment(record.createdAt).format("HH")) < getupEarlyPoint) {
record.early = true
} else {
record.early = false
}
rows.push(record)
}
result.rows = rows
return result
},
// back 相關
async addBackRecord(userId) {
const user = await User.findOne({ where: { id: userId } })
const todyRecord = await BackRecord.findOne({
where: {
userId: user.id,
roomId: user.roomId,
createdAt: {
[Op.gt]: moment()
.startOf("day")
.toDate(),
[Op.lt]: moment()
.endOf("day")
.toDate()
}
}
})
if (todyRecord) {
throw new Error("當天已經有記錄,記錄失敗!")
}
return await BackRecord.create({ userId: user.id, roomId: user.roomId })
},
async getUserBackRecords(userId, days, pure = false) {
days = parseInt(days)
const user = await User.findOne({ where: { id: userId } })
const roomId = user.roomId
const room = await Room.findOne({ where: { id: roomId } })
// 獲取最近 days 天的記錄
const startTime = moment()
.subtract(days - 1 /* -1 代表查詢天數包括今天 */, "days")
.startOf("day")
.toDate()
const allRecords = []
for (let i = 0; i < days; i++) {
const todayStart = moment(startTime)
.add(i, "days")
.toDate()
const todayEnd = moment(todayStart)
.endOf("day")
.toDate()
let record = await BackRecord.findOne({
where: {
userId,
roomId,
createdAt: {
[Op.gt]: todayStart,
[Op.lt]: todayEnd
}
},
attributes: { exclude: ["updatedAt", "deletedAt"] }
})
if (record) {
// 如果當天有記錄就推入結果
record = record.toJSON()
record.time = moment(record.createdAt).format("HH:mm")
} else if (!record && !pure) {
// 如果獲取的是全部資料且當前天無資料
// 就建立一條空記錄
record = BackRecord.build({
id: "fake" + i,
roomId,
userId,
createdAt: todayStart
}).toJSON()
record.time = null
} else {
continue
}
record.date = moment(record.createdAt).format("YYYY-MM-DD")
record.userName = user.name
record.roomNumber = room.number
allRecords.push(record)
}
return allRecords.reverse()
},
async getRoomBackRecords(roomId, days, pure = false) {
days = parseInt(days)
const room = await Room.findOne({ where: { id: roomId } })
const users = await room.getUsers()
const records = {}
for (let user of users) {
records[user.name] = await this.getUserBackRecords(user.id, days, pure)
}
return records
},
async getBackRecordLineCharData(roomId) {
const room = await Room.findOne({ where: { id: roomId } })
const users = await room.getUsers()
const data = { columns: ["周期"], rows: [] }
const dataCount = 5 // 獲取的記錄條數
const dataStep = 7 // 每條記錄相隔的條數
// 初始化記錄值
for (let i = 0; i < dataCount; i++) {
data.rows.push({ 周期: `最近${(i + 1) * dataStep}天` })
}
// 遍歷當前宿舍的用戶
for (let user of users) {
data.columns.push(user.name)
for (let i = 0; i < dataCount; i++) {
const days = (i + 1) * dataStep
// 獲取某學生最近 days 天的歸宿記錄
const records = await this.getUserBackRecords(user.id, days, true)
let earlyTimes = 0
records.forEach(record => {
// 統計這些記錄中有幾天是早歸的
const timeHour = parseInt(moment(record.createdAt).format("HH"))
if (timeHour < backEarlyPoint) {
earlyTimes++
}
})
// 計算早起率
const probability = (earlyTimes / days).toFixed(4)
data.rows[i][user.name] = probability
}
}
return data
},
async getBackTableData({
current,
step,
buildingId,
floorId,
roomId,
userId,
startTime,
endTime
}) {
// 初始化時間
startTime = startTime
? moment(startTime)
.startOf("day")
.toDate()
: moment(0).toDate()
endTime = endTime
? moment(endTime)
.endOf("day")
.toDate()
: moment()
.endOf("day")
.toDate()
// 開始分情況獲取資料
let result
if (userId) {
result = await BackRecord.findAndCountAll({
where: {
userId: userId,
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (roomId) {
result = await BackRecord.findAndCountAll({
where: {
roomId: roomId,
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (floorId) {
result = await BackRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
include: [
{
model: Room,
where: { floorId }
}
],
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (buildingId) {
result = await BackRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
include: [
{
model: Room,
where: { buildingId }
}
],
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else {
result = await BackRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
}
const getStudentInfo = require("./user_controller").getStudentInfo
let rows = []
for (let record of result.rows) {
record = record.toJSON()
delete record.room
const userInfo = await getStudentInfo(record.userId)
record = Object.assign(userInfo, record)
record.time = moment(record.createdAt).format("HH:mm")
record.date = moment(record.createdAt).format("YYYY-MM-DD")
if (parseInt(moment(record.createdAt).format("HH")) < backEarlyPoint) {
record.early = true
} else {
record.early = false
}
rows.push(record)
}
result.rows = rows
return result
},
// clean 相關
async addCleanRecord(userId) {
const user = await User.findOne({ where: { id: userId } })
const todyRecord = await CleanRecord.findOne({
where: {
roomId: user.roomId,
createdAt: {
[Op.gt]: moment()
.startOf("day")
.toDate(),
[Op.lt]: moment()
.endOf("day")
.toDate()
}
}
})
if (todyRecord) {
throw new Error("當天已經有清掃記錄,記錄失敗")
}
return await CleanRecord.create({
userId: user.id,
roomId: user.roomId
})
},
async getUserCleanRecords(userId, days) {
// 獲取打掃記錄不會自動補全每一天的資訊
days = parseInt(days)
const user = await User.findOne({ where: { id: userId } })
const roomId = user.roomId
const room = await Room.findOne({ where: { id: roomId } })
// 獲取最近 days 天的記錄
const startTime = moment()
.subtract(days - 1 /* -1 代表查詢天數包括今天 */, "days")
.startOf("day")
.toDate()
const todayEnd = moment()
.endOf("day")
.toDate()
const records = await CleanRecord.findAll({
where: {
userId,
roomId,
createdAt: {
[Op.gt]: startTime,
[Op.lt]: todayEnd
}
},
attributes: { exclude: ["updatedAt", "deletedAt"] },
order: [["createdAt", "DESC"]]
})
const allRecords = []
records.forEach(record => {
record = record.toJSON()
record.time = moment(record.createdAt).format("HH:mm")
record.date = moment(record.createdAt).format("YYYY-MM-DD")
record.userName = user.name
record.roomNumber = room.number
allRecords.push(record)
})
return allRecords
},
async getRoomCleanRecords(roomId, days) {
const room = await Room.findOne({ where: { id: roomId } })
const startTime = moment()
.subtract(days - 1 /* -1 代表查詢天數包括今天 */, "days")
.startOf("day")
.toDate()
const todayEnd = moment()
.endOf("day")
.toDate()
const records = await room.getCleanRecords({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: todayEnd
}
},
attributes: { exclude: ["updatedAt", "deletedAt"] },
order: [["createdAt", "DESC"]]
})
const allRecords = []
for (let record of records) {
const user = await record.getUser()
record = record.toJSON()
record.date = moment(record.createdAt).format("YYYY-MM-DD")
record.time = moment(record.createdAt).format("HH:mm")
record.userName = user.name
record.roomNumber = room.number
allRecords.push(record)
}
return allRecords
},
async getCleanTableData({
current,
step,
buildingId,
floorId,
roomId,
userId,
startTime,
endTime
}) {
// 初始化時間
startTime = startTime
? moment(startTime)
.startOf("day")
.toDate()
: moment(0).toDate()
endTime = endTime
? moment(endTime)
.endOf("day")
.toDate()
: moment()
.endOf("day")
.toDate()
// 開始分情況獲取資料
let result
if (userId) {
result = await CleanRecord.findAndCountAll({
where: {
userId: userId,
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (roomId) {
result = await CleanRecord.findAndCountAll({
where: {
roomId: roomId,
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (floorId) {
result = await CleanRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
include: [
{
model: Room,
where: { floorId }
}
],
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else if (buildingId) {
result = await CleanRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
include: [
{
model: Room,
where: { buildingId }
}
],
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
} else {
result = await CleanRecord.findAndCountAll({
where: {
createdAt: {
[Op.gt]: startTime,
[Op.lt]: endTime
}
},
limit: step,
offset: step * (current - 1),
order: [["createdAt", "DESC"]]
})
}
const getStudentInfo = require("./user_controller").getStudentInfo
let rows = []
for (let record of result.rows) {
record = record.toJSON()
delete record.room
const userInfo = await getStudentInfo(record.userId)
record = Object.assign(userInfo, record)
record.time = moment(record.createdAt).format("HH:mm")
record.date = moment(record.createdAt).format("YYYY-MM-DD")
record.early = null
rows.push(record)
}
result.rows = rows
return result
},
// 通用
async getUserProbability(type, userId) {
const user = await User.findById(userId)
const startTime = user.checkTime
let records = []
let allRecords = []
switch (type) {
case "getup":
allRecords = await user.getGetupRecords({
where: {
createdAt: { [Op.gt]: startTime }
}
})
allRecords.forEach(record => {
let hour = parseInt(moment(record.createdAt).format("HH"))
if (hour < getupEarlyPoint) {
records.push(record)
}
})
break
case "back":
allRecords = await user.getBackRecords({
where: {
createdAt: { [Op.gt]: startTime }
}
})
allRecords.forEach(record => {
let hour = parseInt(moment(record.createdAt).format("HH"))
if (hour < backEarlyPoint) {
records.push(record)
}
})
break
case "clean":
records = await user.getCleanRecords({
where: {
createdAt: { [Op.gt]: startTime }
}
})
break
default:
throw new Error("引數傳入錯誤")
}
// 計算從入住到現在有幾天了
const days = Math.abs(moment(startTime).diff(moment(), "days"))
return (records.length / (days + 1)).toFixed(4)
}
}
const { User } = require("../model")
const _ = require("lodash")
const RecordController = require("./record_controller")
module.exports = {
/**
* 獲取學生用戶的完整資訊
* @param {Number} userId
*/
async getStudentInfo(userId) {
const student = await User.findOne({
where: { id: userId },
attributes: { exclude: ["password", "deletedAt"] }
})
const room = await student.getRoom()
const floor = await room.getFloor()
const building = await floor.getBuilding()
const getupProb = await RecordController.getUserProbability("getup", userId)
const backProb = await RecordController.getUserProbability("back", userId)
const cleanProb = await RecordController.getUserProbability("clean", userId)
const info = Object.assign(student.dataValues, {
roomNumber: room.number,
floorId: floor.id,
floorLayer: floor.layer,
buildingId: building.id,
buildingName: building.name,
getupProb,
backProb,
cleanProb
})
return info
},
/**
* 獲取學生用戶們的完整資訊
* @param {Array} users
*/
async getStudentsInfo(users) {
const cloneUsers = _.cloneDeep(users)
for (let user of cloneUsers) {
delete user.dataValues.password
delete user.dataValues.deletedAt
const room = await user.getRoom()
const floor = await room.getFloor()
const building = await floor.getBuilding()
Object.assign(user.dataValues, {
roomNumber: room.number,
floorId: floor.id,
floorLayer: floor.layer,
buildingId: building.id,
buildingName: building.name
})
}
return cloneUsers
},
async setStudentRoomNull(id) {
const student = await User.findOne({ where: { id, role: "student" } })
const result = await student.update({ roomId: null })
return result
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/404384.html
標籤:其他
