/***文档注释***********************************************
 * 作者               :                   易鑫
 * 创建日期      :                   2017.12.17
 * 描述               :        			物料进程表
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/

package market.Mes.MaterielTrack.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.transaction.annotation.Transactional;

import market.Api.Mes.impl.MesApiServiceImpl;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.mapper.YdmProductDetailMapper;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Mes.MaterielTrack.mapper.PrcMaterielProcessMapper;
import market.Mes.MaterielTrack.mapper.PrcMaterielRelationMapper;
import market.Mes.MaterielTrack.service.PrcMaterielProcessService;
import market.Mes.MaterielTrack.vo.PrcMaterielProcess;
import market.Mes.MaterielTrack.vo.PrcMaterielRelation;
import market.Mes.MaterielTrack.vo.Proc_Tree;

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

@RemoteService(path="PrcMaterielProcessService")
public class PrcMaterielProcessServiceImpl extends BaseServiceImpl<PrcMaterielProcess,Object> implements PrcMaterielProcessService {

	/*
	 * prc_materiel_process 物料进程表
	 * PRC_MATERIEL_RELATION  物料关系表
	 */
	
	/*
	 * 物料进程查询
	 * @param PrcMaterielProcess
	 * pageIndex 页码   pageSize 条数
	 * @return
	 */
  public ResultModel doQueryWeb(CommonPage<PrcMaterielProcess> sum){
	  ResultModel rm = new ResultModel();
	  
	  //PrcMaterielProcess prc = sum.getObject()!=null?sum.getObject():new PrcMaterielProcess();
	  PrcMaterielProcess prc = new PrcMaterielProcess();
	  String swhere = "1=1";
	  if(sum.getObject().getObject_no()!=null&&!"".equals(sum.getObject().getObject_no())){
		  swhere += "and object_no like '%"+sum.getObject().getObject_no().trim()+"%'";
	  }
	  try {
		  PageModel<PrcMaterielProcess> pm=new PageModel<PrcMaterielProcess>(sum.getPageIndex(), sum.getPageSize());;
		  pm.setRows(this.getMapper(PrcMaterielProcessMapper.class).findWithCondition(prc, swhere,"object_no,serial_no",sum.getPageIndex(),sum.getPageSize()));
		  pm.setTotal(this.getMapper(PrcMaterielProcessMapper.class).countWithCondition(prc, swhere));
		rm.setData(pm);
		rm.setState("200");
	} catch (Exception e) {
		// TODO: handle exception
		e.getStackTrace();
		rm.setState("500");
		rm.setMsgInfo(e.getMessage());
	}
	  return rm;
  }
  
  /*
   * 物料进程查询-获取所有祖先节点及其祖先兄弟节点
   */
   public ResultModel doQueryWebBySql(CommonPage<PrcMaterielProcess>sum){
	   ResultModel rm =new ResultModel();
	 try {
		 //获取所有祖先节点及其祖先兄弟节点
		//List<HashMap> lm = this.getMapper(PrcMaterielProcessMapper.class).doFindFather(sum.getObject().getObject_no());
		//获取所有兄弟节点
		//List<HashMap> lb = this.getMapper(PrcMaterielProcessMapper.class).doFindBrother(sum.getObject().getObject_no());
		//获取所有子孙节点
		
		//List<HashMap> ls = this.getMapper(PrcMaterielProcessMapper.class).doFindSon(sum.getObject().getObject_no());
		
		rm.setData(this.getMapper(PrcMaterielProcessMapper.class).doFindFather(sum.getObject().getObject_no()));
		rm.setState("200");
	    
	 } catch (Exception e) {
		// TODO: handle exception
		 rm.setState("500");
		 e.getStackTrace();
		 rm.setMsgInfo(e.getMessage());
	}
	   return rm;
   }
   
   /**
    * 物料进程查询-查找兄弟节点
    */
   public ResultModel doQueryWebBySql1(CommonPage<PrcMaterielProcess> sum){
	   ResultModel rm =new ResultModel();
	   try {
		rm.setData(this.getMapper(PrcMaterielProcessMapper.class).doFindBrother(sum.getObject().getObject_no()));
		rm.setState("200");
	} catch (Exception e) {
		// TODO: handle exception
		rm.setState("500");
		e.getStackTrace();
		rm.setMsgInfo(e.getMessage());
	}
	   return rm;
   }
   
   /**
    * 
    * 物料进程查询-查找子孙节点
    */
   public ResultModel doQueryWebBySql2(CommonPage<PrcMaterielProcess> sum){
	   ResultModel rm =new ResultModel();
		 try {
			rm.setData(this.getMapper(PrcMaterielProcessMapper.class).doFindSon(sum.getObject().getObject_no()));  
			rm.setState("200");
		 } catch (Exception e) {
			// TODO: handle exception
			 rm.setState("500");
			 e.getStackTrace();
			 rm.setMsgInfo(e.getMessage());
		}
		   return rm;
   }
   
   /**
    * 物料进程查询-库存查询
    * @param YdmProductDetail.coilno
    * pageIndex 页码   pageSize 条数
    * @return
    */
   public ResultModel doQueryWebDetail(CommonPage<YdmProductDetail> sum){
	   ResultModel rm = new ResultModel();
	   YdmProductDetail ypd = new YdmProductDetail();
	   String swhere = "1=1";
	   if(sum.getObject().getCoilno()!=null&&!"".equals(sum.getObject().getCoilno())){
		    swhere += "and coilno like '%"+sum.getObject().getCoilno().trim()+"%'";		    
		   }
	   try {
		  PageModel<YdmProductDetail> pm = new PageModel<>(sum.getPageIndex(), sum.getPageSize());
		  pm.setRows(this.getMapper(YdmProductDetailMapper.class).findWithCondition(ypd, swhere, "coilno", sum.getPageIndex(),sum.getPageSize()));
	      pm.setTotal(this.getMapper(YdmProductDetailMapper.class).countWithCondition(ypd, swhere));
	      rm.setData(pm);
	      rm.setState("200");
	   } catch (Exception e) {
		// TODO: handle exception
		   rm.setState("500");
		   e.getStackTrace();
		   rm.setMsgInfo(e.getMessage());
	}
	   return rm;
   }

   
   
   public ResultModel doQueryGatherNode(CommonPage<PrcMaterielRelation> sum){
		  ResultModel rm = new ResultModel();
		  try {
			 if(sum==null || sum.getObject()==null || sum.getObject().getObject_no()==null){
				 rm.setMsgInfo("请输入物料号");
				 return rm;
			 }
			String object_no=sum.getObject().getObject_no();
			Proc_Tree PlateTree=null;
			List<PrcMaterielRelation> lmr =this.getMapper(PrcMaterielRelationMapper.class).doFindRelation(object_no);
			if(lmr!=null && lmr.size()>0){
			    PlateTree=new Proc_Tree(lmr.get(0).getObject_no());
			}else if(lmr!=null && lmr.size()==0){
				PlateTree=new Proc_Tree(object_no);
			}
			this.GatherNode(PlateTree,object_no);
			PlateTree.CalcMaxDepth();
			rm.setData(PlateTree);
			rm.setState("200");
 		} catch (Exception e) {
			e.getStackTrace();
			rm.setState("500");
			rm.setMsgInfo(e.getMessage());
		}
		  return rm;
	}
   
   
   
   
   //获取子母板关系树
