nasUploadFileManager.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. 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];
  257. [cachesFileManager writeLogsWithMsg:logstr];
  258. }
  259. if(!_isSuspendType || dataModel.curUploadStateType != uploadStateSuspend){
  260. HLog(@"nasUploadFileChangeingOneFileFunBy uploadFileRefreshNotification")
  261. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:dataModel];
  262. }
  263. }
  264. #pragma mark 删除上传中的任务
  265. - (void)handleDatabaseArrDeleteObjectInUploading:(uploadFileDataModel*)model
  266. {
  267. HLog(@"删除上传中的任务")
  268. @synchronized (self) {
  269. if(_databaseArr && _databaseArr.count == 3 ){
  270. NSMutableArray *ingArr = _databaseArr[0];
  271. NSInteger count = ingArr.count;
  272. [ingArr removeObject:model];
  273. NSInteger atferDelCount = ingArr.count;
  274. if(count == atferDelCount){
  275. NSString* logStr = [[NSString alloc] initWithFormat:@"DeleteObjectInUploading error begin %ld--model:%@--%@",ingArr.count,model,model.filename];
  276. HLog(@"%@",logStr)
  277. [cachesFileManager writeLogsWithMsg:logStr];
  278. for (uploadFileDataModel*preModel in ingArr) {
  279. if(preModel.bg_id.integerValue == model.bg_id.integerValue){
  280. if ([preModel.filename isEqualToString:model.filename]) {
  281. [ingArr removeObject:preModel];
  282. }else{
  283. NSString* logStr22 = [[NSString alloc] initWithFormat:@"filename error %@",preModel.filename];
  284. [cachesFileManager writeLogsWithMsg:logStr22];
  285. }
  286. break;
  287. }
  288. }
  289. NSString* logStr2 = [[NSString alloc] initWithFormat:@"DeleteObjectInUploading error end %ld",ingArr.count];
  290. HLog(@"%@",logStr2)
  291. [cachesFileManager writeLogsWithMsg:logStr2];
  292. }
  293. }
  294. }
  295. }
  296. #pragma mark 添加任务到上传完成
  297. - (void)handleDatabaseArrAddObjectInDone:(uploadFileDataModel*)model
  298. {
  299. HLog(@"添加任务到上传完成")
  300. @synchronized (self) {
  301. if(_databaseArr && _databaseArr.count == 3 ){
  302. NSMutableArray *doneArr = _databaseArr[1];
  303. if(model){
  304. [doneArr insertObject:model atIndex:0];
  305. }
  306. }
  307. }
  308. }
  309. #pragma mark 添加任务到上传失败
  310. - (void)handleDatabaseArrAddObjectInFail:(uploadFileDataModel*)model
  311. {
  312. HLog(@"添加任务到上传失败")
  313. @synchronized(self){
  314. if(_databaseArr && _databaseArr.count == 3 ){
  315. NSMutableArray *failArr = _databaseArr[2];
  316. if(model){
  317. [failArr insertObject:model atIndex:0];
  318. }
  319. }
  320. }
  321. }
  322. #pragma mark 上传完成所有任务
  323. - (void)didUploadAllTaskDoneFun
  324. {
  325. //self->_isUploadIngType = NO;
  326. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileAllTaskDoneNotification object:nil];
  327. }
  328. #pragma mark 删除内存数据记录
  329. - (void)handleDatabaseArrByDelete:(uploadFileDataModel*)delModel
  330. {
  331. if(_databaseArr && _databaseArr.count == 3 ){
  332. NSMutableArray *uploadingArr = nil;
  333. if(delModel.curUploadStateType == uploadStateUploading
  334. ||delModel.curUploadStateType == uploadStateSuspend
  335. ||delModel.curUploadStateType == uploadStateWait){
  336. uploadingArr = _databaseArr[0];
  337. }
  338. else if(delModel.curUploadStateType == uploadStateDone)
  339. {
  340. uploadingArr = _databaseArr[1];
  341. }
  342. else{
  343. uploadingArr = _databaseArr[2];
  344. }
  345. [uploadingArr removeObject:delModel];
  346. }
  347. }
  348. #pragma mark 删除本地数据库记录
  349. - (void)deleteUploadFileRecordBy:(NSMutableArray *)delArr withDelCache:(BOOL)isDelCache complete:(custom_complete_B)complete
  350. {
  351. //
  352. BOOL isDelUploadingModel = NO;
  353. NSMutableArray *curDelArr = [NSMutableArray arrayWithArray:delArr];
  354. for (uploadFileDataModel *uploadFileDataMod in curDelArr) {
  355. //1.删除数据库
  356. NSMutableString* where = [[NSMutableString alloc] initWithString:@"where "];
  357. NSString *curStr = [NSString stringWithFormat:@"%@=%@ ",bg_sqlKey(@"bg_id"),bg_sqlValue(uploadFileDataMod.bg_id)];
  358. [where appendString:curStr];
  359. //isSuc = [uploadFileDataModel bg_delete:nasUpLoadFile_tableName where:where];
  360. [uploadFileDataModel bg_deleteAsync:nasUpLoadFile_tableName where:where complete:^(BOOL isSuccess) {
  361. if(isSuccess){
  362. }
  363. }];
  364. // 2.删除内存数据
  365. [self handleDatabaseArrByDelete:uploadFileDataMod];
  366. //3.删除缓存的本地数据
  367. if(isDelCache){
  368. //判断是否可以删除本地缓存
  369. if(_databaseArr && _databaseArr.count == 3 ){
  370. NSMutableArray *uploadingArr = _databaseArr[0];
  371. NSMutableArray *uploadDoneArr = _databaseArr[1];
  372. NSMutableArray *uploadFailArr = _databaseArr[2];
  373. BOOL isNeedDel = YES;
  374. for (uploadFileDataModel *baseUploadFileDataMod in uploadingArr) {
  375. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  376. isNeedDel = NO;
  377. break;
  378. }
  379. }
  380. if(isNeedDel){
  381. for (uploadFileDataModel *baseUploadFileDataMod in uploadDoneArr) {
  382. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  383. isNeedDel = NO;
  384. break;
  385. }
  386. }
  387. }
  388. if(isNeedDel){
  389. for (uploadFileDataModel *baseUploadFileDataMod in uploadFailArr) {
  390. if([uploadFileDataMod.filename isEqualToString:baseUploadFileDataMod.filename] ){
  391. isNeedDel = NO;
  392. break;
  393. }
  394. }
  395. }
  396. if(isNeedDel){
  397. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  398. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  399. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  400. }
  401. else{
  402. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  403. }
  404. }
  405. }
  406. }
  407. if(!isDelCache){//上传报云机已经存在文件了 查询本地是否有任务记录
  408. [self handleRetryUploadAndDelRecordFun:uploadFileDataMod];
  409. }
  410. //是否在删除上传中的任务
  411. if(uploadFileDataMod.curUploadStateType == uploadStateUploading){
  412. [[nasMixUploadManager shareManager] cancelUploadTaskFunWith:uploadFileDataMod];
  413. }
  414. }
  415. uploadFileDataModel *RefreshUploadFileDataMod = nil;
  416. if(delArr.count >= 1){
  417. RefreshUploadFileDataMod = delArr.firstObject;
  418. }
  419. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:RefreshUploadFileDataMod];
  420. complete(YES);
  421. }
  422. #pragma mark 处理重复上传文件的问题
  423. - (void)handleRetryUploadAndDelRecordFun:(uploadFileDataModel *)uploadFileDataMod
  424. {
  425. @synchronized (self) {
  426. if (!_databaseArr || _databaseArr.count != 3) {
  427. return;
  428. }
  429. NSMutableArray * doneArr = _databaseArr[1];
  430. NSMutableArray * failArr = _databaseArr[2];
  431. BOOL isNeedDelType = YES;
  432. for (uploadFileDataModel *doneModel in doneArr) {
  433. if([doneModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  434. isNeedDelType = NO;
  435. break;
  436. }
  437. }
  438. if(isNeedDelType){
  439. for (uploadFileDataModel *failModel in failArr) {
  440. if([failModel.localIdentifier isEqualToString:uploadFileDataMod.localIdentifier]){
  441. isNeedDelType = NO;
  442. break;
  443. }
  444. }
  445. }
  446. if(isNeedDelType){
  447. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  448. [cachesFileManager removeItemAtPath:uploadFileDataMod.videoFirstImageName type:uploadFileTypeImage error:nil];
  449. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  450. }
  451. else{
  452. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeImage error:nil];
  453. }
  454. }
  455. else{//视频文件是要删除的
  456. if(uploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  457. [cachesFileManager removeItemAtPath:uploadFileDataMod.filename type:uploadFileTypeVideo error:nil];
  458. }
  459. }
  460. }
  461. }
  462. - (void)checkHadUploadTaskWithComplete:(custom_complete_B)complete
  463. {
  464. [self getDataInDatabaseFun:NO complete:^(NSMutableArray * _Nonnull Arr) {
  465. mainBlock(^{
  466. if(!Arr || Arr.count != 3){
  467. complete(NO);
  468. }
  469. else{
  470. NSArray *firstArr = Arr[0];
  471. if(firstArr.count >0){
  472. complete(YES);
  473. }
  474. else{
  475. complete(NO);
  476. }
  477. }
  478. });
  479. }];
  480. }
  481. //文件重新上传
  482. - (void)reUploadFileFunBy:(NSMutableArray*)Arr withAll:(BOOL)isAllType
  483. {
  484. if(!Arr || Arr.count == 0){
  485. return;
  486. }
  487. //1.修改状态从失败改为上传中
  488. for (uploadFileDataModel * model in Arr) {
  489. model.curUploadStateType = uploadStateUploading;
  490. //修改已经读取的内存数据
  491. NSMutableArray * failArr = _databaseArr[2];
  492. for (uploadFileDataModel * failModel in failArr) {
  493. if(model.bg_id.integerValue == failModel.bg_id.integerValue){
  494. [failArr removeObject:failModel];
  495. break;
  496. }
  497. }
  498. }
  499. //2、保存到数控库
  500. [uploadFileDataModel bg_saveOrUpdateArrayAsync:Arr complete:^(BOOL isSuccess) {
  501. }];
  502. //重新开始上传
  503. [[nasMixUploadManager shareManager] addUploadWithModels:Arr];
  504. }
  505. //暂停上传 当 isSuspendAll为NO时候 传指定暂停哪一个
  506. - (void)suspendUploadFileFun:(BOOL)isSuspendAll withModel:(uploadFileDataModel*)model
  507. {
  508. //[[nasUploadManager shareInstance] cancelUploadTaskFun];
  509. if (isSuspendAll) {
  510. [[nasMixUploadManager shareManager] cancelUploadAllTaskFun];
  511. }
  512. else{
  513. [[nasMixUploadManager shareManager] cancelUploadTaskFunWith:model];
  514. }
  515. [self suspendUploadingTaskBy:isSuspendAll withModel:model];
  516. }
  517. #pragma mark 暂停任务
  518. - (void)suspendUploadingTaskBy:(BOOL)isDeleteAll withModel:(uploadFileDataModel*)model
  519. {
  520. NSMutableArray *uploadingArr = [NSMutableArray new];
  521. if(!isDeleteAll){
  522. if(model){
  523. model.curUploadStateType = uploadStateSuspend;
  524. [uploadingArr addObject:model];
  525. }
  526. }
  527. else
  528. {
  529. if (!_databaseArr || _databaseArr.count != 3) {
  530. return;
  531. }
  532. uploadingArr = _databaseArr[0];
  533. for (uploadFileDataModel*preModel in uploadingArr) {
  534. preModel.curUploadStateType = uploadStateSuspend;
  535. }
  536. }
  537. if(uploadingArr.count > 0){
  538. //更新数据库
  539. [uploadFileDataModel bg_saveOrUpdateArrayAsync:uploadingArr complete:^(BOOL isSuccess) {
  540. }];
  541. }
  542. }
  543. - (NSMutableArray*)uploadingArr
  544. {
  545. if(_databaseArr && _databaseArr.count == 3){
  546. NSMutableArray *uploadingArr = _databaseArr[0];
  547. return uploadingArr;
  548. }
  549. return [NSMutableArray new];
  550. }
  551. //网络失败后任务暂时保存
  552. - (void)saveUploadingTaskByNetWorkErrorFun
  553. {
  554. HLog(@"hxd saveUploadingTaskByNetWorkErrorFun")
  555. if(self.uploadingArr.count > 0){
  556. [[nasMixUploadManager shareManager] cancelUploadAllTaskFun];
  557. _needToReUploadTaskType = YES;
  558. }
  559. }
  560. //重新上传网络失败的任务
  561. - (void)reUploadFileFunByNetWork
  562. {
  563. [nasUploadFileManager shareInstance].needToReUploadTaskType = NO;
  564. HLog(@"reUploadFileFunByNetWork")
  565. [cachesFileManager writeLogsWithMsg:@"reUploadFileFunByNetWork"];
  566. if(self.uploadingArr.count >0 && _databaseArr.count == 3){
  567. NSMutableArray* doneArr = _databaseArr[1];
  568. NSMutableArray* failArr = _databaseArr[2];
  569. NSString *logstr = [[NSString alloc] initWithFormat:@"ing:%ld,done:%ld,fail:%ld",self.uploadingArr.count,doneArr.count,failArr.count];
  570. [cachesFileManager writeLogsWithMsg:logstr];
  571. //1.修改状态从失败改为上传中
  572. // for (uploadFileDataModel * model in _netWorkFailSaveArr) {
  573. // model.curUploadStateType = uploadStateUploading;
  574. //// for (uploadFileDataModel * uploadingModel in self.uploadingArr){
  575. //// if (model.bg_id.integerValue == uploadingModel.bg_id.integerValue) {
  576. //// uploadingModel.curUploadStateType = uploadStateUploading;
  577. //// continue;
  578. //// }
  579. //// }
  580. // }
  581. //2、保存到数控库
  582. // [uploadFileDataModel bg_saveOrUpdateArrayAsync:_netWorkFailSaveArr complete:^(BOOL isSuccess) {
  583. //
  584. // }];
  585. //3.判断是否可以上传
  586. BOOL isCanUseCellular = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_canUse_Cellular_all)];
  587. if(!isCanUseCellular){//不允许流量上传
  588. //
  589. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusReachableViaWWAN){
  590. return;
  591. }
  592. }
  593. //4.重新开始上传
  594. [[nasMixUploadManager shareManager] addUploadWithModels:self.uploadingArr];
  595. }
  596. }
  597. @end