##############################
##@author:gong.chengbo      ##
##@createdate:2023-08-10    ##
##@for:用于归档在线产量明细     ##
##############################
drop procedure if exists UpdateSiteOnlineYield;
create
    definer = admin@`%` procedure UpdateSiteOnlineYield() comment '更新产量表存储过称'
BEGIN
replace into reportdata.siteonlinecrucibleyield(crystalbarno,MESmoname,eqpid,actualprodname,actualproddesc,outweight,outlength,outtime)
SELECT code,order_no,stove_no,product_no,product_name,weight,length,update_time
from mes_sync.mes_production mp
order by update_time;

##更新炉台信息
UPDATE reportdata.siteonlinecrucibleyield sy
inner join reportdata.equipment eq on sy.eqpid = eq.eqpid
set sy.area = eq.area,sy.area1= eq.area1,sy.eqpmodel=eq.eqpmodel
where sy.eqpmodel is null;

##更新坩埚信息
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN mes_sync.mes_crystal_order co
on sy.mesmoname = co.order_no
set sy.erpmoname = co.job_code,sy.moprodname = co.material_code,sy.moproddesc = co.material_name,sy.crusize = co.thermal_field
where sy.crusize is null;

##更新工单相关信息
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN reportdata.mostatus mo on (sy.mesmoname = mo.MESmoname)
set sy.crucible = mo.crucible,sy.crunum = mo.cruciblesn,sy.crudesc = mo.crucproddesc,sy.subprod = mo.subprod,sy.stovetime=mo.stovetime
where sy.crucible is null;

###更新折重系数
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN mes_sync.yz_mes_spec yms on(sy.subprod = yms.spec_code)
set sy.hair_rod_coefficient = yms.hair_rod_coefficient,sy.round_rod_coefficient = yms.round_rod_coefficient
where sy.hair_rod_coefficient is null or sy.round_rod_coefficient is null or sy.hair_square_rod_coefficient is null;

###更新折重系数——MES中未维护的使用自己维护的补充
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN reportdata.prod p on(sy.subprod = p.code)
set sy.hair_rod_coefficient = p.round,sy.hair_square_rod_coefficient = p.square
where sy.hair_rod_coefficient is null or sy.round_rod_coefficient is null or sy.hair_square_rod_coefficient is null;

###更新有效长度
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN (
SELECT
	production_code,sum(draw_length) qclength
 FROM mes_sync.mes_round_bar
where right(code,1) = '0'
group by production_code
)ps on (sy.crystalbarno = ps.production_code)
set sy.qclength = ps.qclength
where sy.qclength is null;

##更新QC时间及计产日期
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN mes_sync.mes_draw_line_task dlt on (sy.crystalbarno = dlt.hairy_rod_crystal_code and dlt.`status` = '3')
set sy.qctime = dlt.update_time,
sy.mfgdate = DATE_FORMAT(DATE_add(dlt.update_time,interval -510 MINUTE),'%Y-%m-%d')
where sy.qctime is null;

##更新QC重量
UPDATE reportdata.siteonlinecrucibleyield set qcweight = qclength/hair_rod_coefficient where qcweight is null;
UPDATE reportdata.siteonlinecrucibleyield set squareqcweight = qclength/hair_square_rod_coefficient where squareqcweight is null;

######建临时表处理两棒工序时间########################################
DROP TEMPORARY TABLE IF EXISTS crystalbarno;
CREATE TEMPORARY TABLE crystalbarno (
id int,
idd varchar(100),
eqpid VARCHAR(100),
crystalbarno varchar(200),
outtime datetime,
lastcrystalbarno VARCHAR(200),
lastouttime datetime
);
insert into crystalbarno(id,idd,eqpid,crystalbarno,outtime)
SELECT
	ROW_NUMBER() over(partition by stove_no order by UPDATE_time desc) id,
	CONCAT(stove_no,'-',ROW_NUMBER() over(partition by stove_no order by UPDATE_time desc)) idd,
	stove_no,code, UPDATE_time
	from mes_sync.mes_production;
UPDATE crystalbarno cy
INNER JOIN
(
	SELECT
	ROW_NUMBER() over(partition by stove_no order by UPDATE_time desc) -1 id,
	CONCAT(stove_no,'-',ROW_NUMBER() over(partition by stove_no order by UPDATE_time desc)-1) idd,
	stove_no,code, UPDATE_time
	from mes_sync.mes_production
) mp on (cy.idd = mp.idd)
set cy.lastouttime = mp.UPDATE_time,cy.lastcrystalbarno = mp.code;
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN crystalbarno cr on (sy.crystalbarno = cr.crystalbarno)
set sy.lastcrystalbarno = cr.lastcrystalbarno,sy.lastouttime = cr.lastouttime
where sy.lastcrystalbarno is null;
DROP TEMPORARY TABLE crystalbarno;
######删除临时表########################################


###更新运行时间
UPDATE reportdata.siteonlinecrucibleyield sy
set timeinterval = timestampdiff(second,lastouttime,outtime) / (24 * 60 * 60)
where timeinterval is null;

###更新首棒停炉时间过长的运行时间
UPDATE reportdata.siteonlinecrucibleyield sy
set timeinterval = timestampdiff(second,stovetime,outtime) / (24 * 60 * 60) + 8/24
where 1=1
and datediff(stovetime,lastouttime) >= 18/24
and crystalbarno like '%11N';

##处理坩埚厂商乱码
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN reportdata.adjustcruc ac on (sy.mesmoname = ac.mesmoname)
set sy.adjustcrucible = ac.crucible
where sy.adjustcrucible is null;
##根据坩埚
UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN reportdata.dict d on (sy.adjustcrucible = d.dictkey and d.cate = 'cruccode')
set sy.adjustcruciblecode = d.value;

UPDATE reportdata.siteonlinecrucibleyield sy
INNER JOIN reportdata.dict d on (sy.crucible = d.dictkey and d.cate = 'crucname')
set sy.adjustcruciblecode = d.value,sy.adjustcrucible = d.value1;

END;
