nasUploadFileManager~.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. //
  2. // nasUploadFileManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/8/7.
  6. //
  7. #import "nasUploadFileManager.h"
  8. #import "nasUploadManager.h"
  9. @implementation nasUploadFileManager
  10. + (instancetype)shareInstance {
  11. static nasUploadFileManager *_instance;
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. _instance = [[self alloc] init];
  15. });
  16. return _instance;
  17. }
  18. - (id)init
  19. {
  20. self = [super init];
  21. if (self) {
  22. [self initManager];
  23. }
  24. return self;
  25. }
  26. - (void)initManager
  27. {
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadTaskFinishedNoti:) name:nasUploadTaskExeEnd object:nil];
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadTaskErrorNoti:) name:nasUploadTaskExeError object:nil];
  30. }
  31. #pragma mark 读取数据库数据
  32. - (void)getDataInDatabaseFun:(BOOL)isReGet complete:(custom_complete_Arr)complete
  33. {
  34. if(_databaseArr && _databaseArr.count == 3 && !isReGet){
  35. complete(_databaseArr);
  36. return;
  37. }
  38. if(!_databaseArr)
  39. {
  40. _databaseArr = [NSMutableArray new];
  41. }
  42. //KWeakSelf
  43. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
  44. HLog(@"begin bg_findAsync");
  45. [uploadFileDataModel bg_findAsync:nasUpLoadFile_tableName limit:0 orderBy:nil desc:YES complete:^(NSArray * _Nullable array) {
  46. HLog(@"end bg_findAsync");
  47. NSMutableArray *failArr = [NSMutableArray new];
  48. NSMutableArray *doneArr = [NSMutableArray new];
  49. NSMutableArray *otherArr = [NSMutableArray new];
  50. for (uploadFileDataModel * curModel in array) {
  51. //图片 和视频 还原
  52. if(curModel.curUploadFileType == uploadFileTypeImage){
  53. }
  54. else if(curModel.curUploadFileType == uploadFileTypeVideo){
  55. if(curModel.curUploadStateType != uploadStateDone){
  56. }
  57. }
  58. if(curModel.curUploadStateType == uploadStateFail){
  59. [failArr addObject:curModel];
  60. }
  61. else if(curModel.curUploadStateType == uploadStateDone){
  62. [doneArr addObject:curModel];
  63. }
  64. else{
  65. [otherArr addObject:curModel];
  66. }
  67. }
  68. NSMutableArray *newArr = [NSMutableArray new];
  69. [newArr addObject:otherArr];
  70. [newArr addObject:doneArr];
  71. [newArr addObject:failArr];
  72. self->_databaseArr = newArr;
  73. complete(self->_databaseArr);
  74. }];
  75. });
  76. }
  77. //把TZAssetModel 转成 我们需要上传的model
  78. - (void)handlTZAssetModelToUploadFileDataFunBy:(NSMutableArray*)indexPathsForSelectedItems complete:(custom_complete_Arr)complete
  79. {
  80. if(!indexPathsForSelectedItems && indexPathsForSelectedItems.count == 0){
  81. complete(_databaseArr);
  82. return;
  83. }
  84. if(ksharedAppDelegate.DisabledFileTransferType){
  85. if(ksharedAppDelegate.isImageNewFor130){
  86. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip",nil)] show];
  87. }
  88. else{
  89. [[iToast makeText:NSLocalizedString(@"File_Transfer_Disable_tip2",nil)] show];
  90. }
  91. complete(_databaseArr);
  92. return;
  93. }
  94. _isNewAddTaskType = YES;
  95. //self.curUploadModelNumbers = indexPathsForSelectedItems.count;
  96. NSMutableArray *newUploadTaskArr = [NSMutableArray new];
  97. for (TZAssetModel * model in indexPathsForSelectedItems) {
  98. uploadFileDataModel * curModel = [uploadFileDataModel new];
  99. curModel.asset = model.asset;
  100. curModel.localIdentifier = model.asset.localIdentifier;
  101. BOOL isRepeatingTasksType = NO;
  102. //查询上传任务是否存在
  103. if(_databaseArr && _databaseArr.count == 3 ){
  104. NSMutableArray *uploadingArr = [[NSMutableArray alloc] initWithArray:_databaseArr[0]];
  105. for (uploadFileDataModel *preModel in uploadingArr) {
  106. if([preModel.localIdentifier isEqualToString:curModel.localIdentifier]){
  107. isRepeatingTasksType = YES;
  108. break;
  109. }
  110. }
  111. }
  112. if(isRepeatingTasksType){
  113. continue;
  114. }
  115. NSString * uploadDefaultPath = [HWDataManager getStringWithKey:stringKeyAddSn(Const_photo_upload_default_path)];
  116. if(!uploadDefaultPath || uploadDefaultPath.length == 0){
  117. [HWDataManager setStringWithKey:stringKeyAddSn(Const_photo_upload_default_path) value:Const_default_upload_path];
  118. uploadDefaultPath = Const_default_upload_path;
  119. }
  120. //判断最后一个是否是/
  121. NSString *lastStr= [uploadDefaultPath substringFromIndex:uploadDefaultPath.length-1];
  122. if(![lastStr isEqualToString:@"/"]){
  123. uploadDefaultPath = [[NSString alloc] initWithFormat:@"%@/",uploadDefaultPath];
  124. }
  125. //NSString*savePath = [[NSString alloc] initWithFormat:@"%@%@",uploadDefaultPath,filePath];
  126. curModel.savePath = uploadDefaultPath;
  127. // curModel.imageData = model.imageData;
  128. // curModel.videoData = model.videoData;
  129. curModel.filename = [model.asset valueForKey:@"filename"];
  130. //curModel.curUploadStateType = uploadStateWait;
  131. curModel.curUploadStateType = uploadStateUploading;
  132. if(model.type == TZAssetModelMediaTypeVideo){
  133. curModel.curUploadFileType = uploadFileTypeVideo;
  134. [cachesFileManager getFileNameWithContent:curModel.videoData fileName:curModel.filename type:uploadFileTypeVideo];
  135. //curModel.totalBytes = [model.videoData length];
  136. curModel.totalBytes = model.totalBytes;
  137. curModel.videoData = [NSData new];//视频文件存储到文件后内存清空
  138. NSString *imgName1 = [curModel.filename stringByReplacingOccurrencesOfString:@"." withString:@"_"];
  139. curModel.videoFirstImageName = [[NSString alloc] initWithFormat:@"%@.png",imgName1];
  140. //第一帧图片
  141. [[PHImageManager defaultManager] requestImageDataForAsset:curModel.asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
  142. // 直接得到最终的 NSData 数据
  143. if (imageData) {
  144. curModel.imageData = imageData;
  145. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.videoFirstImageName type:uploadFileTypeImage];
  146. }
  147. }];
  148. }
  149. else{
  150. curModel.curUploadFileType = uploadFileTypeImage;
  151. curModel.totalBytes = model.totalBytes;
  152. if(curModel.imageData)
  153. {
  154. [cachesFileManager getFileNameWithContent:curModel.imageData fileName:curModel.filename type:uploadFileTypeImage];
  155. curModel.imageData = nil;
  156. }
  157. else{
  158. }
  159. }
  160. //保存到数据库
  161. curModel.bg_tableName = nasUpLoadFile_tableName;
  162. [newUploadTaskArr addObject:curModel];
  163. }
  164. KWeakSelf
  165. if(newUploadTaskArr.count == 0){
  166. [self getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  167. complete(Arr);
  168. [weakSelf handleUploadFileModelBg_idFun:Arr];
  169. }];
  170. }
  171. else{
  172. [uploadFileDataModel bg_saveOrUpdateArrayAsync:newUploadTaskArr complete:^(BOOL isSuccess) {
  173. [weakSelf getDataInDatabaseFun:YES complete:^(NSMutableArray * _Nonnull Arr) {
  174. complete(Arr);
  175. [weakSelf handleUploadFileModelBg_idFun:Arr];
  176. }];
  177. }];
  178. }
  179. }
  180. #pragma mark 处理当前的model 加上bg_id
  181. - (void)handleUploadFileModelBg_idFun:(NSMutableArray*)totalArr
  182. {
  183. if(!totalArr || totalArr.count != 3){
  184. return;
  185. }
  186. NSMutableArray* uploadTaskArr = [NSMutableArray arrayWithArray:totalArr[0]];
  187. //多任务同时进行模式
  188. if(uploadTaskArr.count >0){
  189. [[nasMixUploadManager shareManager] addUploadWithModels:uploadTaskArr];
  190. }
  191. }
  192. #pragma mark 上传完成
  193. - (void)uploadTaskFinishedNoti:(NSNotification *)notification
  194. {
  195. uploadFileDataModel *model = notification.object;
  196. if(!model)return;
  197. model.curUploadStateType = uploadStateDone;
  198. [self nasUploadFileChangeingOneFileFunBy:model];
  199. [self deleteCacheDataBy:model];
  200. }
  201. #pragma mark 上传失败
  202. - (void)uploadTaskErrorNoti:(NSNotification *)notification
  203. {
  204. uploadFileDataModel *model = notification.object;
  205. if(!model)return;
  206. model.curUploadStateType = uploadStateFail;
  207. [self nasUploadFileChangeingOneFileFunBy:model];
  208. [self deleteCacheDataBy:model];
  209. }
  210. #pragma mark 上传完成 清理缓存
  211. - (void)deleteCacheDataBy:(uploadFileDataModel*)uploadFileDataMod
  212. {
  213. HLog(@"上传--清理缓存")
  214. uploadFileDataMod.imageData = nil;
  215. uploadFileDataMod.videoData = nil;
  216. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  217. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  218. }
  219. uploadFileDataMod = nil;
  220. }
  221. #pragma mark 更新数据状态
  222. - (void)nasUploadFileChangeingOneFileFunBy:(uploadFileDataModel*)dataModel
  223. {
  224. //test code
  225. //return;
  226. HLog(@"更新数据状态")
  227. if(dataModel.curUploadStateType == uploadStateDone){
  228. [self handleDatabaseArrDeleteObjectInUploading:dataModel];
  229. [self handleDatabaseArrAddObjectInDone:dataModel];
  230. }
  231. else if(dataModel.curUploadStateType == uploadStateFail){
  232. if (!_databaseArr || _databaseArr.count != 3) {
  233. return;
  234. }
  235. //删除操作导致的失败
  236. NSMutableArray *ingArr = _databaseArr[0];
  237. NSInteger count = ingArr.count;
  238. if(count == 0){
  239. return;
  240. }
  241. [self handleDatabaseArrDeleteObjectInUploading:dataModel];
  242. [self handleDatabaseArrAddObjectInFail:dataModel];
  243. }
  244. else{
  245. //[cachesFileManager writeLogsWithMsg:@"nasUploadFileChangeingOneFileFunBy else"];
  246. }
  247. dataModel.bg_tableName = nasUpLoadFile_tableName;
  248. [dataModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
  249. HLog(@"%@ 写入 %@", dataModel.filename, isSuccess ? @"成功":@"失败");
  250. }];
  251. // if(_databaseArr.count == 3)
  252. // {
  253. // NSMutableArray *ingArr = _databaseArr[0];
  254. // NSMutableArray* doneArr = _databaseArr[1];
  255. // NSMutableArray* failArr = _databaseArr[2];
  256. //
  257. // NSString *logstr = [[NSString alloc] initWithFormat:@"nasUploadFileChangeingOneFileFunBy ing:%ld,done:%ld,fail:%ld---total:%ld",ingArr.count,doneArr.count,failArr.count,ingArr.count + doneArr.count +failArr.count];
  258. // [cachesFileManager writeLogsWithMsg:logstr];
  259. // }
  260. if(!_isSuspendType || dataModel.curUploadStateType != uploadStateSuspend){
  261. HLog(@"nasUploadFileChangeingOneFileFunBy uploadFileRefreshNotification")
  262. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:dataModel];
  263. }
  264. }
  265. #pragma mark 删除上传中的任务
  266. - (void)handleDatabaseArrDeleteObjectInUploading:(uploadFileDataModel*)model
  267. {
  268. HLog(@"删除上传中的任务")
  269. @synchronized (self) {
  270. if(_databaseArr && _databaseArr.count == 3 ){
  271. NSMutableArray *ingArr = _databaseArr[0];
  272. NSInteger count = ingArr.count;
  273. [ingArr removeObject:model];
  274. NSInteger atferDelCount = ingArr.count;
  275. if(count == atferDelCount){
  276. // NSString* logStr = [[NSString alloc] initWithFormat:@"DeleteObjectInUploading error begin %ld--model:%@--%@",ingArr.count,model,model.filename];
  277. // HLog(@"%@",logStr)
  278. // [cachesFileManager writeLogsWithMsg:logStr];
  279. for (uploadFileDataModel*preModel in ingArr) {
  280. if(preModel.bg_id.integerValue == model.bg_id.integerValue){
  281. if (//[preModel.filename isEqualToString:model.filename]&&
  282. [preModel.localIdentifier isEqualToString:model.localIdentifier]) {
  283. [ingArr removeObject:preModel];
  284. }else{
  285. // NSString* logStr22 = [[NSString alloc] initWithFormat:@"filename error %@",preModel.filename];
  286. // [cachesFileManager writeLogsWithMsg:logStr22];
  287. }
  288. break;
  289. }
  290. }
  291. NSString* logStr2 = [[NSString alloc] initWithFormat:@"DeleteObjectInUploading error end %ld",ingArr.count];
  292. HLog(@"%@",logStr2)
  293. //[cachesFileManager writeLogsWithMsg:logStr2];
  294. }
  295. }
  296. }
  297. }
  298. #pragma mark 添加任务到上传完成
  299. - (void)handleDatabaseArrAddObjectInDone:(uploadFileDataModel*)model
  300. {
  301. HLog(@"添加任务到上传完成")
  302. @synchronized (self) {
  303. if(_databaseArr && _databaseArr.count == 3 ){
  304. NSMutableArray *doneArr = _databaseArr[1];
  305. if(model){
  306. [doneArr insertObject:model atIndex:0];
  307. }
  308. }
  309. }
  310. }
  311. #pragma mark 添加任务到上传失败
  312. - (void)handleDatabaseArrAddObjectInFail:(uploadFileDataModel*)model
  313. {
  314. HLog(@"添加任务到上传失败")
  315. @synchronized(self){
  316. if(_databaseArr && _databaseArr.count == 3 ){
  317. NSMutableArray *failArr = _databaseArr[2];
  318. if(model){
  319. [failArr insertObject:model atIndex:0];
  320. }
  321. }
  322. }
  323. }
  324. #pragma mark 上传完成所有任务
  325. - (void)didUploadAllTaskDoneFun
  326. {
  327. //self->_isUploadIngType = NO;
  328. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileAllTaskDoneNotification object:nil];
  329. }
  330. #pragma mark 删除内存数据记录
  331. - (void)handleDatabaseArrByDelete:(uploadFileDataModel*)delModel
  332. {
  333. if(_databaseArr && _databaseArr.count == 3 ){
  334. NSMutableArray *uploadingArr = nil;
  335. if(delModel.curUploadStateType == uploadStateUploading
  336. ||delModel.curUploadStateType == uploadStateSuspend
  337. ||delModel.curUploadStateType == uploadStateWait){
  338. uploadingArr = _databaseArr[0];
  339. }
  340. else if(delModel.curUploadStateType == uploadStateDone)
  341. {
  342. uploadingArr = _databaseArr[1];
  343. }
  344. else{
  345. uploadingArr = _databaseArr[2];
  346. }
  347. [uploadingArr removeObject:delModel];
  348. }
  349. }
  350. #pragma mark 删除本地数据库记录
  351. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache complete:(custom_complete_B)complete
  352. {
  353. //
  354. BOOL isDelUploadingModel = NO;
  355. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  356. for (uploadFileDataModel *uploadFileDataMod in curDelArr) {
  357. //1.删除数据库
  358. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  359. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  360. [where appendString:curStr];
  361. //isSuc = [uploadFileDataModel bg_delete:nasUpLoadFile_tableName where:where];
  362. [uploadFileDataModel bg_deleteAsync:nasUpLoadFile_tableName where:where complete:^(BOOL isSuccess) {
  363. if(isSuccess){
  364. }
  365. }];
  366. // 2.删除内存数据
  367. [self handleDatabaseArrByDelete:uploadFileDataMod];
  368. //3.删除缓存的本地数据
  369. if(isDelCache){
  370. //判断是否可以删除本地缓存
  371. if(_databaseArr && _databaseArr.count == 3 ){
  372. NSMutableArray *uploadingArr = _databaseArr[0];
  373. NSMutableArray *uploadDoneArr = _databaseArr[1];
  374. NSMutableArray *uploadFailArr = _databaseArr[2];
  375. BOOL isNeedDel = YES;
  376. for (uploadFileDataModel *baseUploadFileDataMod in uploadingArr) {
  377. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  378. isNeedDel = NO;
  379. break;
  380. }
  381. }
  382. if(isNeedDel){
  383. for (uploadFileDataModel *baseUploadFileDataMod in uploadDoneArr) {
  384. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  385. isNeedDel = NO;
  386. break;
  387. }
  388. }
  389. }
  390. if(isNeedDel){
  391. for (uploadFileDataModel *baseUploadFileDataMod in uploadFailArr) {
  392. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  393. isNeedDel = NO;
  394. break;
  395. }
  396. }
  397. }
  398. if(isNeedDel){
  399. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  400. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  401. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  402. }
  403. else{
  404. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  405. }
  406. }
  407. }
  408. }
  409. if(!isDelCache){//上传报云机已经存在文件了 查询本地是否有任务记录
  410. [self handleRetryUploadAndDelRecordFun:uploadFileDataMod];
  411. }
  412. //是否在删除上传中的任务
  413. if(uploadFileDataMod.curUploadStateType == uploadStateUploading){
  414. [[nasMixUploadManager shareManager] cancelUploadTaskFunWith:uploadFileDataMod];
  415. }
  416. }
  417. uploadFileDataModel *RefreshUploadFileDataMod = nil;
  418. if(delArr.count >= 1){
  419. RefreshUploadFileDataMod = delArr.firstObject;
  420. }
  421. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:RefreshUploadFileDataMod];
  422. complete(YES);
  423. }
  424. #pragma mark 处理重复上传文件的问题
  425. - (void)handleRetryUploadAndDelRecordFun:(uploadFileDataModel *)uploadFileDataMod
  426. {
  427. @synchronized (self) {
  428. if (!_databaseArr || _databaseArr.count != 3) {
  429. return;
  430. }
  431. NSMutableArray * doneArr = _databaseArr[1];
  432. NSMutableArray * failArr = _databaseArr[2];
  433. BOOL isNeedDelType = YES;
  434. for (uploadFileDataModel *doneModel in doneArr) {
  435. if([doneModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  436. isNeedDelType = NO;
  437. break;
  438. }
  439. }
  440. if(isNeedDelType){
  441. for (uploadFileDataModel *failModel in failArr) {
  442. if([failModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  443. isNeedDelType = NO;
  444. break;
  445. }
  446. }
  447. }
  448. if(isNeedDelType){
  449. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  450. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  451. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  452. }
  453. else{
  454. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  455. }
  456. }
  457. else{//视频文件是要删除的
  458. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  459. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  460. }
  461. }
  462. }
  463. }
  464. - (void)checkHadUploadTaskWithComplete:(custom_complete_B)complete
  465. {
  466. [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
  467. mainBlock(^{
  468. if(!Arr || Arr.count != 3){
  469. complete(NO);
  470. }
  471. else{
  472. NSArray *firstArr = Arr[0];
  473. if(firstArr.count >0){
  474. complete(YES);
  475. }
  476. else{
  477. complete(NO);
  478. }
  479. }
  480. });
  481. }];
  482. }
  483. //文件重新上传
  484. - (void)reUploadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
  485. {
  486. if(!Arr || Arr.count == 0){
  487. return;
  488. }
  489. //1.修改状态从失败改为上传中
  490. for (uploadFileDataModel * model in Arr) {
  491. model.curUploadStateType = uploadStateUploading;
  492. //修改已经读取的内存数据
  493. NSMutableArray * failArr = _databaseArr[2];
  494. for (uploadFileDataModel * failModel in failArr) {
  495. if(model.bg_id.integerValue == failModel.bg_id.integerValue){
  496. [failArr removeObject:failModel];
  497. break;
  498. }
  499. }
  500. }
  501. //2、保存到数控库
  502. [uploadFileDataModel bg_saveOrUpdateArrayAsync:Arr complete:^(BOOL isSuccess) {
  503. }];
  504. //重新开始上传
  505. [[nasMixUploadManager shareManager] addUploadWithModels:Arr];
  506. }
  507. //暂停上传 当 isSuspendAll为NO时候 传指定暂停哪一个
  508. - (void)suspendUploadFileFun:(BOOL)isSuspendAll withModel:(uploadFileDataModel*)model
  509. {
  510. //[[nasUploadManager shareInstance] cancelUploadTaskFun];
  511. if (isSuspendAll) {
  512. [[nasMixUploadManager shareManager] cancelUploadAllTaskFun];
  513. }
  514. else{
  515. [[nasMixUploadManager shareManager] cancelUploadTaskFunWith:model];
  516. }
  517. [self suspendUploadingTaskBy:isSuspendAll withModel:model];
  518. }
  519. #pragma mark 暂停任务
  520. - (void)suspendUploadingTaskBy:(BOOL)isDeleteAll withModel:(uploadFileDataModel*)model
  521. {
  522. NSMutableArray *uploadingArr = [NSMutableArray new];
  523. if(!isDeleteAll){
  524. if(model){
  525. model.curUploadStateType = uploadStateSuspend;
  526. [uploadingArr addObject:model];
  527. }
  528. }
  529. else
  530. {
  531. if (!_databaseArr || _databaseArr.count != 3) {
  532. return;
  533. }
  534. uploadingArr = _databaseArr[0];
  535. for (uploadFileDataModel*preModel in uploadingArr) {
  536. preModel.curUploadStateType = uploadStateSuspend;
  537. }
  538. }
  539. if(uploadingArr.count > 0){
  540. //更新数据库
  541. [uploadFileDataModel bg_saveOrUpdateArrayAsync:uploadingArr complete:^(BOOL isSuccess) {
  542. }];
  543. }
  544. }
  545. - (NSMutableArray*)uploadingArr
  546. {
  547. if(_databaseArr && _databaseArr.count == 3){
  548. NSMutableArray *uploadingArr = _databaseArr[0];
  549. return uploadingArr;
  550. }
  551. return [NSMutableArray new];
  552. }
  553. //网络失败后任务暂时保存
  554. - (void)saveUploadingTaskByNetWorkErrorFun
  555. {
  556. HLog(@"hxd saveUploadingTaskByNetWorkErrorFun")
  557. if(self.uploadingArr.count > 0){
  558. [[nasMixUploadManager shareManager] cancelUploadAllTaskFun];
  559. _needToReUploadTaskType = YES;
  560. }
  561. }
  562. //重新上传网络失败的任务
  563. - (void)reUploadFileFunByNetWork
  564. {
  565. [nasUploadFileManager shareInstance].needToReUploadTaskType = NO;
  566. HLog(@"reUploadFileFunByNetWork")
  567. //[cachesFileManager writeLogsWithMsg:@"reUploadFileFunByNetWork"];
  568. if(self.uploadingArr.count >0 && _databaseArr.count == 3){
  569. // NSMutableArray* doneArr = _databaseArr[1];
  570. // NSMutableArray* failArr = _databaseArr[2];
  571. //
  572. // NSString *logstr = [[NSString alloc] initWithFormat:@"ing:%ld,done:%ld,fail:%ld",self.uploadingArr.count,doneArr.count,failArr.count];
  573. //
  574. // [cachesFileManager writeLogsWithMsg:logstr];
  575. //1.修改状态从失败改为上传中
  576. // for (uploadFileDataModel * model in _netWorkFailSaveArr) {
  577. // model.curUploadStateType = uploadStateUploading;
  578. //// for (uploadFileDataModel * uploadingModel in self.uploadingArr){
  579. //// if (model.bg_id.integerValue == uploadingModel.bg_id.integerValue) {
  580. //// uploadingModel.curUploadStateType = uploadStateUploading;
  581. //// continue;
  582. //// }
  583. //// }
  584. // }
  585. //2、保存到数控库
  586. // [uploadFileDataModel bg_saveOrUpdateArrayAsync:_netWorkFailSaveArr complete:^(BOOL isSuccess) {
  587. //
  588. // }];
  589. //3.判断是否可以上传
  590. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  591. if(!isCanUseCellular){//不允许流量上传
  592. //
  593. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  594. return;
  595. }
  596. }
  597. //4.重新开始上传
  598. [[nasMixUploadManager shareManager] addUploadWithModels:self.uploadingArr];
  599. }
  600. }
  601. @end