/***文档注释***********************************************
 * 作者            ：panbin
 * 创建日期     ：2018-11-08
 * 描述            ：物料附加信息表
 * 注意事项     ：
 * 遗留BUG   ：
 * 修改日期     ：
 * 修改人员     ：
 * 修改内容     ：
***********************************************************/
package market.Mes.MaterielTrack.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import market.Api.Mes.impl.MesApiDesignInfoImpl;
import market.Api.Mes.impl.MesApiServiceImpl;
import market.Api.Mes.impl.ProcessPoint;
import market.Api.Mes.impl.Utils;
import market.Com.mapper.ComBaseInfoMapper;
import market.Com.vo.ComBaseInfo;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Lms.Warehouse.vo.YdmProductDetail;
import market.Mes.Lengthwise.Plan.mapper.PlnMaterielDetailMapper;
import market.Mes.Lengthwise.Plan.vo.PlnMaterielDetail;
import market.Mes.MaterielTrack.mapper.PrcMaterielExtinfoMapper;
import market.Mes.MaterielTrack.service.PrcMaterielExtinfoService;
import market.Mes.MaterielTrack.vo.PrcMaterielExtinfo;

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

@RemoteService(path="PrcMaterielExtinfoService")
public class PrcMaterielExtinfoServiceImpl extends BaseServiceImpl<PrcMaterielExtinfo,Object> implements PrcMaterielExtinfoService {
	
	/**
	 * 查询已挂单未完成的虚拟物料信息
	 * @param oci
	 * @return
	 */
	public ResultModel doQueryWeb(CommonPage<PlnMaterielDetail> oci){
		ResultModel rm=new ResultModel();
		PlnMaterielDetail pme = oci.getObject()==null? new PlnMaterielDetail():oci.getObject();
		String sWhere = "where 1=1" ; 
		if(Utils.isNotEmpty(oci.getStartTime())){
			sWhere+=" and p.CREATE_TIME >= to_date('"+oci.getStartTime()+"','yyyy-MM-dd HH24:mi:ss') ";
		}
		if(Utils.isNotEmpty(oci.getEndTime())){
			sWhere+=" and p.CREATE_TIME <= to_date('"+oci.getEndTime()+"','yyyy-MM-dd HH24:mi:ss')";
		}
		// 工序点信息
				List<ComBaseInfo> infoi = this.getMapper(ComBaseInfoMapper.class)
						.findWithCondition(null, "sort_code = '2002' AND validflag = '1'",
								null, 0, 0);
				ProcessPoint processpoint = new ProcessPoint(infoi);
				
		try{
			List<PlnMaterielDetail> lplps=this.getMapper(PlnMaterielDetailMapper.class).doQueryMaterielInfo(pme,sWhere,processpoint);
			rm.setData(lplps);
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 查询物料附加信息
	 * @param object_no
	 * @return
	 */
	public ResultModel doQueryMaterielExinfo(String object_no){
		ResultModel rm=new ResultModel();
		try{
			PlnMaterielDetail lplps=this.getMapper(PlnMaterielDetailMapper.class).findById(object_no);
			rm.setData(lplps);
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 * 查询物料作业路径/工艺路径/实际完成路径
	 * @param object_no
	 * @return
	 */
	public ResultModel doQueryMatInfo(String object_no){
		ResultModel rm=new ResultModel();
		try{
			PrcMaterielExtinfo pme = this.getMapper(PrcMaterielExtinfoMapper.class).findById(object_no);
			if (Utils.isNotEmpty(pme)) {
				if (pme.getActual_process() !=null) {
					String[] Apath = pme.getActual_process().split("-");
					ArrayList<String> Avalidpath = new ArrayList<String>(Arrays.asList(Apath));
					pme.setActual_process_names(this.getBean(MesApiDesignInfoImpl.class).getpath_name(Avalidpath));
				}
			}
			rm.setData(pme);
			rm.setState("200");
		}catch(Exception ex){
			ex.printStackTrace();
			rm.setState("500");
			rm.setMsgInfo("查询失败!"+ex.getMessage());
		}
		return rm;
	}
	
	/**
	 *成品挂单 库存品查询
	 * */
	public ResultModel queProducts(CommonPage<YdmProductDetail> cp) {
		ResultModel rm=new ResultModel();
		int pageIndex=cp.getPageIndex();
		int pageSize=cp.getPageSize();
		PageModel<YdmProductDetail> pm = new PageModel<YdmProductDetail>(pageIndex,pageSize);
		try {
			Object wjmin="",wjmax="",thickmin="",thickmax="";
			if(cp.getMemo1()!=null&&!"".equals(cp.getMemo1()))
				wjmin=Double.valueOf(cp.getMemo1()).doubleValue();
			if(cp.getMemo2()!=null&&!"".equals(cp.getMemo2()))
				wjmax=Double.valueOf(cp.getMemo2()).doubleValue();

			if(cp.getMemo3()!=null&&!"".equals(cp.getMemo3()))
				thickmin=Double.valueOf(cp.getMemo3()).doubleValue();
			if(cp.getMemo4()!=null&&!"".equals(cp.getMemo4()))
				thickmax=Double.valueOf(cp.getMemo4()).doubleValue();

			Object thick,width;
			if(cp.getObject().getThick()!=null&&cp.getObject().getThick()==0)
				thick="";
			else 
				thick=cp.getObject().getThick();
			if(cp.getObject().getWidth()!=null&&cp.getObject().getWidth()==0)
				width="";
			else 
				width=cp.getObject().getWidth();
			String order =null , condition = null ,ord_pk = null ;
			// 匹配采购订单号
			if (cp.getObject().getOrderno()!=null&&!"".equals(cp.getObject().getOrderno())) {
				ResultModel rms =this.getBean(MesApiServiceImpl.class).getPurchaseContractNoInfo(cp.getObject().getOrderno());
				if (rms.getData()==null) {
					order =null;
				}else {
					order =rms.getData().toString();
				}
			}
			if((cp.getObject().getJudgesteelcode()==null||"".equals(cp.getObject().getJudgesteelcode()))&&cp.getObject().getSteelcode()!=null&&!"".equals(cp.getObject().getSteelcode()))
				cp.getObject().setJudgesteelcode(cp.getObject().getSteelcode());

			pm.setRows(this.getMapper(PrcMaterielExtinfoMapper.class).queProducts(
					(pageIndex-1)*pageSize, (pageIndex-1)*pageSize+pageSize, null, null, cp.getObject().getCoilno(), cp.getObject().getSteel_name(), 
					thick,width,null,null,cp.getObject().getJudgesteelcode()
					,thickmin,thickmax,order,cp.getObject().getOrder_nature(),cp.getObject().getStorageattr(),cp.getObject().getOrder_nature()
					,cp.getObject().getMaterailcoilno(),cp.getMemo5(),condition)
					);
			pm.setTotal(this.getMapper(PrcMaterielExtinfoMapper.class).queProductsCount
					(null, null, cp.getObject().getCoilno(), cp.getObject().getSteel_name(), 
							thick,width,null,null,cp.getObject().getJudgesteelcode()
							,thickmin,thickmax,order,cp.getObject().getOrder_nature(),cp.getObject().getStorageattr(),cp.getObject().getOrder_nature()
							,cp.getObject().getMaterailcoilno(),cp.getMemo5(),condition
							));
			rm.setData(pm);
			rm.setState("200");
		}
		catch(Exception ex)
		{
			rm.setState("500");
			rm.setMsgInfo("服务端异常，查询失败!");
			ex.printStackTrace();
		}
		return rm;
	}
	
	
}
