a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.basic.security.manager;
 
import android.os.SystemClock;
import android.text.TextUtils;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.model.ModelAdapter;
import com.basic.security.model.Person;
import com.basic.security.model.PersonA;
import com.basic.security.model.PersonCameraImagePath;
import com.basic.security.model.Sync;
import com.basic.security.utils.Constants;
import com.basic.security.utils.WeedUpload;
 
import java.io.File;
import java.net.URL;
import java.util.List;
 
public class PersonCameraImagePathManager extends BaseManager {
 
    public static void startDownloadCameraImagePath() {
        BaseApplication.getApplication().executorService.execute(() -> {
            while (true) {
                try {
                    SystemClock.sleep(10000);
                    String sql = "select a.* from " + PersonCameraImagePath.tableName + " a where 1=1 and a." + PersonCameraImagePath.camera_image_path_exist
                            + "='false' and " + PersonCameraImagePath.camera_image_path + "!='' limit 10";
//                    System.out.println("CameraImagePathManager.startDownloadCameraImagePath sql=" + sql);
                    List<ModelAdapter> personCameraImagePathList = findList(sql);
                    for (ModelAdapter personCameraImagePath : personCameraImagePathList) {
                        if (personCameraImagePath != null) {
                            ModelAdapter personA = PersonAManager.findPersonAById(personCameraImagePath.getString(PersonCameraImagePath.id));
                            ModelAdapter person = PersonManager.findPersonById(personCameraImagePath.getString(PersonCameraImagePath.id));
                            if (personA != null) {
                                String personPicUrl = personA.getString(PersonA.personPicUrl);
                                if (download_camera_image_path(personPicUrl, person)) {
                                    updateCameraImagePathExist(person);
                                }
                            }
                        }
                    }
 
                    if (personCameraImagePathList.size() < 10) {
                        SystemClock.sleep(3000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    public static void startUploadCameraImagePath() {
        BaseApplication.getApplication().executorService.execute(() -> {
            while (true) {
                try {
                    SystemClock.sleep(10000);
                    String sql = "select a.* from " + PersonCameraImagePath.tableName + " a left join "
                            + PersonA.tableName + " b on a.id=b.id  where 1=1 " +
//                            " and " + PersonCameraImagePath.camera_image_path_upload + "='false' " +
                            " and " + PersonA.personPicUrl + "='' limit 10";
                    List<ModelAdapter> personCameraImagePathList = findList(sql);
//                    System.out.println("PersonCameraImagePathManager.startUploadCameraImagePath sql=" + sql + " personCameraImagePathList=" + personCameraImagePathList);
                    for (ModelAdapter personCameraImagePath : personCameraImagePathList) {
                        if (personCameraImagePath != null) {
                            ModelAdapter personA = PersonAManager.findPersonAById(personCameraImagePath.getString(PersonCameraImagePath.id));
                            ModelAdapter person = PersonManager.findPersonById(personCameraImagePath.getString(PersonCameraImagePath.id));
                            if (personA != null) {
                                if (uploadCameraImagePath(personA, person)) {
                                    personA.setString(Sync.needSync, Constants.TRUE);
                                    PersonAManager.savePersonA(personA);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    System.out.println("PersonCameraImagePathManager.startUploadCameraImagePath " + e.getMessage());
//                    e.printStackTrace();
                }
            }
        });
    }
 
    public static void updateCameraImagePathExist(ModelAdapter person) {
        String table = person.getString(Constants.TABLE);
        if (table == null) {
            table = "";
        }
        if (table.equals("person")) {
            try {
                ModelAdapter personCameraImagePath = findOrCreatePersonCameraImagePath(person);
                String camera_image_path = person.getString(Person.camera_image_path);
                String exists = Constants.FALSE;
                if (!TextUtils.isEmpty(camera_image_path)) {
                    File camera_image_path_file = new File(camera_image_path);
                    if (camera_image_path_file.exists()) {
                        exists = Constants.TRUE;
                    }
                }
                personCameraImagePath.setString(PersonCameraImagePath.camera_image_path_exist, exists);
                save(personCameraImagePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    public static ModelAdapter findOrCreatePersonCameraImagePath(ModelAdapter person) {
        ModelAdapter personCameraImagePath = findById(PersonCameraImagePath.tableName, person.getString(Person.id));
        if (personCameraImagePath == null) {
            personCameraImagePath = new ModelAdapter();
            personCameraImagePath.setString(PersonCameraImagePath.id, person.getString(Person.id));
            personCameraImagePath.setString(Constants.TABLE, PersonCameraImagePath.tableName);
            personCameraImagePath.setString(PersonCameraImagePath.camera_image_path_upload, Constants.FALSE);
            personCameraImagePath.setString(PersonCameraImagePath.company_id, CompanyManager.getCompanyId());
            personCameraImagePath.setString(PersonCameraImagePath.device_id, DeviceManager.getDeviceId());
            personCameraImagePath.setString(PersonCameraImagePath.from_device_id, person.getString(Person.device_id));
            personCameraImagePath.setString(PersonCameraImagePath.camera_image_path, person.getString(Person.camera_image_path));
            save(personCameraImagePath);
        }
        return personCameraImagePath;
    }
 
    public static boolean download_camera_image_path(String personPicUrl, ModelAdapter person) {
        try {
            String camera_image_path = ModelAdapter.getAttachmentPath(person.getId(), Person.camera_image_path, Person.tableName);
            if (!TextUtils.isEmpty(camera_image_path)) {
                File camera_image_path_file = new File(camera_image_path);
                if (!camera_image_path_file.exists()) {
                    if (camera_image_path_file.length() == 0) {
                        camera_image_path_file.delete();
                    }
                    if (!TextUtils.isEmpty(personPicUrl)) {
                        if (!camera_image_path_file.exists()) {
                            org.apache.commons.io.FileUtils.copyURLToFile(new URL(personPicUrl), camera_image_path_file);
                        }
                        person.setString(Person.camera_image_path, camera_image_path);
                        if (camera_image_path_file.exists()) {
                            updateCameraImagePathExist(person);
                            return true;
                        }
                    } else {
                        System.out.println("PersonCameraImagePathManager.download_camera_image_path personPicUrl=" + personPicUrl);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    public static boolean uploadCameraImagePath(ModelAdapter personA, ModelAdapter person) {
        String personPicUrl = "";
        try {
            personPicUrl = personA.getString(PersonA.personPicUrl);
            if (TextUtils.isEmpty(personPicUrl)) {
                if (person != null) {
                    File camera_image_path_file = new File(person.getString(Person.camera_image_path));
                    if (camera_image_path_file.exists()) {
//                        System.out.println("PersonCameraImagePathManager.uploadCameraImagePath personId=" + personA.getString(PersonA.id));
                        personPicUrl = WeedUpload.upload(camera_image_path_file);
                    }
                    personA.setString(PersonA.personPicUrl, personPicUrl);
                    if (!TextUtils.isEmpty(personPicUrl)) {
                        ModelAdapter personCameraImagePath = findOrCreatePersonCameraImagePath(person);
                        personCameraImagePath.setString(PersonCameraImagePath.camera_image_path_upload, Constants.TRUE);
                        save(personCameraImagePath);
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
}