insert_electricity.sql 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. CREATE DEFINER=`eq`@`%` PROCEDURE `insert_electricity`(
  2. pastureIN VARCHAR (100),
  3. meterNameIN VARCHAR (50),
  4. meterNumberIN VARCHAR (50),
  5. lastTimeIN VARCHAR (100),
  6. lastAmountINs VARCHAR (50),
  7. NowTimeIN VARCHAR (255),
  8. useAmounts VARCHAR (255),
  9. NowAmountINs VARCHAR(50) ,
  10. priceIN DECIMAL(10,2),
  11. empIN VARCHAR (100),
  12. noteIN VARCHAR (255),
  13. jwt_username VARCHAR (255)
  14. )
  15. SQL SECURITY INVOKER
  16. label:BEGIN
  17. DECLARE employeeIdS,
  18. pastureIdS,
  19. measureIdS,
  20. meterIdS,
  21. lastendAmountS INT (11);
  22. DECLARE _lastdate,measureAmountS,lastAmountIN,
  23. measureDateS,
  24. inputTime,rates varchar (100);
  25. DECLARE useAmount,NowAmountIN DECIMAL (15, 2);
  26. DECLARE msg1 TEXT;
  27. DECLARE code1 CHAR(5) DEFAULT '00000';
  28. DECLARE t_error INTEGER DEFAULT 0;
  29. DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
  30. BEGIN
  31. -- 获取异常code,异常信息
  32. GET DIAGNOSTICS CONDITION 1
  33. code1 = RETURNED_SQLSTATE, msg1 = MESSAGE_TEXT;
  34. END;
  35. #开启事务
  36. START TRANSACTION;
  37. #日期格式化
  38. IF(LENGTH(NowTimeIN)=8)
  39. THEN
  40. SELECT CONCAT('20',SUBSTRING(NowTimeIN,7,2),'-',LEFT(NowTimeIN,2),'-',SUBSTRING(NowTimeIN,4,2)) INTO NowTimeIN;
  41. END IF;
  42. IF(LENGTH(lastTimeIN)=8)
  43. THEN
  44. SELECT CONCAT('20',SUBSTRING(lastTimeIN,7,2),'-',LEFT(lastTimeIN,2),'-',SUBSTRING(lastTimeIN,4,2)) INTO lastTimeIN;
  45. END IF;
  46. #SELECT CONCAT('20',SUBSTRING(NowTimeIN,7,2),'-',LEFT(NowTimeIN,2),'-',SUBSTRING(NowTimeIN,4,2)) INTO NowTimeIN;
  47. #SELECT CONCAT('20',SUBSTRING(lastTimeIN,7,2),'-',LEFT(lastTimeIN,2),'-',SUBSTRING(lastTimeIN,4,2)) INTO lastTimeIN;
  48. IF (useAmounts='' )
  49. THEN
  50. SELECT '用量必须输入' msg;
  51. LEAVE label;
  52. END IF ;
  53. #判断输入的数据
  54. IF (DATE_FORMAT(NowTimeIN, '%Y-%m-%d') > DATE_FORMAT(NOW(), '%Y-%m-%d') )
  55. THEN
  56. SELECT
  57. '本次抄表日期不可大于当前日期' msg;
  58. LEAVE label;
  59. END IF ;
  60. #获取牧场id
  61. SELECT
  62. p.id INTO pastureIdS
  63. FROM
  64. pasture p
  65. WHERE p.`name` = pastureIN LIMIT 1;
  66. #获取计量表id
  67. SELECT
  68. m.id , DATE_FORMAT(m.endDate, '%Y-%m-%d'),m.endAmount,m.Multiple
  69. INTO measureIdS,measureDateS,measureAmountS,rates
  70. FROM
  71. measure m
  72. left JOIN electricity w ON w.measureId = m.id
  73. WHERE m.meterNumber = meterNumberIN AND m.meterType = '电表' and m.meterName=meterNameIN and m.pastureId=pastureIdS AND m.isDel = '0' LIMIT 1;
  74. IF (measureIdS IS NULL)
  75. THEN
  76. SELECT
  77. '计量表不存在' msg;
  78. LEAVE label;
  79. END IF ;
  80. #获取录入人id
  81. SELECT
  82. e.id INTO employeeIdS
  83. FROM
  84. emp e
  85. WHERE e.`empname` = empIN limit 1;
  86. INSERT INTO electricity (
  87. elecNumber,
  88. elecName,
  89. elecConsumption,
  90. price,
  91. sumPrice,
  92. employeId,
  93. `DATE`,
  94. note,
  95. pastureId,
  96. measureId
  97. )
  98. VALUES
  99. (
  100. meterNumberIN,
  101. meterNameIN,
  102. useAmounts,
  103. priceIN,
  104. (useAmounts * priceIN*rates),
  105. employeeIdS,
  106. NowTimeIN,
  107. noteIN,
  108. pastureIdS,
  109. measureIdS
  110. )
  111. ON DUPLICATE KEY
  112. UPDATE
  113. elecConsumption = useAmounts, sumPrice= (useAmounts * priceIN*rates),note=noteIN,
  114. employeId = employeeIdS,price=priceIN;
  115. /*SELECT id INTO meterIdS FROM electricity WHERE measureId = measureIdS AND `DATE` = NowTimeIN limit 1;
  116. if (meterIdS is null)
  117. then
  118. SELECT
  119. '插入水表记录失败,请重试' msg;
  120. LEAVE label;
  121. end if; */
  122. #修改计量设备表中的上次值
  123. UPDATE
  124. measure
  125. SET
  126. lastDate = endDate,
  127. lastAmount = endAmount,
  128. endDate = NowTimeIN,
  129. endAmount = NowAmountIN,
  130. lastRecodeId = meterIdS
  131. WHERE id = measureIdS;
  132. IF code1 = '00000' THEN
  133. COMMIT;
  134. SELECT
  135. 'success' msg;
  136. ELSE
  137. ROLLBACK;
  138. SELECT
  139. msg1 msg;
  140. END IF;
  141. END