瀏覽代碼

調整教室管理的UI和操作

close #322
osbert 4 年之前
父節點
當前提交
8f3f276e61

+ 15 - 0
TEAMModelOS/ClientApp/src/view/schoolmgmt/ClassroomSetting/ClassroomSetting.less

@@ -438,6 +438,21 @@
             }            
             cursor: not-allowed;
         }
+        &-linkBtn{
+            cursor: pointer;
+            border-radius: 5px;
+            padding: 5px 12px;
+            font-size: 12px;
+            background-color: #1cc0f3;
+            color: white;
+            text-align: center;
+            font-weight: 700;
+            margin-right: 70px;
+            border-color: transparent;
+            &:hover{
+                background-color: #0ba2d1;
+            }
+        }
     }
 }
 

+ 68 - 46
TEAMModelOS/ClientApp/src/view/schoolmgmt/ClassroomSetting/ClassroomSetting.vue

@@ -212,9 +212,9 @@
                                         <template v-if="item.deviceBound.length >0">
                                             <div v-for="(dbItem, dbIndex) in item.deviceBound" :key="dbIndex" class="hiteach-collapse-sub gradient" :class="{'linked': dbItem.classId != '' && dbItem.classId != null && classroomListShow[curClassIndex].id != dbItem.classId}">
                                                 <!-- 被關聯 disabled -->
-                                                <Button style="margin-right: 70px;" v-model="dbItem.hiteachLink" >{{ dbItem.hiteachLink ? '解除關聯' : '關聯' }}</Button>
+                                                <Button class="hiteach-collapse-sub-linkBtn" v-model="dbItem.hiteachLink" @click="confirmLink(dbItem.hiteachLink, item.id, dbItem.uuid, dbItem.uuid2, classroomListShow[curClassIndex].id)" >{{ dbItem.hiteachLink ? '解除關聯' : '關聯' }}</Button>
                                                 <!-- <Checkbox :id="'checkbox-' + (dbItem.uuid + dbItem.uuid2)" @on-change="whatUpdHiLink(dbItem, classroomListShow[curClassIndex].id, item.id)" style="margin-right: 70px;" v-model="dbItem.hiteachLink" :disabled="dbItem.classId != '' && dbItem.classId != null && classroomListShow[curClassIndex].id != dbItem.classId"></Checkbox> -->
-                                                <div class="hiteach-collapse-sub-detail" @click="pushCheckbox(dbItem, (dbItem.classId && classroomListShow[curClassIndex].id != dbItem.classId))">
+                                                <div class="hiteach-collapse-sub-detail">
                                                     <ul>
                                                         <li>
                                                             <h3>{{ dbItem.pcname }}</h3>
