app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
@@ -122,6 +122,8 @@
        binding.tabDateRange.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                currentDate = Calendar.getInstance()
                currentDateType = when(tab?.position) {
                    0 -> DateType.DAY
                    1 -> DateType.WEEK
@@ -194,6 +196,81 @@
    }
    private fun updateDateDisplay() {
        val dateFormat = when (currentDateType) {
            DateType.DAY -> {
                // 获取当天的起始和结束时间
                val calendar = currentDate.clone() as Calendar
                // 设置为当天的 00:00:00
                calendar.set(Calendar.HOUR_OF_DAY, 0)
                calendar.set(Calendar.MINUTE, 0)
                calendar.set(Calendar.SECOND, 0)
                calendar.set(Calendar.MILLISECOND, 0)
                startDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                // 设置为当天的 23:59:59
                calendar.set(Calendar.HOUR_OF_DAY, 23)
                calendar.set(Calendar.MINUTE, 59)
                calendar.set(Calendar.SECOND, 59)
                calendar.set(Calendar.MILLISECOND, 999)
                endDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                // 返回当天的日期格式
                SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault()).format(currentDate.time)
            }
            DateType.WEEK -> {
                // 获取本周的起始和结束日期
                val calendar = currentDate.clone() as Calendar
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
                val startDate = SimpleDateFormat("MM月dd日", Locale.getDefault()).format(calendar.time)
                startDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                calendar.add(Calendar.DAY_OF_WEEK, 6)
                val endDate = SimpleDateFormat("MM月dd日", Locale.getDefault()).format(calendar.time)
                endDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                "$startDate-$endDate"
            }
            DateType.MONTH -> {
                // 获取本月的起始和结束日期
                val calendar = currentDate.clone() as Calendar
                calendar.set(Calendar.DAY_OF_MONTH, 1)
                startDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                calendar.add(Calendar.MONTH, 1)
                calendar.set(Calendar.DAY_OF_MONTH, 0)
                endDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                "yyyy年MM月"
            }
            DateType.YEAR -> {
                // 获取当前年的起始和结束日期
                val calendar = currentDate.clone() as Calendar
                // 设置为当前年份的 1 月 1 日
                calendar.set(Calendar.MONTH, Calendar.JANUARY)
                calendar.set(Calendar.DAY_OF_MONTH, 1)
                startDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                // 设置为当前年份的 12 月 31 日
                calendar.set(Calendar.MONTH, Calendar.DECEMBER)
                calendar.set(Calendar.DAY_OF_MONTH, 31)
                endDateCur = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
                "yyyy年"
            }
        }
        // 更新界面显示
        if (currentDateType == DateType.WEEK) {
            binding.textCurrentDate.text = dateFormat
        } else {
            binding.textCurrentDate.text = SimpleDateFormat(dateFormat, Locale.getDefault())
                .format(currentDate.time)
        }
    }
    private fun updateDateDisplay_bak() {
        val dateFormat = when (currentDateType) {
            DateType.DAY -> "yyyy年MM月dd日"
            DateType.WEEK -> {
@@ -520,7 +597,116 @@
        return arrayOf("周一", "周二", "周三", "周四", "周五", "周六", "周日")
    }
    private fun loadPackages() {
    private fun loadPackages(){
//            根据日、周、月、年 获取统计数字
        when (currentDateType) {
            DateType.DAY -> {
                viewModel.getCurrentDayStatsByType(startDateCur,endDateCur,"快递") .observe(viewLifecycleOwner) { stats ->
                    binding.textPackageCount.text = "${stats}个"
                }
                // 获取取件记录
                viewModel.getPackagesReaded(currentDate.timeInMillis,
                    currentDateType.name)
                    .observe(viewLifecycleOwner) { unpackages->
                        // 只读取未取件的包裹
                        packageAdapter.updatePackages(unpackages)
                    }
            }
            DateType.WEEK -> {
                viewModel.getCurrentDayStatsByType(startDateCur,endDateCur,"快递") .observe(viewLifecycleOwner) { stats ->
                    binding.textPackageCount.text = "${stats}个"
                }
            }
            DateType.MONTH -> {
                viewModel.getCurrentDayStatsByType(startDateCur,endDateCur,"快递") .observe(viewLifecycleOwner) { stats ->
                    binding.textPackageCount.text = "${stats}个"
                }
            }
            DateType.YEAR -> {
                viewModel.getCurrentDayStatsByType(startDateCur,endDateCur,"快递") .observe(viewLifecycleOwner) { stats ->
                    binding.layoutYearStats.textTotalPackages.text = "${stats}个"
                }
            }
        }
    }
    private fun loadPackages_bak2(){
        val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
//            根据日、周、月、年 获取统计数字
        when (currentDateType) {
            DateType.DAY -> {
                val today = Calendar.getInstance()
                val tmpCurDateStart = formatter.format(currentDate.time)
                val tmpCurDateEnd = formatter.format(currentDate.time)
                viewModel.getCurrentDayStatsByType(tmpCurDateStart,tmpCurDateEnd,"快递") .observe(viewLifecycleOwner) { stats ->
                    binding.textPackageCount.text = "${stats}个"
                }
                // 获取本周统计
                viewModel.getPackagesReaded(currentDate.timeInMillis,
                    currentDateType.name)
                    .observe(viewLifecycleOwner) { unpackages->
                        // 只读取未取件的包裹
                        packageAdapter.updatePackages(unpackages)
                    }
            }
            DateType.WEEK -> {
                val today = Calendar.getInstance()
                // 获取本周的周一(第一天)
                today.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
                val firstDayOfWeek = formatter.format(today.time)
                // 获取本周的周日(最后一天)
                today.add(Calendar.DATE, 6)  // 加6天
                val lastDayOfWeek = formatter.format(today.time)
                viewModel.getCurrentDayStatsByType(firstDayOfWeek,lastDayOfWeek,"快递") .observe(viewLifecycleOwner) { stats ->
                    binding.textPackageCount.text = "${stats}个"
                }
            }
            DateType.MONTH -> {
                val today = Calendar.getInstance()
                today.set(Calendar.DAY_OF_MONTH, 1)  // 设置为本月第一天
                val firstDayOfMonth = formatter.format(today.time)
                today.add(Calendar.MONTH, 1)  // 移动到下个月
                today.set(Calendar.DAY_OF_MONTH, 0)  // 设置为下个月的最后一天
                val lastDayOfMonth = formatter.format(today.time)
                viewModel.getCurrentDayStatsByType(firstDayOfMonth,lastDayOfMonth,"快递") .observe(viewLifecycleOwner) { stats ->
                    binding.textPackageCount.text = "${stats}个"
                }
            }
            DateType.YEAR -> {
                val today = Calendar.getInstance()
                today.set(Calendar.MONTH, Calendar.JANUARY)  // 设置为第一月
                today.set(Calendar.DAY_OF_MONTH, 1)  // 设置为第一天
                val firstDayOfYear = formatter.format(today.time)
                today.add(Calendar.YEAR, 1)  // 移动到下一年
                today.set(Calendar.MONTH, Calendar.DECEMBER)  // 设置为最后一月
                today.set(Calendar.DAY_OF_MONTH, 31)  // 设置为最后一天
                val lastDayOfYear = formatter.format(today.time)
                viewModel.getCurrentDayStatsByType(firstDayOfYear,lastDayOfYear,"快递") .observe(viewLifecycleOwner) { stats ->
//                    binding.textPackageCount.text = "${stats}个"
                    binding.layoutYearStats.textTotalPackages.text = "${stats}个"
                }
            }
        }
    }
    private fun loadPackages_bak() {
        viewModel.getPackages(
            currentDate.timeInMillis,
            currentDateType.name
@@ -530,7 +716,7 @@
                    binding.textPackageCount.text = "${packages.size}个"
                    // 获取本周统计
                    viewModel.getPackagesUnread(currentDate.timeInMillis,
                    viewModel.getPackagesReaded(currentDate.timeInMillis,
                        currentDateType.name)
                        .observe(viewLifecycleOwner) { unpackages->
                            // 只读取未取件的包裹
@@ -708,7 +894,7 @@
            if (stats.isEmpty()) return@observe
            
            // 更新年度包裹总数
            binding.layoutYearStats.textTotalPackages.text = "${stats.sumOf { it.count }}个"
//            binding.layoutYearStats.textTotalPackages.text = "${stats.sumOf { it.count }}个"
            
            // 更新平均每天包裹数
            val avgDaily = stats.sumOf { it.count }.toFloat() / 365