创建插入前触发器语句.txt 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. -- CREATE TABLE my_table (
  2. -- id INT,
  3. -- data TEXT
  4. -- );
  5. --
  6. CREATE OR REPLACE FUNCTION set_timely_status_before_insert() RETURNS TRIGGER AS $$
  7. BEGIN
  8. if(NEW.scene='0') then
  9. if(NEW.report_time is not null AND NEW.disposal_time is not null) then
  10. if(EXTRACT(EPOCH FROM (CAST(NEW.disposal_time AS timestamp) - CAST(NEW.report_time AS timestamp))) / 3600<=2) then
  11. NEW.timely_status='1';
  12. return NEW;
  13. else
  14. NEW.timely_status='0';
  15. return NEW;
  16. END if;
  17. END if;
  18. END if;
  19. if(NEW.scene='1' or NEW.scene='5') then
  20. if(NEW.report_time is not null AND NEW.disposal_time is not null) then
  21. if(EXTRACT(EPOCH FROM (CAST(NEW.disposal_time AS timestamp) - CAST(NEW.report_time AS timestamp))) / 3600<=12) then
  22. NEW.timely_status='1';
  23. return NEW;
  24. else
  25. NEW.timely_status='0';
  26. return NEW;
  27. END if;
  28. END if;
  29. END if;
  30. if(NEW.scene='2' ) then
  31. if(NEW.report_time is not null AND NEW.disposal_time is not null) then
  32. if(EXTRACT(EPOCH FROM (CAST(NEW.disposal_time AS timestamp) - CAST(NEW.report_time AS timestamp))) / 3600<=4) then
  33. NEW.timely_status='1';
  34. return NEW;
  35. else
  36. NEW.timely_status='0';
  37. return NEW;
  38. END if;
  39. END if;
  40. END if;
  41. if(NEW.scene='3' ) then
  42. if(NEW.report_time is not null AND NEW.disposal_time is not null) then
  43. if(EXTRACT(EPOCH FROM (CAST(NEW.disposal_time AS timestamp) - CAST(NEW.report_time AS timestamp))) / 3600<=1) then
  44. NEW.timely_status='1';
  45. return NEW;
  46. else
  47. NEW.timely_status='0';
  48. return NEW;
  49. END if;
  50. END if;
  51. END if;
  52. RETURN NEW;
  53. END;
  54. $$ LANGUAGE plpgsql;
  55. CREATE TRIGGER before_insert_set_timely_status
  56. BEFORE INSERT ON warning_info
  57. FOR EACH ROW EXECUTE FUNCTION set_timely_status_before_insert();
  58. -- INSERT INTO "public"."warning_info" ("id", "scene", "warning_name", "warning_source", "equipment_name", "equipment_number", "warning_type", "warning_site", "area", "warning_rank", "generated_time", "report_time", "warning_describe", "report", "analyses", "disposal_state", "disposal_user_name", "disposal_user_id", "disposal_time", "disposal_suggest", "disposal_complete_time", "disposal_report", "remark", "geom_data", "defined", "deleted", "created_at", "created_by", "updated_at", "updated_by", "analyses_report", "warning_state", "streaming_url", "table_name", "threshold_value", "warning_number") VALUES ('c', '1', '可燃气体探测器报警', '1', '可燃气体探测器', 'R16lkjdi', NULL, '120.480965,31.595133', '320205', '3', '2024-03-21 19:40:30', '2023-09-11 07:30:40', NULL, NULL, '已完成', '3', NULL, NULL, '2024-03-21 19:41:05', '{"action":"接单","details":{"企业接单,接单人员":"华润值班"},"operator":"无锡华润燃气有限公司","stepType":"13","timestamp":"2024-03-21 19:41:05"}', '2024-05-22 17:52:19', NULL, NULL, NULL, NULL, 1, '2024-10-31 10:30:37.902', '-1', NULL, NULL, NULL, 2, NULL, 'rq_jc_jcbj', NULL, 'BJ2403210003');