shengjiupdatedownloadedplanDone.sql 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. CREATE DEFINER=`root`@`%` PROCEDURE `createdownloadedplanUndo`(dateIN VARCHAR(50),
  2. dateStopIN VARCHAR(50),
  3. pastureidIN BIGINT (20))
  4. label:BEGIN
  5. DECLARE positionIdS,
  6. checkResults,
  7. amount,amount1,
  8. providerIdA,
  9. countId2,fttypes INT (20);
  10. DECLARE
  11. positionNameS,examineS,standardS,
  12. noteS,lpplanids,dateTem,timesOs,issmallmaterial,isEnableRemainFeed VARCHAR (100);
  13. DECLARE i INT(11) DEFAULT 0;
  14. DECLARE a INT(11) DEFAULT 0;
  15. DECLARE b INT(11) DEFAULT 0;
  16. DECLARE msg1 TEXT;
  17. DECLARE code1 CHAR(5) DEFAULT '00000';
  18. DECLARE t_error INTEGER DEFAULT 0;
  19. DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
  20. BEGIN
  21. -- 获取异常code,异常信息
  22. GET DIAGNOSTICS CONDITION 1
  23. code1 = RETURNED_SQLSTATE, msg1 = MESSAGE_TEXT;
  24. END;
  25. #开启事务
  26. START TRANSACTION;
  27. CALL refreshdata(pastureidIN);
  28. DELETE FROM downloadedplan WHERE pastureid = pastureidIN AND mydate = dateIN;
  29. DELETE FROM downloadplandtl1 WHERE pastureid = pastureidIN AND `date` = dateIN;
  30. DELETE FROM downloadplandtl2 WHERE pastureid = pastureidIN AND `date` = dateIN;
  31. DELETE FROM downloadplandtl1_exec WHERE pastureid = pastureidIN AND `date` = dateIN;
  32. CALL createLPPdate(dateIN,pastureidIN,'');
  33. CALL creatFTdate(dateIN,pastureidIN,'');
  34. CALL creatFPdate(dateIN,pastureidIN,'');
  35. CALL createPreReHis(dateIN,pastureidIN,'');
  36. SELECT sysopt.inforvalue INTO timesOs FROM sysopt
  37. WHERE sysopt.`pastureid`= pastureidIN AND sysopt.`inforname`='times';
  38. SELECT sysopt.inforvalue INTO issmallmaterial FROM sysopt
  39. WHERE sysopt.`pastureid`= pastureidIN AND sysopt.`inforname`='isSmallMaterial';
  40. SELECT sysopt.inforvalue INTO isEnableRemainFeed FROM sysopt
  41. WHERE sysopt.`pastureid`= pastureidIN AND sysopt.`inforname`='isEnableRemainFeed';
  42. ### 混料与撒料是相同车
  43. INSERT INTO downloadedplan(pastureid,mydate,projname,datacaptureno,
  44. tmrid,tmrtname,times,sort,remark,itemnum,outitems,pid,tempid,
  45. templetname,lweight,lpplantype,plantime,feedpname,feedpcount,tversion)
  46. SELECT
  47. lpplan.pastureid,
  48. dateIN,
  49. -- CONCAT(times,'.',
  50. -- LPAD(lpplan.sort ,3,'0'),'2'
  51. -- ),
  52. CONCAT(times,'.',
  53. LPAD(lpplan.sort ,3,'0'),'0'
  54. ),
  55. tmr.`datacaptureno`,
  56. tmrid,tmrname,
  57. lpplan.times,
  58. lpplan.sort,
  59. lpplan.display,
  60. 0,
  61. 0,
  62. lpplan.id,
  63. lpplan.`ftid`,
  64. lpplan.`ftname`,
  65. 0,
  66. 0,# 类型
  67. lpplan.`begintime`,
  68. (SELECT GROUP_CONCAT(barname) FROM lpplandtl1 WHERE lpplandtl1.pastureid = pastureidIN AND lppid= lpplan.`id` AND lpplandtl1.`tmrid`=lpplan.`tmrid`),
  69. (SELECT SUM(cowcount) FROM lpplandtl1 WHERE lpplandtl1.pastureid = pastureidIN AND lppid= lpplan.`id` ),
  70. (SELECT feedtemplet.`version` FROM `feedtemplet` WHERE feedtemplet.pastureid = lpplan.`pastureid` AND feedtemplet.`id` =lpplan.`ftid` )
  71. FROM `lpplan`
  72. LEFT JOIN tmr ON
  73. tmr.id = lpplan.`tmrid` AND tmr.`pastureid` = lpplan.`pastureid`
  74. WHERE lpplan.pastureid =pastureidIN AND lpplan.times <= timesOs AND lpplan.sel=1
  75. AND (SELECT SUM(lweight) FROM `lpplandtl1`
  76. INNER JOIN `fpdetail` fd
  77. ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = lpplandtl1.pastureid
  78. WHERE lpplandtl1.`pastureid` = lpplan.`pastureid` AND lpplandtl1.`lppid` = lpplan.`id`) >0
  79. ORDER BY lpplan.`sort`;
  80. INSERT INTO downloadedplan(pastureid,mydate,projname,datacaptureno,
  81. tmrid,tmrtname,times,sort,remark,itemnum,outitems,pid,tempid,
  82. templetname,lweight,lpplantype,plantime,feedpname,feedpcount,tversion,oldtmrid)
  83. ### 混料与撒料是不同车
  84. SELECT
  85. lpplan.pastureid,
  86. dateIN,
  87. CONCAT(times,'.',
  88. LPAD(lpplan.sort ,3,'0'),"333"
  89. ),
  90. tmr.`datacaptureno`,
  91. lpplandtl1.tmrid,
  92. lpplandtl1.tmrname,
  93. lpplan.times,
  94. lpplan.sort,
  95. lpplan.display,
  96. ###
  97. 0,
  98. 0,
  99. ###
  100. lpplan.id,
  101. lpplan.`ftid`,
  102. lpplan.`ftname`,
  103. 0,
  104. 2,# 类型
  105. lpplan.`begintime`,
  106. (SELECT GROUP_CONCAT(barname) FROM lpplandtl1 WHERE lpplandtl1.pastureid = pastureidIN AND lppid= lpplan.`id` AND lpplandtl1.`tmrid`<>lpplan.`tmrid` ),
  107. (SELECT SUM(cowcount) FROM lpplandtl1 WHERE lpplandtl1.pastureid = pastureidIN AND lppid= lpplan.`id` ),
  108. (SELECT feedtemplet.`version` FROM `feedtemplet` WHERE feedtemplet.pastureid = lpplan.`pastureid` AND feedtemplet.`id` =lpplan.`ftid` ),lpplandtl1.tmrid
  109. FROM `lpplan`
  110. JOIN `lpplandtl1` ON lpplandtl1.`pastureid` = lpplan.`pastureid` AND lpplandtl1.`lppid` = lpplan.`id` AND lpplandtl1.`tmrid` <> lpplan.`tmrid`
  111. LEFT JOIN tmr ON
  112. tmr.id = lpplandtl1.`tmrid` AND tmr.`pastureid` = lpplandtl1.`pastureid`
  113. WHERE lpplan.pastureid =pastureidIN AND lpplan.times <= timesOs AND lpplan.sel=1 AND lpplandtl1.`lweight`> 0
  114. GROUP BY lpplan.id,tmr.`id`, lpplandtl1.`tmrid` ORDER BY lpplan.`sort`,lpplandtl1.`sort`;
  115. # 日执行子表1
  116. INSERT INTO downloadplandtl1(pastureid,pid,flpid,fid,lweight,sort,feedcode,fname,
  117. feedallowratio,stirdelay,fweight,tmrloadname,`date`,dryfeed,pricefeed,optdevice)
  118. SELECT
  119. lpplandtl1.pastureid,
  120. (SELECT id FROM downloadedplan d WHERE d.pastureid = lpplandtl1.`pastureid` AND d.pid = lpplan.`id` AND d.mydate =dateIN AND lpplantype = 0 LIMIT 1),
  121. lpplandtl1.`lppid`,
  122. ftd.fid,
  123. IF((SELECT sysopt.inforvalue FROM sysopt WHERE sysopt.`pastureid`= pastureidIN AND sysopt.`inforname`='isLockCount')=0,
  124. ftd.fweight/(SELECT SUM(fweight) FROM ftdetail WHERE ftid =ft.id AND pastureid=ft.pastureid )*lpplandtl1.`lweight`, ##未启用锁定牛头数
  125. IF(ftd.islockcount=0,
  126. ((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =ft.id AND ftdetail.pastureid=ft.pastureid AND ftdetail.`islockcount`=0 )
  127. /
  128. (SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =ft.id AND ftdetail.pastureid=ft.pastureid )
  129. *lpplandtl1.`lweight` *
  130. ftd.fweight/
  131. (SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =ft.id AND ftdetail.pastureid=ft.pastureid AND ftdetail.`islockcount`=0 )),
  132. ((SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =ft.id AND ftdetail.pastureid=ft.pastureid AND ftdetail.`islockcount`=1 )
  133. /
  134. (SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =ft.id AND ftdetail.pastureid=ft.pastureid )
  135. *lpplandtl1.`lweight` *
  136. ftd.fweight/
  137. (SELECT SUM(fweight) FROM ftdetail WHERE ftdetail.ftid =ft.id AND ftdetail.pastureid=ft.pastureid AND ftdetail.`islockcount`=1 ))
  138. )
  139. ),
  140. (SELECT COUNT(*)+1 FROM ftdetail WHERE ftdetail.`pastureid` = lpplandtl1.`pastureid` AND ftdetail.`ftid` = ft.`id` AND ftdetail.sort <ftd.sort),
  141. IF(ftd.preftid=0,CONCAT(f.feedcode," "),'-1'),
  142. IFNULL(f.fname,ftd.fname),
  143. f.allowratio,
  144. ftd.autosecond,
  145. ftd.fweight,
  146. ftd.feedgroup,
  147. dateIN,
  148. fu.dry,
  149. f.uprice,
  150. lpplan.`tmrid`
  151. FROM `lpplandtl1`
  152. INNER JOIN lpplan
  153. ON lpplan.`id` = lpplandtl1.`lppid` AND lpplan.`pastureid` = lpplandtl1.`pastureid`
  154. LEFT JOIN `fpdetail` fd
  155. ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = pastureidIN
  156. INNER JOIN feedtemplet ft
  157. ON IF (lpplandtl1.`fttype`=1,fd.`ptid` = ft.id,fd.`ptsid` = ft.id ) AND ft.pastureid = pastureidIN
  158. LEFT JOIN ftdetail ftd
  159. ON ftd.`ftid` = ft.`id` AND ftd.pastureid = pastureidIN
  160. LEFT JOIN feed f
  161. ON ftd.`fid` = f.`id` AND f.pastureid = pastureidIN
  162. LEFT JOIN feednur fu
  163. ON fu.fid=f.id
  164. WHERE lpplandtl1.pastureid = pastureidIN AND lpplan.times <= timesOs AND lpplan.sel=1 AND lpplandtl1.`lweight` > 0
  165. ON DUPLICATE KEY UPDATE downloadplandtl1.lweight = downloadplandtl1.lweight+ftd.fweight/(SELECT SUM(fweight) FROM ftdetail WHERE ftid =ft.id AND pastureid=ft.pastureid )*lpplandtl1.`lweight`
  166. ;
  167. INSERT INTO downloadplandtl1(pastureid,pid,flpid,fid,lweight,sort,feedcode,fname,
  168. feedallowratio,stirdelay,fweight,tmrloadname,`date`,dryfeed,pricefeed,optdevice,TYPE)
  169. SELECT
  170. lpplandtl1.pastureid,
  171. (SELECT id FROM downloadedplan d WHERE d.pastureid = lpplandtl1.`pastureid` AND d.pid = lpplan.`id` AND d.mydate =dateIN AND lpplantype = 2 AND d.`tmrid` =lpplandtl1.`tmrid` LIMIT 1),
  172. lpplandtl1.`lppid`,
  173. lpplan.`tmrid`,
  174. SUM(lpplandtl1.`lweight`),
  175. 1,
  176. '',
  177. lpplan.`tmrname`,
  178. 0,
  179. 0,
  180. 0,
  181. lpplandtl1.`tmrname`,
  182. dateIN,
  183. 0,
  184. 0,
  185. lpplandtl1.`tmrid`,
  186. 1
  187. FROM `lpplandtl1`
  188. INNER JOIN lpplan
  189. ON lpplan.`id` = lpplandtl1.`lppid` AND lpplan.`pastureid` = lpplandtl1.`pastureid`
  190. WHERE lpplandtl1.pastureid = pastureidIN AND lpplan.times <= timesOs
  191. AND lpplan.sel=1 AND lpplandtl1.`tmrid` <> lpplan.`tmrid` AND lpplandtl1.`lweight` > 0
  192. GROUP BY lpplan.id,lpplandtl1.`tmrid`;
  193. # 日执行子表2
  194. SELECT COUNT(lpplan.id)INTO amount1 FROM `lpplan` WHERE pastureid=pastureidIN AND lpplan.times <= timesOs AND lpplan.sel=1;
  195. WHILE amount1>0 DO
  196. SELECT amount1-1 INTO amount1;
  197. SELECT lpplan.id INTO lpplanids FROM `lpplan` WHERE pastureid=pastureidIN AND lpplan.times <= timesOs AND lpplan.sel=1
  198. ORDER BY id DESC LIMIT 1 OFFSET amount1;
  199. SELECT COUNT(lpplandtl1.id)INTO amount FROM `lpplandtl1`
  200. INNER JOIN `fpdetail` fd
  201. ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = pastureidIN
  202. INNER JOIN feedtemplet ft
  203. ON IF(lpplandtl1.`fttype`=1,fd.`ptid` = ft.id,fd.`ptsid` = ft.id ) AND ft.pastureid = pastureidIN
  204. WHERE lpplandtl1.pastureid = pastureidIN
  205. AND lpplandtl1.lppid=lpplanids ;
  206. SELECT 0,0 INTO a,b;
  207. WHILE amount>0 DO
  208. SELECT amount-1 INTO amount;
  209. SELECT lpplandtl1.`fttype` INTO fttypes FROM `lpplandtl1`
  210. WHERE lpplandtl1.pastureid = pastureidIN AND lpplandtl1.lppid=lpplanids
  211. ORDER BY lpplandtl1.sort DESC LIMIT 1 OFFSET amount;
  212. IF (fttypes = 0)
  213. THEN
  214. SELECT b+1 INTO b;
  215. END IF;
  216. IF (fttypes= 1)
  217. THEN
  218. SELECT a+1 INTO a;
  219. END IF;
  220. INSERT INTO downloadplandtl2
  221. (pastureid,pid,fbarid,fname,lweight,sort,flpid,
  222. feedtempletid,feedtempletname,fpid,`date`,allowratio,optdevice)
  223. SELECT lpplandtl1.`pastureid`,(SELECT id FROM downloadedplan d WHERE d.pastureid = lpplandtl1.`pastureid` AND d.pid = lpplandtl1.`lppid` AND d.mydate =dateIN AND lpplantype = IF(lpplandtl1.`tmrid`=lpplan.`tmrid`,0,2)AND lpplandtl1.`tmrid`=d.`tmrid`),
  224. lpplandtl1.barid,lpplandtl1.`barname`,lpplandtl1.lweight,
  225. IF(lpplandtl1.`fttype`=1,a+100,b+150),
  226. lpplandtl1.`lppid`,ft.id ftid,ft.`tname`,
  227. lpplandtl1.`fpdid`,dateIN,
  228. (SELECT allowratio FROM bar WHERE bar.`pastureid` =lpplandtl1.`pastureid` AND bar.id = lpplandtl1.`barid` LIMIT 1),
  229. lpplandtl1.`tmrid`
  230. FROM `lpplandtl1`
  231. INNER JOIN lpplan ON
  232. lpplandtl1.`pastureid` = lpplan.`pastureid` AND lpplandtl1.`lppid` = lpplan.id
  233. INNER JOIN `fpdetail` fd
  234. ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = pastureidIN
  235. INNER JOIN feedtemplet ft
  236. ON IF(lpplandtl1.`fttype`=1,fd.`ptid` = ft.id,fd.`ptsid` = ft.id ) AND ft.pastureid = pastureidIN
  237. WHERE lpplandtl1.pastureid = pastureidIN AND lpplandtl1.lppid=lpplanids AND lpplandtl1.`lweight` > 0
  238. ORDER BY lpplandtl1.sort DESC LIMIT 1 OFFSET amount;
  239. INSERT INTO downloadplandtl2
  240. (pastureid,pid,fbarid,fname,lweight,sort,flpid,
  241. feedtempletid,feedtempletname,fpid,`date`,allowratio,optdevice,`type`)
  242. SELECT lpplandtl1.`pastureid`,(SELECT id FROM downloadedplan d WHERE d.pastureid = lpplandtl1.`pastureid` AND d.pid = lpplandtl1.`lppid` AND d.mydate =dateIN AND lpplantype = 0),
  243. lpplandtl1.`tmrid`,lpplandtl1.`tmrname`,SUM(lpplandtl1.lweight),
  244. IF(lpplandtl1.`fttype`=1,a+100,b+150),
  245. lpplandtl1.`lppid`,ft.id ftid,ft.`tname`,
  246. lpplandtl1.`fpdid`,dateIN,
  247. (SELECT allowratio FROM bar WHERE bar.`pastureid` =lpplandtl1.`pastureid` AND bar.id = lpplandtl1.`barid` LIMIT 1),
  248. lpplan.`tmrid`,
  249. 1
  250. FROM `lpplandtl1`
  251. INNER JOIN lpplan ON
  252. lpplandtl1.`pastureid` = lpplan.`pastureid` AND lpplandtl1.`lppid` = lpplan.id
  253. INNER JOIN `fpdetail` fd
  254. ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = pastureidIN
  255. INNER JOIN feedtemplet ft
  256. ON IF(lpplandtl1.`fttype`=1,fd.`ptid` = ft.id,fd.`ptsid` = ft.id ) AND ft.pastureid = pastureidIN
  257. WHERE lpplandtl1.pastureid = pastureidIN AND lpplandtl1.lppid=lpplanids AND lpplandtl1.`tmrid` <> lpplan.`tmrid` AND lpplandtl1.`lweight` > 0
  258. GROUP BY lpplan.`id`,lpplandtl1.`tmrid`
  259. ORDER BY lpplandtl1.sort DESC LIMIT 1 OFFSET amount;
  260. END WHILE;
  261. END WHILE;
  262. UPDATE `downloadedplan` d
  263. SET d.`lpplantype` = 1
  264. WHERE d.pastureid = pastureidIN AND
  265. (SELECT COUNT(*) FROM `downloadplandtl2` d2 WHERE d2.pastureid = d.pastureid AND d2.pid = d.id AND d2.`type` = 1 ) > 0
  266. AND d.lpplantype = 0 AND d.mydate = dateIN ;
  267. SELECT dateIN INTO dateTem;
  268. IF issmallmaterial = '1' # 是否开启小料
  269. THEN
  270. CALL createsmallmaterialplan(dateIN,timesOs,pastureidIN);
  271. END IF ;
  272. ################################## 预混计划
  273. INSERT INTO downloadedplan(pastureid,mydate,projname,datacaptureno,
  274. tmrid,tmrtname,times,sort,remark,itemnum,outitems,pid,tempid,
  275. templetname,lweight,lpplantype,plantime,feedpname,feedpcount,tversion)
  276. SELECT
  277. premixplan.pastureid,dateIN,
  278. IF(premixplan.lppcode IS NULL, CONCAT(times,'.0000'),
  279. CONCAT(times,'.',
  280. LPAD(SUBSTRING_INDEX( premixplan.lppcode, ' ',1 ),3,'0'),'01',LPAD(premixplan.sort,3,'0')
  281. )
  282. ),
  283. tmr.`datacaptureno`,
  284. tmrid,tmrcode,
  285. premixplan.times,
  286. premixplan.sort,
  287. '预混计划',
  288. ###
  289. ( SELECT COUNT(*) FROM
  290. ftdetail ftd
  291. WHERE ftd.`ftid` = premixplan.`ftid` AND ftd.`pastureid`=premixplan.pastureid),
  292. 0 ,
  293. ###
  294. premixplan.id,
  295. premixplan.`ftid`,
  296. premixplan.`ftname`,
  297. premixplan.`operateweight`,
  298. 4,
  299. premixplan.`ptime`,
  300. '',
  301. 0,
  302. (SELECT feedtemplet.`version` FROM `feedtemplet` WHERE feedtemplet.pastureid = premixplan.`pastureid` AND feedtemplet.`id` =premixplan.`ftid` )
  303. FROM `premixplan`
  304. INNER JOIN tmr ON
  305. tmr.pastureid = premixplan.`pastureid` AND tmr.id = premixplan.`tmrid`
  306. WHERE premixplan.pastureid =pastureidIN AND premixplan.times <= timesOs AND premixplan.enable=1;
  307. INSERT INTO downloadplandtl1(pastureid,pid,flpid,fid,lweight,sort,feedcode,fname,
  308. feedallowratio,stirdelay,fweight,tmrloadname,`date`,dryfeed,pricefeed,optdevice)
  309. SELECT
  310. premixplan.pastureid,
  311. (SELECT id FROM downloadedplan d WHERE d.pastureid = premixplan.`pastureid` AND d.pid = premixplan.`id` AND d.mydate =dateIN AND lpplantype = 4 ),
  312. premixplan.`id`,
  313. ftd.fid,
  314. ftd.fweight/(SELECT SUM(fweight) FROM ftdetail WHERE ftid =ft.id AND pastureid=ft.pastureid )*premixplan.`operateweight`,
  315. ftd.sort,
  316. f.feedcode,
  317. IFNULL(f.fname,ftd.fname),
  318. f.allowratio,
  319. ftd.autosecond,
  320. ftd.fweight,
  321. ftd.feedgroup,
  322. dateIN,
  323. fu.dry,
  324. f.uprice,
  325. premixplan.`tmrid`
  326. FROM `premixplan`
  327. INNER JOIN feedtemplet ft
  328. ON premixplan.`ftid` =ft.id AND ft.pastureid = premixplan.pastureid
  329. LEFT JOIN ftdetail ftd
  330. ON ftd.`ftid` = ft.`id` AND ftd.pastureid = premixplan.pastureid
  331. LEFT JOIN feed f
  332. ON ftd.`fid` = f.`id` AND f.pastureid = premixplan.pastureid
  333. LEFT JOIN feednur fu
  334. ON fu.fid=f.id AND fu.pastureid= premixplan.pastureid
  335. WHERE premixplan.pastureid = pastureidIN AND premixplan.times <= timesOs AND premixplan.enable=1;
  336. UPDATE premixplan
  337. SET `enable` = 0
  338. WHERE premixplan.pastureid =pastureidIN AND premixplan.enable=1;
  339. #################剩料计划
  340. IF isEnableRemainFeed = '1' # 是否开启剩料
  341. THEN
  342. INSERT INTO downloadedplan(pastureid,mydate,projname,datacaptureno,
  343. tmrid,tmrtname,times,sort,remark,itemnum,outitems,pid,tempid,
  344. templetname,lweight,lpplantype,plantime,feedpname,feedpcount)
  345. SELECT
  346. remainplan.pastureid,
  347. dateIN,
  348. IF(remainplan.lppcode IS NULL, CONCAT(times,'.0001'),
  349. CONCAT(times,'.',
  350. LPAD(SUBSTRING_INDEX( remainplan.lppcode, ' ',1 ),3,'0'),'31',LPAD(remainplan.sort,3,'0')
  351. )
  352. ),
  353. tmr.`datacaptureno`,
  354. tmrid,tmrcode,
  355. remainplan.times,
  356. remainplan.sort,
  357. '剩料计划',
  358. 0,
  359. 1,
  360. remainplan.id,
  361. NULL,
  362. '',
  363. 0,
  364. 3,
  365. remainplan.`rtime`,
  366. bar,
  367. IFNULL((SELECT cowsum FROM barmilk WHERE barmilk.pastureid = remainplan.`pastureid` AND barmilk.`barid`= remainplan.`barid`
  368. ORDER BY barmilk.productdate DESC LIMIT 1
  369. ),0)
  370. FROM `remainplan`
  371. INNER JOIN tmr ON
  372. tmr.pastureid = remainplan.`pastureid` AND tmr.id = remainplan.`tmrid`
  373. WHERE remainplan.pastureid =pastureidIN AND remainplan.times <= timesOs AND remainplan.enable=1;
  374. INSERT INTO downloadplandtl2(pastureid,pid,fbarid,fname,lweight,sort,flpid,useinbartype,fpid,`date`,allowratio,optdevice)
  375. SELECT remainplan.`pastureid`,
  376. (SELECT id FROM downloadedplan d WHERE d.pastureid = remainplan.`pastureid` AND d.pid = remainplan.`id` AND d.mydate =dateIN AND lpplantype = 3),
  377. rpd.barid,
  378. rpd.`bar`,0,
  379. rpd.sort,remainplan.`id`,
  380. IF(treatmethod='继续饲喂',2, rpd.type),
  381. remainplan.`id`,dateIN,
  382. (SELECT allowratio FROM bar WHERE bar.`pastureid` =remainplan.`pastureid` AND bar.id = rpd.`barid` LIMIT 1),
  383. remainplan.`tmrid`
  384. FROM `remainplan`
  385. INNER JOIN rpdetail rpd
  386. ON rpd.pastureid =remainplan.pastureid AND rpd.bigid = remainplan.id
  387. WHERE remainplan.pastureid = pastureidIN
  388. AND remainplan.times <= timesOs AND remainplan.enable=1;
  389. END IF ;
  390. ############################################################################################################################################
  391. UPDATE downloadplandtl2
  392. SET lweighthis = lweight,
  393. cowclassname = (SELECT `cowclass` FROM `barmilk` WHERE pastureid = downloadplandtl2.`pastureid` AND `barid` = downloadplandtl2.`fbarid` ORDER BY `productdate` DESC LIMIT 1 ),
  394. cowclassid = (SELECT `cowclassid` FROM `barmilk` WHERE pastureid = downloadplandtl2.`pastureid` AND `barid` = downloadplandtl2.`fbarid` ORDER BY `productdate` DESC LIMIT 1)
  395. WHERE pastureid = pastureidIN AND DATE = dateIN;
  396. INSERT INTO downloadplandtl1_exec (id,pastureid,pid,flpid,lweight,optdevice,sort,fname,fcount,tmrloadname,feedallowratio,stirdelay,DATE,TYPE)
  397. SELECT
  398. id,pastureid,pid,flpid,SUM(lweight),optdevice,sort,GROUP_CONCAT(fname),COUNT(fname),tmrloadname,feedallowratio,stirdelay,DATE,TYPE
  399. FROM downloadplandtl1
  400. WHERE downloadplandtl1.`pastureid`= pastureidIN AND downloadplandtl1.date =dateIN
  401. GROUP BY pid,sort
  402. ORDER BY sort;
  403. CALL updatedownloadprojname(pastureidIN,dateIN); #
  404. UPDATE downloadedplan SET
  405. itemnum = (SELECT COUNT(*) FROM `downloadplandtl1` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`),
  406. outitems = (SELECT COUNT(*) FROM `downloadplandtl2` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`),
  407. lweight =IFNULL(IFNULL((SELECT SUM(lweight) FROM `downloadplandtl2` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`),
  408. (SELECT SUM(lweight) FROM `downloadplandtl1` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`)),0)
  409. WHERE pastureid = pastureidIN AND mydate = dateIN;
  410. # 复制
  411. WHILE dateIN<dateStopIN DO
  412. SELECT DATE_ADD(dateIN,INTERVAL 1 DAY) INTO dateIN;
  413. DELETE FROM downloadedplan WHERE pastureid = pastureidIN AND mydate = dateIN;
  414. DELETE FROM downloadplandtl1 WHERE pastureid = pastureidIN AND `date` = dateIN;
  415. DELETE FROM downloadplandtl2 WHERE pastureid = pastureidIN AND `date` = dateIN;
  416. DELETE FROM downloadplandtl1_exec WHERE pastureid = pastureidIN AND `date` = dateIN;
  417. CALL createLPPdate(dateIN,pastureidIN,'');
  418. CALL creatFTdate(dateIN,pastureidIN,'');
  419. CALL creatFPdate(dateIN,pastureidIN,'');
  420. CALL createPreReHis(dateIN,pastureidIN,'');
  421. ################
  422. INSERT INTO `downloadedplan`(pastureid,mydate,projname,datacaptureno,tmrid,tmrtname,havebutton,inbuttontype,intime,completedtime,processtime,iscompleted,lpplantype,times,sort,remark,itemnum,outitems,feedpname,feedpcount,pid,tempid,templetname,thoweight,fpweight,lweight,iweight,oweight,plantime)
  423. SELECT pastureid,dateIN,projname,datacaptureno,tmrid,tmrtname,havebutton,inbuttontype,intime,completedtime,processtime,iscompleted,lpplantype,times,sort,remark,itemnum,outitems,feedpname,feedpcount,pid,tempid,templetname,thoweight,fpweight,lweight,iweight,oweight,plantime
  424. FROM downloadedplan
  425. WHERE pastureid= pastureidIN AND mydate = dateTem;
  426. ###########################
  427. INSERT INTO `downloadplandtl1`(pastureid,pid,flpid,fid,lweight,sort,lastactualweight,actualweight,actualweightminus,havebuttom,begintime,intime,buttontype,optdevice,feedcode,fname,feedsort,feedallowratio,stirdelay,fweight,tmrloadname,dryfeed,pricefeed,outweight,`date`,`type`)
  428. SELECT d1.pastureid,
  429. (SELECT id FROM downloadedplan d WHERE d.pastureid = d1.`pastureid` AND d.pid = downloadedplan.`pid` AND d.mydate =dateIN AND d.lpplantype = downloadedplan.`lpplantype` AND d.`projname` =downloadedplan.projname),
  430. d1.flpid,d1.fid,d1.lweight,d1.sort,d1.lastactualweight,d1.actualweight,d1.actualweightminus,d1.havebuttom,d1.begintime,d1.intime,d1.buttontype,
  431. d1.optdevice,d1.feedcode,d1.fname,d1.feedsort,d1.feedallowratio,d1.stirdelay,d1.fweight,d1.tmrloadname,d1.dryfeed,d1.pricefeed,d1.outweight,dateIN ,d1.`type`
  432. FROM downloadplandtl1 d1
  433. INNER JOIN downloadedplan
  434. ON downloadedplan.`pastureid` = d1.`pastureid` AND downloadedplan.`id` = d1.pid
  435. WHERE d1.pastureid= pastureidIN AND `date` = dateTem;
  436. #####################
  437. INSERT INTO `downloadplandtl2`(pastureid,pid,fbarid,fname,lastactualweight,actualweight,lweight,actualweightminus,sort,havebuttom,begintime,begintag,intime,endtag,buttontype,optdevice,flpid,cowcount,cowclassid,cowclassname,bigcowname,pweight,ccountratio,sumrtime,feedtempletid,feedtempletname,dryweight,sumweight,priceweight,processtime,fpid,`date`,allowratio,TYPE,useinbartype)
  438. SELECT d2.pastureid,
  439. (SELECT id FROM downloadedplan d WHERE d.pastureid = d2.`pastureid` AND d.pid = downloadedplan.`pid` AND d.mydate =dateIN AND d.lpplantype = downloadedplan.`lpplantype` AND d.`projname` =downloadedplan.projname),
  440. d2.fbarid,d2.fname,d2.lastactualweight,d2.actualweight,d2.lweight,d2.actualweightminus,d2.sort,d2.havebuttom,d2.begintime,d2.begintag,
  441. d2.intime,d2.endtag,d2.buttontype,d2.optdevice,d2.flpid,d2.cowcount,d2.cowclassid,d2.cowclassname,d2.bigcowname,d2.pweight,d2.ccountratio,d2.sumrtime,d2.feedtempletid,
  442. d2.feedtempletname,d2.dryweight,d2.sumweight,d2.priceweight,d2.processtime,d2.fpid,dateIN,allowratio,d2.type,d2.useinbartype
  443. FROM downloadplandtl2 d2
  444. INNER JOIN downloadedplan
  445. ON downloadedplan.`pastureid` = d2.`pastureid` AND downloadedplan.`id` = d2.pid
  446. WHERE d2.pastureid= pastureidIN AND d2.`date` = dateTem;
  447. INSERT INTO downloadplandtl1_exec (id,pastureid,pid,flpid,lweight,optdevice,sort,fname,fcount,tmrloadname,feedallowratio,stirdelay,DATE,`type`)
  448. SELECT
  449. id,pastureid,pid,flpid,SUM(lweight),optdevice,sort,GROUP_CONCAT(fname),COUNT(fname),tmrloadname,feedallowratio,stirdelay,DATE,`type`
  450. FROM downloadplandtl1
  451. WHERE downloadplandtl1.`pastureid`= pastureidIN AND downloadplandtl1.date =dateIN
  452. GROUP BY pid,sort
  453. ORDER BY sort;
  454. /* INSERT INTO `downloadplandtl1_exec`(pastureid,pid,flpid,lastactualweight,actualweight,lweight,actualweightminus,begintime,intime,havebuttom,buttontype,optdevice,sort,fname,fcount,tmrloadname,feedallowratio,processtime,stirdelay,date)
  455. SELECT pastureid,pid,flpid,lastactualweight,actualweight,lweight,actualweightminus,begintime,intime,havebuttom,buttontype,optdevice,sort,fname,fcount,tmrloadname,feedallowratio,processtime,stirdelay,dateIN
  456. FROM downloadplandtl1_exec
  457. WHERE pastureid= pastureidIN AND `date` = dateTem;*/
  458. END WHILE;
  459. IF code1 = '00000' THEN
  460. COMMIT;
  461. SELECT
  462. 'success' msg;
  463. ELSE
  464. ROLLBACK;
  465. SELECT
  466. msg1 msg;
  467. END IF;
  468. END