/***文档注释***********************************************
 * 作者               :                   龙文航
 * 创建日期      :                   2017.12.12
 * 描述               :                   原料库管理-外购入库管理
 * 注意事项      :                   后续修改从登陆获取班次班组
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Lms.Warehouse.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import market.Api.Qms.impl.QmsApiServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.IfRawMtrCoilChemMapper;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.service.IfRawMtrCoilChemService;
import market.Lms.Warehouse.vo.IfRawMtrCoilChem;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Qms.qcm.vo.QcmJhyElements;

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

@SuppressWarnings("rawtypes")
@RemoteService(path="IfRawMtrCoilChemService")
public class IfRawMtrCoilChemServiceImpl extends BaseServiceImpl<IfRawMtrCoilChem, String> implements IfRawMtrCoilChemService {

	/**
	 * 生成主键
	 * @return
	 * @throws Exception 
	 */
	public String createXGChemID() throws Exception
	{
		try 
		{
			String chemid="";
			List<HashMap> list = this.getMapper(IfRawMtrCoilChemMapper.class).getNo("CHXG"+(new SimpleDateFormat("yyyyMMdd").format(new Date())));
			chemid = (String) list.get(0).get("MAXNO");
			return chemid;
		} 
		catch (Exception ex) 
		{
			throw (ex);
		}
	}
	
	/**
	 * 生成主键
	 * @return
	 * @throws Exception 
	 */
	public String createWGChemID() throws Exception
	{
		try 
		{
			String chemid="";
			List<HashMap> list = this.getMapper(IfRawMtrCoilChemMapper.class).getNo("CHWG"+(new SimpleDateFormat("yyyyMMdd").format(new Date())));
			chemid = (String) list.get(0).get("MAXNO");
			return chemid;
		} 
		catch (Exception ex) 
		{
			throw (ex);
		}
	}
	
	/**
	 * 外购入库管理 成分结果ID编辑
	 * */
	public ResultModel doEditChemID(CommonPage<IfRawMtrCoilChem> cp)
	{
		ResultModel rm=new ResultModel();
		try
		{
			IfRawMtrCoilChem chem=cp.getObject();
			if(chem.getChemid()!=null&&chem.getChemid()!="")
			{
				//若流水号为空生成
				if("CHWG".equals(chem.getChemid()))
				{
					rm.setState("200");
					rm.setData(this.createWGChemID());
				}
				else 
				{
					//若不为空，判断是否存在
					List<IfRawMtrCoilChem> list=this.getMapper(IfRawMtrCoilChemMapper.class).find(chem, 0, 0);
					//存在返回成分项数据
					if(list!=null&&list.size()>0)
					{
						rm.setState("600");
						rm.setData(list);
					}
					//不存在，生成
					else 
					{
						rm.setState("200");
						rm.setData(this.createWGChemID());
					}
				}
			}
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，成分结果ID编辑失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 外购入库管理 入库确认(成分)
	 * */
	public ResultModel doConfirmOut(CommonPage<IfRawMtrCoilChem> cp)
	{
		ResultModel rm=new ResultModel();
		try
		{
			IfRawMtrCoilChem chem=cp.getObject();
			if(chem.getChemid()!=null&&chem.getChemid()!="")
			{
				YdmProductDetail ypd=new YdmProductDetail();
				String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
				ypd.setChemid(chem.getChemid());
				ypd.setState("5");
				List<YdmProductDetail> list=this.getMapper(YdmProductDetailMapper.class).find(ypd, 0, 0);
				if(list!=null&&list.size()>0)
				{				
				}
				else 
				{
					IfRawMtrCoilChem chem2=new IfRawMtrCoilChem();
					chem2.setChemid(chem.getChemid());
					List<IfRawMtrCoilChem> lChem=this.getMapper(IfRawMtrCoilChemMapper.class).find(chem2, 0, 0);
					if(lChem!=null&&lChem.size()>0)
					{
						//删除成分实绩
						this.getMapper(IfRawMtrCoilChemMapper.class).doDeleteByChemID(chem.getChemid());
					}
					//插入成分实绩
					IfRawMtrCoilChem chem1=new IfRawMtrCoilChem();
					chem1.setCreateOpr(username);
					chem1.setCreateTime(new Date());
					chem1.setChemid(chem.getChemid());
					chem1.setSource("1");
					if(chem.getC()!=null&&chem.getC()!="")
					{
						chem1.setChemCode("C");
						chem1.setChemValue(Integer.valueOf(chem.getC()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getSi()!=null&&chem.getSi()!="")
					{
						chem1.setChemCode("SI");
						chem1.setChemValue(Integer.valueOf(chem.getSi()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getMn()!=null&&chem.getMn()!="")
					{
						chem1.setChemCode("MN");
						chem1.setChemValue(Integer.valueOf(chem.getMn()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getP()!=null&&chem.getP()!="")
					{
						chem1.setChemCode("P");
						chem1.setChemValue(Integer.valueOf(chem.getP()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getS()!=null&&chem.getS()!="")
					{
						chem1.setChemCode("S");
						chem1.setChemValue(Integer.valueOf(chem.getS()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getNi()!=null&&chem.getNi()!="")
					{
						chem1.setChemCode("NI");
						chem1.setChemValue(Integer.valueOf(chem.getNi()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCr()!=null&&chem.getCr()!="")
					{
						chem1.setChemCode("CR");
						chem1.setChemValue(Integer.valueOf(chem.getCr()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCu()!=null&&chem.getCu()!="")
					{
						chem1.setChemCode("CU");
						chem1.setChemValue(Integer.valueOf(chem.getCu()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getAsn()!=null&&chem.getAsn()!="")
					{
						chem1.setChemCode("ASN");
						chem1.setChemValue(Integer.valueOf(chem.getAsn()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getTi()!=null&&chem.getTi()!="")
					{
						chem1.setChemCode("TI");
						chem1.setChemValue(Integer.valueOf(chem.getTi()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getV()!=null&&chem.getV()!="")
					{
						chem1.setChemCode("V");
						chem1.setChemValue(Integer.valueOf(chem.getV()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getMo()!=null&&chem.getMo()!="")
					{
						chem1.setChemCode("MO");
						chem1.setChemValue(Integer.valueOf(chem.getMo()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getB()!=null&&chem.getB()!="")
					{
						chem1.setChemCode("B");
						chem1.setChemValue(Integer.valueOf(chem.getB()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getW()!=null&&chem.getW()!="")
					{
						chem1.setChemCode("W");
						chem1.setChemValue(Integer.valueOf(chem.getW()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getAl()!=null&&chem.getAl()!="")
					{
						chem1.setChemCode("AL");
						chem1.setChemValue(Integer.valueOf(chem.getAl()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getZn()!=null&&chem.getZn()!="")
					{
						chem1.setChemCode("ZN");
						chem1.setChemValue(Integer.valueOf(chem.getZn()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getPb()!=null&&chem.getPb()!="")
					{
						chem1.setChemCode("PB");
						chem1.setChemValue(Integer.valueOf(chem.getPb()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getSn()!=null&&chem.getSn()!="")
					{
						chem1.setChemCode("SN");
						chem1.setChemValue(Integer.valueOf(chem.getSn()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCa()!=null&&chem.getCa()!="")
					{
						chem1.setChemCode("CA");
						chem1.setChemValue(Integer.valueOf(chem.getCa()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getLa()!=null&&chem.getLa()!="")
					{
						chem1.setChemCode("LA");
						chem1.setChemValue(Integer.valueOf(chem.getLa()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getSb()!=null&&chem.getSb()!="")
					{
						chem1.setChemCode("SB");
						chem1.setChemValue(Integer.valueOf(chem.getSb()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCe()!=null&&chem.getCe()!="")
					{
						chem1.setChemCode("CE");
						chem1.setChemValue(Integer.valueOf(chem.getCe()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getSe()!=null&&chem.getSe()!="")
					{
						chem1.setChemCode("SE");
						chem1.setChemValue(Integer.valueOf(chem.getSe()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getMg()!=null&&chem.getMg()!="")
					{
						chem1.setChemCode("MG");
						chem1.setChemValue(Integer.valueOf(chem.getMg()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getNb()!=null&&chem.getNb()!="")
					{
						chem1.setChemCode("NB");
						chem1.setChemValue(Integer.valueOf(chem.getNb()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCd()!=null&&chem.getCd()!="")
					{
						chem1.setChemCode("CD");
						chem1.setChemValue(Integer.valueOf(chem.getCd()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getBi()!=null&&chem.getBi()!="")
					{
						chem1.setChemCode("BI");
						chem1.setChemValue(Integer.valueOf(chem.getBi()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getN()!=null&&chem.getN()!="")
					{
						chem1.setChemCode("N");
						chem1.setChemValue(Integer.valueOf(chem.getN()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getH()!=null&&chem.getH()!="")
					{
						chem1.setChemCode("H");
						chem1.setChemValue(Integer.valueOf(chem.getH()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getO()!=null&&chem.getO()!="")
					{
						chem1.setChemCode("O");
						chem1.setChemValue(Integer.valueOf(chem.getO()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getPe()!=null&&chem.getPe()!="")
					{
						chem1.setChemCode("PE");
						chem1.setChemValue(Integer.valueOf(chem.getPe()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getAlt()!=null&&chem.getAlt()!="")
					{
						chem1.setChemCode("ALT");
						chem1.setChemValue(Integer.valueOf(chem.getAlt()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getAls()!=null&&chem.getAls()!="")
					{
						chem1.setChemCode("ALS");
						chem1.setChemValue(Integer.valueOf(chem.getAls()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getAlins()!=null&&chem.getAlins()!="")
					{
						chem1.setChemCode("ALINS");
						chem1.setChemValue(Integer.valueOf(chem.getAlins()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getBs()!=null&&chem.getBs()!="")
					{
						chem1.setChemCode("BS");
						chem1.setChemValue(Integer.valueOf(chem.getBs()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getBn()!=null&&chem.getBn()!="")
					{
						chem1.setChemCode("BN");
						chem1.setChemValue(Integer.valueOf(chem.getBn()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getMgo()!=null&&chem.getMgo()!="")
					{
						chem1.setChemCode("MGO");
						chem1.setChemValue(Integer.valueOf(chem.getMgo()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getSio2()!=null&&chem.getSio2()!="")
					{
						chem1.setChemCode("SIO2");
						chem1.setChemValue(Integer.valueOf(chem.getSio2()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getAl2o3()!=null&&chem.getAl2o3()!="")
					{
						chem1.setChemCode("AL2O3");
						chem1.setChemValue(Integer.valueOf(chem.getAl2o3()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getTfeo()!=null&&chem.getTfeo()!="")
					{
						chem1.setChemCode("TFEO");
						chem1.setChemValue(Integer.valueOf(chem.getTfeo()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getFe2o3()!=null&&chem.getFe2o3()!="")
					{
						chem1.setChemCode("FE2O3");
						chem1.setChemValue(Integer.valueOf(chem.getFe2o3()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getH2o()!=null&&chem.getH2o()!="")
					{
						chem1.setChemCode("H2O");
						chem1.setChemValue(Integer.valueOf(chem.getH2o()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCaf2()!=null&&chem.getCaf2()!="")
					{
						chem1.setChemCode("CAF2");
						chem1.setChemValue(Integer.valueOf(chem.getCaf2()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getP2o5()!=null&&chem.getP2o5()!="")
					{
						chem1.setChemCode("P2O5");
						chem1.setChemValue(Integer.valueOf(chem.getP2o5()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getTfe()!=null&&chem.getTfe()!="")
					{
						chem1.setChemCode("TFE");
						chem1.setChemValue(Integer.valueOf(chem.getTfe()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCr2o3()!=null&&chem.getCr2o3()!="")
					{
						chem1.setChemCode("CR2O3");
						chem1.setChemValue(Integer.valueOf(chem.getCr2o3()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getTio2()!=null&&chem.getTio2()!="")
					{
						chem1.setChemCode("TIO2");
						chem1.setChemValue(Integer.valueOf(chem.getTio2()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getMno()!=null&&chem.getMno()!="")
					{
						chem1.setChemCode("MNO");
						chem1.setChemValue(Integer.valueOf(chem.getMno()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getV2o5()!=null&&chem.getV2o5()!="")
					{
						chem1.setChemCode("V2O5");
						chem1.setChemValue(Integer.valueOf(chem.getV2o5()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getFeo()!=null&&chem.getFeo()!="")
					{
						chem1.setChemCode("FEO");
						chem1.setChemValue(Integer.valueOf(chem.getFeo()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
					if(chem.getCao()!=null&&chem.getCao()!="")
					{
						chem1.setChemCode("CAO");
						chem1.setChemValue(Integer.valueOf(chem.getCao()));
						this.getMapper(IfRawMtrCoilChemMapper.class).insert(chem1);
					}
				}
				//调用质量成分接口
				CommonPage<QcmJhyElements> q=new CommonPage<QcmJhyElements>();
				QcmJhyElements qcm=new QcmJhyElements();
				qcm.setAssay_no(chem.getChemid());
				q.setObject(qcm);
				this.getBean(QmsApiServiceImpl.class).GetOutsideChemResult(q);
			}
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，入库确认成分失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 成分查询
	 */
	public ResultModel doQueryChemList(CommonPage<IfRawMtrCoilChem> sum){
		  ResultModel rm = new ResultModel();
	  	if(sum.getMemo1()==null||"".equals(sum.getMemo1())){
	  		rm.setState("500");
	  		rm.setMsgInfo("请传入原料卷号");
	  		return rm;
	  	  }
	  	  try {
	  		    rm.setData(this.getMapper(IfRawMtrCoilChemMapper.class).doQueryChemList(sum.getMemo1()));
				rm.setState("200");
			} catch (Exception e) {
				// TODO: handle exception
				rm.setState("500");
				e.getStackTrace();
				rm.setMsgInfo("查询失败！"+e.getMessage());
			}
	  	return rm;
	  }
}
