##############################
##@author:gong.chengbo      ##
##@createdate:2023-08-10    ##
##@for:用于归档工单配料明细     ##
##############################
drop procedure if exists UpdateAllfeedingdetail;
create
    definer = admin@`%` procedure UpdateAllfeedingdetail()
BEGIN
#配料明细没有主键,每次开始前先删除原有的
DELETE from reportdata.allfeedingdetail;
replace into reportdata.allfeedingdetail(`month`,mfgdate,mesmoname,eqpid,moprodname,moproddesc,
	subprod,rx,stovetype,stovetypedesc,stovetime,prodname,proddesc,material_type,factory,serial_no,weight,
	cate1,cate2,cate3,material_level,type,area,area1,eqpmodel,feeding_task_code)
SELECT
	CONCAT('M',DATE_FORMAT(DATE_add(ft.update_time,interval -510 MINUTE),'%m')) `month`,
	DATE_FORMAT(DATE_add(ft.update_time,interval -510 MINUTE),'%Y-%m-%d') mfgdate,
	ft.order_no mesmoname,
	ft.stove_no eqpid,
	ft.product_no moprodname,
	ft.product_name moproddesc,
	case when LOCATE('自产',ft.product_name) >0 then mid(ft.product_name,LOCATE('自产',ft.product_name)+3,6)
		when LOCATE('代工',ft.product_name) >0 then mid(ft.product_name,LOCATE('代工',ft.product_name)+3,6) end subprod,
	ft.feeding_times rx,
	ft.type stovetype,
	case when ft.type = '1' then '初装'
		when ft.type = '2' then '二加' end stovetypedesc,
	ft.update_time stovetime,
	fd.material_no prodname,
	fd.material_name proddesc,
	fd.material_type,
	fd.factory,
	fd.batch_no serial_no,
	fd.weight,
	p.cate1,
	p.cate2,
	p.cate3,
	p.`level`,
	p.type,
	eq.area,
	eq.area1,
	eq.eqpmodel,
	ft.feeding_task_code
	from  mes_sync.mes_furnace_task ft
	right join mes_sync.mes_feeding_detail fd on(ft.feeding_task_id = fd.feeding_task_id and fd.material_type = '2')
	left join reportdata.product p on (fd.material_no = p.prodname)
	left join reportdata.Equipment eq on (ft.stove_no = eq.eqpid)
	where ft.`status` = '3';
END;

