Browse Source

完成PDA组盘操作

flower_mr 2 tháng trước cách đây
mục cha
commit
dd5b68684f

+ 2 - 0
app/src/main/java/com/example/pda/model/AppPrefs.kt

@@ -14,6 +14,8 @@ object AppPrefs {
 
     // 使用 Lambda 延迟访问 prefs
     var username by StringPreference({ prefs }, "username", "")
+    var userappid by StringPreference({ prefs }, "userappid", "")
+    var stafftype by StringPreference({ prefs }, "stafftype", "")
 
     var token by StringPreference({ prefs }, "token", "")
     var ip_server by StringPreference({ prefs }, "ip_server", "")

+ 16 - 0
app/src/main/java/com/example/pda/model/ContainerData.kt

@@ -0,0 +1,16 @@
+package com.example.pda.model
+
+import com.google.gson.annotations.SerializedName
+
+
+data class ContainerData(
+    @SerializedName("container")
+    val container: String,
+
+    @SerializedName("batches")
+    val batches: List<InventoryItem>,
+
+    @SerializedName("creater")
+    val username:String,
+)
+

+ 3 - 3
app/src/main/java/com/example/pda/model/InventoryItem.kt

@@ -3,9 +3,9 @@ package com.example.pda.model
 import com.google.gson.annotations.SerializedName
 
 data class InventoryItem(
-    @SerializedName("barcode")
+    @SerializedName("goods_code")
     val normalizedResult: String,
 
-    @SerializedName("count")
+    @SerializedName("goods_qty")
     val occurrences: Int
-)
+)

+ 1 - 1
app/src/main/java/com/example/pda/model/loginItem.kt

