shengjiinsertFitHistory.sql 23 KB

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