| package com.example.firstapp.service | 
|   | 
| import android.app.NotificationChannel | 
| import android.app.NotificationManager | 
| import android.app.PendingIntent | 
| import android.content.Context | 
| import android.content.Intent | 
| import android.os.Build | 
| import androidx.core.app.NotificationCompat | 
| import androidx.work.* | 
| import androidx.work.CoroutineWorker | 
| import androidx.work.WorkerParameters | 
| import com.example.firstapp.MainActivity | 
| import com.example.firstapp.R | 
| import com.example.firstapp.activity.PhoneLoginActivity | 
| import com.example.firstapp.core.Core | 
| import com.example.firstapp.database.entity.ReminderRecord | 
| import com.example.firstapp.database.repository.ReminderRecordRepository | 
| import com.example.firstapp.database.repository.ReminderRepository | 
| import com.example.firstapp.model.StationGroup | 
| import com.example.firstapp.utils.PreferencesManager | 
| import kotlinx.coroutines.Dispatchers | 
| import kotlinx.coroutines.flow.first | 
| import kotlinx.coroutines.withContext | 
| import java.util.Calendar | 
| import java.util.concurrent.TimeUnit | 
|   | 
| /** | 
|  * 提醒工作类,定期检查是否有需要提醒的内容 | 
|  * 目前设置为每天检查一次 | 
|  */ | 
| class ReminderWorker( | 
|     appContext: Context, | 
|     workerParams: WorkerParameters | 
| ) : CoroutineWorker(appContext, workerParams) { | 
|   | 
|     private val reminderRepository = ReminderRepository(appContext) | 
|     private val reminderRecordRepository = ReminderRecordRepository(appContext) | 
|   | 
|     override suspend fun doWork(): Result = withContext(Dispatchers.IO) { | 
|         android.util.Log.d("ReminderWorker", "doWork 开始执行") | 
|         try { | 
|             // 强制发送一条测试通知(确认Worker是否执行) | 
|             // sendTestNotification() | 
|             android.util.Log.d("ReminderWorker", "发送测试通知确认Worker已执行") | 
|              | 
|             // 获取所有提醒设置 | 
|             val reminderList = reminderRepository.getAllReminders().first() | 
|             android.util.Log.d("ReminderWorker", "获取到 ${reminderList.size} 条提醒设置") | 
|              | 
|             if (reminderList.isNotEmpty()) { | 
|                 for (reminder in reminderList) { | 
|                     try { | 
|                         android.util.Log.d("ReminderWorker", "处理提醒: ${reminder.categoryName}") | 
|                         checkCategoryContent(reminder.categoryId, reminder.categoryName, reminder.notificationMethod) | 
|                     } catch (e: Exception) { | 
|                         e.printStackTrace() | 
|                         android.util.Log.e("ReminderWorker", "处理提醒失败: ${e.message}") | 
|                     } | 
|                 } | 
|             } | 
|              | 
|             android.util.Log.d("ReminderWorker", "doWork 执行成功") | 
|             Result.success() | 
|         } catch (e: Exception) { | 
|             e.printStackTrace() | 
|             android.util.Log.e("ReminderWorker", "doWork 执行失败: ${e.message}") | 
|             Result.failure() | 
|         } | 
|     } | 
|      | 
|     private suspend fun checkCategoryContent(categoryId: Int, categoryName: String, notificationMethod: String) { | 
|         // 根据categoryId从Code表中查询pickup=0的数据 | 
|         when (categoryId) { | 
|             1, 2, 3, 4, 5 -> { | 
|                 val type = getCategoryType(categoryId) | 
|                 val stationGroups = Core.code.getStationsByType(type) | 
|                  | 
|                 if (stationGroups.isNotEmpty()) { | 
|                     // 有未处理的数据,创建提醒 | 
|                     createRemindersForStations(categoryId, categoryName, stationGroups, notificationMethod) | 
|                 } | 
|             } | 
|             else -> { | 
|                 // 其他类型的提醒处理 | 
|             } | 
|         } | 
|     } | 
|      | 
|     private fun getCategoryType(categoryId: Int): String { | 
|         return when(categoryId) { | 
|             1 -> "快递" | 
|             2 -> "还款" | 
|             3 -> "收入" | 
|             4 -> "航班" | 
|             5 -> "火车票" | 
|             else -> "" | 
|         } | 
|     } | 
|      | 
|     private suspend fun createRemindersForStations( | 
|         categoryId: Int,  | 
|         categoryName: String,  | 
|         stationGroups: List<StationGroup>, | 
|         notificationMethod: String | 
|     ) { | 
|         stationGroups.forEach { stationGroup -> | 
|             val content = generateReminderContent(categoryId, stationGroup.stationName, stationGroup.count) | 
|              | 
|             // 创建提醒记录 | 
|             val record = ReminderRecord( | 
|                 categoryId = categoryId, | 
|                 categoryName = categoryName, | 
|                 content = content, | 
|                 notificationMethod = notificationMethod, | 
|                 status = ReminderRecord.STATUS_UNREAD | 
|             ) | 
|             val recordId = reminderRecordRepository.insertRecord(record) | 
|              | 
|             // 发送通知 | 
|             if (notificationMethod.contains("PHONE")) { | 
|                 sendNotification( | 
|                     recordId, | 
|                     "新${categoryName}提醒", | 
|                     content | 
|                 ) | 
|             } | 
|         } | 
|     } | 
|      | 
|     private fun generateReminderContent(categoryId: Int, stationName: String, count: Int): String { | 
|         return when(categoryId) { | 
|             1 -> "您有${count}个包裹在${stationName}未取,请及时取件" | 
|             2 -> "您有${count}笔还款,请及时处理" | 
|             3 -> "您有${count}笔收入待确认" | 
|             4 -> "您有${count}个航班信息待处理" | 
|             5 -> "您有${count}张火车票信息待处理" | 
|             else -> "您有新的提醒" | 
|         } | 
|     } | 
|   | 
|     private fun sendNotification(id: Long, title: String, content: String) { | 
|         val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | 
|   | 
|         // 创建启动应用的Intent | 
|         val intent = createAppIntent() | 
|          | 
|         // 创建PendingIntent | 
|         val pendingIntent = PendingIntent.getActivity( | 
|             applicationContext,  | 
|             id.toInt(),  | 
|             intent, | 
|             PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE | 
|         ) | 
|   | 
|         // 创建通知渠道(Android 8.0及以上需要) | 
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | 
|             val channel = NotificationChannel( | 
|                 CHANNEL_ID, | 
|                 "提醒通知", | 
|                 NotificationManager.IMPORTANCE_DEFAULT | 
|             ) | 
|             notificationManager.createNotificationChannel(channel) | 
|         } | 
|   | 
|         // 创建通知 | 
|         val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID) | 
|             .setSmallIcon(R.drawable.ic_reminder) // 使用自定义图标 | 
|             .setContentTitle(title) | 
|             .setContentText(content) | 
|             .setPriority(NotificationCompat.PRIORITY_DEFAULT) | 
|             .setAutoCancel(true) | 
|             .setContentIntent(pendingIntent) | 
|             .build() | 
|   | 
|         // 发送通知 | 
|         notificationManager.notify(id.toInt(), notification) | 
|     } | 
|      | 
|     // 添加创建跳转Intent的方法 | 
|     private fun createAppIntent(): Intent { | 
|         // 判断用户是否已登录 | 
|         val isLoggedIn = PreferencesManager.getToken() != null | 
|   | 
|         return if (isLoggedIn) { | 
|             // 已登录,跳转到主页 | 
|             Intent(applicationContext, MainActivity::class.java).apply { | 
|                 flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | 
|             } | 
|         } else { | 
|             // 未登录,跳转到登录页 | 
|             Intent(applicationContext, PhoneLoginActivity::class.java).apply { | 
|                 flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK | 
|             } | 
|         } | 
|     } | 
|      | 
|     // 修改测试通知方法,添加点击事件 | 
|     private fun sendTestNotification() { | 
|         val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | 
|   | 
|         // 创建启动应用的Intent | 
|         val intent = createAppIntent() | 
|          | 
|         // 创建PendingIntent | 
|         val pendingIntent = PendingIntent.getActivity( | 
|             applicationContext,  | 
|             1000,  | 
|             intent, | 
|             PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE | 
|         ) | 
|   | 
|         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | 
|             val channel = NotificationChannel( | 
|                 CHANNEL_ID, | 
|                 "测试通知", | 
|                 NotificationManager.IMPORTANCE_HIGH | 
|             ) | 
|             notificationManager.createNotificationChannel(channel) | 
|         } | 
|   | 
|         val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID) | 
|             .setSmallIcon(R.drawable.ic_reminder) // 使用自定义图标 | 
|             .setContentTitle("测试通知") | 
|             .setContentText("这是一条测试通知,证明 Worker 已执行") | 
|             .setPriority(NotificationCompat.PRIORITY_HIGH) | 
|             .setAutoCancel(true) | 
|             .setContentIntent(pendingIntent) | 
|             .build() | 
|   | 
|         notificationManager.notify(1000, notification) | 
|     } | 
|   | 
|     companion object { | 
|         private const val CHANNEL_ID = "reminder_channel" | 
|          | 
|         // 定时任务执行频率 - 可选不同配置 | 
|         val REPEAT_INTERVAL = 15L  // 设置为每15分钟运行一次 | 
|         val REPEAT_INTERVAL_TIME_UNIT = TimeUnit.MINUTES | 
|          | 
|         // 创建在指定时间运行的定时任务 | 
|         fun setupScheduledWorker(context: Context, hour: Int, minute: Int) { | 
|             val calendar = Calendar.getInstance() | 
|             val now = Calendar.getInstance() | 
|              | 
|             calendar.set(Calendar.HOUR_OF_DAY, hour) | 
|             calendar.set(Calendar.MINUTE, minute) | 
|             calendar.set(Calendar.SECOND, 0) | 
|              | 
|             // 如果设定时间已经过了,则设置为明天这个时间 | 
|             if (calendar.timeInMillis < now.timeInMillis) { | 
|                 calendar.add(Calendar.DAY_OF_MONTH, 1) | 
|             } | 
|              | 
|             // 计算初始延迟 | 
|             val initialDelay = calendar.timeInMillis - now.timeInMillis | 
|              | 
|             // 创建周期性工作请求(每天同一时间) | 
|             val dailyWorkRequest = PeriodicWorkRequestBuilder<ReminderWorker>( | 
|                 24, TimeUnit.HOURS | 
|             ) | 
|                 .setInitialDelay(initialDelay, TimeUnit.MILLISECONDS) | 
|                 .build() | 
|                  | 
|             // 使用 REPLACE 策略确保新配置生效 | 
|             WorkManager.getInstance(context).enqueueUniquePeriodicWork( | 
|                 "daily_reminder_${hour}_${minute}", | 
|                 ExistingPeriodicWorkPolicy.REPLACE, | 
|                 dailyWorkRequest | 
|             ) | 
|         } | 
|     } | 
| }  |