import { getToken, getTokenFromApi, getCurrentUserNameSync, getCurrentUserSync } from './auth'
import { saveTaskInfo, saveTaskPhoto, saveTaskKeyProcess, saveTaskRecord, saveTaskRecordItem, getLatestTask, removeTaskAndRecord, getList, updateData, addOperateLog } from '@/api/work'
import { globalConfig } from '@/config'


// 类型定义保持不变
export type ApiResponse = {
	code : number;
	msg : string;
	data : AppTaskInfo;
}

export type AppTaskInfo = {
	gxpk : string;
	pk_serial : string;
	cardno ?: string;
	productcode ?: string;
	model ?: string;
	workorder ?: string;
	invname ?: string;
	graphid ?: string;
	processno ?: string;
	ver ?: string;
	lastupdatetime ?: string;
	qcrecord ?: AppTaskRecord;
	photolist ?: AppTaskPhoto[];
	keyprocesslist ?: AppTaskKeyProcess[];
	username ?: string;
	user ?: string;
	gxno ?: string;
}

export type AppTaskRecord = {
	pk ?: string;
	fk_invcode ?: string;
	no ?: string;
	invcode ?: string;
	invname : string;
	processStep ?: string;
	fk_processTask ?: string;
	checkTarget ?: string;
	checknum ?: number;
	oknum ?: number;
	ngnum ?: number;
	status ?: string;
	result ?: string;
	checkTime ?: string;
	cs ?: string;
	ts ?: string;
	items ?: AppTaskRecordItem[];
}

export type AppTaskRecordItem = {
	pk ?: string;
	fk_qcRecord ?: string;
	fk_prodcode ?: string;
	prodno ?: string;
	name ?: string;
	no ?: string;
	nature ?: string;
	unit ?: string;
	maxNum ?: number;
	minNum ?: number;
	status ?: string;
	memo ?: string;
	measuredvalue ?: string;
	result ?: string;
	cs ?: string;
	ts ?: string;
	recorder ?: string;
	measuredvaluemax ?: string;
}

export type AppTaskPhoto = {
	pk : string;
	photographpoint ?: string;
	photographdescription ?: string;
	photourl ?: string;
	photoname ?: string;
	fk_qcRecord ?: string;
	fk_prodcode ?: string;
	prodno ?: string;
	fk_creator ?: string;
	fks_operator ?: string;
	operator ?: string;
	processStep ?: string;
	fk_processTask ?: string;
	cs ?: string;
	ts ?: string;
}

export type AppTaskKeyProcess = {
	pk ?: string;
	pdid ?: string;
	testapparatus ?: string;
	tableid ?: string;
	testrequirelower ?: string;
	testrequireupper ?: string;
	parametername ?: string;
	parameterorder ?: string;
	measureunit ?: string;
	parameterinstruction ?: string;
	parameterid ?: string;
	fk_creator ?: string;
	fk_prodcode ?: string;
	prodno ?: string;
	processstep ?: string;
	fk_processtask ?: string;
	measuredvaluemin ?: string;
	measuredvaluemax ?: string;
	fks_operator ?: string;
	cs ?: string;
	ts ?: string;
	operator ?: string;
	result ?: string;
	memo ?: string;
}

export type UploadImg = {
	sxid : string,
	pk : string,
	path : string
}

export type Task = {
	pdid : number,
	gxpk : string,
	cardno : string,
	productcode : string,
	model : string,
	workorder : string,
	invname : string,
	graphid : string,
	processno : string,
	gxno : string,
	ver : string,
	lastupdatetime : string
}

export const showProgress = (index : number, title : string) => {
	// 在Android设备上，需要给hideLoading和showLoading之间添加延迟
	// 先隐藏之前的加载提示
	uni.hideLoading();
	// 添加50ms的延迟，确保hideLoading完全执行后再显示新的进度
	setTimeout(() => {
		uni.showLoading({
			title: `正在${title}第${index}个任务数据`,
			mask: true
		});
	}, 50);
}

