RoomRollCallPopup.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <template>
  2. <view class="room-rollcall-container">
  3. <u-loading-page v-if="loading" loading-text="加载床位信息..."></u-loading-page>
  4. <view v-if="!loading" class="rollcall-content">
  5. <!-- 床位网格 -->
  6. <view v-if="bedDetails.length > 0" class="beds-grid">
  7. <view
  8. v-for="bed in bedDetails"
  9. :key="bed.bedId"
  10. class="bed-card"
  11. :class="getBedCardClass(bed)"
  12. @click="handleBedClick(bed)"
  13. >
  14. <view class="bed-name">{{ bed.bedName }}</view>
  15. <view v-if="bed.studentName" class="student-info">
  16. <view class="student-name">{{ bed.studentName }}</view>
  17. <view class="student-no">{{ bed.studentNo }}</view>
  18. </view>
  19. <view v-else class="empty-bed">空床位</view>
  20. <view v-if="bed.isLeaveAuto" class="leave-tag">
  21. <u-tag text="请假" type="warning" size="mini"></u-tag>
  22. </view>
  23. <view class="status-label" :class="getStatusColorClass(bed.rollCallStatus)">
  24. {{ getStatusLabel(bed.rollCallStatus) }}
  25. </view>
  26. </view>
  27. </view>
  28. <view v-else style="padding: 60rpx 0">
  29. <u-empty text="暂无床位信息" mode="data"></u-empty>
  30. </view>
  31. <!-- 汇总统计 -->
  32. <view v-if="bedDetails.length > 0" class="summary-bar">
  33. <view class="summary-item">
  34. <text class="summary-label">在寝:</text>
  35. <text class="summary-value present-count">{{ summary.present }}</text>
  36. </view>
  37. <view class="summary-item">
  38. <text class="summary-label">请假:</text>
  39. <text class="summary-value leave-count">{{ summary.leave }}</text>
  40. </view>
  41. <view class="summary-item">
  42. <text class="summary-label">不在:</text>
  43. <text class="summary-value absent-count">{{ summary.absent }}</text>
  44. </view>
  45. <view class="summary-item">
  46. <text class="summary-label">空床位:</text>
  47. <text class="summary-value empty-count">{{ summary.empty }}</text>
  48. </view>
  49. </view>
  50. <!-- 提示文字 -->
  51. <view v-if="bedDetails.length > 0" class="hint-bar">
  52. <u-icon name="info-circle" color="#909399" size="14"></u-icon>
  53. <text class="hint-text">点击床位切换"在寝"和"不在"状态,请假不可修改</text>
  54. </view>
  55. <!-- 提交按钮 -->
  56. <view v-if="bedDetails.length > 0" class="submit-bar">
  57. <u-button type="primary" @click="handleSubmit" :loading="submitting" :disabled="submitting">
  58. 提交点名
  59. </u-button>
  60. </view>
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. import { getRoomStudents, getDormBedList, submitRollCall } from '@/api/dormRollCall.js';
  66. export default {
  67. name: 'RoomRollCallPopup',
  68. props: {
  69. visible: {
  70. type: Boolean,
  71. default: false,
  72. },
  73. roomId: {
  74. type: [String, Number],
  75. default: '',
  76. },
  77. roomName: {
  78. type: String,
  79. default: '',
  80. },
  81. buildingId: {
  82. type: [String, Number],
  83. default: '',
  84. },
  85. batchNo: {
  86. type: String,
  87. default: '',
  88. },
  89. },
  90. data() {
  91. return {
  92. loading: false,
  93. submitting: false,
  94. bedDetails: [],
  95. };
  96. },
  97. computed: {
  98. summary() {
  99. let present = 0, leave = 0, absent = 0, empty = 0;
  100. this.bedDetails.forEach((bed) => {
  101. const status = bed.rollCallStatus;
  102. if (status === '0') present++;
  103. else if (status === '1') leave++;
  104. else if (status === '2') absent++;
  105. else if (status === '3') empty++;
  106. });
  107. return { present, leave, absent, empty };
  108. },
  109. },
  110. watch: {
  111. visible: {
  112. immediate: true,
  113. handler(val) {
  114. if (val) {
  115. this.loadBedDetails();
  116. }
  117. },
  118. },
  119. roomId(val) {
  120. if (val && this.visible) {
  121. this.loadBedDetails();
  122. }
  123. },
  124. },
  125. mounted() {
  126. // v-if 现挂载时 visible 已为 true,watch 不会触发初始值,这里兜底
  127. if (this.visible && this.roomId) {
  128. this.loadBedDetails();
  129. }
  130. },
  131. methods: {
  132. async loadBedDetails() {
  133. if (!this.roomId) return;
  134. this.loading = true;
  135. try {
  136. // 1. 获取有学生的床位(与web端一致,使用已验证可用的接口)
  137. const studentRes = await getRoomStudents(this.roomId);
  138. const studentBeds = studentRes.data || studentRes || [];
  139. const studentBedList = Array.isArray(studentBeds) ? studentBeds : [];
  140. // 2. 收集已占用的床位ID
  141. const occupiedBedIds = new Set();
  142. studentBedList.forEach((s) => {
  143. if (s.bedId != null) {
  144. occupiedBedIds.add(Number(s.bedId));
  145. }
  146. });
  147. // 3. 查询该房间所有床位,补充空床位
  148. const allBedsRes = await getDormBedList(this.roomId);
  149. const allBeds = allBedsRes.rows || allBedsRes.data || [];
  150. // 4. 合并:有学生的床位 + 空床位
  151. const mergedList = [...studentBedList];
  152. (Array.isArray(allBeds) ? allBeds : []).forEach((bed) => {
  153. if (!occupiedBedIds.has(Number(bed.id))) {
  154. mergedList.push({
  155. bedId: bed.id,
  156. bedCode: bed.bedCode,
  157. bedName: bed.bedName,
  158. studentId: null,
  159. studentName: null,
  160. studentNo: null,
  161. classId: null,
  162. className: null,
  163. majorId: null,
  164. majorName: null,
  165. collegeId: null,
  166. collegeName: null,
  167. });
  168. }
  169. });
  170. // 5. 映射为统一格式,并处理点名状态与请假信息
  171. this.bedDetails = mergedList.map((item) => {
  172. const bed = {
  173. bedId: item.bedId || item.id,
  174. bedCode: item.bedCode,
  175. bedName: item.bedName,
  176. studentId: item.studentId,
  177. studentName: item.studentName,
  178. studentNo: item.studentNo,
  179. classId: item.classId,
  180. className: item.className,
  181. majorId: item.majorId,
  182. majorName: item.majorName,
  183. collegeId: item.collegeId,
  184. collegeName: item.collegeName,
  185. rollCallStatus: '0',
  186. isLeaveAuto: false,
  187. leaveId: null,
  188. leaveTypeName: null,
  189. leaveStartTime: null,
  190. leaveEndTime: null,
  191. };
  192. if (!bed.studentId) {
  193. bed.rollCallStatus = '3';
  194. } else if (item.leaveInfo) {
  195. bed.rollCallStatus = '1';
  196. bed.isLeaveAuto = true;
  197. bed.leaveId = item.leaveInfo.leaveId;
  198. bed.leaveTypeName = item.leaveInfo.leaveTypeName;
  199. bed.leaveStartTime = item.leaveInfo.leaveStartTime;
  200. bed.leaveEndTime = item.leaveInfo.leaveEndTime;
  201. }
  202. return bed;
  203. });
  204. // 6. 按床位名称排序
  205. this.bedDetails.sort((a, b) => {
  206. const aName = a.bedName || '';
  207. const bName = b.bedName || '';
  208. return aName.localeCompare(bName, undefined, { numeric: true });
  209. });
  210. } catch (error) {
  211. console.error('加载床位信息失败:', error);
  212. uni.showToast({ title: '加载床位信息失败', icon: 'none' });
  213. } finally {
  214. this.loading = false;
  215. }
  216. },
  217. getBedCardClass(bed) {
  218. if (bed.rollCallStatus === '0') return 'bed-present';
  219. if (bed.rollCallStatus === '1') return 'bed-leave';
  220. if (bed.rollCallStatus === '2') return 'bed-absent';
  221. if (bed.rollCallStatus === '3') return 'bed-empty';
  222. return '';
  223. },
  224. getStatusLabel(status) {
  225. if (status === '0') return '在寝';
  226. if (status === '1') return '请假';
  227. if (status === '2') return '不在';
  228. if (status === '3') return '空床位';
  229. return '';
  230. },
  231. getStatusColorClass(status) {
  232. if (status === '0') return 'color-present';
  233. if (status === '1') return 'color-leave';
  234. if (status === '2') return 'color-absent';
  235. if (status === '3') return 'color-empty';
  236. return '';
  237. },
  238. handleBedClick(bed) {
  239. if (bed.isLeaveAuto) {
  240. uni.showToast({ title: '该学生处于请假状态,不可修改', icon: 'none' });
  241. return;
  242. }
  243. if (!bed.studentId) {
  244. return;
  245. }
  246. if (bed.rollCallStatus === '0') {
  247. bed.rollCallStatus = '2';
  248. } else if (bed.rollCallStatus === '2') {
  249. bed.rollCallStatus = '0';
  250. }
  251. },
  252. formatLocalDateTime(date) {
  253. const pad = (n) => String(n).padStart(2, '0');
  254. return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
  255. },
  256. async handleSubmit() {
  257. if (!this.buildingId || !this.roomId) {
  258. uni.showToast({ title: '缺少必要参数', icon: 'none' });
  259. return;
  260. }
  261. this.submitting = true;
  262. try {
  263. const details = this.bedDetails.map((bed) => ({
  264. bedId: bed.bedId,
  265. bedCode: bed.bedCode,
  266. bedName: bed.bedName,
  267. studentId: bed.studentId,
  268. studentName: bed.studentName,
  269. studentNo: bed.studentNo,
  270. classId: bed.classId,
  271. className: bed.className,
  272. majorId: bed.majorId,
  273. majorName: bed.majorName,
  274. collegeId: bed.collegeId,
  275. collegeName: bed.collegeName,
  276. rollCallStatus: bed.rollCallStatus,
  277. leaveId: bed.leaveId,
  278. leaveTypeName: bed.leaveTypeName,
  279. leaveStartTime: bed.leaveStartTime,
  280. leaveEndTime: bed.leaveEndTime,
  281. }));
  282. const submitData = {
  283. batchNo: this.batchNo,
  284. checkDate: this.formatLocalDateTime(new Date()),
  285. buildingId: this.buildingId,
  286. roomId: this.roomId,
  287. roomName: this.roomName,
  288. totalBeds: this.bedDetails.length,
  289. presentCount: this.summary.present,
  290. leaveCount: this.summary.leave,
  291. absentCount: this.summary.absent,
  292. emptyCount: this.summary.empty,
  293. details,
  294. };
  295. const res = await submitRollCall(submitData);
  296. if (res.code === 200) {
  297. uni.showToast({ title: '点名提交成功', icon: 'success' });
  298. this.$emit('submit', submitData);
  299. } else {
  300. uni.showToast({ title: res.msg || '提交失败', icon: 'none' });
  301. }
  302. } catch (error) {
  303. console.error('提交点名失败:', error);
  304. uni.showToast({ title: '提交点名失败', icon: 'none' });
  305. } finally {
  306. this.submitting = false;
  307. }
  308. },
  309. },
  310. };
  311. </script>
  312. <style lang="scss" scoped>
  313. .room-rollcall-container {
  314. padding: 20rpx;
  315. }
  316. .rollcall-content {
  317. width: 100%;
  318. }
  319. .beds-grid {
  320. display: grid;
  321. grid-template-columns: repeat(3, 1fr);
  322. gap: 16rpx;
  323. margin-bottom: 24rpx;
  324. }
  325. .bed-card {
  326. border: 2rpx solid #e4e7ed;
  327. border-radius: 12rpx;
  328. padding: 16rpx;
  329. text-align: center;
  330. position: relative;
  331. transition: all 0.2s;
  332. }
  333. .bed-present {
  334. background-color: #e8f5e9;
  335. border-color: #67c23a;
  336. }
  337. .bed-leave {
  338. background-color: #fff8e1;
  339. border-color: #e6a23c;
  340. }
  341. .bed-absent {
  342. background-color: #ffebee;
  343. border-color: #f56c6c;
  344. }
  345. .bed-empty {
  346. background-color: #f5f5f5;
  347. border-color: #dcdfe6;
  348. }
  349. .bed-name {
  350. font-weight: 600;
  351. font-size: 24rpx;
  352. color: #303133;
  353. margin-bottom: 8rpx;
  354. }
  355. .student-info {
  356. margin-bottom: 8rpx;
  357. }
  358. .student-name {
  359. font-size: 24rpx;
  360. font-weight: 500;
  361. color: #303133;
  362. overflow: hidden;
  363. text-overflow: ellipsis;
  364. white-space: nowrap;
  365. }
  366. .student-no {
  367. font-size: 20rpx;
  368. color: #909399;
  369. }
  370. .empty-bed {
  371. color: #c0c4cc;
  372. font-size: 24rpx;
  373. }
  374. .leave-tag {
  375. margin: 8rpx 0;
  376. }
  377. .status-label {
  378. font-size: 22rpx;
  379. margin-top: 4rpx;
  380. font-weight: 500;
  381. }
  382. .color-present {
  383. color: #19be6b;
  384. }
  385. .color-leave {
  386. color: #e6a23c;
  387. }
  388. .color-absent {
  389. color: #f56c6c;
  390. }
  391. .color-empty {
  392. color: #909399;
  393. }
  394. .summary-bar {
  395. display: flex;
  396. justify-content: space-around;
  397. padding: 24rpx 16rpx;
  398. background-color: #f5f7fa;
  399. border-radius: 12rpx;
  400. margin-bottom: 20rpx;
  401. }
  402. .summary-item {
  403. display: flex;
  404. flex-direction: column;
  405. align-items: center;
  406. }
  407. .summary-label {
  408. font-size: 22rpx;
  409. color: #909399;
  410. margin-bottom: 4rpx;
  411. }
  412. .summary-value {
  413. font-size: 36rpx;
  414. font-weight: 700;
  415. }
  416. .present-count {
  417. color: #19be6b;
  418. }
  419. .leave-count {
  420. color: #e6a23c;
  421. }
  422. .absent-count {
  423. color: #f56c6c;
  424. }
  425. .empty-count {
  426. color: #909399;
  427. }
  428. .hint-bar {
  429. display: flex;
  430. align-items: center;
  431. padding: 12rpx 16rpx;
  432. background-color: #f4f4f5;
  433. border-radius: 8rpx;
  434. margin-bottom: 24rpx;
  435. }
  436. .hint-text {
  437. font-size: 22rpx;
  438. color: #909399;
  439. margin-left: 8rpx;
  440. }
  441. .submit-bar {
  442. padding: 16rpx 0;
  443. }
  444. </style>