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;
|
}
|
|
}
|