Odomhae / weightRecord_Android

0 stars 0 forks source link

realm create , update #21

Open Odomhae opened 3 years ago

Odomhae commented 3 years ago

{

            val roomSeq = chatRoomJSON.getLong("ROOM_SEQ")

            val chatMembers: RealmList<ChatMember> = RealmList()

            if (OpenriderUtil.isHasJSONData(chatRoomJSON, "PARTICIPANT")) {
                //채팅 멤버 처리

                val participantJSONArray = chatRoomJSON.getJSONArray("PARTICIPANT")

                for (i in 0 until participantJSONArray.length()) {
                    //채팅멤버 DB 추가 및 업데이트

                    val participantJSON =  participantJSONArray.getJSONObject(i)

                    if (OpenriderUtil.isHasJSONData(participantJSON, "MEMBER_SEQ")) {

                        val memberSeq = participantJSON.getLong("MEMBER_SEQ")

                        val realmQueryChatMember = realm.where<ChatMember>().equalTo("idMember", memberSeq).findFirst()

                        if (realmQueryChatMember != null) {
                            //채팅 멤버 업데이트

                            if (OpenriderUtil.isHasJSONData(participantJSON, "NICK_NAME")) {
                                realmQueryChatMember.nickName = participantJSON.getString("NICK_NAME")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "PROFILE_URL")) {
                                realmQueryChatMember.profileUrl = participantJSON.getString("PROFILE_URL")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "LEVEL")) {
                                realmQueryChatMember.level = participantJSON.getLong("LEVEL")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "EMAIL")) {
                                realmQueryChatMember.email = participantJSON.getString("EMAIL")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "SPONSOR")) {
                                realmQueryChatMember.sponsor = participantJSON.getString("SPONSOR")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "MANIA")) {
                                realmQueryChatMember.sponsor = participantJSON.getString("MANIA")
                            }

                            chatMembers.add(realm.copyToRealm(realmQueryChatMember))

                        } else {
                            //채팅 멤버 생성

                            val idMax = realm.where<ChatMember>().max("id")
                            val id = idMax?.toLong()?.plus(1L) ?: 0L

                            val chatMember = realm.createObject<ChatMember>(id)

                            chatMember.idMember = memberSeq

                            if (OpenriderUtil.isHasJSONData(participantJSON, "NICK_NAME")) {
                                chatMember.nickName = participantJSON.getString("NICK_NAME")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "PROFILE_URL")) {
                                chatMember.profileUrl = participantJSON.getString("PROFILE_URL")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "LEVEL")) {
                                chatMember.level = participantJSON.getLong("LEVEL")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "EMAIL")) {
                                chatMember.email = participantJSON.getString("EMAIL")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "SPONSOR")) {
                                chatMember.sponsor = participantJSON.getString("SPONSOR")
                            }

                            if (OpenriderUtil.isHasJSONData(participantJSON, "MANIA")) {
                                chatMember.sponsor = participantJSON.getString("MANIA")
                            }
                            chatMembers.add(realm.copyToRealm(chatMember))
                        }
                    }
                }
            }

            //채팅방 처리
            val realmQueryChatRoom = realm.where<ChatRoom>().equalTo("idRoom", roomSeq).findFirst()

            if (realmQueryChatRoom != null) {
                //채팅방 업데이트

                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "MESSAGE_SEQ")) {
                    realmQueryChatRoom.idServerLastMessage = chatRoomJSON.getLong("MESSAGE_SEQ")
                }

                //                                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "TITLE")) {
                //                                    realmQueryChatRoom.name = chatRoomJSON.getString("TITLE")
                //                                }

                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "INS_DT")) {
                    realmQueryChatRoom.lastMessageDateInsert = chatRoomJSON.getString("INS_DT")
                }

                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "CONTENT")) {
                    realmQueryChatRoom.lastMessageContent = chatRoomJSON.getString("CONTENT")
                }

                realmQueryChatRoom.chatMembers = chatMembers

            } else {
                //채팅방 생성

                val idMax = realm.where<ChatRoom>().max("id")
                val id = idMax?.toLong()?.plus(1L) ?: 0L

                val chatRoom = realm.createObject<ChatRoom>(id)

                chatRoom.idRoom = roomSeq

                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "MEMBER_SEQ")) {
                    chatRoom.idMember = chatRoomJSON.getLong("MEMBER_SEQ")
                }

                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "MESSAGE_SEQ")) {
                    chatRoom.idServerLastMessage = chatRoomJSON.getLong("MESSAGE_SEQ")
                }

                //                                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "TITLE")) {
                //                                    chatRoom.name = chatRoomJSON.getString("TITLE")
                //                                }

                val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                simpleDateFormat.timeZone = TimeZone.getTimeZone("GMT")
                chatRoom.dateInsert = simpleDateFormat.format(Date())

                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "INS_DT")) {
                    chatRoom.lastMessageDateInsert = chatRoomJSON.getString("INS_DT")
                }

                if (OpenriderUtil.isHasJSONData(chatRoomJSON, "CONTENT")) {
                    chatRoom.lastMessageContent = chatRoomJSON.getString("CONTENT")
                }

                chatRoom.chatMembers = chatMembers
            }
        }