importDiesel.sql 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. CREATE DEFINER=`eq`@`%` PROCEDURE `importDiesel`(
  2. pastureIN VARCHAR (100),
  3. cardNumberIN VARCHAR (50),
  4. eqNameIN VARCHAR (50),
  5. eqCodeIN VARCHAR (50),
  6. NowTimeIN VARCHAR (255),
  7. -- NowAmountIN DECIMAL(10,2) ,
  8. -- priceIN DECIMAL(10,2),
  9. NowAmountIN VARCHAR (255) ,
  10. priceIN VARCHAR (255),
  11. empIN VARCHAR (100),
  12. empLIN VARCHAR (100),
  13. oliClassIN VARCHAR (100),
  14. noteIN varchar (255),
  15. userNameIN VARCHAR (255)
  16. )
  17. SQL SECURITY INVOKER
  18. label:BEGIN
  19. DECLARE employeeIdS,
  20. employeeLIdS,
  21. pastureIdS,
  22. measureIdS,
  23. meterIdS,
  24. lastendAmountS INT (11);
  25. DECLARE _lastdate,
  26. measureDateS,
  27. deptIdS,
  28. eqIdS,
  29. eqIdx,
  30. inputTime varchar (100);
  31. DECLARE measureAmountS DECIMAL (10, 2);
  32. DECLARE msg1 TEXT;
  33. DECLARE code1 CHAR(5) DEFAULT '00000';
  34. DECLARE t_error INTEGER DEFAULT 0;
  35. DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
  36. BEGIN
  37. -- 获取异常code,异常信息
  38. GET DIAGNOSTICS CONDITION 1
  39. code1 = RETURNED_SQLSTATE, msg1 = MESSAGE_TEXT;
  40. END;
  41. #开启事务
  42. START TRANSACTION;
  43. #SELECT CONCAT('20',SUBSTRING(NowTimeIN,7,2),'-',LEFT(NowTimeIN,2),'-',SUBSTRING(NowTimeIN,4,2)) INTO NowTimeIN;
  44. #SELECT CONCAT('20',SUBSTRING(lastTimeIN,7,2),'-',LEFT(lastTimeIN,2),'-',SUBSTRING(lastTimeIN,4,2)) INTO lastTimeIN;
  45. IF (NowAmountIN is null or NowAmountIN='' )
  46. then
  47. select '0' into NowAmountIN;
  48. end if ;
  49. #获取牧场id
  50. SELECT
  51. p.id INTO pastureIdS
  52. FROM
  53. pasture p
  54. WHERE p.`name` = pastureIN;
  55. IF (pastureIdS IS NULL)
  56. THEN
  57. SELECT
  58. '牧场不存在' msg;
  59. LEAVE label;
  60. END IF ;
  61. #获取油卡id
  62. SELECT
  63. O.id,O.eqId
  64. INTO measureIdS,eqIdx
  65. FROM
  66. oilcard O
  67. WHERE O.cardCode = cardNumberIN and O.pastureId = pastureIdS LIMIT 1;
  68. IF (measureIdS IS NULL)
  69. THEN
  70. SELECT
  71. '油卡不存在' msg;
  72. LEAVE label;
  73. END IF ;
  74. #获取设备id
  75. SELECT
  76. e.id,e.departmentId
  77. INTO eqIdS,deptIdS
  78. FROM
  79. equipment e
  80. WHERE e.eqCode = eqCodeIN and e.pastureId = pastureIdS and e.eqname =eqNameIN;
  81. IF (eqIdS IS NULL)
  82. THEN
  83. SELECT
  84. '设备不存在' msg;
  85. LEAVE label;
  86. END IF ;
  87. IF (eqIdS <> eqIdX)
  88. THEN
  89. SELECT
  90. '设备与油卡不匹配' msg;
  91. LEAVE label;
  92. END IF ;
  93. #获取录入人id
  94. SELECT
  95. e.id INTO employeeIdS
  96. FROM
  97. emp e
  98. WHERE e.`empname` = empIN AND e.pastureId = pastureIdS;
  99. IF(LENGTH(NowTimeIN)=8)
  100. THEN
  101. SELECT CONCAT('20',SUBSTRING(NowTimeIN,7,2),'-',LEFT(NowTimeIN,2),'-',SUBSTRING(NowTimeIN,4,2)) INTO NowTimeIN;
  102. END IF;
  103. IF (DATE_FORMAT(NowTimeIN, '%Y-%m-%d') > DATE_FORMAT(NOW(), '%Y-%m-%d') )
  104. THEN
  105. SELECT
  106. '本次抄表日期不可大于当前日期' msg;
  107. LEAVE label;
  108. END IF ;
  109. INSERT INTO diesel (
  110. pastureId,
  111. deptId,
  112. eqId,
  113. eqCode,
  114. oilcardId,
  115. cardNumber,
  116. oilClass,
  117. oilAmount,
  118. nowPrice,
  119. price,
  120. empId,
  121. inputId,
  122. selTime,
  123. note
  124. )
  125. VALUES
  126. (
  127. pastureIdS,
  128. deptIdS,
  129. eqIdS,
  130. eqCodeIN,
  131. measureIdS,
  132. cardNumberIN,
  133. oliClassIN,
  134. CAST( NowAmountIN as decimal(20,2) ),
  135. CAST( NowAmountIN*priceIN as decimal(10,2)) ,
  136. -- NowAmountIN,
  137. -- NowAmountIN*priceIN,
  138. priceIN,
  139. empLIN,
  140. employeeIdS,
  141. NowTimeIN,
  142. noteIN
  143. )
  144. ON DUPLICATE KEY
  145. UPDATE
  146. note = noteIN;
  147. #修改计量设备表中的上次值
  148. UPDATE
  149. oilcard
  150. SET
  151. lastcardAmount = cardAmount,
  152. cardAmount = cardAmount-CAST( NowAmountIN*priceIN as decimal(10,2))
  153. WHERE id = measureIdS;
  154. IF code1 = '00000'
  155. THEN COMMIT;
  156. SELECT
  157. 'success' msg;
  158. ELSE ROLLBACK;
  159. SELECT
  160. msg1 msg;
  161. END IF;
  162. END