shengjicreatedownloadedplanDone.sql 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. CREATE DEFINER=`root`@`%` PROCEDURE `createdownloadedplanDone`(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. -- 刷新数据(主要是各个模块的名称保持和基础数据中一致)
  28. CALL refreshdata(pastureidIN);
  29. -- 删除未执行的计划,已执行的保留
  30. DELETE d,d1,d2,d1e FROM downloadedplan d
  31. LEFT JOIN `downloadplandtl1` d1 ON d1.pastureid = d.pastureid AND d1.pid = d.id
  32. LEFT JOIN `downloadplandtl2` d2 ON d2.pastureid = d.pastureid AND d2.pid = d.id
  33. LEFT JOIN `downloadplandtl1_exec` d1e ON d1e.pastureid = d.pastureid AND d1e.pid = d.id
  34. WHERE d.pastureid = pastureidIN AND d.id IN (SELECT * FROM ( SELECT id FROM downloadedplan dv WHERE dv.`pastureid` =pastureidIN
  35. AND dv.`havebutton`=0 AND dv.`mydate` = dateIN )tem);
  36. -- 将配方、栏舍配方、撒料计划、预混计划、剩料计划复制到date历史表中
  37. CALL createLPPdate(dateIN,pastureidIN,'');
  38. CALL creatFTdate(dateIN,pastureidIN,'');
  39. CALL creatFPdate(dateIN,pastureidIN,'');
  40. CALL createPreReHis(dateIN,pastureidIN,'');
  41. SELECT 2;
  42. -- 读取基础数据中默认参数配置
  43. SELECT sysopt.inforvalue INTO timesOs FROM sysopt
  44. WHERE sysopt.`pastureid`= pastureidIN AND sysopt.`inforname`='times';
  45. SELECT sysopt.inforvalue INTO issmallmaterial FROM sysopt
  46. WHERE sysopt.`pastureid`= pastureidIN AND sysopt.`inforname`='isSmallMaterial';
  47. SELECT sysopt.inforvalue INTO isEnableRemainFeed FROM sysopt
  48. WHERE sysopt.`pastureid`= pastureidIN AND sysopt.`inforname`='isEnableRemainFeed';
  49. ################################## 撒料计划
  50. ### 混料与撒料是相同的tmr车
  51. INSERT INTO downloadedplan(pastureid,mydate,projname,datacaptureno,
  52. tmrid,tmrtname,times,sort,remark,itemnum,outitems,pid,tempid,
  53. templetname,lweight,lpplantype,plantime,feedpname,feedpcount,tversion)
  54. SELECT
  55. lpplan.pastureid,
  56. dateIN,
  57. -- CONCAT(times,'.',
  58. -- LPAD(lpplan.sort ,3,'0'),'2'
  59. -- ),
  60. CONCAT(times,'.',
  61. LPAD(lpplan.sort ,3,'0'),'0'
  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(ccount) from feedp where barid in(SELECT barid FROM lpplandtl1 WHERE lpplandtl1.pastureid = pastureidIN AND lppid= lpplan.`id`) and feedp.pastureid = pastureidIN ),
  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'),"333"
  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(ccount) from feedp where barid in(SELECT barid FROM lpplandtl1 WHERE lpplandtl1.pastureid = pastureidIN AND lppid= lpplan.`id`) and feedp.pastureid = pastureidIN ),
  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 lpplandtl1.tmrid = dv.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.`tmrid` =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.`tmrid` =lpplandtl1.`tmrid` and d.havebutton = 1 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. group by lpplan.id 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`
  249. -- AND d.pid = lpplandtl1.`lppid` AND d.mydate =dateIN AND lpplantype = IF(lpplandtl1.`tmrid`=lpplan.`tmrid`,0,2)AND lpplandtl1.`tmrid`=d.`tmrid`),
  250. -- lpplandtl1.barid,lpplandtl1.`barname`,lpplandtl1.lweight,
  251. -- IF(lpplandtl1.`fttype`=1,a+100,b+150),
  252. -- lpplandtl1.`lppid`,ft.id ftid,ft.`tname`,
  253. -- lpplandtl1.`fpdid`,dateIN,
  254. -- (SELECT allowratio FROM bar WHERE bar.`pastureid` =lpplandtl1.`pastureid` AND bar.id = lpplandtl1.`barid` LIMIT 1),
  255. -- lpplandtl1.`tmrid`
  256. -- FROM `lpplandtl1`
  257. -- INNER JOIN lpplan ON
  258. -- lpplandtl1.`pastureid` = lpplan.`pastureid` AND lpplandtl1.`lppid` = lpplan.id
  259. -- INNER JOIN `fpdetail` fd
  260. -- ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = pastureidIN
  261. -- INNER JOIN feedtemplet ft
  262. -- ON IF(lpplandtl1.`fttype`=1,fd.`ptid` = ft.id,fd.`ptsid` = ft.id ) AND ft.pastureid = pastureidIN
  263. -- WHERE lpplandtl1.pastureid = pastureidIN AND lpplandtl1.lppid=lpplanids AND lpplandtl1.`lweight`>0
  264. -- AND (SELECT havebutton FROM downloadedplan d WHERE d.pastureid = lpplandtl1.`pastureid` AND d.pid = lpplandtl1.`lppid`
  265. -- AND d.mydate =dateIN AND lpplantype = IF(lpplandtl1.`tmrid`=lpplan.`tmrid`,0,2)AND lpplandtl1.`tmrid`=d.`tmrid`) != 1
  266. --
  267. -- ORDER BY lpplandtl1.sort DESC LIMIT 1 OFFSET amount;
  268. INSERT INTO downloadplandtl2
  269. (pastureid,pid,fbarid,fname,lweight,sort,flpid,
  270. feedtempletid,feedtempletname,fpid,`date`,allowratio,optdevice)
  271. SELECT lpplandtl1.`pastureid`,(SELECT id FROM downloadedplan d WHERE d.pastureid = lpplandtl1.`pastureid` AND d.pid = lpplandtl1.`lppid` AND d.mydate =dateIN
  272. AND lpplantype = IF(lpplandtl1.`tmrid`=lpplan.`tmrid`,0,2)AND lpplandtl1.`tmrid`=d.`tmrid`),
  273. lpplandtl1.barid,lpplandtl1.`barname`,lpplandtl1.lweight,
  274. IF(lpplandtl1.`fttype`=1,a+100,b+150),
  275. lpplandtl1.`lppid`,ft.id ftid,ft.`tname`,
  276. lpplandtl1.`fpdid`,dateIN,
  277. (SELECT allowratio FROM bar WHERE bar.`pastureid` =lpplandtl1.`pastureid` AND bar.id = lpplandtl1.`barid` LIMIT 1),
  278. lpplandtl1.`tmrid`
  279. FROM `lpplandtl1`
  280. INNER JOIN lpplan ON
  281. lpplandtl1.`pastureid` = lpplan.`pastureid` AND lpplandtl1.`lppid` = lpplan.id
  282. INNER JOIN `fpdetail` fd
  283. ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = pastureidIN
  284. INNER JOIN feedtemplet ft
  285. ON IF(lpplandtl1.`fttype`=1,fd.`ptid` = ft.id,fd.`ptsid` = ft.id ) AND ft.pastureid = pastureidIN
  286. WHERE lpplandtl1.pastureid = pastureidIN AND lpplandtl1.lppid=lpplanids AND lpplandtl1.`lweight`>0
  287. 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
  288. ORDER BY lpplandtl1.sort DESC LIMIT 1 OFFSET amount;
  289. INSERT INTO downloadplandtl2
  290. (pastureid,pid,fbarid,fname,lweight,sort,flpid,
  291. feedtempletid,feedtempletname,fpid,`date`,allowratio,optdevice,`type`)
  292. 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),
  293. lpplandtl1.`tmrid`,lpplandtl1.`tmrname`,SUM(lpplandtl1.lweight),
  294. IF(lpplandtl1.`fttype`=1,a+100,b+150),
  295. lpplandtl1.`lppid`,ft.id ftid,ft.`tname`,
  296. lpplandtl1.`fpdid`,dateIN,
  297. (SELECT allowratio FROM bar WHERE bar.`pastureid` =lpplandtl1.`pastureid` AND bar.id = lpplandtl1.`barid` LIMIT 1),
  298. lpplan.`tmrid`,
  299. 1
  300. FROM `lpplandtl1`
  301. INNER JOIN lpplan ON
  302. lpplandtl1.`pastureid` = lpplan.`pastureid` AND lpplandtl1.`lppid` = lpplan.id
  303. INNER JOIN `fpdetail` fd
  304. ON fd.`id` = lpplandtl1.fpdid AND fd.pastureid = pastureidIN
  305. INNER JOIN feedtemplet ft
  306. ON IF(lpplandtl1.`fttype`=1,fd.`ptid` = ft.id,fd.`ptsid` = ft.id ) AND ft.pastureid = pastureidIN
  307. WHERE lpplandtl1.pastureid = pastureidIN AND lpplandtl1.lppid=lpplanids AND lpplandtl1.`tmrid` <> lpplan.`tmrid` AND lpplandtl1.`lweight`>0
  308. 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
  309. 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
  310. GROUP BY lpplan.`id`,lpplandtl1.`tmrid`
  311. ORDER BY lpplandtl1.sort DESC LIMIT 1 OFFSET amount;
  312. END WHILE;
  313. END WHILE;
  314. IF issmallmaterial = '1' # 是否开启小料
  315. THEN
  316. ## 开启小料后,会在原来的基础上再生成一个主计划
  317. CALL createsmallmaterialplan(dateIN,timesOs,pastureidIN);
  318. END IF ;
  319. -- 设置为撒料-混料
  320. UPDATE `downloadedplan` d
  321. SET d.`lpplantype` = 1
  322. WHERE d.pastureid = pastureidIN AND
  323. (SELECT COUNT(*) FROM `downloadplandtl2` d2 WHERE d2.pastureid = d.pastureid AND d2.pid = d.id AND d2.`type` = 1 ) > 0
  324. AND d.lpplantype = 0 AND d.mydate = dateIN ;
  325. SELECT dateIN INTO dateTem;
  326. INSERT INTO downloadedplan(pastureid,mydate,projname,datacaptureno,
  327. tmrid,tmrtname,times,sort,remark,itemnum,outitems,pid,tempid,
  328. templetname,lweight,lpplantype,plantime,feedpname,feedpcount,tversion)
  329. SELECT
  330. premixplan.pastureid,dateIN,
  331. IF(premixplan.lppcode IS NULL, CONCAT(times,'.0000'),
  332. CONCAT(times,'.',
  333. LPAD(SUBSTRING_INDEX( premixplan.lppcode, ' ',1 ),3,'0'),'01',LPAD(premixplan.sort,3,'0')
  334. )
  335. ),
  336. tmr.`datacaptureno`,
  337. tmrid,tmrcode,
  338. premixplan.times,
  339. premixplan.sort,
  340. '预混计划',
  341. ###
  342. ( SELECT COUNT(*) FROM
  343. ftdetail ftd
  344. WHERE ftd.`ftid` = premixplan.`ftid` AND ftd.`pastureid`=premixplan.pastureid),
  345. 0 ,
  346. ###
  347. premixplan.id,
  348. premixplan.`ftid`,
  349. premixplan.`ftname`,
  350. premixplan.`operateweight`,
  351. 4,
  352. premixplan.`ptime`,
  353. '',
  354. 0,
  355. (SELECT feedtemplet.`version` FROM `feedtemplet` WHERE feedtemplet.pastureid = premixplan.`pastureid` AND feedtemplet.`id` =premixplan.`ftid` )
  356. FROM `premixplan`
  357. INNER JOIN tmr ON
  358. tmr.pastureid = premixplan.`pastureid` AND tmr.id = premixplan.`tmrid`
  359. WHERE premixplan.pastureid =pastureidIN AND premixplan.times <= timesOs AND premixplan.enable=1
  360. AND premixplan.id NOT IN (SELECT * FROM
  361. (SELECT pid FROM downloadedplan d
  362. WHERE d.pastureid = pastureidIN AND d.mydate = dateIN
  363. AND
  364. (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
  365. )tem
  366. GROUP BY pid)
  367. ;
  368. INSERT INTO downloadplandtl1(pastureid,pid,flpid,fid,lweight,sort,feedcode,fname,
  369. feedallowratio,stirdelay,fweight,tmrloadname,`date`,dryfeed,pricefeed,optdevice)
  370. SELECT
  371. premixplan.pastureid,
  372. (SELECT id FROM downloadedplan d WHERE d.pastureid = premixplan.`pastureid` AND d.pid = premixplan.`id` AND d.mydate =dateIN AND lpplantype = 4 ),
  373. premixplan.`id`,
  374. ftd.fid,
  375. ftd.fweight/(SELECT SUM(fweight) FROM ftdetail WHERE ftid =ft.id AND pastureid=ft.pastureid )*premixplan.`operateweight`,
  376. ftd.sort,
  377. f.feedcode,
  378. IFNULL(f.fname,ftd.fname),
  379. f.allowratio,
  380. ftd.autosecond,
  381. ftd.fweight,
  382. ftd.feedgroup,
  383. dateIN,
  384. fu.dry,
  385. f.uprice,
  386. premixplan.`tmrid`
  387. FROM `premixplan`
  388. INNER JOIN feedtemplet ft
  389. ON premixplan.`ftid` =ft.id AND ft.pastureid = premixplan.pastureid
  390. LEFT JOIN ftdetail ftd
  391. ON ftd.`ftid` = ft.`id` AND ftd.pastureid = premixplan.pastureid
  392. LEFT JOIN feed f
  393. ON ftd.`fid` = f.`id` AND f.pastureid = premixplan.pastureid
  394. LEFT JOIN feednur fu
  395. ON fu.fid=f.id AND fu.pastureid= premixplan.pastureid
  396. WHERE premixplan.pastureid = pastureidIN AND premixplan.times <= timesOs AND premixplan.enable=1
  397. 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;
  398. -- 预混计划一次生成之后会都禁用掉
  399. UPDATE premixplan
  400. SET `enable` = 0
  401. WHERE premixplan.pastureid =pastureidIN AND premixplan.enable=1
  402. AND premixplan.id NOT IN (SELECT * FROM
  403. (SELECT pid FROM downloadedplan d
  404. WHERE d.pastureid = pastureidIN AND d.mydate = dateIN
  405. AND
  406. (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
  407. )tem
  408. GROUP BY pid)
  409. ;
  410. #################剩料计划
  411. IF isEnableRemainFeed = '1' # 是否开启剩料
  412. THEN
  413. -- 日执行主表
  414. INSERT INTO downloadedplan(pastureid,mydate,projname,datacaptureno,
  415. tmrid,tmrtname,times,sort,remark,itemnum,outitems,pid,tempid,
  416. templetname,lweight,lpplantype,plantime,feedpname,feedpcount)
  417. SELECT
  418. remainplan.pastureid,
  419. dateIN,
  420. IF(remainplan.lppcode IS NULL, CONCAT(times,'.0001'),
  421. CONCAT(times,'.',
  422. LPAD(SUBSTRING_INDEX( remainplan.lppcode, ' ',1 ),3,'0'),'31',LPAD(remainplan.sort,3,'0')
  423. )
  424. ),
  425. tmr.`datacaptureno`,
  426. tmrid,tmrcode,
  427. remainplan.times,
  428. remainplan.sort,
  429. '剩料计划',
  430. 0,
  431. 1,
  432. remainplan.id,
  433. NULL,
  434. '',
  435. 0,
  436. 3,
  437. remainplan.`rtime`,
  438. bar,
  439. IFNULL((SELECT cowsum FROM barmilk WHERE barmilk.pastureid = remainplan.`pastureid` AND barmilk.`barid`= remainplan.`barid`
  440. ORDER BY barmilk.productdate DESC LIMIT 1
  441. ),0)
  442. FROM `remainplan`
  443. INNER JOIN tmr ON
  444. tmr.pastureid = remainplan.`pastureid` AND tmr.id = remainplan.`tmrid`
  445. WHERE remainplan.pastureid =pastureidIN 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. -- 日执行子表2(剩料只有子表2)
  454. INSERT INTO downloadplandtl2(pastureid,pid,fbarid,fname,lweight,sort,flpid,useinbartype,fpid,`date`,allowratio,optdevice)
  455. SELECT remainplan.`pastureid`,
  456. (SELECT id FROM downloadedplan d WHERE d.pastureid = remainplan.`pastureid` AND d.pid = remainplan.`id` AND d.mydate =dateIN AND lpplantype = 3),
  457. rpd.barid,
  458. rpd.`bar`,0,
  459. rpd.sort,remainplan.`id`,
  460. IF(treatmethod='继续饲喂',2, rpd.type),
  461. remainplan.`id`,dateIN,
  462. (SELECT allowratio FROM bar WHERE bar.`pastureid` =remainplan.`pastureid` AND bar.id = rpd.`barid` LIMIT 1),
  463. remainplan.`tmrid`
  464. FROM `remainplan`
  465. INNER JOIN rpdetail rpd
  466. ON rpd.pastureid =remainplan.pastureid AND rpd.bigid = remainplan.id
  467. WHERE remainplan.pastureid = pastureidIN
  468. AND remainplan.times <= timesOs AND remainplan.enable=1
  469. AND remainplan.id NOT IN (SELECT * FROM
  470. (SELECT pid FROM downloadedplan d
  471. WHERE d.pastureid = pastureidIN AND d.mydate = dateIN
  472. AND
  473. (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
  474. )tem
  475. GROUP BY pid);
  476. END IF ;
  477. ############################################################################################################################################
  478. UPDATE downloadplandtl2
  479. SET lweighthis = lweight,
  480. cowclassname = (SELECT `cowclass` FROM `barmilk` WHERE pastureid = downloadplandtl2.`pastureid` AND `barid` = downloadplandtl2.`fbarid` ORDER BY `productdate` DESC LIMIT 1 ),
  481. cowclassid = (SELECT `cowclassid` FROM `barmilk` WHERE pastureid = downloadplandtl2.`pastureid` AND `barid` = downloadplandtl2.`fbarid` ORDER BY `productdate` DESC LIMIT 1)
  482. WHERE pastureid = pastureidIN AND DATE = dateIN;
  483. SELECT 5;
  484. -- 将子表1更新到 1e
  485. INSERT INTO downloadplandtl1_exec (id,pastureid,pid,flpid,lweight,optdevice,sort,fname,fcount,tmrloadname,feedallowratio,stirdelay,DATE,TYPE)
  486. SELECT
  487. id,pastureid,pid,flpid,SUM(lweight),optdevice,sort,GROUP_CONCAT(fname),COUNT(fname),tmrloadname,feedallowratio,stirdelay,DATE,TYPE
  488. FROM downloadplandtl1
  489. WHERE downloadplandtl1.`pastureid`= pastureidIN AND downloadplandtl1.date =dateIN
  490. GROUP BY pid,sort
  491. ORDER BY sort ON DUPLICATE KEY UPDATE sort = sort;
  492. -- 重新将计划编号生成
  493. CALL updatedownloadprojname(pastureidIN,dateIN); #
  494. UPDATE downloadedplan SET
  495. itemnum = (SELECT COUNT(*) FROM `downloadplandtl1` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`),
  496. outitems = (SELECT COUNT(*) FROM `downloadplandtl2` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`),
  497. lweight =IFNULL(IFNULL((SELECT SUM(lweight) FROM `downloadplandtl2` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`),
  498. (SELECT SUM(lweight) FROM `downloadplandtl1` WHERE pastureid = downloadedplan.`pastureid` AND pid =downloadedplan.`id`)),0)
  499. WHERE pastureid = pastureidIN AND mydate = dateIN;
  500. IF code1 = '00000' THEN
  501. COMMIT;
  502. SELECT
  503. 'success' msg;
  504. ELSE
  505. ROLLBACK;
  506. SELECT
  507. msg1 msg;
  508. END IF;
  509. END