1 /* 2 * Copyright (C) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef OHOS_MEDIA_BACKUP_PHOTOS_COUNT_STATISTIC_H 16 #define OHOS_MEDIA_BACKUP_PHOTOS_COUNT_STATISTIC_H 17 18 #include <string> 19 #include <vector> 20 21 #include "rdb_store.h" 22 #include "result_set_utils.h" 23 #include "media_backup_report_data_type.h" 24 25 namespace OHOS::Media { 26 class PhotosCountStatistic { 27 public: SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb)28 PhotosCountStatistic &SetMediaLibraryRdb(std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb) 29 { 30 this->mediaLibraryRdb_ = mediaLibraryRdb; 31 return *this; 32 } SetSceneCode(int32_t sceneCode)33 PhotosCountStatistic &SetSceneCode(int32_t sceneCode) 34 { 35 this->sceneCode_ = sceneCode; 36 return *this; 37 } SetTaskId(const std::string & taskId)38 PhotosCountStatistic &SetTaskId(const std::string &taskId) 39 { 40 this->taskId_ = taskId; 41 return *this; 42 } SetPeriod(const int32_t & period)43 PhotosCountStatistic &SetPeriod(const int32_t &period) 44 { 45 this->period_ = period; 46 return *this; 47 } 48 std::vector<AlbumMediaStatisticInfo> Load(); 49 50 private: 51 int32_t GetCount(const std::string &query, const std::vector<NativeRdb::ValueObject> &args = {}); 52 int32_t QueryTotalCount(SearchCondition searchCondition); 53 int32_t QueryAllRestoreCount(SearchCondition searchCondition); 54 int32_t QueryPicturesTotalCount(SearchCondition searchCondition); 55 std::vector<AlbumStatisticInfo> QueryAlbumCountByName( 56 const std::string &albumName, SearchCondition searchCondition); 57 int32_t QueryLiveCount(int32_t searchType); 58 AlbumMediaStatisticInfo GetAllStatInfo(); 59 AlbumMediaStatisticInfo GetAllImageStatInfo(); 60 AlbumMediaStatisticInfo GetAllVideoStatInfo(); 61 AlbumMediaStatisticInfo GetAllRestoreStatInfo(); 62 AlbumMediaStatisticInfo GetAllRestoreImageStatInfo(); 63 AlbumMediaStatisticInfo GetAllRestoreVideoStatInfo(); 64 AlbumMediaStatisticInfo GetImageAlbumInfo(); 65 AlbumMediaStatisticInfo GetVideoAlbumInfo(); 66 std::vector<AlbumMediaStatisticInfo> GetAlbumInfoByName(const std::string &albumName); 67 AlbumMediaStatisticInfo GetFavoriteAlbumStatInfo(); 68 AlbumMediaStatisticInfo GetTrashedAlbumStatInfo(); 69 AlbumMediaStatisticInfo GetHiddenAlbumStatInfo(); 70 AlbumMediaStatisticInfo GetGalleryAlbumCountInfo(); 71 AlbumMediaStatisticInfo GetGalleryDeletedAlbumCountInfo(); 72 AlbumMediaStatisticInfo GetLiveStatInfo(); 73 74 private: 75 std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_; 76 int32_t sceneCode_; 77 std::string taskId_; 78 int32_t period_ = 0; // 0 - BEFORE, 1 - AFTER 79 80 private: 81 const std::string SQL_PHOTOS_ALL_TOTAL_COUNT = "\ 82 SELECT COUNT(1) AS count \ 83 FROM Photos \ 84 WHERE (0 = ? OR media_type = ?) AND \ 85 (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \ 86 (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \ 87 (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \ 88 (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \ 89 (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \ 90 ;"; 91 const std::string SQL_PHOTOS_ALL_RESTORE_COUNT = "\ 92 SELECT COUNT(1) AS count \ 93 FROM Photos \ 94 LEFT JOIN PhotoAlbum \ 95 ON Photos.owner_album_id = PhotoAlbum.album_id \ 96 WHERE position IN (1, 3) AND \ 97 (COALESCE(PhotoAlbum.album_type, 0) != 2048 OR COALESCE(PhotoAlbum.album_name, '') != '.hiddenAlbum') AND \ 98 (0 = ? OR media_type = ?) AND \ 99 (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \ 100 (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \ 101 (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \ 102 (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \ 103 (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \ 104 ;"; 105 const std::string SQL_PHOTOS_PICTURES_TOTAL_COUNT = "\ 106 SELECT COUNT(1) AS count \ 107 FROM Photos \ 108 WHERE COALESCE(burst_cover_level, 1) = 1 AND \ 109 (0 = ? OR media_type = ?) AND \ 110 (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \ 111 (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \ 112 (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \ 113 (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \ 114 (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \ 115 ;"; 116 const std::string SQL_PHOTOS_COUNT_BY_ALBUM_NAME = "\ 117 SELECT PhotoAlbum.lpath, \ 118 album_plugin.album_name AS albumName, \ 119 COUNT(1) AS count \ 120 FROM Photos \ 121 INNER JOIN PhotoAlbum \ 122 ON Photos.owner_album_id = PhotoAlbum.album_id \ 123 INNER JOIN album_plugin \ 124 ON LOWER(PhotoAlbum.lpath) = LOWER(album_plugin.lpath) \ 125 WHERE album_plugin.album_name = ? AND \ 126 COALESCE(burst_cover_level, 1) = 1 AND \ 127 COALESCE(date_trashed, 0) = 0 AND \ 128 COALESCE(hidden, 0) = 0 AND \ 129 (0 = ? OR media_type = ?) AND \ 130 (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \ 131 (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \ 132 (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \ 133 (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \ 134 (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \ 135 GROUP BY PhotoAlbum.lpath, album_plugin.album_name;"; 136 const std::string SQL_PHOTO_ALBUM_COUNT = "\ 137 SELECT COUNT(1) AS count \ 138 FROM PhotoAlbum \ 139 WHERE album_id IN \ 140 ( \ 141 SELECT DISTINCT owner_album_id \ 142 FROM Photos \ 143 ) OR \ 144 album_id IN \ 145 ( \ 146 SELECT DISTINCT map_album \ 147 FROM PhotoMap \ 148 );"; 149 const std::string SQL_PHOTO_DELETED_ALBUM_COUNT = "\ 150 SELECT COUNT(1) AS count \ 151 FROM PhotoAlbum \ 152 WHERE dirty = ?;"; 153 const std::string SQL_PHOTOS_LIVE_COUNT = "\ 154 SELECT COUNT(1) AS count \ 155 FROM Photos \ 156 WHERE subtype = 3 AND \ 157 (0 = ? OR position = 2) AND \ 158 COALESCE(burst_cover_level, 1) = 1 AND \ 159 COALESCE(date_trashed, 0) = 0 AND \ 160 COALESCE(hidden, 0) = 0;"; 161 }; 162 } // namespace OHOS::Media 163 #endif // OHOS_MEDIA_BACKUP_PHOTOS_COUNT_STATISTIC_H