/**
  * @param \Mageplaza\Blog\Model\Topic $topic
  * @return $this
  */
 protected function savePostRelation(\Mageplaza\Blog\Model\Topic $topic)
 {
     $topic->setIsChangedPostList(false);
     $id = $topic->getId();
     $posts = $topic->getPostsData();
     if ($posts === null) {
         return $this;
     }
     $oldPosts = $topic->getPostsPosition();
     $insert = array_diff_key($posts, $oldPosts);
     $delete = array_diff_key($oldPosts, $posts);
     $update = array_intersect_key($posts, $oldPosts);
     $_update = [];
     foreach ($update as $key => $settings) {
         if (isset($oldPosts[$key]) && $oldPosts[$key] != $settings['position']) {
             $_update[$key] = $settings;
         }
     }
     $update = $_update;
     $adapter = $this->getConnection();
     if (!empty($delete)) {
         $condition = ['post_id IN(?)' => array_keys($delete), 'topic_id=?' => $id];
         $adapter->delete($this->topicPostTable, $condition);
     }
     if (!empty($insert)) {
         $data = [];
         foreach ($insert as $postId => $position) {
             $data[] = ['topic_id' => (int) $id, 'post_id' => (int) $postId, 'position' => (int) $position['position']];
         }
         $adapter->insertMultiple($this->topicPostTable, $data);
     }
     if (!empty($update)) {
         foreach ($update as $postId => $position) {
             $where = ['topic_id = ?' => (int) $id, 'post_id = ?' => (int) $postId];
             $bind = ['position' => (int) $position['position']];
             $adapter->update($this->topicPostTable, $bind, $where);
         }
     }
     if (!empty($insert) || !empty($delete)) {
         $postIds = array_unique(array_merge(array_keys($insert), array_keys($delete)));
         $this->eventManager->dispatch('mageplaza_blog_topic_change_posts', ['topic' => $topic, 'post_ids' => $postIds]);
     }
     if (!empty($insert) || !empty($update) || !empty($delete)) {
         $topic->setIsChangedPostList(true);
         $postIds = array_keys($insert + $delete + $update);
         $topic->setAffectedPostIds($postIds);
     }
     return $this;
 }
 /**
  * Add Topic id to error message
  *
  * @param \Mageplaza\Blog\Model\Topic $topic
  * @param string $errorText
  * @return string
  */
 protected function getErrorWithTopicId(\Mageplaza\Blog\Model\Topic $topic, $errorText)
 {
     return '[Topic ID: ' . $topic->getId() . '] ' . $errorText;
 }