/***文档注释***********************************************
 * 作者            ：
 * 创建日期     ：2019-03-26
 * 描述            ：可逆轧机出口卷线上称重实绩
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.Rolling.Performance.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import market.Mes.Rolling.Performance.mapper.MesRcmDMapper;
import market.Mes.Rolling.Performance.mapper.MesRcmWgtMapper;
import market.Mes.Rolling.Performance.service.MesRcmWgtService;
import market.Mes.Rolling.Performance.vo.IfRoll01L2Pdo;
import market.Mes.Rolling.Performance.vo.MesRcmD;
import market.Mes.Rolling.Performance.vo.MesRcmM;
import market.Mes.Rolling.Performance.vo.MesRcmWgt;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@Service("MesRcmWgtService")
@RemoteService(path="MesRcmWgtService")
public class MesRcmWgtServiceImpl extends BaseServiceImpl<MesRcmWgt,Object> implements MesRcmWgtService {
	
	/**
	  * MES系统的二级称重数据自动获取
	  * @param cp
	  * @return
	  */
	@Override
	public void doUpdateMesRcmDWgt() {
		String now = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		//SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<IfRoll01L2Pdo> pdoList = this.getMapper(MesRcmWgtMapper.class).doQueryMesRcmDWgt();
		if (CollectionUtils.isNotEmpty(pdoList)) {
			for (IfRoll01L2Pdo pdo : pdoList) {
				//获取轧机主信息
				MesRcmM mrm = this.getMapper(MesRcmWgtMapper.class).doQueryMesRcmM(pdo.getPlan_no(),pdo.getEntry_coil_no());
				if (mrm==null || "1".equals(mrm.getHand_over_status())) {
					continue; //已经交工的不能调整重量
				}
				List<MesRcmD> mrdList = this.getMapper(MesRcmWgtMapper.class).doQueryMesRcmD(mrm.getProcess_no());
				if (CollectionUtils.isEmpty(mrdList) || mrdList.size() > 1) {
					continue; //分卷的轧机子卷暂时不进行分配
				}
				//20201127 如果手动保存了重量，就不自动匹配2级的重量
				List<MesRcmWgt> mrwList = this.getMapper(MesRcmWgtMapper.class).doQueryMesRcmWgtCount(mrm.getProcess_no());
				if (CollectionUtils.isNotEmpty(mrwList)) {
					continue;//已经手动保存重量的不要继续修改，防止覆盖
				}
				
				for (MesRcmD mrd : mrdList) {
					mrd.setCoil_act_wgt(Double.parseDouble(pdo.getExit_weight()));					
					mrd.setCoil_thr_wgt(Double.parseDouble(pdo.getExit_weight()));
					mrd.setAct_thk(Double.parseDouble(pdo.getExit_thick())/1000);
					mrd.setAct_wdt(Double.parseDouble(pdo.getExit_width()));
					//mrd.setAct_len(Double.parseDouble(pdo.getExit_length())*1000);
					mrd.setAct_inn_dia(Double.parseDouble(pdo.getInner_diameter()));
					//mrd.setAct_out_dia(Double.parseDouble(pdo.getOuter_diameter()));
					mrd.setCoil_wgt_time(pdo.getMsg_time());
					mrd.setMemo(now+"自动获取轧机2级称重信息");
					this.getMapper(MesRcmDMapper.class).update(mrd);
				}
			}
		}
	}
	 
}
