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

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

import market.Api.Mes.impl.MesApiServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.IfRawMtrCoilMMapper;
import market.Lms.Warehouse.mapper.YdmProductButtresslistMapper;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.mapper.YdmProductInlistMapper;
import market.Lms.Warehouse.mapper.YdmProductOutlistMapper;
import market.Lms.Warehouse.service.IfRawMtrCoilMService;
import market.Lms.Warehouse.vo.IfRawMtrCoilM;
import market.Lms.Warehouse.vo.YdmProductButtresslist;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Lms.Warehouse.vo.YdmProductInlist;
import market.Mes.MaterielTrack.vo.PrcMaterielProcess;

import org.springframework.transaction.annotation.Transactional;

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

@SuppressWarnings("rawtypes")
@RemoteService(path="IfRawMtrCoilMService")
public class IfRawMtrCoilMServiceImpl extends BaseServiceImpl<IfRawMtrCoilM, String> implements IfRawMtrCoilMService {
	/**
	 * 生成卷号doConfirm
	 * @return
	 * @throws Exception 
	 */
	public String createCoilNo() throws Exception
	{
		try 
		{
			String coilno="";
			String month=Integer.toHexString(Integer.valueOf(new SimpleDateFormat("MM").format(new Date())).intValue()).toUpperCase();
			List<HashMap> list = this.getMapper(IfRawMtrCoilMMapper.class).getNoA("Y"+(new SimpleDateFormat("yy").format(new Date()))+month);
			coilno = (String) list.get(0).get("MAXNO");
			return coilno;
		} 
		catch (Exception ex) 
		{
			throw (ex);
		}
	}

