scale_spot_welding_current.sql 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. -- 历史点焊电流 * 10,与当前入库规则对齐
  2. -- 只处理 weldingType=spot;电流绝对值已经 >= 3 的视为已放大,会跳过
  3. -- 可重复执行。执行前建议先备份 mes_product_arc_welding
  4. -- 1. 先看有多少点焊 / 弧焊(确认只动点焊)
  5. SELECT
  6. SUM(content LIKE '%"weldingType":"spot"%') AS spot_cnt,
  7. SUM(content LIKE '%"weldingType":"arc"%') AS arc_cnt
  8. FROM mes_product_arc_welding;
  9. -- 2. 看几条点焊当前电流(放大前一般是 0.81,放大后是 8.1)
  10. SELECT
  11. id,
  12. oprno,
  13. create_date,
  14. JSON_UNQUOTE(JSON_EXTRACT(content, '$.samples[0].robot1Current')) AS i1,
  15. JSON_UNQUOTE(JSON_EXTRACT(content, '$.samples[0].robot2Current')) AS i2
  16. FROM mes_product_arc_welding
  17. WHERE content LIKE '%"weldingType":"spot"%'
  18. ORDER BY create_date DESC
  19. LIMIT 10;
  20. -- 3. 执行放大(Navicat 里把分隔符改成 $$ 再跑下面这段)
  21. DROP PROCEDURE IF EXISTS scale_spot_welding_current;
  22. DELIMITER $$
  23. CREATE PROCEDURE scale_spot_welding_current()
  24. BEGIN
  25. DECLARE done INT DEFAULT 0;
  26. DECLARE rid VARCHAR(64);
  27. DECLARE rdate DATETIME;
  28. DECLARE rcontent LONGTEXT;
  29. DECLARE i INT;
  30. DECLARE n INT;
  31. DECLARE c1 DECIMAL(20,6);
  32. DECLARE c2 DECIMAL(20,6);
  33. DECLARE maxc DECIMAL(20,6);
  34. DECLARE updated_cnt INT DEFAULT 0;
  35. DECLARE skipped_cnt INT DEFAULT 0;
  36. DECLARE cur CURSOR FOR
  37. SELECT id, create_date, content
  38. FROM mes_product_arc_welding
  39. WHERE content LIKE '%"weldingType":"spot"%';
  40. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  41. OPEN cur;
  42. read_loop: LOOP
  43. FETCH cur INTO rid, rdate, rcontent;
  44. IF done = 1 THEN
  45. LEAVE read_loop;
  46. END IF;
  47. IF rcontent IS NULL OR JSON_VALID(rcontent) = 0 THEN
  48. SET skipped_cnt = skipped_cnt + 1;
  49. ITERATE read_loop;
  50. END IF;
  51. IF JSON_UNQUOTE(JSON_EXTRACT(rcontent, '$.weldingType')) <> 'spot' THEN
  52. SET skipped_cnt = skipped_cnt + 1;
  53. ITERATE read_loop;
  54. END IF;
  55. SET n = IFNULL(JSON_LENGTH(rcontent, '$.samples'), 0);
  56. IF n <= 0 THEN
  57. SET skipped_cnt = skipped_cnt + 1;
  58. ITERATE read_loop;
  59. END IF;
  60. SET maxc = 0;
  61. SET i = 0;
  62. WHILE i < n DO
  63. SET c1 = CAST(JSON_UNQUOTE(JSON_EXTRACT(rcontent, CONCAT('$.samples[', i, '].robot1Current'))) AS DECIMAL(20,6));
  64. SET c2 = CAST(JSON_UNQUOTE(JSON_EXTRACT(rcontent, CONCAT('$.samples[', i, '].robot2Current'))) AS DECIMAL(20,6));
  65. IF c1 IS NOT NULL AND ABS(c1) > maxc THEN
  66. SET maxc = ABS(c1);
  67. END IF;
  68. IF c2 IS NOT NULL AND ABS(c2) > maxc THEN
  69. SET maxc = ABS(c2);
  70. END IF;
  71. SET i = i + 1;
  72. END WHILE;
  73. -- 没有电流,或已经放大过(>=3),跳过
  74. IF maxc <= 0 OR maxc >= 3 THEN
  75. SET skipped_cnt = skipped_cnt + 1;
  76. ITERATE read_loop;
  77. END IF;
  78. SET i = 0;
  79. WHILE i < n DO
  80. SET c1 = CAST(JSON_UNQUOTE(JSON_EXTRACT(rcontent, CONCAT('$.samples[', i, '].robot1Current'))) AS DECIMAL(20,6));
  81. SET c2 = CAST(JSON_UNQUOTE(JSON_EXTRACT(rcontent, CONCAT('$.samples[', i, '].robot2Current'))) AS DECIMAL(20,6));
  82. IF c1 IS NOT NULL THEN
  83. SET rcontent = JSON_SET(rcontent, CONCAT('$.samples[', i, '].robot1Current'), c1 * 10);
  84. END IF;
  85. IF c2 IS NOT NULL THEN
  86. SET rcontent = JSON_SET(rcontent, CONCAT('$.samples[', i, '].robot2Current'), c2 * 10);
  87. END IF;
  88. SET i = i + 1;
  89. END WHILE;
  90. UPDATE mes_product_arc_welding
  91. SET content = rcontent
  92. WHERE id = rid AND create_date = rdate;
  93. SET updated_cnt = updated_cnt + 1;
  94. END LOOP;
  95. CLOSE cur;
  96. SELECT updated_cnt AS updated_cnt, skipped_cnt AS skipped_cnt;
  97. END$$
  98. DELIMITER ;
  99. CALL scale_spot_welding_current();
  100. DROP PROCEDURE IF EXISTS scale_spot_welding_current;
  101. -- 4. 执行后再抽查,点焊应为 8.1 左右,弧焊仍是原来的电流/电压
  102. SELECT
  103. id,
  104. oprno,
  105. create_date,
  106. JSON_UNQUOTE(JSON_EXTRACT(content, '$.weldingType')) AS welding_type,
  107. JSON_UNQUOTE(JSON_EXTRACT(content, '$.samples[0].robot1Current')) AS i1,
  108. JSON_UNQUOTE(JSON_EXTRACT(content, '$.samples[0].robot2Current')) AS i2
  109. FROM mes_product_arc_welding
  110. WHERE content LIKE '%"weldingType":"spot"%'
  111. ORDER BY create_date DESC
  112. LIMIT 10;