From 27bbd0435881e408f267c99e6a253d2e17873bcc Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期五, 11 四月 2025 17:44:11 +0800
Subject: [PATCH] 1.2

---
 app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt |  223 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 193 insertions(+), 30 deletions(-)

diff --git a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
index 794ece9..f8808f2 100644
--- a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
+++ b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
@@ -22,7 +22,13 @@
 import java.text.SimpleDateFormat
 import android.graphics.Color
 import android.widget.GridLayout
+import android.widget.Toast
+import androidx.lifecycle.lifecycleScope
+import com.example.firstapp.database.response.UserInfo
+import com.example.firstapp.database.service.RetrofitClient
 import com.example.firstapp.model.DailyStat
+import com.example.firstapp.utils.PreferencesManager
+import kotlinx.coroutines.launch
 
 class DashboardFragment : Fragment() {
 
@@ -34,6 +40,12 @@
     private lateinit var barChart: BarChart
     private lateinit var pieChart: PieChart
     private lateinit var heatmapView: View
+    private var currentUserInfo: UserInfo? = null // 确保使用你的实际数据类
+
+    private var startDateCur:String = ""
+    private var endDateCur:String = ""
+
+
     enum class DateType {
         DAY, WEEK, MONTH, YEAR
     }
@@ -48,6 +60,12 @@
         return binding.root
     }
 
+    override fun onResume() {
+        super.onResume()
+        // 重新加载用户信息
+        loadUserInfo()
+    }
+
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
 
@@ -60,7 +78,31 @@
         setupView(view)
         updateDateDisplay()
         loadPackages()
+
+        // 遮罩层
+        loadUserInfo()
+
+        // 遮罩层点击时间
+        binding.overlayContent.setOnClickListener {
+            // 跳转到vipActivity
+            val intent = android.content.Intent(requireContext(), com.example.firstapp.activity.VipActivity::class.java)
+            startActivity(intent)
+        }
     }