	/**
	 * 待入库查询
	 * SQL实现
	 * 条件+汇总+分页
	 * */
	public ResultModel doQueryWebBySQL(CommonPage<IfRawMtrCoilM> cp)
	{
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<IfRawMtrCoilM> pm = new PageModel<IfRawMtrCoilM>(pageIndex,pageSize);
		try
		{
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null && cp.getMemo1() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}

				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null && cp.getMemo2() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null && cp.getMemo3() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && cp.getMemo4() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			pm.setRows(this.getMapper(IfRawMtrCoilMMapper.class).doQueryWebBySQL
					((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilNo(), cp.getObject().getSteelName(),hmin,hmax,wmin,wmax,cp.getObject().getStatus(),cp.getObject().getCoilSort()));
			pm.setTotal(this.getMapper(IfRawMtrCoilMMapper.class).doQueryWebBySQLCount
					(cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilNo(), cp.getObject().getSteelName(),hmin,hmax,wmin,wmax,cp.getObject().getStatus(),cp.getObject().getCoilSort()));
			rm.setData(pm);
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 待入库查询
	 * SQL实现
	 * 条件+汇总+分页
	 * */
	public ResultModel doQueryWebBySQLNew(CommonPage<YdmProductDetail> cp)
	{
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<IfRawMtrCoilM> pm = new PageModel<IfRawMtrCoilM>(pageIndex,pageSize);
		try
		{
			String regex = "^(\\d+\\.?\\d*)$";	
			boolean isMatch = true;
			Object hmin="",hmax="",wmin="",wmax="";
			if (cp.getMemo1() != null && cp.getMemo1() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo1().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}

				hmin = Double.valueOf(cp.getMemo1()).doubleValue();
			}
			if (cp.getMemo2() != null && cp.getMemo2() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo2().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				hmax = Double.valueOf(cp.getMemo2()).doubleValue();
			}
			if (cp.getMemo3() != null && cp.getMemo3() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo3().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmin = Double.valueOf(cp.getMemo3()).doubleValue();
			}
			if (cp.getMemo4() != null && cp.getMemo4() != "") {
				isMatch = Pattern.matches(regex, cp.getMemo4().trim());
				if (!isMatch) {
					rm.setState("500");
					rm.setMsgInfo("请输入正确的厚度和宽度!");
					return rm;
				}
				wmax = Double.valueOf(cp.getMemo4()).doubleValue();
			}
			pm.setRows(this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLNew
					((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getSteel_name(),hmin,hmax,wmin,wmax));
			pm.setTotal(this.getMapper(YdmProductDetailMapper.class).doQueryWebBySQLNewCount((pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getSteel_name(),hmin,hmax,wmin,wmax));
			rm.setData(pm);
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}


	/**
	 * 待入库查询 App用
	 * SQL实现
	 * 条件+汇总+分页
	 * */
	public ResultModel doQueryWebBySQLApp(CommonPage<IfRawMtrCoilM> cp)
	{
		ResultModel rm=new ResultModel();
		int pageSize=cp.getPageSize();
		int pageIndex=cp.getPageIndex();
		PageModel<IfRawMtrCoilM> pm = new PageModel<IfRawMtrCoilM>(pageIndex,pageSize);
		try
		{
			//Object hmin="",hmax="",wmin="",wmax="";

			List<IfRawMtrCoilM> CoilM = this.getMapper(IfRawMtrCoilMMapper.class).doQueryWebBySQL
					(0,99, "", "", cp.getObject().getCoilNo(), "",null,null,null,null,cp.getObject().getStatus(),null);
			pm.setRows(CoilM);
			pm.setTotal(this.getMapper(IfRawMtrCoilMMapper.class).doQueryWebBySQLCount
					("","", cp.getObject().getCoilNo(),  "",null,null,null,null,cp.getObject().getStatus(),null));

			if(CoilM==null||CoilM.size()==0)
			{
				rm.setState("500");
				rm.setMsgInfo("未查到原料数据!");
				return rm;
			}
			rm.setData(pm);
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 接收入库管理 入库确认(贸易库)
	 * memo1-4传区域/货位id，位置，备注
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doConfirmNew(CommonPage<IfRawMtrCoilM> cp) throws Exception
	{
		ResultModel rm=new ResultModel();
		SimpleDateFormat sdff = new SimpleDateFormat("yyyyMMddHHmmss");
		String remark = sdff.format(new Date());
		List<IfRawMtrCoilM> list=cp.getObjectList();
		String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String order=(String)this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
		String team=(String)this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);

		int i=0;
		int xx=0;
		String coilno=this.createCoilNo();
		int coild=Integer.parseInt(coilno.substring(1,coilno.length()));

		for(IfRawMtrCoilM irmcm:list)
		{
			//将选择的数据逐一插入库存信息表 YDM_PRODUCT_DETAIL，所属仓库编码STORAGEATTR = '500205' 
			//卷类别 COILTYPE = '0' 原料卷；卷来源 SOURCE = '0' 新钢；在库状态 STATE = ‘0’在库
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoilno("Y"+coild);
			coild++;
			ypd.setCoiltype("0");
			ypd.setMaterailtype(irmcm.getCoilSort());
			ypd.setSource("0");
			ypd.setState("0");
			ypd.setStorageattr("500205");
			 
			ypd.setCreater(username); 
			ypd.setCreatedate(new Date()); 
			
			  
			if (irmcm.getCoilNo()!=null) {
				ypd.setMaterailcoilno(irmcm.getCoilNo());
			}
			if (irmcm.getCoilSort()!=null) {
				ypd.setMaterailtype(irmcm.getCoilSort());
			}
			if (irmcm.getSupplierName()!=null) {
				ypd.setMaterailsupplier(irmcm.getSupplierName());
			}
			if (irmcm.getHeatno()!=null) {
				ypd.setHeatno(irmcm.getHeatno());
			}
			if (irmcm.getSmpNo()!=null) { 
				ypd.setSmpno(irmcm.getSmpNo());
			}
			if (irmcm.getPrdCode()!=null) {
				ypd.setProd_code(irmcm.getPrdCode());
			}
			if (irmcm.getPrdName()!=null) {
				ypd.setProd_name(irmcm.getPrdName());
			}
			if (irmcm.getStdCode()!=null) {
				ypd.setStd_code(irmcm.getStdCode());
			}
			if (irmcm.getStdName()!=null) {
				ypd.setStd_name(irmcm.getStdName());
			}
			if (irmcm.getSteelCode()!=null) { 
				String Strsteel =this.getMapper(IfRawMtrCoilMMapper.class).dofindSteel(irmcm.getSteelCode());
				if(Strsteel!=null&&!"".equals(Strsteel))
				{
					ypd.setJudgesteelcode(Strsteel);

					ypd.setSteelcode(Strsteel);
				}
				else
				{
					rm.setState("500");
					rm.setMsgInfo("系统内不存在传递过来的新钢牌号!"+irmcm.getSteelCode());
					return rm;
				}
			} 
			ypd.setThick(irmcm.getCoilThk());
			ypd.setWidth(irmcm.getCoilWdt());
			ypd.setLength(irmcm.getCoilLen());
			ypd.setDiameter_inn(irmcm.getCoilInnDia());
			ypd.setDiameter_out(irmcm.getCoilOutDia());
			ypd.setTheoryweight(irmcm.getCoilThrWgt());
			ypd.setFactweight(irmcm.getCoilActWgt());
			if(irmcm.getProduceTime()!=null&&irmcm.getProduceTime()!="")
			{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				ypd.setProducedate(sdf.parse(irmcm.getProduceTime()));
			}
			if (irmcm.getChemDecRstCode()!=null) {
				ypd.setChemresult(irmcm.getChemDecRstCode());
			}
			if (irmcm.getSizeDecRstCode()!=null) {
				ypd.setSizeresult(irmcm.getSizeDecRstCode());
			}
			if (irmcm.getSurfaceDecRstCode()!=null) {
				ypd.setSurfaceresult(irmcm.getSurfaceDecRstCode());
			}
			if (irmcm.getWgtDecRstCode()!=null) {
				ypd.setWeightresult(irmcm.getWgtDecRstCode());
			}
			if (irmcm.getTotalDecRstCode()!=null) {
				ypd.setJudgeresult(irmcm.getTotalDecRstCode());
			}
			if (irmcm.getPfmcDecRstCode()!=null) {
				ypd.setPhysresult(irmcm.getPfmcDecRstCode());
			} 
			if (irmcm.getOrderno()!=null) {
				 
				ypd.setLzorderno(irmcm.getOrderno());
			}
			ypd.setIndate(new Date());
			cp.getObjectList().get(xx).setIndate(new Date());
			xx++;
			 
			ypd.setInorder(order);
			ypd.setInteam(team);
			if (irmcm.getChemid()!=null) {
				ypd.setChemid(irmcm.getChemid());
			}
			if (irmcm.getPhysid()!=null) {
				ypd.setPhysid(irmcm.getPhysid());
			}
			ypd.setStorageattr(irmcm.getStorageattr());
			ypd.setObject_attr("507101");
			this.getMapper(YdmProductDetailMapper.class).insert(ypd);
			
			
			//调用生产计划模块物料跟踪接口
			PrcMaterielProcess pmp = new PrcMaterielProcess();
			Date date =new  Date();
			pmp.setObject_no(ypd.getCoilno());
			pmp.setPrc_code("5BYA05");
			pmp.setCreate_time(date);
			pmp.setRemark("原料接收入库");
			pmp.setCreate_man_name(username);
			PrcMaterielProcess pmp1 = new PrcMaterielProcess();
			pmp1.setObject_no(ypd.getCoilno());
			pmp1.setPrc_code("5BYA04");
			pmp1.setCreate_time(date);
			pmp1.setRemark(""+remark);
			pmp1.setCreate_man_name(username);
			int tmp1= this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp1);
			int tmp= this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
			if(tmp1==-1){
				throw new Exception("物料跟踪失败");
			}
			if(tmp==-1){
				throw new Exception("物料跟踪失败");
			}
			System.out.println(pmp.getObject_no()+""+pmp1.getObject_no());
			
			
			//插入入库信息
			YdmProductInlist ypi=new YdmProductInlist();
			if (ypi.getCreatedate()==null||"".equals(ypi.getCreatedate())) {
				ypi.setCreatedate(new Date());
			}
			if (ypi.getCreater()==null||"".equals(ypi.getCreater())) {
				ypi.setCreater(username);
			}
			ypi.setIntype("500407");
			ypi.setSource("0");
			ypi.setOperator(username);
			ypi.setMemo(cp.getMemo4());
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
				ypi.setPosition(String.valueOf(Integer.valueOf(cp.getMemo3()).intValue()+i).toString());
			ypi.setButtressno(cp.getMemo2());
			ypi.setLocality("1");
			ypi.setCoilno(ypd.getCoilno());
			ypi.setCoiltype("0");
			ypi.setStorageattr(irmcm.getStorageattr());
			if (irmcm.getCoilSort()!=null) {
				ypi.setMaterailtype(irmcm.getCoilSort());
			}
			if (irmcm.getSupplierName()!=null) {
				ypi.setMaterailsupplier(irmcm.getSupplierName());
			}
			if (irmcm.getHeatno()!=null) {
				ypi.setHeatno(irmcm.getHeatno());
			}
			if (irmcm.getSmpNo()!=null) {
				ypi.setSmpno(irmcm.getSmpNo());
			}
			if (irmcm.getPrdCode()!=null) {
				ypi.setProd_code(irmcm.getPrdCode());
			}
			if (irmcm.getPrdName()!=null) {
				ypi.setProd_name(irmcm.getPrdName());
			}
			if (irmcm.getStdCode()!=null) {
				ypi.setStd_code(irmcm.getStdCode());
			}
			if (irmcm.getStdName()!=null) {
				ypi.setStd_name(irmcm.getStdName());
			}
			if (irmcm.getSteelCode()!=null) {
				ypi.setJudgesteelcode(irmcm.getSteelCode());
			}

			ypi.setThick(irmcm.getCoilThk());
			ypi.setWidth(irmcm.getCoilWdt());
			ypi.setLength(irmcm.getCoilLen());
			ypi.setDiameter_inn(irmcm.getCoilInnDia());
			ypi.setDiameter_out(irmcm.getCoilOutDia());
			ypi.setTheoryweight(irmcm.getCoilThrWgt());
			ypi.setFactweight(irmcm.getCoilActWgt());
			if(irmcm.getProduceTime()!=null&&irmcm.getProduceTime()!="")
			{
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
				ypi.setProducedate(sdf1.parse(irmcm.getProduceTime()));
			}
			if (irmcm.getChemDecRstCode()!=null) {
				ypi.setChemresult(irmcm.getChemDecRstCode());
			}
			if (irmcm.getSizeDecRstCode()!=null) {
				ypi.setSizeresult(irmcm.getSizeDecRstCode());
			}
			if (irmcm.getSurfaceDecRstCode()!=null) {
				ypi.setSurfaceresult(irmcm.getSurfaceDecRstCode());
			}
			if (irmcm.getWgtDecRstCode()!=null) {
				ypi.setWeightresult(irmcm.getWgtDecRstCode());
			}
			if (irmcm.getTotalDecRstCode()!=null) {
				ypi.setJudgeresult(irmcm.getTotalDecRstCode());
			}
			if (irmcm.getPfmcDecRstCode()!=null) {
				ypi.setPhysresult(irmcm.getPfmcDecRstCode());
			}

			if (irmcm.getOrderno()!=null) {
				ypi.setOrderno(irmcm.getOrderno());
			} 
			ypi.setIndate(new Date());
			ypi.setInorder(order);
			ypi.setInteam(team);
			///需要修改所属仓库属性
			ypi.setStorageattr("500205");
			ypi.setInbillno(this.getBean(YdmProductInlistServiceImpl.class).createINBILLNO());
			
			// 加入物料属性：20230725刘侣
			ypi.setObj_attr_in(ypd.getObject_attr());
			// end
			
			this.getMapper(YdmProductInlistMapper.class).insert(ypi);
			
			//插入垛位信息
			YdmProductButtresslist ypb=new YdmProductButtresslist();
			ypb.setCreatedate(new Date());
			ypb.setCreater(username);
			ypb.setLayer(1);
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
				ypb.setLocality(Integer.valueOf(cp.getMemo3()).intValue()+i);
			ypb.setButtressno(cp.getMemo2());
			ypb.setAreano(cp.getMemo1());
			ypb.setCoilno(ypd.getCoilno());
			this.getMapper(YdmProductButtresslistMapper.class).doInsert(ypb);
			
			//修改原料卷接口实绩
			IfRawMtrCoilM irmcm1=new IfRawMtrCoilM();
			if(irmcm.getCoilNo()!=null&&irmcm.getCoilNo()!="")
				irmcm1=this.getMapper(IfRawMtrCoilMMapper.class).findById(irmcm.getCoilNo());
			if(irmcm1!=null)
			{
				irmcm1.setStatus("1");
				this.getMapper(IfRawMtrCoilMMapper.class).update(irmcm1);
			} 
			i++;
		}
		rm.setState("200");
		rm.setData(cp);

		return rm;
	}

	/**
	 * 接收入库管理 入库确认(原料库)
	 * memo1-4传区域/货位id，位置，备注
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doConfirmNewYL(CommonPage<IfRawMtrCoilM> cp) throws Exception
	{
		ResultModel rm=new ResultModel();
		//SimpleDateFormat sdff = new SimpleDateFormat("yyyyMMddHHmmss");
		//String remark = sdff.format(new Date());
		List<IfRawMtrCoilM> list=cp.getObjectList();
		String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		String order=(String)this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
		String team=(String)this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);

		int i=0; 
		for(IfRawMtrCoilM irmcm:list)
		{
			if (irmcm==null || irmcm.getCoilNo()==null) {
				continue;
			}
			
			//将选择的数据逐一修改库存信息表 YDM_PRODUCT_DETAIL，YDM_PRODUCT_DETAIL.STORAGEATTR = "500201"；
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoilno(irmcm.getCoilNo());
			
			//有效性检查YDM_PRODUCT_DETAIL.STORAGEATTR = "500205" && YDM_PRODUCT_DETAIL.STATE = "0"
			YdmProductDetail ch = this.getMapper(YdmProductDetailMapper.class).check(ypd);
			if (ch==null) {
				continue;
			}
			
			ypd.setStorageattr("500201");
			this.getMapper(YdmProductDetailMapper.class).update(ypd);
			
			//插入入库信息
			YdmProductInlist ypi=new YdmProductInlist();
			if (ypi.getCreatedate()==null||"".equals(ypi.getCreatedate())) {
				ypi.setCreatedate(new Date());
			}
			if (ypi.getCreater()==null||"".equals(ypi.getCreater())) {
				ypi.setCreater(username);
			}
			ypi.setIntype("500201");
			ypi.setSource("0");
			ypi.setOperator(username);
			ypi.setMemo(cp.getMemo4());
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
				ypi.setPosition(String.valueOf(Integer.valueOf(cp.getMemo3()).intValue()+i).toString());
			ypi.setButtressno(cp.getMemo2());
			ypi.setLocality("1");
			ypi.setCoilno(ypd.getCoilno());
			ypi.setCoiltype("0");
			ypi.setStorageattr(irmcm.getStorageattr());
			if (irmcm.getCoilSort()!=null) {
				ypi.setMaterailtype(irmcm.getCoilSort());
			}
			if (irmcm.getSupplierName()!=null) {
				ypi.setMaterailsupplier(irmcm.getSupplierName());
			}
			if (irmcm.getHeatno()!=null) {
				ypi.setHeatno(irmcm.getHeatno());
			}
			if (irmcm.getSmpNo()!=null) {
				ypi.setSmpno(irmcm.getSmpNo());
			}
			if (irmcm.getPrdCode()!=null) {
				ypi.setProd_code(irmcm.getPrdCode());
			}
			if (irmcm.getPrdName()!=null) {
				ypi.setProd_name(irmcm.getPrdName());
			}
			if (irmcm.getStdCode()!=null) {
				ypi.setStd_code(irmcm.getStdCode());
			}
			if (irmcm.getStdName()!=null) {
				ypi.setStd_name(irmcm.getStdName());
			}
			if (irmcm.getSteelCode()!=null) {
				ypi.setJudgesteelcode(irmcm.getSteelCode());
			}

			ypi.setThick(irmcm.getCoilThk());
			ypi.setWidth(irmcm.getCoilWdt());
			ypi.setLength(irmcm.getCoilLen());
			ypi.setDiameter_inn(irmcm.getCoilInnDia());
			ypi.setDiameter_out(irmcm.getCoilOutDia());
			ypi.setTheoryweight(irmcm.getCoilThrWgt());
			ypi.setFactweight(irmcm.getCoilActWgt());
			if(irmcm.getProduceTime()!=null&&irmcm.getProduceTime()!="")
			{
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
				ypi.setProducedate(sdf1.parse(irmcm.getProduceTime()));
			}
			if (irmcm.getChemDecRstCode()!=null) {
				ypi.setChemresult(irmcm.getChemDecRstCode());
			}
			if (irmcm.getSizeDecRstCode()!=null) {
				ypi.setSizeresult(irmcm.getSizeDecRstCode());
			}
			if (irmcm.getSurfaceDecRstCode()!=null) {
				ypi.setSurfaceresult(irmcm.getSurfaceDecRstCode());
			}
			if (irmcm.getWgtDecRstCode()!=null) {
				ypi.setWeightresult(irmcm.getWgtDecRstCode());
			}
			if (irmcm.getTotalDecRstCode()!=null) {
				ypi.setJudgeresult(irmcm.getTotalDecRstCode());
			}
			if (irmcm.getPfmcDecRstCode()!=null) {
				ypi.setPhysresult(irmcm.getPfmcDecRstCode());
			}

			if (irmcm.getOrderno()!=null) {
				ypi.setOrderno(irmcm.getOrderno());
			} 
			ypi.setIndate(new Date());
			ypi.setInorder(order);
			ypi.setInteam(team);
			///需要修改所属仓库属性
			ypi.setStorageattr("500205");
			ypi.setInbillno(this.getBean(YdmProductInlistServiceImpl.class).createINBILLNO());
			
			// 加入物料属性：20230725刘侣
			ypi.setObj_attr_in(ypd.getObject_attr());
			// end
			
			this.getMapper(YdmProductInlistMapper.class).insert(ypi);
			
			//插入垛位信息
			YdmProductButtresslist ypb=new YdmProductButtresslist();
			ypb.setCreatedate(new Date());
			ypb.setCreater(username);
			ypb.setLayer(1);
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
				ypb.setLocality(Integer.valueOf(cp.getMemo3()).intValue()+i);
			ypb.setButtressno(cp.getMemo2());
			ypb.setAreano(cp.getMemo1());
			ypb.setCoilno(ypd.getCoilno());
			this.getMapper(YdmProductButtresslistMapper.class).doInsert(ypb);
			
			//将出库信息表中YDM_PRODUCT_OUTLIST的出库类型"OUTTYPE" 改成取COM_BASE_INFO表出库类型基础信息中的"正常出库"
			//ypo.setCoilno(ypd.getCoilno());
			String coilno = ypd.getCoilno();
			if (coilno!=null && !"".equals(coilno)) {
				this.getMapper(YdmProductOutlistMapper.class).doUpdate(coilno);
			}
			
			i++;
		}
		rm.setState("200");
		rm.setData(cp);

		return rm;
	}

	
	/**
	 * 接收入库管理 入库确认(贸易库)
	 * memo1-4传区域/货位id，位置，备注
	 * @throws Exception 
	 * */
	public ResultModel doConfirm(CommonPage<IfRawMtrCoilM> cp) throws Exception
	{
		ResultModel rm=new ResultModel();
		SimpleDateFormat sdff = new SimpleDateFormat("yyyyMMddHHmmss");
		String remark = sdff.format(new Date());
		List<IfRawMtrCoilM> list=cp.getObjectList();
		String username=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		int i=0;
		String order=(String)this.getSession().getAttribute(HomeService.SESSION_WORK_ORD);
		String team=(String)this.getSession().getAttribute(HomeService.SESSION_WORK_GRP);

		int xx=0;
		String coilno=this.createCoilNo();
		String beforeCoilno = coilno.substring(0,4);
		int coild=Integer.parseInt(coilno.substring(4,coilno.length()));

		if(cp.getSortField()!=null&&!"".equals(cp.getSortField())){
			List<YdmProductDetail> lypd=this.getMapper(YdmProductDetailMapper.class).doFindcount(cp.getSortField(),null);
			if(lypd!=null&&lypd.size()>0)
			{
				rm.setState("500");
				rm.setMsgInfo("系统内存在指定的卷号!"+cp.getSortField().toString());
				return rm;
			}
			else
			{
				coilno =cp.getSortField();
			}
		}
		
		for(IfRawMtrCoilM irmcm:list)
		{
			//插入库存信息
			YdmProductDetail ypd=new YdmProductDetail();
			ypd.setCoiltype("0");
			ypd.setSource("0");
			ypd.setState("0");
			if (ypd.getCreater()==null||"".equals(ypd.getCreater())) {
				ypd.setCreater(username);
			}
			if (ypd.getCreatedate()==null||"".equals(ypd.getCreatedate())) {
				ypd.setCreatedate(new Date());
			}
			if(cp.getSortField()!=null&&!"".equals(cp.getSortField())){
				ypd.setCoilno(coilno);
			}
			else
			{
				ypd.setCoilno(beforeCoilno+String.format("%04d", coild));
				coild++;
			}
			ypd.setCoiltype("0");
			if (irmcm.getCoilNo()!=null) {
				ypd.setMaterailcoilno(irmcm.getCoilNo());
			}
			if (irmcm.getCoilSort()!=null) {
				ypd.setMaterailtype(irmcm.getCoilSort());
			}
			if (irmcm.getSupplierName()!=null) {
				ypd.setMaterailsupplier(irmcm.getSupplierName());
			}
			if (irmcm.getHeatno()!=null) {
				ypd.setHeatno(irmcm.getHeatno());
			}
			if (irmcm.getSmpNo()!=null) {

				ypd.setSmpno(irmcm.getSmpNo());
			}
			if (irmcm.getPrdCode()!=null) {
				ypd.setProd_code(irmcm.getPrdCode());
			}
			if (irmcm.getPrdName()!=null) {
				ypd.setProd_name(irmcm.getPrdName());
			}
			if (irmcm.getStdCode()!=null) {
				ypd.setStd_code(irmcm.getStdCode());
			}
			if (irmcm.getStdName()!=null) {
				ypd.setStd_name(irmcm.getStdName());
			}
			if (irmcm.getSteelCode()!=null) {

				String Strsteel =this.getMapper(IfRawMtrCoilMMapper.class).dofindSteel(irmcm.getSteelCode());
				if(Strsteel!=null&&!"".equals(Strsteel))
				{
					ypd.setJudgesteelcode(Strsteel);
					ypd.setSteelcode(Strsteel);
				}
				else
				{
					rm.setState("500");
					rm.setMsgInfo("系统内不存在传递过来的新钢牌号!"+irmcm.getSteelCode());
					return rm;
				}
			}

			ypd.setThick(irmcm.getCoilThk());
			ypd.setWidth(irmcm.getCoilWdt());
			ypd.setLength(irmcm.getCoilLen());
			ypd.setDiameter_inn(irmcm.getCoilInnDia());
			ypd.setDiameter_out(irmcm.getCoilOutDia());
			ypd.setTheoryweight(irmcm.getCoilThrWgt());
			ypd.setFactweight(irmcm.getCoilActWgt());
			if(irmcm.getProduceTime()!=null&&irmcm.getProduceTime()!="")
			{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
				ypd.setProducedate(sdf.parse(irmcm.getProduceTime()));
			}
			if (irmcm.getChemDecRstCode()!=null) {
				ypd.setChemresult(irmcm.getChemDecRstCode());
			}
			if (irmcm.getSizeDecRstCode()!=null) {
				ypd.setSizeresult(irmcm.getSizeDecRstCode());
			}
			if (irmcm.getSurfaceDecRstCode()!=null) {
				ypd.setSurfaceresult(irmcm.getSurfaceDecRstCode());
			}
			if (irmcm.getWgtDecRstCode()!=null) {
				ypd.setWeightresult(irmcm.getWgtDecRstCode());
			}
			if (irmcm.getTotalDecRstCode()!=null) {
				ypd.setJudgeresult(irmcm.getTotalDecRstCode());
			}
			if (irmcm.getPfmcDecRstCode()!=null) {
				ypd.setPhysresult(irmcm.getPfmcDecRstCode());
			}
			if (irmcm.getOrderno()!=null) {
				//ypd.setOrderno(irmcm.getOrderno());
				ypd.setLzorderno(irmcm.getOrderno());
			}
			ypd.setIndate(new Date());
			cp.getObjectList().get(xx).setIndate(new Date());
			xx++;
			ypd.setInorder(order);
			ypd.setInteam(team);
			if (irmcm.getChemid()!=null) {
				ypd.setChemid(irmcm.getChemid());
			}
			if (irmcm.getPhysid()!=null) {
				ypd.setPhysid(irmcm.getPhysid());
			}
			ypd.setStorageattr(irmcm.getStorageattr());
			this.getMapper(YdmProductDetailMapper.class).doInsert(ypd);
			//调用生产计划模块物料跟踪接口
			PrcMaterielProcess pmp = new PrcMaterielProcess();
			Date date =new  Date();
			pmp.setObject_no(ypd.getCoilno());
			pmp.setPrc_code("5BYA05");
			pmp.setCreate_time(date);
			pmp.setRemark("原料接收入库");
			pmp.setCreate_man_name(username);
			PrcMaterielProcess pmp1 = new PrcMaterielProcess();
			pmp1.setObject_no(ypd.getCoilno());
			pmp1.setPrc_code("5BYA04");
			pmp1.setCreate_time(date);
			pmp1.setRemark(""+remark);
			pmp1.setCreate_man_name(username);
			int tmp1= this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp1);
			int tmp= this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
			if(tmp1==-1){
				throw new Exception("物料跟踪失败");
			}
			if(tmp==-1){
				throw new Exception("物料跟踪失败");
			}
			System.out.println(pmp.getObject_no()+""+pmp1.getObject_no());
			//插入入库信息
			YdmProductInlist ypi=new YdmProductInlist();
			if (ypi.getCreatedate()==null||"".equals(ypi.getCreatedate())) {
				ypi.setCreatedate(new Date());
			}
			if (ypi.getCreater()==null||"".equals(ypi.getCreater())) {
				ypi.setCreater(username);
			}
			ypi.setIntype("500407");
			ypi.setSource("0");
			ypi.setOperator(username);
			ypi.setMemo(cp.getMemo4());
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
				ypi.setPosition(String.valueOf(Integer.valueOf(cp.getMemo3()).intValue()+i).toString());
			ypi.setButtressno(cp.getMemo2());
			ypi.setLocality("1");
			ypi.setCoilno(ypd.getCoilno());
			ypi.setCoiltype("0");
			ypi.setStorageattr(irmcm.getStorageattr());
			if (irmcm.getCoilSort()!=null) {
				ypi.setMaterailtype(irmcm.getCoilSort());
			}
			if (irmcm.getSupplierName()!=null) {
				ypi.setMaterailsupplier(irmcm.getSupplierName());
			}
			if (irmcm.getHeatno()!=null) {
				ypi.setHeatno(irmcm.getHeatno());
			}
			if (irmcm.getSmpNo()!=null) {
				ypi.setSmpno(irmcm.getSmpNo());
			}
			if (irmcm.getPrdCode()!=null) {
				ypi.setProd_code(irmcm.getPrdCode());
			}
			if (irmcm.getPrdName()!=null) {
				ypi.setProd_name(irmcm.getPrdName());
			}
			if (irmcm.getStdCode()!=null) {
				ypi.setStd_code(irmcm.getStdCode());
			}
			if (irmcm.getStdName()!=null) {
				ypi.setStd_name(irmcm.getStdName());
			}
			if (irmcm.getSteelCode()!=null) {
				ypi.setJudgesteelcode(irmcm.getSteelCode());
			}

			ypi.setThick(irmcm.getCoilThk());
			ypi.setWidth(irmcm.getCoilWdt());
			ypi.setLength(irmcm.getCoilLen());
			ypi.setDiameter_inn(irmcm.getCoilInnDia());
			ypi.setDiameter_out(irmcm.getCoilOutDia());
			ypi.setTheoryweight(irmcm.getCoilThrWgt());
			ypi.setFactweight(irmcm.getCoilActWgt());
			if(irmcm.getProduceTime()!=null&&irmcm.getProduceTime()!="")
			{
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
				ypi.setProducedate(sdf1.parse(irmcm.getProduceTime()));
			}
			if (irmcm.getChemDecRstCode()!=null) {
				ypi.setChemresult(irmcm.getChemDecRstCode());
			}
			if (irmcm.getSizeDecRstCode()!=null) {
				ypi.setSizeresult(irmcm.getSizeDecRstCode());
			}
			if (irmcm.getSurfaceDecRstCode()!=null) {
				ypi.setSurfaceresult(irmcm.getSurfaceDecRstCode());
			}
			if (irmcm.getWgtDecRstCode()!=null) {
				ypi.setWeightresult(irmcm.getWgtDecRstCode());
			}
			if (irmcm.getTotalDecRstCode()!=null) {
				ypi.setJudgeresult(irmcm.getTotalDecRstCode());
			}
			if (irmcm.getPfmcDecRstCode()!=null) {
				ypi.setPhysresult(irmcm.getPfmcDecRstCode());
			}

			if (irmcm.getOrderno()!=null) {
				ypi.setOrderno(irmcm.getOrderno());
			}
			ypi.setIndate(new Date());
			ypi.setInorder(order);
			ypi.setInteam(team);
			///需要修改所属仓库属性
			ypi.setStorageattr(irmcm.getStorageattr());
			ypi.setInbillno(this.getBean(YdmProductInlistServiceImpl.class).createINBILLNO());
			
			// 加入物料属性：20230725刘侣
			ypi.setObj_attr_in(ypd.getObject_attr());
			// end
			
			this.getMapper(YdmProductInlistMapper.class).insert(ypi);
			//插入垛位信息
			YdmProductButtresslist ypb=new YdmProductButtresslist();
			ypb.setCreatedate(new Date());
			ypb.setCreater(username);
			ypb.setLayer(1);
			if(cp.getMemo3()!=null&&cp.getMemo3()!="")
				ypb.setLocality(Integer.valueOf(cp.getMemo3()).intValue()+i);
			ypb.setButtressno(cp.getMemo2());
			ypb.setAreano(cp.getMemo1());
			ypb.setCoilno(ypd.getCoilno());
			this.getMapper(YdmProductButtresslistMapper.class).doInsert(ypb);
			//修改原料卷接口实绩
			IfRawMtrCoilM irmcm1=new IfRawMtrCoilM();
			if(irmcm.getCoilNo()!=null&&irmcm.getCoilNo()!="")
				irmcm1=this.getMapper(IfRawMtrCoilMMapper.class).findById(irmcm.getCoilNo());
			if(irmcm1!=null)
			{
				irmcm1.setStatus("1");
				this.getMapper(IfRawMtrCoilMMapper.class).update(irmcm1);
			} 	 
			i++;
		}
		rm.setState("200");
		rm.setData(cp);

		return rm;
	}

	
	/**
	 * 接收入库管理 入库撤销(贸易库)
	 * */
	public ResultModel doCancelNew(CommonPage<YdmProductInlist> cp)
	{
		ResultModel rm=new ResultModel();
		try
		{
			List<YdmProductInlist> list=cp.getObjectList();
			for(YdmProductInlist ypi:list)
			{
				//删除入库信息
				if(ypi.getInbillno()!=null&&ypi.getInbillno()!="")
					this.getMapper(YdmProductInlistMapper.class).doDelete(ypi.getInbillno());
				//删除垛位信息
				if(ypi.getCoilno()!=null&&ypi.getCoilno()!="") 
					this.getMapper(YdmProductDetailMapper.class).doDelete(ypi.getCoilno(), null); 
				 	
				//删除库存信息
				YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).findById(ypi.getCoilno());
				if (ypd!=null) {
					this.getMapper(YdmProductDetailMapper.class).delete(ypd.getCoilno());
					//修改原料卷实绩
					if (ypd.getMaterailcoilno()!=null && ypd.getMaterailcoilno()!="") {
						this.getMapper(IfRawMtrCoilMMapper.class).doUpdate("0", ypd.getMaterailcoilno());
					}
				}  
			}
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，撤销失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 接收入库管理 入库撤销(原料库)
	 * */
	public ResultModel doCancelNewYL(CommonPage<YdmProductInlist> cp)
	{
		ResultModel rm=new ResultModel();
		try
		{
			List<YdmProductInlist> list=cp.getObjectList();
			for(YdmProductInlist ypi:list)
			{
				//删除入库信息
				if(ypi.getInbillno()!=null&&ypi.getInbillno()!="")
					this.getMapper(YdmProductInlistMapper.class).doDelete(ypi.getInbillno());
				//删除垛位信息
				if(ypi.getCoilno()!=null&&ypi.getCoilno()!="") 
					this.getMapper(YdmProductDetailMapper.class).doDelete(ypi.getCoilno(), null); 
				 	
				//修改库存信息
				YdmProductDetail ypd = this.getMapper(YdmProductDetailMapper.class).findById(ypi.getCoilno());
				if (ypd!=null) {
					YdmProductDetail ypdNew =new YdmProductDetail();
					ypdNew.setCoilno(ypi.getCoilno());
					ypdNew.setStorageattr("500205");
					this.getMapper(YdmProductDetailMapper.class).update(ypdNew); 
				}  
			}
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，撤销失败!");
			ex.printStackTrace();
		}
		return rm;
	}

	
	/**
	 * 接收入库管理 入库撤销
	 * @throws Exception 
	 * */
	public ResultModel doCancel(CommonPage<YdmProductInlist> cp) throws Exception
	{
		ResultModel rm=new ResultModel();
		List<YdmProductInlist> list=cp.getObjectList();
		//入库撤销检查
		for(YdmProductInlist ypi:list)
		{
			if(ypi.getInbillno()!=null&&ypi.getInbillno()!="")
			{
			   YdmProductInlist ych = this.getMapper(YdmProductInlistMapper.class).findById(ypi.getInbillno());
			   if (ych!=null)
			   {
				   if (ych.getUp_flag()!=null && "1".equals(ych.getUp_flag().trim())) //已上传
				   {
					   throw new Exception("原料卷号:"+ych.getCoilno() +"已上传,无法撤销入库");
				   }
			   }
			}
		}
		
		for(YdmProductInlist ypi:list)
		{
			//删除入库信息
			if(ypi.getInbillno()!=null&&ypi.getInbillno()!="")
				this.getMapper(YdmProductInlistMapper.class).doDelete(ypi.getInbillno());
			//删除垛位信息
			if((ypi.getBoxno()!=null&&ypi.getBoxno()!="")||(ypi.getCoilno()!=null&&ypi.getCoilno()!=""))
			{
				this.getMapper(YdmProductButtresslistMapper.class).doDelete(ypi.getCoilno(), ypi.getBoxno());
				//删除库存信息
				YdmProductDetail ypd=this.getMapper(YdmProductDetailMapper.class).doFind(ypi.getCoilno(), ypi.getBoxno());
				if(ypd!=null)
				{
					//修改原料卷实绩
					if(ypd.getMaterailcoilno()!=null&&ypd.getMaterailcoilno()!="")
						this.getMapper(IfRawMtrCoilMMapper.class).doUpdate("0", ypd.getMaterailcoilno());
					//删除检验材质实绩
					/*YdmChem yc=new YdmChem();
					yc.setHeatno(ypd.getMaterailcoilno());
					this.getMapper(YdmChemMapper.class).delete(yc);
					//删除检验成分实绩
					YdmPfmc yp=new YdmPfmc(); 
					yp.setSmp_no(ypd.getSmpno());
					this.getMapper(YdmPfmcMapper.class).delete(yp);*/
				}
				this.getMapper(YdmProductDetailMapper.class).doDelete(ypi.getCoilno(), ypi.getBoxno());
				//调用生产计划模块物料跟踪撤销接口
				PrcMaterielProcess pmp =new PrcMaterielProcess();
				pmp.setObject_no(ypd.getCoilno());
				pmp.setPrc_code("5BYA05");
				pmp.setKeep_sametime_status(0);
				pmp.setRemark("原料入库撤销");
				this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp).getState();
				/*String tmp = this.getBean(MesApiServiceImpl.class).ARRAYLIST(pmp).getState();
				if(tmp=="-1"){
					throw new Exception("出现程序异常！撤销跟踪失败！");
				}else if(tmp=="-2"){
					throw new Exception("出现程序异常！撤销跟踪失败");
				}else{
					
				}*/
			}
		}
		rm.setState("200");
		return rm;
	}

	 
}