// private void GatherNode( Proc_Tree PlateTree, DataTable Relation, DataTable Process, object sender)
    private boolean GatherNode(Proc_Tree PlateTree,String cur_object_no)
    {
     if (PlateTree == null) return false;
     String object_no = PlateTree.getId();
     PrcMaterielProcess pmp=new PrcMaterielProcess();
     pmp.setObject_no(object_no);
     List<PrcMaterielProcess> drs = this.getMapper(PrcMaterielProcessMapper.class).doQueryAry(pmp);
     
    // DataRow[] drs = Process.Select(string.Format("OBJECTNO='{0}'", object_no), "CREATE_TIME");
     if(drs!=null && drs.size()>0){
    	 for (int iter = 0; iter < drs.size(); iter++)
         {
             PlateTree.self.add(drs.get(iter));
         }
     }else{
    	 PrcMaterielProcess ppp=new PrcMaterielProcess();
    	 //ppp.setObject_no(cur_object_no);
    	 ppp.setObject_no(PlateTree.getId());
    	 ppp.setPrc_code("0");
    	 ppp.setPrc_code_desc("暂无跟踪");
    	 PlateTree.self.add(ppp);
     }
     // PlateTree.setSelf(drs);

    // drs = Relation.Select(string.Format("object_no='{0}'", strPlate));
     List<PrcMaterielRelation> lmm =this.getMapper(PrcMaterielRelationMapper.class).findByObjectNo(object_no);
     
     if(lmm!=null && lmm.size()>0){
         for (int iter = 0; iter < lmm.size(); iter++)
         {
        	 boolean higli;
             Proc_Tree chld_tree = new Proc_Tree(lmm.get(iter).getSub_object_no());
             higli=GatherNode(chld_tree,cur_object_no);
            if(higli){            	
            	PlateTree.setHighLight(true);
            };
            
            if (PlateTree.children == null) PlateTree.children = new ArrayList<Proc_Tree>();
             PlateTree.children.add(chld_tree);
         } 
     }
                  
     if(PlateTree.getId().trim().equals(cur_object_no.trim()))
     {
    	 PlateTree.setHighLight(true);
     }             
     if(PlateTree.getHighLight()){
    	 return true;
     }else{
    	 return false;
     }         
        
	//return false;
 }
 
    /**
     * 物料进程查询-库存查询
     * @param YdmProductDetail.coilno卷号  orderno 销售订单号  prodorderno生产订单号
     * @return
     * @throws Exception 
     */
    @Transactional(value="tm", rollbackFor=Exception.class)
    public ResultModel doQueryWebDetail1(CommonPage<YdmProductDetail> cp) throws Exception{
    	
    	// 补进程数据20230308刘侣
//    	PrcMaterielProcess pmp = new PrcMaterielProcess();
//		Date date =new  Date();
//		pmp.setObject_no("Y2321063P0101");
//		pmp.setPrc_code("5BYS10");
//		pmp.setCreate_time(date);
//		pmp.setRemark("装车出库:赣K62273");
//		pmp.setCreate_man_name("施燕");
//		PrcMaterielProcess pmp1 = new PrcMaterielProcess();
//		pmp1.setObject_no("Y2321063P0101");
//		pmp1.setPrc_code("5BYZ10");
//		pmp1.setCreate_time(date);
//		pmp1.setRemark("20230226110444");
//		pmp1.setCreate_man_name("施燕");
//		this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp1);
//		this.getBean(MesApiServiceImpl.class).INSERT_OBJ_PROCESS(pmp);
    	// end
    	
 	   	ResultModel rm = new ResultModel();
 	   	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();
		} 	      
 	   try {
 		   String s1="4";
 		   String s2="45";
 		   rm.setData(this.getMapper(YdmProductDetailMapper.class).doQueryWebByProcess1(s1,s2, cp.getStartTime(), cp.getEndTime(), cp.getObject().getCoilno(), cp.getObject().getJudgesteelcode(), 
					hmin,hmax,wmin,wmax,cp.getObject().getStorageattr(),cp.getObject().getState(),cp.getObject().getMaterailcoilno(),cp.getObject().getOrderno(),cp.getObject().getProdorderno(),
					cp.getObject().getButtressno(),cp.getObject().getAreano(),cp.getObject().getCustomer_no(),cp.getMemo5()
 				  ));
 		   rm.setState("200");
 	   } catch (Exception e) {
 		   rm.setState("500");
 		   rm.setMsgInfo(e.getMessage());
 	   }
 	   return rm;
    }
    
    /**
     * 生产进程查询-查询对应的物料
     * @param YdmProductDetail.coilno卷号  orderno 销售订单号  prodorderno生产订单号
     *memo5 : 1 标示生产订单进程页面调用 
     * 
     * @return
     */
    public ResultModel doQueryOrderMat(CommonPage<YdmProductDetail> cp){
    	ResultModel rm = new ResultModel();
    	try {
    		rm.setData(this.getMapper(YdmProductDetailMapper.class).doQueryOrderMat(cp.getObject().getProdorderno()));
    		rm.setState("200");
    	} catch (Exception e) {
    		rm.setState("500");
    		e.getStackTrace();
    		rm.setMsgInfo(e.getMessage());
    	}
    	return rm;
    }
    


}