+
+    private fun showOverlay() {
+        binding.viewOverlay.visibility = View.VISIBLE
+        binding.overlayContent.visibility = View.VISIBLE
+
+    }
+
+    private fun hiddleOverlay() {
+        binding.viewOverlay.visibility = View.GONE
+        binding.overlayContent.visibility = View.GONE
+
+
+    }
+
 
     private fun setupRecyclerView() {
         binding.recyclerPackages.apply {
@@ -70,6 +112,10 @@
     }
 
     private fun setupTabLayout() {
+        val weekStatsView = binding.layoutWeekStats.root
+        val bar_title:TextView = weekStatsView.findViewById(R.id.bar_title)
+        val pie_title:TextView = weekStatsView.findViewById(R.id.pie_title)
+
         binding.tabDateRange.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
             override fun onTabSelected(tab: TabLayout.Tab?) {
                 currentDateType = when(tab?.position) {
@@ -79,6 +125,40 @@
                     3 -> DateType.YEAR
                     else -> DateType.DAY
                 }
+
+                when (currentDateType) {
+                    DateType.DAY -> {
+                        binding.cardPackageStatsTitleText.text = "本日收到包裹总数"
+                        binding.layoutYearStatsTitleText.text = "包裹取件码记录"
+
+                        binding.cardPackageStatsTitleText.visibility = View.VISIBLE
+                        binding.layoutYearStatsTitleText.visibility =  View.VISIBLE
+                    }
+                    DateType.WEEK -> {
+                        binding.cardPackageStatsTitleText.text = "本周收到包裹总数"
+                        bar_title.text = "本周收到包裹数分布 ->"
+                        pie_title.text = "本周包裹物流公司分布 ->"
+                        binding.cardPackageStatsTitleText.visibility = View.VISIBLE
+                        binding.layoutYearStatsTitleText.visibility =  View.GONE
+
+                    }
+                    DateType.MONTH -> {
+                        binding.cardPackageStatsTitleText.text = "本月收到包裹总数"
+                        bar_title.text = "本月收到包裹数分布 ->"
+                        pie_title.text = "本月包裹物流公司分布 ->"
+                        binding.cardPackageStatsTitleText.visibility = View.VISIBLE
+                        binding.layoutYearStatsTitleText.visibility =  View.GONE
+                    }
+                    DateType.YEAR -> {
+                        bar_title.text = "本年收到包裹数分布 ->"
+                        pie_title.text = "本年包裹物流公司分布 ->"
+                        binding.cardPackageStatsTitleText.visibility = View.GONE
+                        binding.layoutYearStatsTitleText.visibility =  View.GONE
+                    }
+                }
+
+
+
                 updateDateDisplay()
                 updateCharts()
                 loadPackages()
@@ -117,10 +197,13 @@
                 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 -> "yyyy年MM月"
@@ -135,10 +218,15 @@
         }
     }
     private fun setupView(view: View) {
+
         val weekStatsView = binding.layoutWeekStats.root
         barChart = weekStatsView.findViewById(R.id.chart_daily_packages)
         pieChart = weekStatsView.findViewById(R.id.chart_courier_distribution)
         heatmapView = weekStatsView.findViewById(R.id.heatmap_yearly)
+
+        barChart.setViewPortOffsets(100f, 100f, 100f, 200f)
+
+//        barChart.invalidate()
         
         // 初始化时隐藏统计视图
         weekStatsView.visibility = View.GONE
@@ -147,6 +235,7 @@
         setupPieChart()
         setupHeatmap()
         updateCharts()
+
     }
     private fun setupBarChart() {
         barChart.apply {
@@ -163,7 +252,8 @@
                 setDrawGridLines(false)
                 granularity = 1f
                 labelRotationAngle = 0f
-                textSize = 12f
+                textSize = 10f  //标签字体
+                setExtraLeftOffset(5f)  // 减少左侧留白
                 setExtraBottomOffset(15f)
             }
 
@@ -189,12 +279,15 @@
             setExtraOffsets(10f, 10f, 10f, 20f)
         }
 
+
+        
         updateBarChartData()
     }
     private fun updateBarChartData() {
         val statsFlow = when (currentDateType) {
             DateType.WEEK -> {
-                viewModel.getWeeklyStats(currentDate.timeInMillis, 6)
+//                viewModel.getWeeklyStats(currentDate.timeInMillis, 6)
+                viewModel.getWeeklyStatsChart(startDateCur,endDateCur)
             }
             DateType.MONTH -> {
                 viewModel.getYearMonthlyStats(currentDate.timeInMillis)
@@ -370,14 +463,26 @@
             when (currentDateType) {
                 DateType.DAY -> {
                     binding.textPackageCount.text = "${packages.size}个"
+
+                    // 获取本周统计
+                    viewModel.getPackagesUnread(currentDate.timeInMillis,
+                        currentDateType.name)
+                        .observe(viewLifecycleOwner) { unpackages->
+                            // 只读取未取件的包裹
+                            packageAdapter.updatePackages(unpackages)
+                        }
+
                 }
                 DateType.WEEK -> {
                     // 获取本周统计
-                    viewModel.getCurrentWeekStats(currentDate.timeInMillis)
-                        .observe(viewLifecycleOwner) { stats ->
-                            val weekTotal = stats.sumOf { it.count }
-                            binding.textPackageCount.text = "${weekTotal}个"
-                        }
+                    viewModel.getCurrentWeekStats2(startDateCur,endDateCur).observe(viewLifecycleOwner) { stats ->
+                        binding.textPackageCount.text = "${stats}个"
+                    }
+//                    viewModel.getCurrentWeekStats(currentDate.timeInMillis)
+//                        .observe(viewLifecycleOwner) { stats ->
+//                            val weekTotal = stats.sumOf { it.count }
+//                            binding.textPackageCount.text = "${weekTotal}个"
+//                        }
                 }
                 DateType.MONTH -> {
                     // 获取本月统计
@@ -396,8 +501,8 @@
                         }
                 }
             }
-            packageAdapter.updatePackages(packages)
-            packageAdapter.updatePackages(packages)
+
+//            packageAdapter.updatePackages(packages)
 //            binding.textPackageCount.text = "${packages.size}个"
         }
     }
