migration_20260627_bas_product_spec_standard.sql 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. -- 数据库: sanqiang
  2. -- 说明: 将所有未删除商品的规格(spec)统一设为「标准」
  3. -- 规范: 先写入 bas_product_his 历史快照,再更新主表
  4. -- 建议: 先在测试库执行并核对预览结果,再在生产库执行
  5. USE sanqiang;
  6. -- ---------------------------------------------------------------------------
  7. -- 1. 预览:将要变更的记录
  8. -- ---------------------------------------------------------------------------
  9. SELECT
  10. id,
  11. name,
  12. sku_code,
  13. spec AS spec_before
  14. FROM bas_product
  15. WHERE is_del = 0
  16. AND (spec IS NULL OR TRIM(spec) = '' OR spec <> '标准');
  17. -- ---------------------------------------------------------------------------
  18. -- 2. 历史表:更新前完整复制(仅涉及将要变更的行)
  19. -- ---------------------------------------------------------------------------
  20. INSERT INTO bas_product_his (
  21. id,
  22. name,
  23. sku_code,
  24. category_id,
  25. unit_id,
  26. measure_value,
  27. spec,
  28. description,
  29. detail_content,
  30. image_url,
  31. price_coefficient,
  32. suggested_supplier_price,
  33. is_active,
  34. create_at,
  35. create_by,
  36. create_name,
  37. update_at,
  38. update_by,
  39. his_create_at
  40. )
  41. SELECT
  42. id,
  43. name,
  44. sku_code,
  45. category_id,
  46. unit_id,
  47. measure_value,
  48. spec,
  49. description,
  50. detail_content,
  51. image_url,
  52. price_coefficient,
  53. suggested_supplier_price,
  54. is_active,
  55. create_at,
  56. create_by,
  57. create_name,
  58. update_at,
  59. update_by,
  60. NOW()
  61. FROM bas_product
  62. WHERE is_del = 0
  63. AND (spec IS NULL OR TRIM(spec) = '' OR spec <> '标准');
  64. -- ---------------------------------------------------------------------------
  65. -- 3. 主表:统一规格为「标准」
  66. -- ---------------------------------------------------------------------------
  67. UPDATE bas_product
  68. SET
  69. spec = '标准',
  70. update_at = NOW()
  71. WHERE is_del = 0
  72. AND (spec IS NULL OR TRIM(spec) = '' OR spec <> '标准');
  73. -- ---------------------------------------------------------------------------
  74. -- 4. 验证:未删除商品应全部为「标准」
  75. -- ---------------------------------------------------------------------------
  76. SELECT
  77. COUNT(*) AS total_active,
  78. SUM(CASE WHEN spec = '标准' THEN 1 ELSE 0 END) AS spec_standard_count,
  79. SUM(CASE WHEN spec IS NULL OR spec <> '标准' THEN 1 ELSE 0 END) AS spec_not_standard_count
  80. FROM bas_product
  81. WHERE is_del = 0;