@@ -535,24 +535,29 @@ export default {
 
             if(nowSeletedClassId != null && this.isSmart(nowSeletedClassId)){
                 
+                // 篩選有 deviceBound 且classID 和現在的教室相同的
                 data = data.filter(item => {
-                    return Array.isArray(item.deviceBound) && item.deviceBound.length > 0
+                    return Array.isArray(item.deviceBound) && item.deviceBound.length > 0 && item.deviceBound.some(s => { return s.classId == nowSeletedClassId })
                 })
 
-                data = data.filter(item => {
-                    return item.deviceBound.some(s => {
-                        return s.classId == nowSeletedClassId
+                // 整形 拿掉不相關的
+                data = data.map(item => {
+                    let newD = item
+                    newD.deviceBound = newD.deviceBound.filter( f => {
+                        return f.classId != null && f.classId != ''
                     })
+                    return newD
                 })
             } else {
-                data = data.filter(item => {
-                    let flag = false
+                // 整形 拿掉沒被關聯的
+                data = data.map(item => {
+                    let newD = item
                     if(Array.isArray(item.deviceBound) && item.deviceBound.length > 0){
-                        flag = item.deviceBound.some(s => {
-                            return s.classId == null || s.classId == ''
+                        newD.deviceBound = newD.deviceBound.filter( f => {
+                            return f.classId == null || f.classId == ''
                         })
                     }
-                    return flag
+                    return newD
                 })
             }
                     
@@ -1139,7 +1144,7 @@ export default {
                                 classId: this.classroomListShow[index].id,
                                 school_code: this.$store.state.userInfo.schoolCode
                             })
-                            // this.unlinkHiteach(this.classroomListShow[index].id) // 刪掉指定的classId                                
+                            this.unlinkHiteachByClassId(this.classroomListShow[index].id) // 刪掉指定的classId                                
                             this.$store.dispatch('user/delSchoolClasses', this.classroomListShow[index].id);
                             this.classroomList.splice(originIndex, 1)
                             this.classroomListShow.splice(index, 1)
@@ -1160,7 +1165,7 @@ export default {
                     this.updateBefore = JSON.stringify(this.classroomListShow[this.curClassIndex])
                 }
                 this.classroomListShow.splice(index, 1)
-                this.unlinkHiteach(this.classroomListShow[index].id)// 刪掉指定的classId
+                this.unlinkHiteachByClassId(this.classroomListShow[index].id)// 刪掉指定的classId
                 this.isListLoading = false
                 this.updated = false
                 this.updHiteachLink = []
@@ -1418,7 +1423,7 @@ export default {
                 )
             }
         },
-        unlinkHiteach: function (classId) {
+        unlinkHiteachByClassId: function (classId) {
             // 刪掉指定的classId
             let orgHiData = JSON.parse(this.orgHiteachData)
             orgHiData.forEach(item => {
@@ -1434,49 +1439,66 @@ export default {
             this.hiteachData = orgHiData
             this.orgHiteachData = JSON.stringify(orgHiData)
         },
-        linkHiteach: function(){
+        // 詢問視窗
+        confirmLink: function(linkFlag, pid, uuid, uuid2, classId){
+            let action = linkFlag ? 'unLink' : 'link';
+            if(action == 'unLink'){
+                this.$Modal.confirm({
+                    title: this.$t('解除關聯'),
+                    content: `${this.$t('請問您確定要解除關聯')}?`,
+                    onOk: () => {
+                        this.executeHiteachLink(action, pid, uuid, uuid2, classId)                        
+                    }
+                })
+            } else {
+                this.executeHiteachLink(action, pid, uuid, uuid2, classId)
+            }
+        },
+        executeHiteachLink: function(action, pid, uuid, uuid2, classId){
             // Hiteach 與教室綁定
+            this.hiteachListLoading = true
             this.$api.schoolSetting.hiteachLink({
                 school_code: this.$store.state.userInfo.schoolCode,
-                linkList: this.updHiteachLink
-            }).then(hiteachLinkRes => {
-
-                if (hiteachLinkRes.error == 0) {
+                // linkList: this.updHiteachLink
+                action: action,
+                pid: pid,
+                uuid: uuid,
+                uuid2: uuid2,
+                classId: classId
+            }).then(res => {
+                if (res.error == 0) {
                     let orgHiData = JSON.parse(this.orgHiteachData)
 
-                    this.updHiteachLink.forEach(item => {
-                        let tempIndex;
-                        let temp = orgHiData.find((hiItem, hiIndex) => {
-                            tempIndex = hiIndex
-                            return hiItem.id == item.id
-                        })
-                        if (temp.deviceBound != null && Array.isArray(temp.deviceBound) && temp.deviceBound.length > 0) {
-                            temp.deviceBound.forEach((dbItem, dbIndex, dbArray) => {
-                                if ((dbItem.uuid + dbItem.uuid2) == (item.uuid + item.uuid2)) {
-                                    dbItem.classId = item.classId
-
-                                    if (item.classId != null) {
-                                        dbItem.hiteachLink = true
-                                    } else {
-                                        dbItem.hiteachLink = false
-                                    }
-                                }
-                            })
-
-                            orgHiData[tempIndex] = temp
-                        }
-                    }, err => {
-                        this.$Message.error(this.$t('schoolBaseInfo.bindingErr'))
-                    }).finally(
-                        () => {
-
+                    let tempIndex;
+                    let temp = orgHiData.find((hiItem, hiIndex) => {
+                        tempIndex = hiIndex
+                        return hiItem.id == pid
+                    })
+                    temp.deviceBound.forEach((dbItem, dbIndex, dbArray) => {
+                        if ((dbItem.uuid + dbItem.uuid2) == (uuid + uuid2)) {
+                            if(action == 'link'){
+                                dbItem.classId = classId
+                                dbItem.hiteachLink = true
+                            } else if(action == 'unLink'){
+                                dbItem.classId = null
+                                dbItem.hiteachLink = false
+                            }
                         }
-                    )
+                    })
 
+                    orgHiData[tempIndex] = temp
+                    
                     // 新增加
                     this.hiteachData = orgHiData
                     this.orgHiteachData = JSON.stringify(orgHiData)
+                } else if (res.error == 1){
+                    this.$Message.error(this.$t('無法關聯已被關聯的Hiteach'))
                 }
+            },
+            err => {
+                this.$Message.error(this.$t('參數錯誤'))
+            }).finally(()=>{
+                this.hiteachListLoading = false
             })
         }
     },

+ 47 - 36
TEAMModelOS/Controllers/School/ClassRoomController.cs

@@ -439,57 +439,68 @@ namespace TEAMModelOS.Controllers
         [HttpPost("hiteach-link")]
         public async Task<IActionResult> HiteachLink(JsonElement request)
         {
-            // 必要檢查
+            // 必要檢查            
             if (!request.TryGetProperty("school_code", out JsonElement code)) return BadRequest();
-            if (!request.TryGetProperty("linkList", out JsonElement links)) return BadRequest();
+            // if (!request.TryGetProperty("linkList", out JsonElement links)) return BadRequest();
+
+            if (!request.TryGetProperty("uuid", out JsonElement a)) return BadRequest();
+            if (!request.TryGetProperty("uuid2", out JsonElement b)) return BadRequest();
+            if (!request.TryGetProperty("pid", out JsonElement d)) return BadRequest();
+            if (!request.TryGetProperty("action", out JsonElement e)) return BadRequest();
+            if (!request.TryGetProperty("classId", out JsonElement c)) return BadRequest();
 
+            // 如果是link classId 必填
+            string action = e.ToString();
+            if (action != "link" && action != "unLink") return BadRequest();
             try
             {
-                if (links.GetArrayLength() > 0) 
-                {
-                    // [變數宣告]
-                    string school_code = code.ToString(); // 學校簡碼
-                    JsonElement.ArrayEnumerator linkLists = request.GetProperty("linkList").EnumerateArray(); // 需要修改的資料
-                    int link_length = links.GetArrayLength(); // 需要修改的筆數
-
-                    // [取得DB資料]
-                    var client = _azureCosmos.GetCosmosClient();
-                    var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school_code, new PartitionKey("Product"));
-
-                    if (response.Status == 200)
-                    {
-                        var json = await JsonDocument.ParseAsync(response.ContentStream);
-
-                        //軟體
-                        SchoolProduct schoolProductItem = json.ToObject<SchoolProduct>();
+                // [變數宣告]
+                string school_code = code.GetString(); // 學校簡碼
+                string uuid = a.GetString(); // uuid
+                string uuid2 = b.GetString(); // uuid2                    
+                string pid = d.GetString(); // id
+                string classId = c.GetString(); // classId
 
-                        foreach (var linkList in linkLists)
-                        {
-                            var uuidBoolean = linkList.TryGetProperty("uuid", out var a);
-                            var uuid = a.GetString();
 
-                            var uuid2Boolean = linkList.TryGetProperty("uuid2", out var b);
-                            var uuid2 = b.GetString();
+                // [取得DB資料]
+                var client = _azureCosmos.GetCosmosClient();
+                var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school_code, new PartitionKey("Product"));
+                var error = 0;
 
-                            var classIdBoolean = linkList.TryGetProperty("classId", out var c);
-                            var classId = c.GetString();
+                if (response.Status == 200)
+                {
+                    var json = await JsonDocument.ParseAsync(response.ContentStream);
 
-                            var idBoolean = linkList.TryGetProperty("id", out var d);
-                            var id = d.GetString();
+                    //軟體
+                    SchoolProduct schoolProductItem = json.ToObject<SchoolProduct>();
 
-                            SerialInfoBaseWithdeviceBound updSerialInfo = schoolProductItem.serial.Where(s => s.id == id).FirstOrDefault();
-                            if (updSerialInfo != null)
+                    SerialInfoBaseWithdeviceBound updSerialInfo = schoolProductItem.serial.Where(s => s.id == pid).FirstOrDefault();
+                    if (updSerialInfo != null)
+                    {
+                        deviceBound updDeviceBound = updSerialInfo.deviceBound.Where(d => d.uuid == uuid && d.uuid2 == uuid2).FirstOrDefault();
+                        if (updDeviceBound != null) {
+                            if (action == "link")
                             {
-                                deviceBound updDeviceBound = updSerialInfo.deviceBound.Where(d => d.uuid == uuid && d.uuid2 == uuid2).FirstOrDefault();
-                                updDeviceBound.classId = classId;
+                                if (updDeviceBound.classId != null)
+                                {
+                                    error = 1;
+                                }
+                                else 
+                                {
+                                    updDeviceBound.classId = classId;
+                                }                                
+                            }
+                            else if(action == "unLink")
+                            {
+                                updDeviceBound.classId = null;
                             }
+                            if(error == 0) await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolProduct>(schoolProductItem, school_code, new PartitionKey("Product"));
                         }
-
-                        await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolProduct>(schoolProductItem, school_code, new PartitionKey("Product"));
+                                
                     }
                 }
                     
-                return Ok(new { error = 0 });
+                return Ok(new { error });
             }
             catch (Exception ex)
             {