@@ -423,24 +528,44 @@
 
             // 更新UI
             binding.layoutWeekStats.heatmapYearly.apply {
-                // 清除现有的子视图
                 removeAllViews()
                 
-                // 创建网格布局
                 val gridLayout = GridLayout(context).apply {
-                    rowCount = 7
-                    columnCount = 52
+                    rowCount = 8 // 增加一行用于显示月份
+                    columnCount = 53 // 增加一列用于显示星期标签
                 }
 
-                // 添加日期标签
-                val dayLabels = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")
-                for (i in 0..6) {
+                // 添加月份标签
+                val months = arrayOf("1月", "2月", "3月", "4月", "5月", "6月", 
+                                   "7月", "8月", "9月", "10月", "11月", "12月")
+                months.forEachIndexed { index, month ->
                     val label = TextView(context).apply {
-                        text = dayLabels[i]
+                        text = month
                         textSize = 10f
-                        setPadding(0, 0, 8, 0)
+                        setPadding(0, 0, 8, 4)
+                        // 计算每个月份标签的位置
+                        val weekPosition = (index * 4.3).toInt()
+                        val params = GridLayout.LayoutParams()
+                        params.columnSpec = GridLayout.spec(weekPosition + 1)
+                        params.rowSpec = GridLayout.spec(0)
+                        layoutParams = params
                     }
                     gridLayout.addView(label)
+                }
+
+                // 添加星期标签
+                val dayLabels = arrayOf("周一", "周二", "周三", "周四", "周五", "周六", "周日")
+                dayLabels.forEachIndexed { index, label ->
+                    val textView = TextView(context).apply {
+                        text = label
+                        textSize = 10f
+                        setPadding(4, 0, 8, 0)
+                        val params = GridLayout.LayoutParams()
+                        params.columnSpec = GridLayout.spec(0)
+                        params.rowSpec = GridLayout.spec(index + 1)
+                        layoutParams = params
+                    }
+                    gridLayout.addView(textView)
                 }
 
                 // 添加热力图单元格
@@ -448,12 +573,15 @@
                     for (week in 0..51) {
                         val count = heatmapMatrix[day][week]
                         val cell = View(context).apply {
-                            layoutParams = ViewGroup.LayoutParams(
-                                resources.getDimensionPixelSize(R.dimen.heatmap_cell_size),
-                                resources.getDimensionPixelSize(R.dimen.heatmap_cell_size)
-                            )
+                            val params = GridLayout.LayoutParams().apply {
+                                width = resources.getDimensionPixelSize(R.dimen.heatmap_cell_size)
+                                height = resources.getDimensionPixelSize(R.dimen.heatmap_cell_size)
+                                columnSpec = GridLayout.spec(week + 1)
+                                rowSpec = GridLayout.spec(day + 1)
+                                setMargins(1, 1, 1, 1)
+                            }
+                            layoutParams = params
                             setBackgroundColor(getHeatmapColor(count))
-                            setPadding(1, 1, 1, 1)
                         }
                         gridLayout.addView(cell)
                     }
@@ -465,12 +593,11 @@
     }
 
     private fun getHeatmapColor(count: Int): Int {
-        // 根据数量返回不同深浅的颜色
         return when {
             count == 0 -> Color.parseColor("#EBEDF0")
-            count <= 2 -> Color.parseColor("#9BE9A8")
-            count <= 4 -> Color.parseColor("#40C463")
-            count <= 6 -> Color.parseColor("#30A14E")
+            count == 1 -> Color.parseColor("#9BE9A8")
+            count == 2 -> Color.parseColor("#40C463")
+            count <= 4 -> Color.parseColor("#30A14E")
             else -> Color.parseColor("#216E39")
         }
     }
@@ -482,6 +609,7 @@
                 binding.recyclerPackages.visibility = View.VISIBLE
                 binding.layoutWeekStats.root.visibility = View.GONE
                 binding.layoutYearStats.root.visibility = View.GONE
+                binding.cardPackageStats.visibility = View.VISIBLE
             }
             DateType.WEEK, DateType.MONTH -> {
                 // 周和月视图显示柱状图和饼图,隐藏包裹列表
@@ -489,7 +617,9 @@
                 binding.layoutWeekStats.root.visibility = View.VISIBLE
                 binding.layoutYearStats.root.visibility = View.GONE
                 binding.layoutWeekStats.chartDailyPackages.visibility = View.VISIBLE
+                (binding.layoutWeekStats.chartDailyPackages.parent as? View)?.visibility = View.VISIBLE
                 binding.layoutWeekStats.heatmapYearly.visibility = View.GONE
+                binding.cardPackageStats.visibility = View.VISIBLE
                 updateBarChartData()
                 updatePieChartData()
             }
@@ -498,10 +628,12 @@
                 binding.recyclerPackages.visibility = View.GONE
                 binding.layoutWeekStats.root.visibility = View.VISIBLE
                 binding.layoutYearStats.root.visibility = View.VISIBLE
-                binding.layoutWeekStats.chartDailyPackages.visibility = View.GONE
+                (binding.layoutWeekStats.chartDailyPackages.parent as? View)?.visibility = View.GONE
                 binding.layoutWeekStats.heatmapYearly.visibility = View.VISIBLE
+                binding.cardPackageStats.visibility = View.GONE
                 updateHeatmapData()
                 updatePieChartData()
+                updateYearlyStats()
             }
         }
     }
@@ -523,4 +655,35 @@
         super.onDestroyView()
         _binding = null
     }
+    private fun loadUserInfo() {
+
+        lifecycleScope.launch {
+            try {
+                // 从本地获取保存的手机号
+                val savedPhone = PreferencesManager.getPhone()
+                if (savedPhone.isNullOrEmpty()) {
+                    Toast.makeText(context, "用户未登录", Toast.LENGTH_SHORT).show()
+                    return@launch
+                }
+
+                val response = RetrofitClient.apiService.getUserInfo(savedPhone)
+                if (response.code == "0" && response.data != null) {
+                    // 保存用户信息
+                    currentUserInfo = response.data
+                    val userInfo = response.data
+                    if(userInfo.isMember){
+                        hiddleOverlay()
+                    }else{
+                        // 显示遮罩层
+                        showOverlay()
+                    }
+
+                }
+            } catch (e: Exception) {
+                e.printStackTrace()
+                Toast.makeText(context, "获取用户信息失败", Toast.LENGTH_SHORT).show()
+            }
+        }
+
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3