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);
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 GetLiveStatInfo();
72 
73 private:
74     std::shared_ptr<NativeRdb::RdbStore> mediaLibraryRdb_;
75     int32_t sceneCode_;
76     std::string taskId_;
77     int32_t period_ = 0;  // 0 - BEFORE, 1 - AFTER
78 
79 private:
80     const std::string SQL_PHOTOS_ALL_TOTAL_COUNT = "\
81         SELECT COUNT(1) AS count \
82         FROM Photos \
83         WHERE (0 = ? OR media_type = ?) AND \
84             (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \
85             (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \
86             (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \
87             (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \
88             (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \
89         ;";
90     const std::string SQL_PHOTOS_ALL_RESTORE_COUNT = "\
91         SELECT COUNT(1) AS count \
92         FROM Photos \
93             LEFT JOIN PhotoAlbum \
94             ON Photos.owner_album_id = PhotoAlbum.album_id \
95         WHERE position IN (1, 3) AND \
96             (COALESCE(PhotoAlbum.album_type, 0) != 2048 OR COALESCE(PhotoAlbum.album_name, '') != '.hiddenAlbum') AND \
97             (0 = ? OR media_type = ?) AND \
98             (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \
99             (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \
100             (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \
101             (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \
102             (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \
103         ;";
104     const std::string SQL_PHOTOS_PICTURES_TOTAL_COUNT = "\
105         SELECT COUNT(1) AS count \
106         FROM Photos \
107         WHERE COALESCE(burst_cover_level, 1) = 1 AND \
108             (0 = ? OR media_type = ?) AND \
109             (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \
110             (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \
111             (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \
112             (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \
113             (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \
114         ;";
115     const std::string SQL_PHOTOS_COUNT_BY_ALBUM_NAME = "\
116         SELECT PhotoAlbum.lpath, \
117             album_plugin.album_name AS albumName, \
118             COUNT(1) AS count \
119         FROM Photos \
120             INNER JOIN PhotoAlbum \
121             ON Photos.owner_album_id = PhotoAlbum.album_id \
122             INNER JOIN album_plugin \
123             ON LOWER(PhotoAlbum.lpath) = LOWER(album_plugin.lpath) \
124         WHERE album_plugin.album_name = ? AND \
125             COALESCE(burst_cover_level, 1) = 1 AND \
126             COALESCE(date_trashed, 0) = 0 AND \
127             COALESCE(hidden, 0) = 0 AND \
128             (0 = ? OR media_type = ?) AND \
129             (-1 = ? OR 0 = ? AND COALESCE(hidden, 0) = 0 OR 1 = ? AND COALESCE(hidden, 0) = 1) AND \
130             (-1 = ? OR 0 = ? AND COALESCE(date_trashed, 0) = 0 OR 1 = ? AND COALESCE(date_trashed, 0) <> 0) AND \
131             (-1 = ? OR 0 = ? AND position IN (1, 3) OR 1 = ? AND position = 2) AND \
132             (-1 = ? OR 0 = ? AND COALESCE(is_favorite, 1) = 0 OR 1 = ? AND COALESCE(is_favorite, 1) = 1) AND \
133             (-1 = ? OR 0 = ? AND subtype = 4 OR 1 = ? AND subtype = 4 AND COALESCE(burst_cover_level, 1) = 1) \
134         GROUP BY PhotoAlbum.lpath, album_plugin.album_name;";
135     const std::string SQL_PHOTO_ALBUM_COUNT = "\
136         SELECT COUNT(1) AS count \
137         FROM PhotoAlbum \
138         WHERE album_id IN \
139             ( \
140                 SELECT DISTINCT owner_album_id \
141                 FROM Photos \
142             ) OR \
143             album_id IN \
144             ( \
145                 SELECT DISTINCT map_album \
146                 FROM PhotoMap \
147             );";
148     const std::string SQL_PHOTOS_LIVE_COUNT = "\
149         SELECT COUNT(1) AS count \
150         FROM Photos \
151         WHERE subtype = 3 AND \
152             (0 = ? OR position = 2) AND \
153             COALESCE(burst_cover_level, 1) = 1 AND \
154             COALESCE(date_trashed, 0) = 0 AND \
155             COALESCE(hidden, 0) = 0;";
156 };
157 }  // namespace OHOS::Media
158 #endif  // OHOS_MEDIA_BACKUP_PHOTOS_COUNT_STATISTIC_H