export const moveFile = (oldPath : string) : Promise<string> => {
	return new Promise((resolve, reject) => {
		// 生成唯一的文件名
		const timestamp = Date.now();
		const randomNum = Math.floor(Math.random() * 1000);
		const fileExtension = oldPath.substring(oldPath.lastIndexOf('.'));
		const newImgName = `download_${timestamp}_${randomNum}${fileExtension}`;

		// 定义新的保存路径 - 不放在相册里，放在应用数据目录下的downloadImgs文件夹
		const destPath = `${uni.env.USER_DATA_PATH}/downloadImgs/${newImgName}`;

		try {
			// 确保目标目录存在
			const fs = uni.getFileSystemManager();
			try {
				fs.accessSync(`${uni.env.USER_DATA_PATH}/downloadImgs`);
			} catch (e) {
				// 目录不存在，创建目录
				fs.mkdirSync(`${uni.env.USER_DATA_PATH}/downloadImgs`, true);
			}

			// 先拷贝文件到新路径
			fs.copyFile({
				srcPath: oldPath,
				destPath: destPath,
				success: function () {
					// 删除原文件，释放空间
					fs.unlink({
						filePath: oldPath,
						success: function () {
							console.log('原文件已删除');
						},
						fail: function (unlinkErr) {
							console.error('删除原文件失败', unlinkErr);
						}
					});
					console.log('文件移动成功，新路径：', destPath);
					resolve(destPath);
				},
				fail: function (copyErr) {
					console.error('拷贝文件失败', copyErr);
					reject(copyErr);
				}
			});
		} catch (error) {
			console.error('文件移动过程发生错误', error);
			reject(error);
		}
	});
}

