feat(db): 新增测试数据清除功能,物理清空全部 4 张表并重置 SP 状态
- 各 DAO 新增 deleteAll() 物理清空方法 - DbRepository 新增 clearAllTestData(),按顺序删除食材→菜品→调料→槽位 - DbViewModel 暴露 clearAllTestData(onDone) 供 UI 层调用 - SettingActivity 新增「测试数据清除」入口(隐藏,右上角图标触发显示) - 清除完成后重置 SpTool.cookMode=-1 和 SpTool.canteenId="0" Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -222,6 +222,17 @@ class DbRepository {
|
|||||||
suspend fun countSlotAll() = withContext(Dispatchers.IO) {
|
suspend fun countSlotAll() = withContext(Dispatchers.IO) {
|
||||||
db.seasoningSlotDao().countSlotAll()
|
db.seasoningSlotDao().countSlotAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 物理清空全部 4 张表的测试数据
|
||||||
|
* 清除顺序:先删子表(食材),再删主表(菜品),最后删配置表(调料、槽位)
|
||||||
|
*/
|
||||||
|
suspend fun clearAllTestData() = withContext(Dispatchers.IO) {
|
||||||
|
db.cookFoodGoodsDao().deleteAll()
|
||||||
|
db.cookFoodDao().deleteAll()
|
||||||
|
db.seasoningDao().deleteAll()
|
||||||
|
db.seasoningSlotDao().deleteAll()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
//class SeasoningRepository(val seasoningDao: SeasoningDao) {
|
//class SeasoningRepository(val seasoningDao: SeasoningDao) {
|
||||||
|
|||||||
@@ -239,6 +239,17 @@ class DbViewModel : ViewModel() {
|
|||||||
|
|
||||||
// ---- 数据库调试查看 ----
|
// ---- 数据库调试查看 ----
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 物理清空全部 4 张表的测试数据,完成后通过回调通知 UI
|
||||||
|
* @param onDone 清除完成后在主线程执行的回调
|
||||||
|
*/
|
||||||
|
fun clearAllTestData(onDone: () -> Unit) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
rep.clearAllTestData()
|
||||||
|
onDone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/** 每页加载条数 */
|
/** 每页加载条数 */
|
||||||
const val DB_INSPECT_PAGE_SIZE = 50
|
const val DB_INSPECT_PAGE_SIZE = 50
|
||||||
|
|||||||
@@ -40,4 +40,8 @@ interface CookFoodDao {
|
|||||||
/** 统计菜品记录总数,showDel=true 时包含已删除记录 */
|
/** 统计菜品记录总数,showDel=true 时包含已删除记录 */
|
||||||
@Query("SELECT COUNT(*) FROM dm_cook_food WHERE (:showDel = 1 OR isDel = 0)")
|
@Query("SELECT COUNT(*) FROM dm_cook_food WHERE (:showDel = 1 OR isDel = 0)")
|
||||||
suspend fun countCookFoodAll(showDel: Int): Int
|
suspend fun countCookFoodAll(showDel: Int): Int
|
||||||
|
|
||||||
|
/** 物理清空全表,用于清除测试数据 */
|
||||||
|
@Query("DELETE FROM dm_cook_food")
|
||||||
|
suspend fun deleteAll()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,4 +40,8 @@ interface CookFoodGoodsDao {
|
|||||||
/** 统计食材记录总数,showDel=true 时包含已删除记录 */
|
/** 统计食材记录总数,showDel=true 时包含已删除记录 */
|
||||||
@Query("SELECT COUNT(*) FROM dm_cook_food_goods WHERE (:showDel = 1 OR isDel = 0)")
|
@Query("SELECT COUNT(*) FROM dm_cook_food_goods WHERE (:showDel = 1 OR isDel = 0)")
|
||||||
suspend fun countCookFoodGoodsAll(showDel: Int): Int
|
suspend fun countCookFoodGoodsAll(showDel: Int): Int
|
||||||
|
|
||||||
|
/** 物理清空全表,用于清除测试数据 */
|
||||||
|
@Query("DELETE FROM dm_cook_food_goods")
|
||||||
|
suspend fun deleteAll()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,4 +53,8 @@ interface SeasoningDao {
|
|||||||
/** 统计调料记录总数,showDel=true 时包含已删除记录 */
|
/** 统计调料记录总数,showDel=true 时包含已删除记录 */
|
||||||
@Query("SELECT COUNT(*) FROM dm_seasoning WHERE (:showDel = 1 OR isDel = 0)")
|
@Query("SELECT COUNT(*) FROM dm_seasoning WHERE (:showDel = 1 OR isDel = 0)")
|
||||||
suspend fun countSeasoningAll(showDel: Int): Int
|
suspend fun countSeasoningAll(showDel: Int): Int
|
||||||
|
|
||||||
|
/** 物理清空全表,用于清除测试数据(比 clearAllSeasoning 的逻辑删除更彻底) */
|
||||||
|
@Query("DELETE FROM dm_seasoning")
|
||||||
|
suspend fun deleteAll()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,4 +60,8 @@ interface SeasoningSlotDao {
|
|||||||
*/
|
*/
|
||||||
@Query("DELETE FROM dm_seasoning_slot WHERE deviceId = :deviceId")
|
@Query("DELETE FROM dm_seasoning_slot WHERE deviceId = :deviceId")
|
||||||
suspend fun deleteAllByDeviceId(deviceId: String)
|
suspend fun deleteAllByDeviceId(deviceId: String)
|
||||||
|
|
||||||
|
/** 物理清空全表,用于清除测试数据 */
|
||||||
|
@Query("DELETE FROM dm_seasoning_slot")
|
||||||
|
suspend fun deleteAll()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import com.shuwei.dish.match.utils.SpTool
|
|||||||
import com.shuwei.dish.match.utils.ext.clickWithDebounce
|
import com.shuwei.dish.match.utils.ext.clickWithDebounce
|
||||||
import com.shuwei.dish.match.utils.ext.gone
|
import com.shuwei.dish.match.utils.ext.gone
|
||||||
import com.shuwei.dish.match.utils.ext.startActivity
|
import com.shuwei.dish.match.utils.ext.startActivity
|
||||||
|
import com.shuwei.dish.match.utils.ext.toast
|
||||||
import com.shuwei.dish.match.utils.ext.visible
|
import com.shuwei.dish.match.utils.ext.visible
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,6 +49,7 @@ class SettingActivity : BaseActivity() {
|
|||||||
it.clickWithDebounce {
|
it.clickWithDebounce {
|
||||||
binding.llDbInspect.visible()
|
binding.llDbInspect.visible()
|
||||||
binding.llScaleObserve.visible()
|
binding.llScaleObserve.visible()
|
||||||
|
binding.llClearTestData.visible()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -83,6 +85,27 @@ class SettingActivity : BaseActivity() {
|
|||||||
binding.llScaleObserve.clickWithDebounce {
|
binding.llScaleObserve.clickWithDebounce {
|
||||||
startActivity<MasterScaleActivity>()
|
startActivity<MasterScaleActivity>()
|
||||||
}
|
}
|
||||||
|
binding.llClearTestData.clickWithDebounce {
|
||||||
|
showClearTestDataDialog()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 弹出清除测试数据的二次确认弹窗
|
||||||
|
*/
|
||||||
|
private fun showClearTestDataDialog() {
|
||||||
|
CommonDialog(this)
|
||||||
|
.setTitle("测试数据清除")
|
||||||
|
.setContent("将物理删除全部 4 张表数据(菜品、食材、调料、槽位),此操作不可恢复,同时重置菜品模式和食堂id,确认继续?")
|
||||||
|
.setNegativeButton("取消")
|
||||||
|
.setPositiveButton("确认清除") {
|
||||||
|
appViewModel.clearAllTestData {
|
||||||
|
SpTool.cookMode = -1
|
||||||
|
SpTool.canteenId = "0"
|
||||||
|
toast("测试数据已清除")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openCollectPage() {
|
private fun openCollectPage() {
|
||||||
|
|||||||
@@ -173,6 +173,38 @@
|
|||||||
tools:ignore="ContentDescription" />
|
tools:ignore="ContentDescription" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/llClearTestData"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="120dp"
|
||||||
|
android:layout_marginHorizontal="32dp"
|
||||||
|
android:layout_marginTop="50dp"
|
||||||
|
android:background="@drawable/shape_white_dc_15_corners"
|
||||||
|
android:clipToOutline="true"
|
||||||
|
android:foreground="?android:attr/selectableItemBackground"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:visibility="gone">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="32dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:text="测试数据清除"
|
||||||
|
android:textColor="@color/black333"
|
||||||
|
android:textSize="32sp"
|
||||||
|
android:textStyle="bold" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:layout_width="40dp"
|
||||||
|
android:layout_height="40dp"
|
||||||
|
android:layout_marginEnd="20dp"
|
||||||
|
android:adjustViewBounds="true"
|
||||||
|
android:src="@drawable/ic_arrow_right3"
|
||||||
|
tools:ignore="ContentDescription" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<!-- <!– 底部导航菜单 –>-->
|
<!-- <!– 底部导航菜单 –>-->
|
||||||
<!-- <LinearLayout-->
|
<!-- <LinearLayout-->
|
||||||
<!-- android:id="@+id/bottomMenu"-->
|
<!-- android:id="@+id/bottomMenu"-->
|
||||||
|
|||||||
Reference in New Issue
Block a user