@@ -11,7 +11,7 @@ data class loginItem(
 )
 data class successItem(
     @SerializedName("name")
-    val message: String ,// 状态信息
+    val name: String ,// 状态信息
 
     @SerializedName("openid")
     val openid: String ,// 用户唯一标识

+ 3 - 2
app/src/main/java/com/example/pda/network/ApiService.kt

@@ -4,6 +4,7 @@ import com.example.pda.model.BaseResponse
 import com.example.pda.model.InventoryItem
 import com.example.pda.model.loginItem
 import com.example.pda.model.loginSuccessResult
+import com.example.pda.model.ContainerData
 import retrofit2.Response
 import retrofit2.http.Body
 import retrofit2.http.GET
@@ -19,9 +20,9 @@ interface ApiService {
     ): Response<loginSuccessResult>
     
     // 提交库存数据到服务器的接口
-    @POST("asn/list/")
+    @POST("container/detail/")
     suspend fun submitInventory(
-        @Body data: List<InventoryItem>
+        @Body data: ContainerData
     ): Response<BaseResponse<Unit>>
 
     // 从服务器获取库存列表的接口

+ 2 - 1
app/src/main/java/com/example/pda/network/NetworkHelper.kt

@@ -2,6 +2,7 @@ package com.example.pda.network
 
 import android.util.Log
 import com.example.pda.model.InventoryItem
+import com.example.pda.model.ContainerData
 import com.example.pda.model.loginItem
 import com.example.pda.model.successItem
 import kotlinx.coroutines.CoroutineScope
@@ -15,7 +16,7 @@ object NetworkHelper {
 
     // 提交库存数据到服务器的方法
     fun submitData(
-        data: List<InventoryItem>, // 库存数据列表
+        data: ContainerData, // 库存数据列表
         onSuccess: () -> Unit, // 提交成功后的回调函数
         onError: (String) -> Unit // 提交失败后的回调函数
     ) {

+ 1 - 1
app/src/main/java/com/example/pda/network/RetrofitClient.kt

@@ -6,7 +6,7 @@ import com.example.pda.model.AppPrefs
 // RetrofitClient 对象用于创建和管理 Retrofit 实例
 object RetrofitClient {
     // 私有常量 BASE_URL 定义了 API 的基础 URL
-    private const val BASE_URL = "http://192.168.196.182:8008/"
+    private const val BASE_URL = "http://192.168.18.91:8008/"
 
 
     // val instance 通过 lazy 委托实现懒加载,创建 ApiService 的单例实例

+ 42 - 39
app/src/main/java/com/example/pda/ui/DetailsScreen.kt

@@ -30,9 +30,10 @@ import androidx.lifecycle.viewmodel.compose.viewModel
 import com.example.pda.R
 import com.example.pda.function.ResultCount
 import com.example.pda.function.ResultCounter
+import com.example.pda.model.AppPrefs
 import com.example.pda.ui.viewmodel.InventoryViewModel
 import com.example.pda.model.InventoryItem
-import com.example.pda.ui.viewmodel.PingViewmodel
+import com.example.pda.model.ContainerData
 import kotlinx.coroutines.launch
 
 
@@ -56,7 +57,10 @@ fun DetailsScreen(
             addAll(resultCounter.getStatistics())
         }
     }
-    val  Inventoryitems: MutableList<InventoryItem> = mutableListOf()
+    // 使用 mutableStateList 保证Compose能感知变化
+    val inventoryitems = remember { mutableStateListOf<InventoryItem>() }
+
+
     // 初始化数据
     LaunchedEffect(Unit) {
         result.split(",").forEach {
@@ -64,6 +68,29 @@ fun DetailsScreen(
         }
         editableResults.clear()
         editableResults.addAll(resultCounter.getStatistics())
+
+        // 初始化库存列表
+        inventoryitems.clear()
+        inventoryitems.addAll(
+            editableResults.map {
+                InventoryItem(it.normalizedResult, it.occurrences)
+            }
+        )
+    }
+
+    // 更新文本输入时的数据同步
+    fun updateInventoryItem(index: Int, newCount: Int) {
+        editableResults[index] = editableResults[index].copy(occurrences = newCount)
+        inventoryitems[index] = inventoryitems[index].copy(occurrences = newCount)
+    }
+
+    // 提交按钮的统一处理
+    fun onSubmit() {
+        val items = editableResults.map {
+            InventoryItem(it.normalizedResult, it.occurrences)
+        }
+        inventoryitems.clear()
+        inventoryitems.addAll(items)
     }
     val uiState = viewModel.uiState.collectAsState()
 
@@ -144,29 +171,24 @@ fun DetailsScreen(
 
                 ExtendedFloatingActionButton(
                     modifier = Modifier.weight(1f),
-                    onClick ={onsubmit(resultCounter,editableResults,Inventoryitems)} ,
-                    icon = {
-                        Icon(
-                            painter = painterResource(id = R.drawable.ic_scan),
-                            contentDescription = "确认入库"
-                        )
-                    },
-                    text = { Text("确认入库") }
-                )
 
-                ExtendedFloatingActionButton(
-                    modifier = Modifier.weight(1f),
-                    onClick ={viewModel.submitData(Inventoryitems)} ,
+                    onClick ={
+                        onSubmit()
+                        val containerData = ContainerData(
+                            container = container,
+                            batches = inventoryitems.toList(),
+                            username = AppPrefs.username
+                        )
+                        viewModel.submitData(containerData)
+                             } ,
                     icon = {
                         Icon(
                             painter = painterResource(id = R.drawable.ic_scan),
-                            contentDescription = "ping"
+                            contentDescription = "组盘入库"
                         )
                     },
-                    text = { Text("ping") }
+                    text = { Text("组盘入库") }
                 )
-
-
             }
             }
         }
@@ -232,13 +254,8 @@ fun DetailsScreen(
                         OutlinedTextField(
                             value = item.occurrences.toString(),
                             onValueChange = { newValue ->
-                                newValue.toIntOrNull()?.let {
-                                    val newList = editableResults.toMutableList().apply {
-                                        set(index, item.copy(occurrences = it))
-                                    }
-                                    editableResults.clear()
-                                    editableResults.addAll(newList)
-                                }
+                                newValue.toIntOrNull()?.let { updateInventoryItem(index, it) }
+
                             },
                             modifier = Modifier.width(100.dp),
                             keyboardOptions = KeyboardOptions.Default.copy(
@@ -254,19 +271,5 @@ fun DetailsScreen(
     }
 }
 
-private fun onsubmit(resultCounter: ResultCounter, editableResults: MutableList<ResultCount>, items: MutableList<InventoryItem>) {
-
-
-    // 使用 map 进行转换,并更新 items
-    items.clear()
-    items.addAll(editableResults.map { result ->
-        InventoryItem(
-            normalizedResult = result.normalizedResult, // 将对应字段映射到 InventoryItem 中
-            occurrences = result.occurrences // 将对应字段映射到 InventoryItem 中
-        )
-    })
-    resultCounter.clear()
-    editableResults.clear()
-}
 
 

+ 5 - 2
app/src/main/java/com/example/pda/ui/LoginScreen.kt

@@ -44,7 +44,7 @@ fun LoginScreen(
         loginStatus?.takeIf { it }?.let {
             onMainScreen() // 状态为true时跳转
             // 在登录成功后调用
-            HttpClient.token = AppPrefs.username
+            HttpClient.token = AppPrefs.token
         }
 
     }
@@ -138,7 +138,10 @@ private fun onlogin(
     NetworkHelper.userlogin(
         data = useritem,
         onSuccess = { successItem ->
-            AppPrefs.username = successItem.openid
+            AppPrefs.token = successItem.openid
+            AppPrefs.username = successItem.name
+            AppPrefs.userappid = successItem.appid
+            AppPrefs.stafftype = successItem.staff_type
             AppPrefs.islogin = true
             onResult(true) // 传递成功状态
         },

+ 1 - 1
app/src/main/java/com/example/pda/ui/SettingScreen.kt

@@ -61,7 +61,7 @@ fun SettingScreen(
             }
             Text(
 
-                text = "ping监测${AppPrefs.username}"
+                text = "当前登录用户为:${AppPrefs.username}"
             )
 
         }

+ 2 - 2
app/src/main/java/com/example/pda/ui/viewmodel/InventoryViewModel.kt

@@ -3,7 +3,7 @@ package com.example.pda.ui.viewmodel
 import android.util.Log
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
-import com.example.pda.model.InventoryItem
+import com.example.pda.model.ContainerData
 import com.example.pda.network.NetworkHelper
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.StateFlow
@@ -29,7 +29,7 @@ class InventoryViewModel : ViewModel() {
     }
 
     // 提交库存数据的方法
-    fun submitData(data: List<InventoryItem>) {
+    fun submitData(data: ContainerData) {
         // 在 viewModelScope 中启动协程以提交数据
         viewModelScope.launch {
             // 设置UI状态为 Loading 表示正在加载