//检验任务下载
export const downloadDataFromAPI = async (gxpk : string, callback ?: () => void) : Promise<boolean> => {
	try {
		uni.showLoading({ title: '任务开始下载' });
		const apiToken = await getTokenFromApi();

		if (apiToken == null || apiToken == '') {
			uni.hideLoading();
			uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
			return false;
		}

		//校验是否已经存在未执行的产品号，若已经存在则提示用户该产品号是否需要被覆盖，
		//如果没有则直接保存。如果有存在已经在执行中的产品号，则提示已存在执行中的任务
		const infoJson = await getLatestTask(gxpk, null);
		if (infoJson?.['data'] != null) {
			let info = infoJson?.['data'] as UTSJSONObject ?? {} as UTSJSONObject
			let photoCount = parseInt(info?.['photoCount'] as string);
			let keyCount = parseInt(info?.['keyCount'] as string);
			let recordCount = parseInt(info?.['recordCount'] as string);
			
			//覆盖标识位
			let overwiteFlag = ref(false);
			// 先检查是否有任务正在执行中
			if (info != null && ( photoCount + keyCount + recordCount)> 0) {
				uni.showToast({ title: `当前工序码已有任务在执行中！`, icon: 'error' });
				return false;
			}
			// 使用Promise来处理异步流程
			let deleteDataPromise = new Promise<boolean>((resolve) => {
				// if (info != null && ingNum == 0) {
				if (info != null) {
					//可以被覆盖，需要有提示框，给用户确认
					uni.showModal({
						title: '系统提示',
						content: '该工序码已存在任务是否覆盖掉？',
						cancelText: '取消',
						confirmText: '确定',
						success: function (res) {
							if (res.confirm) {
								// 标记为需要覆盖
								overwiteFlag.value = true;
								// 执行删除数据操作
								removeTaskAndRecord(gxpk).then((recordDelResponse) => {
									console.log('删除数据响应:', recordDelResponse);
									// 删除成功，解析Promise并允许继续执行
									// 确保模态框已完全关闭后再解析Promise
									setTimeout(() => {
										resolve(true);
									}, 300);
								}).catch((error) => {
									console.error('删除数据失败:', error);
									uni.showToast({ title: '删除旧数据失败', icon: 'error' });
									resolve(false);
								});
							} else {
								// 用户取消覆盖
								uni.hideLoading();
								overwiteFlag.value = false;
								resolve(false);
							}
						}
					});
				} else {
					// 不需要显示确认框，直接解析Promise
					resolve(true);
				}
			});

			// 等待删除数据操作完成
			const canContinue : boolean = await deleteDataPromise;
			if (!canContinue) {
				// 如果不能继续（删除失败或用户取消），则终止函数执行
				return false;
			}
		}

		// 直接使用async/await处理HTTP请求，避免嵌套Promise
		const requestResult = await new Promise<UTSJSONObject>((resolve, reject) => {
			console.log(`${globalConfig.host}${globalConfig.downloadTaskURL}${gxpk}`)
			uni.request({
				url: `${globalConfig.host}${globalConfig.downloadTaskURL}${gxpk}`,
				method: 'GET',
				header: {
					'token': apiToken
				},
				success: (res) => resolve(res?.['data'] as UTSJSONObject ?? {} as UTSJSONObject),
				fail: (err) => reject(err)
			});
		});

		// 处理请求结果
		if (requestResult != null && requestResult.code == 666) {
			let taskInfo = requestResult?.['data'] as UTSJSONObject ?? {} as UTSJSONObject;
			if (taskInfo != null) {
				let data = JSON.parse<AppTaskInfo>(taskInfo.toJSONString());
				if (data != null) {
					// 保存任务信息
					const resSave = await saveTaskInfo(taskInfo);
					const lastIdStr = resSave?.['lastId'] as string | null;
					const lastId = lastIdStr != null ? parseInt(lastIdStr) : null;

					if (lastId != null) {
						let photoList = taskInfo?.['photolist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
						if (photoList != null && photoList.length > 0) {
							const totalRecords = photoList.length;

							// 按顺序处理所有图片记录
							for (let i = 0; i < photoList.length; i++) {
								// 更新进度
								showProgress(i + 1, '下载');

								let photoObj = photoList[i] as UTSJSONObject;
								// 获取各个字段的值
								const pk = photoObj['pk'] as string;
								const photographpoint = photoObj['photographpoint'] as string | null;
								const photographdescription = photoObj['photographdescription'] as string | null;
								const photourl = photoObj['photourl'] as string | null;
								const photoname = photoObj['photoname'] as string | null;
								const fk_qcRecord = photoObj['fk_qcRecord'] as string | null;
								const fk_prodcode = photoObj['fk_prodcode'] as string | null;
								const prodno = photoObj['prodno'] as string | null;
								const fk_creator = photoObj['fk_creator'] as string | null;
								const fks_operator = photoObj['fks_operator'] as string | null;
								const operator = photoObj['operator'] as string | null;
								const processStep = photoObj['processStep'] as string | null;
								const fk_processTask = photoObj['fk_processTask'] as string | null;
								const cs = photoObj['cs'] as string | null;
								const ts = photoObj['ts'] as string | null;

								// 获取图片
								let imagePathsArr = [] as string[];
								try {
									// 串行执行，等待前一张图片处理完成再处理下一张
									const tempFilePath = await downloadAndSaveImage(pk, apiToken);
									if (tempFilePath != null && tempFilePath != '') {
										imagePathsArr.push(tempFilePath);
									}
								} catch (error) {
									console.error(`处理图片时出错:`, error);
									// 出错后继续处理下一张图片
								}

								// 拼接图片ID和路径
								const imagePaths = imagePathsArr.join(",");
								// 使用三目运算符判断，当值为null时直接插入null，否则用单引号括起来
								var values = `${lastId === null ? 0 : lastId},${pk === null ? '' : `'${pk}'`}, ${photographpoint === null ? 'null' : `'${photographpoint}'`}, ${photographdescription === null ? 'null' : `'${photographdescription}'`},${photourl === null ? 'null' : `'${photourl}'`},
								${imagePaths === null ? 'null' : `'${imagePaths}'`},${photoname === null ? 'null' : `'${photoname}'`},${fk_qcRecord === null ? 'null' : `'${fk_qcRecord}'`},${fk_prodcode === null ? 'null' : `'${fk_prodcode}'`}, ${prodno === null ? 'null' : `'${prodno}'`},
								${fk_creator === null ? 'null' : `'${fk_creator}'`},${fks_operator === null ? 'null' : `'${fks_operator}'`}, ${operator === null ? 'null' : `'${operator}'`}, ${processStep === null ? 'null' : `'${processStep}'`}, ${fk_processTask === null ? 'null' : `'${fk_processTask}'`}, ${cs === null ? 'null' : `'${cs}'`}, ${ts === null ? 'null' : `'${ts}'`}`;

								// 立即保存当前图片的任务信息
								saveTaskPhoto(values);
							}
						}
						//保存关键工序
						let keyProcessList = taskInfo?.['keyprocesslist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
						if (keyProcessList != null && keyProcessList.length > 0) {
							keyProcessList.forEach(item => {
								item['pdid'] = lastId;
								console.log(item);
								saveTaskKeyProcess(item);
							})
						}
						
						//保存检验记录
						let recordObj = taskInfo?.['qcrecord'] as (UTSJSONObject | null);
						// console.log('------------------------------>');
						// console.log(recordObj);
						if (recordObj != null) {
							recordObj['pdid'] = lastId;
							let recordItemList = recordObj?.['items'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
							if (recordItemList != null && recordItemList.length > 0) {
								const resSave = await saveTaskRecord(recordObj);
								const recordIdStr = resSave?.['lastId'] as string | null;
								const recordId = recordIdStr != null ? parseInt(recordIdStr) : null;
								if (recordId != null) {
									recordItemList.forEach(item => {
										item['psxid'] = recordId;
										console.log(item);
										saveTaskRecordItem(item);
									})
								}
							}
						}
					} else {
						console.log('保存信息成功，但未获取到主键ID');
					}
				}
			}

			// 所有记录处理完成，显示完成提示
			uni.hideLoading();
			uni.showToast({ title: `下载完成`, icon: 'success' });
			addOperateLog({ module: '检验任务', title: '下载检验任务', content: `下载检验任务，工序码：${gxpk}`, msg: null, createuser: getCurrentUserSync()??'' })
			
			if (callback != null) {
				callback();
			}
			return true;
		} else {
			const errorMsg = requestResult?.msg != null ? requestResult.msg : '未知错误';
			uni.hideLoading();
			uni.showToast({ title: `请求失败: ${errorMsg}`, icon: 'error' });
			addOperateLog({ module: '检验任务', title: '下载检验任务失败', content: `下载检验任务失败，工序码：${gxpk}`, msg: `错误信息Err:${errorMsg}`, createuser: getCurrentUserSync()??'' })
			return false;
		}
	} catch (error) {
		console.error('下载数据时发生错误:', error);
		uni.hideLoading();
		addOperateLog({ module: '检验任务', title: '下载检验任务失败', content: `下载检验任务失败，工序码：${gxpk}`, msg: `错误信息Err:${error}`, createuser: getCurrentUserSync()??'' })
		
		uni.showToast({ title: '下载失败，请重试', icon: 'error' });
		return false;
	}
}

// 辅助函数：下载并保存图片
const downloadAndSaveImage = async (pk : string, apiToken : string) : Promise<string> => {
	return new Promise<string>((resolve, reject) => {
		// 使用uni.downloadFile下载图片
		uni.downloadFile({
			url: `${globalConfig.getImgURL}${pk}`,
			header: {
				'token': apiToken
			},
			success: (res) => {
				if (res.statusCode === 200) {
					// 等待一小段时间确保文件完全下载
					setTimeout(() => {
						moveFile(res.tempFilePath).then((newFilePath) => {
							console.log('图片已移动并添加到数组：', newFilePath);

							// 处理完成后等待1秒再处理下一张
							setTimeout(() => {
								resolve(newFilePath);
							}, 500);
						}).catch((error) => {
							console.error('文件移动失败，使用原始路径：', error);
							// 如果移动失败，使用原始路径作为备选
							// 处理完成后等待1秒再处理下一张
							setTimeout(() => {
								resolve(res.tempFilePath);
							}, 1000);
						});
					}, 500); // 等待500ms确保文件完全下载
				} else {
					console.error('下载图片失败，状态码:', res.statusCode);
					reject(new Error(`下载图片失败，状态码: ${res.statusCode}`));
				}
			},
			fail: (err) => {
				console.error('请求图片失败:', err);
				reject(err);
			}
		});
	});
}


//检验任务上传
export const uploadDataToAPI = async (gxpk : string, callback ?: () => void) : Promise<boolean> => {

	try {
		//暂定需要上传的数据文件
		const apiToken = await getTokenFromApi();
		if (apiToken == null || apiToken == '') {
			uni.hideLoading();
			uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
			return false
		}

		// 获取数据
		const infoJson = await getLatestTask(gxpk, null);
		// console.log(infoJson)
		if (infoJson != null) {
			let taskInfoJson = infoJson?.['data'] as UTSJSONObject ?? {};

			if (taskInfoJson == null) {
				uni.hideLoading();
				uni.showToast({ title: '未获取到数据', icon: 'error' });
				return false;
			}
			console.log('--------------------->')
			console.log(taskInfoJson)
			uni.showLoading({ title: `开始上传数据`});
			let taskInfo = JSON.parse<Task>(taskInfoJson.toJSONString());
			let canContinueFlag = true;

			// 1. 收集所有需要上传的图片
			let res = await getList('app_task_photo', 'pdid', taskInfo?.pdid.toString(), 'uploadFlag', '0', null)
			let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
			if (dataList != null && dataList.length > 0) {
				//处理图片对象数组
				let allImagesToUpload : UploadImg[] = [];
				let processStep = 1;

				for (let index = 0; index < dataList.length; index++) {
					const record = dataList[index];

					if (record.getString('photourl') == '' || record.getString('photourl') == null
						|| record.getString('pk') == '' || record.getString('pk') == null) {
						continue
					}

					showProgress(processStep, '准备上传');
					//收集图片信息
					let photourlStr = record.getString('photourl');
					let sxid = record.getString('sxid');
					let pk = record.getString('pk');
					let urlArr = photourlStr?.split(",") ?? [];

					for (let j = 0; j < urlArr.length; j++) {
						let path = urlArr[j];
						const fullFilePath = `${uni.env.USER_DATA_PATH}` + path;
						allImagesToUpload.push({ sxid: sxid ?? '', pk: pk ?? '', path: fullFilePath });
					}
					processStep++;
				}

				// 2. 统计总图片数量
				const totalImages = allImagesToUpload.length;
				console.log(`总共需要上传${totalImages}张图片`);

				if (totalImages === 0) {
					uni.hideLoading();
					uni.showToast({ title: '没有需要上传的图片', icon: 'none' });
					return true;
				}

				// 3. 执行第一次上传
				let failedImages : UploadImg[] = [];
				let successCount = 0;

				uni.showLoading({ title: `正在上传图片 (0/${totalImages})` });

				for (let i = 0; i < allImagesToUpload.length; i++) {
					const { sxid, pk, path } = allImagesToUpload[i];
					console.log(`开始上传文件: ${path}, 索引: ${i}, apiToken: ${apiToken}, billid: ${pk}, sxid: ${sxid}`);

					try {
						// 串行执行，等待前一个图片上传完成再处理下一张
						await new Promise<void>((resolve, reject) => {
							// 使用uni.uploadFile进行文件上传
							console.log(`上传路径：${globalConfig.uploadURL}`)
							const uploadTask = uni.uploadFile({
								url: `${globalConfig.uploadURL}`,
								filePath: path,
								name: 'file', // 文件参数名
								header: {
									'token': apiToken,
									'content-type': 'multipart/form-data'
								},
								formData: {
									'billid': pk
								},
								success: (uploadRes) => {
									if (uploadRes.statusCode === 200) {
										console.log(`文件${path}上传成功`, uploadRes);
										successCount++;
										//更新数据库记录
										let updatedData = " uploadFlag = 1 "
										updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
											console.log(`更新图片记录的上传标识 ${sxid}`)
										});
										// 等待一小段时间确保文件完全上传并处理完成
										setTimeout(() => {
											resolve();
										}, 1000);
									} else {
										console.error(`文件${path}上传失败，状态码:`, uploadRes.statusCode);
										let updatedData = " uploadFlag = 0 "
										updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
											console.log(`上传失败更新图片记录的上传标识 ${sxid}`)
										});
										uni.showModal({
											content: `图片上传失败，状态码：${uploadRes.statusCode}`,
											title: '图片上传失败'
										})
										setTimeout(() => {
											reject(new Error(`上传失败，状态码: ${uploadRes.statusCode}`));
										}, 500);
									}
								},
								fail: (err) => {
									console.error(`文件${path}上传失败`, err);
									// 上传失败也继续处理下一张，但记录错误
									let updatedData = " uploadFlag = 0 "
									updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
										console.log(`上传错误更新图片记录的上传标识 ${sxid}`)
									});
									uni.showModal({
										content: `文件上传失败：${err}`,
										title: '图片上传失败'
									})
									setTimeout(() => {
										reject(err);
									}, 500);
								},
								complete: () => {
									// console.log(`文件${path}上传操作完成`);
									// 更新进度
									uni.hideLoading();
									uni.showLoading({ title: `正在上传图片 (${i + 1}/${totalImages})` });
								}
							});
							//调试时开启
							// uploadTask.onProgressUpdate((res) => {
							// 	console.log('上传进度' + res.progress);
							// 	console.log('已经上传的数据长度' + res.totalBytesSent);
							// 	console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
							// });
						});
					} catch (error) {
						// 捕获上传失败的错误，将失败的图片信息添加到错误数组
						console.log(`处理第${i + 1}张图片时出错:`, error);
						failedImages.push({ sxid, pk, path });
						// 出错后继续处理下一张图片
						uni.showModal({
							content: `文件上传失败：${error}`,
							title: '图片上传失败'
						})
					}

					// 在两次上传之间增加一个短暂的延迟，避免请求过于频繁
					if (i < allImagesToUpload.length - 1) {
						await new Promise<void>((resolve) => {
							setTimeout(() => {
								resolve()
							}, 1000)
						})
					}
				}

				// 4. 执行重试逻辑（最多3次）
				const maxRetries = 3;
				let retryImages = [...failedImages]; // 复制初始失败的图片数组

				for (let retryCount = 1; retryCount <= maxRetries; retryCount++) {
					if (retryImages.length === 0) {
						// 如果没有需要重试的图片，提前结束循环
						break;
					}

					console.log(`开始第${retryCount}次重试上传失败的图片，共${retryImages.length}张`);
					uni.hideLoading();
					uni.showLoading({ title: `第${retryCount}次重试 (0/${retryImages.length})` });

					// 创建新的错误数组，用于收集本次重试失败的图片
					let currentFailedImages : UploadImg[] = [];
					let currentSuccessCount = 0;

					// 串行上传失败的图片
					for (let i = 0; i < retryImages.length; i++) {
						const { sxid, pk, path } = retryImages[i];
						console.log(`重试上传文件: ${path}, 索引: ${i}, 重试次数: ${retryCount}, apiToken: ${apiToken}, billid: ${pk}, sxid: ${sxid}`);
						console.log(`重试上传路径：${globalConfig.uploadURL}`)

						await new Promise<void>((resolve) => {
							uni.uploadFile({
								url: `${globalConfig.uploadURL}`,
								filePath: path,
								name: 'file',
								header: {
									'token': apiToken
								},
								formData: {
									'billid': pk
								},
								success: (uploadRes) => {
									if (uploadRes.statusCode === 200) {
										currentSuccessCount++;
										//更新数据库
										let updatedData = " uploadFlag = 1 "
										updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
											console.log(`更新图片记录的上传标识 ${sxid}`)
										});
										console.log(`重试上传完成，当前成功: ${currentSuccessCount}, 当前失败: ${currentFailedImages.length}`);
										resolve();
									} else {
										currentFailedImages.push({ sxid, pk, path });
									}
									
								},
								fail: (err) => {
									console.error(`重试文件${path}上传失败`, err);
									currentFailedImages.push({ sxid, pk, path });
									resolve();
								},
								complete: () => {
									// console.log(`重试文件${path}上传操作完成`);
									// 更新进度
									uni.hideLoading();
									uni.showLoading({ title: `第${retryCount}次重试 (${i + 1}/${retryImages.length})` });
								}
							});
						});

						// 在两次上传之间增加一个短暂的延迟，避免请求过于频繁
						if (i < retryImages.length - 1) {
							await new Promise<void>((resolve) => {
								setTimeout(() => {
									resolve()
								}, 1000)
							})
						}
					}

					// 更新成功数量
					successCount += currentSuccessCount;
					// 更新下一次重试的图片列表为本次失败的图片
					retryImages = currentFailedImages;
				}
				// 5. 显示总结信息
				const finalFailedCount = retryImages.length;
				console.log(`上传总结: 总共${totalImages}张图片, 成功${successCount}张, 失败${finalFailedCount}张`);

				// 三次重试后如果仍有失败的图片，显示提示
				if (finalFailedCount > 0) {
					uni.hideLoading();
					uni.showModal({
						title: '上传提示',
						content: `总共需要上传${totalImages}张图片，成功${successCount}张，失败${finalFailedCount}张。\n经过${maxRetries}次重试后，仍有${finalFailedCount}张图片上传失败，请检查网络后重新上传。`,
						showCancel: false
					});
				}

				if (finalFailedCount > 0) {
					canContinueFlag = false
				}
			}

			//执行照片上传以外的逻辑
			if (!canContinueFlag) {
				uni.showToast({
					title: `图片上传失败，请重新上传`,
					icon: 'error'
				});
			}

			taskInfoJson.set('photolist', Array<UTSJSONObject>())
			taskInfoJson.set('lastupdatetime', null)
			taskInfoJson.set('user', getCurrentUserNameSync())
			taskInfoJson.set('username', getCurrentUserSync())
			


			//1.填充关键工序数据
			let keyRes = await getList('app_task_keyprocess', 'pdid', taskInfo?.pdid.toString(), null, null, null)
			let keyList = keyRes?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
			if (keyList != null && keyList.length > 0) {
				let keyObjectList = [] as AppTaskKeyProcess[]
				keyList.forEach(item => {
					let keyObject = JSON.parse<AppTaskKeyProcess>(item.toJSONString());
					if (keyObject != null) {
						keyObjectList.push(keyObject)
					}
				})
				taskInfoJson.set('keyprocesslist', keyObjectList)
			}

			//2.填充检验任务数据
			let qcRecoerdRes = await getList('app_task_record', 'pdid', taskInfo?.pdid.toString(), null, null, null)
			let qcRecordList = qcRecoerdRes?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
			if (qcRecordList != null && qcRecordList.length > 0) {
				let qcRecordJson = qcRecordList[0]
				let sxid = qcRecordJson.getString("sxid")

				// 3.填充检验任务item 数据据
				let qcRecoerdItemRes = await getList('app_task_record_item', 'psxid', sxid, null, null, null)
				let qcRecordItemList = qcRecoerdItemRes?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
				if (qcRecordItemList != null && qcRecordItemList.length > 0) {
					let itemList = [] as AppTaskRecordItem[]
					qcRecordItemList.forEach(item => {
						let itemObj = JSON.parse<AppTaskRecordItem>(item.toJSONString());
						if (itemObj != null) {
							itemList.push(itemObj)
						}
					})
					qcRecordJson.set('items', itemList)
				}
				let qcRecord = JSON.parse<AppTaskRecord>(qcRecordJson.toJSONString());
				taskInfoJson.set("qcrecord", qcRecord)
			}

			let requestTask = JSON.parse<AppTaskInfo>(taskInfoJson.toJSONString());
			console.log("请求体:", requestTask)

			const requestResult = await new Promise<UTSJSONObject>((resolve, reject) => {
				// Ensure we properly stringify the JSON object and set the correct content-type
				uni.request({
					url: `${globalConfig.uploadTaskURL}`,
					method: 'POST',
					header: {
						'token': apiToken,
						'content-type': 'application/json'
					},
					data: JSON.stringify(requestTask), // Convert object to JSON string
					success: (res) => resolve(res?.['data'] as UTSJSONObject ?? {} as UTSJSONObject),
					fail: (err) => reject(err)
				});
			});

		
			// 检查返回的code值
			const code = requestResult?.['code'] as number;
			if (code === 666) {
				let updatedData = " uploadFlag = 1, uploadtime = strftime('%Y-%m-%d %H:%M:%S', 'now', 'localtime') "
				updateData('app_task_info', updatedData, 'pdid', taskInfo?.pdid.toString()).then((res : UTSJSONObject) => {
					console.log(`更新检验任务记录的上传标识 ${taskInfo?.pdid.toString()}`)
				});
				addOperateLog({ module: '检验任务', title: '上传检验任务', content: `上传检验任务，工序码：${gxpk}`, msg: null, createuser: getCurrentUserSync()??'' })
				
				uni.showToast({
					title: `上传成功`,
					icon: 'success'
				});
			} else {
				const errorMsg = requestResult?.['msg'] as string ?? '未知错误';
				console.error(`上传失败，错误码: ${code}, 错误信息: ${errorMsg}`);
				uni.showToast({
					title: `上传失败: ${errorMsg}`,
					icon: 'error'
				});
				addOperateLog({ module: '检验任务', title: '上传检验任务失败', content: `上传检验任务失败，工序码：${gxpk}`, msg: `错误信息Err:${errorMsg}`, createuser: getCurrentUserSync()??'' })
			}
			
			if (callback != null) {
				callback();
			}
		}
		return true;
	} catch (error) {
		console.error(error);
		uni.showToast({ title: '上传失败，请重试', icon: 'error' });
		uni.hideLoading();
		addOperateLog({ module: '检验任务', title: '上传检验任务失败', content: `上传检验任务失败，工序码：${gxpk}`, msg: `错误信息Err:${error}`, createuser: getCurrentUserSync()??'' })
		
		uni.showModal({
			content: `文件上传失败：${error}`,
			title: '图片上传失败'
		})
		return false;
	}
}