sc.sql 23 KB

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