package com.dev.swms.server.service.interfaceLog.pojo;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.pdfbox.io.IOUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.dev.energy.server.exception.BusinessException;
import com.dev.energy.server.model.EntityFactory;
import com.dev.energy.server.model.security.User;
import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.web.security.UserHolder;
import com.dev.swms.server.model.abstractEntity.Pinfan;
import com.dev.swms.server.model.base.BillType;
import com.dev.swms.server.model.base.Item;
import com.dev.swms.server.model.base.ItemKey;
import com.dev.swms.server.model.base.ItemRelation;
import com.dev.swms.server.model.base.Location;
import com.dev.swms.server.model.base.LotInfo;
import com.dev.swms.server.model.base.NewLotInfo;
import com.dev.swms.server.model.base.Warehouse;
import com.dev.swms.server.model.base.WarehouseArea;
import com.dev.swms.server.model.interfaceLog.InterfaceLog;
import com.dev.swms.server.model.inventory.Inventory;
import com.dev.swms.server.model.inventory.InventoryLog;
import com.dev.swms.server.model.inventory.InventoryLogType;
import com.dev.swms.server.model.inventory.Task;
import com.dev.swms.server.model.inventory.TaskLog;
import com.dev.swms.server.model.receiving.ASN;
import com.dev.swms.server.model.receiving.ASNDetail;
import com.dev.swms.server.model.receiving.ASNStatus;
import com.dev.swms.server.model.receiving.MoveDoc;
import com.dev.swms.server.model.receiving.MoveDocDetail;
import com.dev.swms.server.model.receiving.MoveDocStatus;
import com.dev.swms.server.model.receiving.ReceivedRecord;
import com.dev.swms.server.model.shipping.PickTicket;
import com.dev.swms.server.model.shipping.PickTicketDetail;
import com.dev.swms.server.model.shipping.PickTicketStatus;
import com.dev.swms.server.model.shipping.PlanPickTicketDetail;
import com.dev.swms.server.model.shipping.PlanShipDetail;
import com.dev.swms.server.model.shipping.PrintXppRecord;
import com.dev.swms.server.service.base.ItemManager;
import com.dev.swms.server.service.interfaceLog.InterfaceManager;
import com.dev.swms.server.service.receiving.ASNManager;
import com.dev.swms.server.service.sequence.BussinessCodeManager;
import com.dev.swms.server.utils.DateUtil;
import com.dev.swms.server.utils.DoubleUtils;
import com.dev.swms.server.web.filter.WarehouseHolder;
public class DefaultInterfaceManager extends DefaultBaseManager implements InterfaceManager{
	private final BussinessCodeManager bussinessCodeManager;
	private final ASNManager asnManager;
	public DefaultInterfaceManager(BussinessCodeManager manager, ASNManager asnManager) {
		super();
		this.bussinessCodeManager = manager;
		this.asnManager = asnManager;
	}
	@Override
	public String readJson(String jsonStr) {
		String data="";
		JSONObject json = JSONObject.fromObject(jsonStr);
		System.out.println(json.toString());
		String methodType=json.getString("methodType");
		if(methodType.equals("queryBhList")){//查询列表
			data=queryBhList(json);
		}else if(methodType.equals("scanKey")){//扫描现品票
			data=scanKey(json);
		}else if(methodType.equals("confirmPick")){//现品票拣货确认
			data=confirmPick(json);
		}else if(methodType.equals("queryBhDetailList")){//备货单明细
			data=queryBhDetailList(json);
		}else if(methodType.equals("queryBhXppDetailList")){//备货现品票
			data=queryBhXppDetailList(json);
		}else if(methodType.equals("queryFhList")){//发货列表
			data=queryFhList(json);
		}else if(methodType.equals("queryFhDetailList")){//发货明细
			data=queryFhDetailList(json);
		}else if(methodType.equals("scanItemCode")){//扫描货品备货
			data=scanItemCode(json);
		}else if(methodType.equals("confirmPickForDetail")){//备货明细数量登记
			data=confirmPickForDetail(json);
		}else if(methodType.equals("confirmPickALL")){//整单备货
			data=confirmPickALL(json);
		}else if(methodType.equals("confirmShip")){//发运确认
			data=confirmShip(json);
		}
		return data;
	}
	@Override
	public String readJsonCk(String jsonStr) {
		String data="";
		JSONObject json = JSONObject.fromObject(jsonStr);
		String methodType=json.getString("methodType");
		if(methodType.equals("queryBhListKams")){//备货查询列表
			data=queryBhListKams(json);
		}else if(methodType.equals("scanCkKey")){//备货扫描现品票
			data=scanCkKey(json);
		}else if(methodType.equals("queryBhDetailKams")){//备货明细备货单明细
			data=queryBhDetailKams(json);
		}else if(methodType.equals("queryPlanBhListKams")){//预备货单
			data=queryPlanBhListKams(json);
		}else if(methodType.equals("scanCkPlanKey")){//预备货现品票扫描采集
			data=scanCkPlanKey(json);
		}else if(methodType.equals("gatherPlanKey")){//预备货现品票采集
			data=gatherPlanKey(json);
		}else if(methodType.equals("queryPlanBhDetailKams")){//预备货明细
			data=queryPlanBhDetailKams(json);
		}else if(methodType.equals("scanUpdateKey")){//修改现品票
			data=scanUpdateKey(json);
		}else if(methodType.equals("confirmUpdateKey")){//现品票修改确认
			data=confirmUpdateKey(json);
		}else if(methodType.equals("scanFhCode")){//发货单号
			data=scanFhCode(json);
		}else if(methodType.equals("scanFhTph")){//发货托盘号
			data=scanFhTph(json);
		}else if(methodType.equals("queryFhDetailKams")){//发货明细
			data=queryFhDetailKams(json);
		}
		return data;
	}
	@Override
	public String readJsonKams(String jsonStr) {
		String data="";
		JSONObject json = JSONObject.fromObject(jsonStr);
		String methodType=json.getString("methodType");
		if(methodType.equals("queryRkList")){//查询入库清单列表
			data=queryRkList(json);
		}else if(methodType.equals("scanRkLp")){//扫描托盘
			data=scanRkLp(json);
		}else if(methodType.equals("scanRkKey")){//扫描现品票
			data=scanRkKey(json);
		}else if(methodType.equals("scanRkDstLoc")){//扫描库位上架
			System.out.println(data+"===");
		}else if(methodType.equals("queryRkDetailList")){//入库清单
			data=queryRkDetailList(json);
		}else if(methodType.equals("queryFhList")){//发货列表
			data=queryFhList(json);
		}else if(methodType.equals("queryFhDetailList")){//发货明细
			data=queryFhDetailList(json);
		}else if(methodType.equals("scanItemCode")){//扫描货品备货
			data=scanItemCode(json);
		}else if(methodType.equals("confirmPickForDetail")){//备货明细数量登记
			data=confirmPickForDetail(json);
		}else if(methodType.equals("confirmPickALL")){//整单备货
			data=confirmPickALL(json);
		}else if(methodType.equals("confirmShip")){//发运确认
			data=confirmShip(json);
		}
		return data;
	}
	
	@Override
	public String readJsonXb(String jsonStr) {
		String data="";
		JSONObject json = JSONObject.fromObject(jsonStr);
		String methodType=json.getString("methodType");
		if(methodType.equals("scanXbjs")){//线边接收-发货托盘号
			data=scanXbjs(json);
		}else if(methodType.equals("queryFhDetailKams")){//发货明细
			data=queryFhDetailKams(json);
		}
		return data;
	}
	
	public String queryBhList(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "备货单列表查询", json.toString(), null);
    	String queryInfo=json.getString("queryInfo");
    	String bhType=json.getString("bhType");
    	String hql="select p.id,p.company.name, p.relatedBill2,DATE_FORMAT(p.orderDate,'%Y-%m-%d '),"
    			+ "cast(p.orderQuantity,int),cast(p.pickedQuantity,int) from PickTicket p where p.status='ALLOCATE' and p.warehouse.id=1020 ";
    	if(queryInfo!=null&&!queryInfo.equals("")){
    		hql+=" and p.relatedBill2='"+queryInfo+"'";
    	}
    	if(bhType.equals("QTCK")){
    		hql+=" and p.billType.code='QTCK'";
    	}else{
    		hql+=" and p.billType.code!='QTCK'";
    	}
    	List<Object[]> objs=this.commonDao.findByQuery(hql);
    	JSONArray  array = new JSONArray();
    	JSONObject h = new JSONObject();
    	h.put("pId", "序号");
    	h.put("companyName", "供应商");
		h.put("relatedBill2", "单号");
		h.put("orderDate", "订单日期");
		h.put("orderQuantity", "订单数量");
		h.put("pickedQuantity", "备货数量");
		array.add(h);
		for(Object[] o:objs) {
			try {
				int j=0;
				String pId=o[j++].toString();
				String companyName=o[j++].toString();
				String relatedBill2=o[j++].toString();
				String orderDate=o[j++].toString();
				Double orderQuantity=Double.parseDouble(o[j++].toString());
				Double pickedQuantity=Double.parseDouble(o[j++].toString());
				JSONObject d = new JSONObject();
				d.put("pId", pId);
				d.put("companyName", companyName);
				d.put("relatedBill2", relatedBill2);
				d.put("orderDate", orderDate);
				d.put("orderQuantity", orderQuantity.intValue());
				d.put("pickedQuantity", pickedQuantity.intValue());
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	public String queryBhListKams(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "备货单列表查询", json.toString(), null);
    	String queryInfo=json.getString("queryInfo");
//    	String bhType=json.getString("bhType");
    	String hql="select p.id from "
    			+ "PickTicket p where p.warehouse.id=1 "
    			+ " and p.relatedBill1 in ('日出库单','成套出库')";
    	if(queryInfo!=null&&!queryInfo.equals("")){
    		hql+=" and (p.relatedBill2='"+queryInfo+"' or p.code='"+queryInfo+"')";
    	}else{
    		hql+=" and p.allocatedQuantity>p.pickedQuantity and p.shippedQuantity<=0";
    	}
    	hql+=" order by p.id ";
//    	if(bhType.equals("QTCK")){
//    		hql+=" and p.billType.code='QTCK'";
//    	}else{
//    		hql+=" and p.billType.code!='QTCK'";
//    	}
    	List<Long> objs=this.commonDao.findByQuery(hql);
    	JSONArray  array = new JSONArray();
		for(Long o:objs) {
			try {
				JSONObject d = new JSONObject();
				PickTicket pt=this.commonDao.get(PickTicket.class, o);
				d.put("pt_id", pt.getId());
				d.put("order_date", DateUtil.formatDateYMD01ToStr(pt.getOrderDate()));
				d.put("order_quantity", pt.getOrderQuantity());
				d.put("allocated_quantity",pt.getAllocatedQuantity());
				d.put("picked_quantity", pt.getPickedQuantity());
				d.put("pt_code", pt.getCode());
				d.put("gw", pt.getRelatedBill3());
				d.put("sb", pt.getApplyDoc());
//				d.put("ybhxs", getPickBhxs(pt.getId())+"");
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	public String scanCkKey(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "出库现品票扫描", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
	    	String xpp=json.getString("xpp");
	    	String tph=json.getString("tph");
	    	Long pt_id=json.getLong("pt_id");
	    	if(xpp!=null&&!xpp.equals("")&&xpp.length()==88){
	    		String pfh=xpp.substring(3, 13);
				String ac=xpp.substring(13, 16);
				String nrs=xpp.substring(46, 52);
				double qty=Double.parseDouble(nrs);
				if(tph==null||tph.equals("")){
					errorMes="托盘号必填";
				}else{
					List<String> qt_its=this.commonDao.findByQuery("select srcLPCode from TaskLog l where l.srcLPCode!='"+tph+"' and l.itemKey.item.code='"+pfh+"'"
							+ " and l.task.pickTicketDetail.pickTicket.id="+pt_id);
					if(qt_its.size()>0){
						errorMes="该品番已在其他托盘"+qt_its.get(0);
					}else{
						PickTicket pt=commonDao.get(PickTicket.class, pt_id);
						List<PickTicketDetail> ptds=commonDao.findByQuery("from PickTicketDetail d where d.pickTicket.id="+pt_id+" and d.item.code='"+pfh+"' and d.allocatedQuantity>pickedQuantity");
						if(ptds.size()>0){
							PickTicketDetail ptd=ptds.get(0);
							List<Task> ts=this.commonDao.findByQuery("from Task t where t.pickTicketDetail.id="+ptd.getId()+" and t.planQuantityMU>movedQuantityMU");
							if(ts.size()>0){
								List<Long> logs=this.commonDao.findByQuery("select id from TaskLog l where l.barCode='"+xpp+"'");
								if(logs.size()>0){
									errorMes="现品票已出库";
								}else{
									List<Long> ps=this.commonDao.findByQuery("select id from PrintXppRecord r where r.xpp='"+xpp+"'");
									Long xxp_id=0l;
									if(ps.size()>0){
										xxp_id=ps.get(0);
									}
									Task t=ts.get(0);
									obj.put("ptd_id", ptd.getId());
									obj.put("task_id", t.getId());
									obj.put("item_code", ptd.getItem().getCode());
									obj.put("item_name", ptd.getItem().getName());
									obj.put("kysl", qty);
									obj.put("ybhs",(int)pt.getPickedQuantity());
									obj.put("srs", ptd.getQuantity());
									obj.put("ybhxs", getPickBhxs(pt_id));
									obj.put("src_loc", t.getSrcLoc().getCode());
									obj.put("xxp_id", xxp_id);
								}
							}
						}else{
							errorMes=pfh+"已备货完成";
						}
					}
		    	}
	    	}else{
	    		errorMes="现品票长度不对";
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	public String queryPlanBhListKams(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "预备货单列表查询", json.toString(), null);
    	String queryInfo=json.getString("queryInfo");
    	String hql="select p.id,DATE_FORMAT(p.orderDate,'%Y-%m-%d ') from PickTicket p where p.orderQuantity>p.pickedQuantity"
    			+ " and p.relatedBill1='月出库单' and p.warehouse.id=1";
    	if(queryInfo!=null&&!queryInfo.equals("")){
    		hql+=" and (p.relatedBill2='"+queryInfo+"' or p.code='"+queryInfo+"')";
    	}
    	List<Object[]> objs=this.commonDao.findByQuery(hql);
    	JSONArray  array = new JSONArray();
		for(Object[] o:objs) {
			try {
				int j=0;
				String pId=o[j++].toString();
				String orderDate=o[j++].toString();
				JSONObject d = new JSONObject();
				PickTicket pt=this.commonDao.get(PickTicket.class, new Long(pId));
				d.put("pt_id", pId);
				d.put("order_date", orderDate);
				d.put("allocated_quantity",pt.getAllocatedQuantity());
				d.put("picked_quantity", pt.getPickedQuantity());
				d.put("pt_code", pt.getCode());
				List<PrintXppRecord> xs=this.commonDao.findByQuery("from PrintXppRecord r where r.ptd.pickTicket.id="+pt.getId());
    			d.put("zxs", xs.size());
    			List<PrintXppRecord> bhxs=this.commonDao.findByQuery("from PrintXppRecord r where r.ptd.pickTicket.id="+pt.getId()+" and r.beGather=1");
    			d.put("ybhxs", bhxs.size());
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	public String scanCkPlanKey(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "预备货现品票扫描采集", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
	    	String xpp=json.getString("xpp");
	    	Long pt_id=json.getLong("pt_id");
	    	if(xpp!=null&&!xpp.equals("")&&xpp.length()==88){
	    		List<PrintXppRecord> rs=this.commonDao.findByQuery("from PrintXppRecord r where r.xpp='"+xpp+"' and r.ptd.pickTicket.id="+pt_id);
	    		if(rs.size()>0){
	    			PrintXppRecord r=rs.get(0);
	    			if(r.getBeGather()!=null&&r.getBeGather()){
	    				errorMes="现品票已采集";
	    			}else{
	    				obj.put("xpp_id", r.getId());
	    				PickTicketDetail d =r.getPtd();
	    				Pinfan pf=d.getPf();
	    				obj.put("sb", pf.getBonded());
	    				obj.put("item_code", d.getItem().getCode());
	    				obj.put("item_name", d.getItem().getName());
	    				obj.put("loc_code",r.getCgkw());
	    				obj.put("qty", r.getShsl());
	    			}
	    		}else{
	    			errorMes="现品票不属于该出库单";
	    		}
				
	    	}else{
	    		errorMes="现品票长度不对";
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	public String scanUpdateKey(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "现品票扫描修改采集", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
	    	String xpp=json.getString("xpp");
	    	if(xpp!=null&&!xpp.equals("")&&xpp.length()==88){
	    		List<PrintXppRecord> rs=this.commonDao.findByQuery("from PrintXppRecord r where r.xpp='"+xpp+"'");
	    		if(rs.size()>0){
	    			PrintXppRecord r=rs.get(0);
	    			if(r.getTaskLog()==null){
	    				obj.put("xpp_id", r.getId());
	    				PickTicketDetail d =r.getPtd();
	    				Pinfan pf=d.getPf();
	    				obj.put("sb", pf.getBonded());
	    				obj.put("item_code", d.getItem().getCode());
	    				obj.put("qty", r.getShsl());
	    			}else{
	    				errorMes="现品票已备货";
	    			}
	    		}else{
	    			errorMes="现品票不存在";
	    		}
				
	    	}else{
	    		errorMes="现品票长度不对";
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
    		log.setRequestContent(data);
			this.commonDao.store(log);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		log.setRequestContent(data);
		this.commonDao.store(log);
		return data;
	}
	
	public String confirmUpdateKey(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "现品票修改确认", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
	    	Long xpp_id=json.getLong("xpp_id");
	    	PrintXppRecord p=this.commonDao.get(PrintXppRecord.class, xpp_id);
	    	if(p.getTaskLog()==null){
	    		String sb=json.getString("sb");
		    	String qty=json.getString("qty");
		    	String userName=json.getString("user_name");
		    	p.setShsl(Integer.parseInt(qty));
				p.setUpdateDate(new Date());
				p.setUpdateBy(userName);
//				DecimalFormat decimalFormat=new DecimalFormat("000000");
//				String nrs_h=decimalFormat.format(p.getShsl());
//				StringBuffer xpp=new StringBuffer();
//				xpp.append(p.getXpp());
//				xpp.replace(46, 52,nrs_h);
//				p.setXpp(xpp.toString());
				this.commonDao.store(p);
	    	}else{
	    		errorMes="现品票已备货";
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
			log.setRequestContent(data);
			this.commonDao.store(log);
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
    		log.setRequestContent(data);
			this.commonDao.store(log);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		log.setRequestContent(data);
		this.commonDao.store(log);
		return data;
	}
	
	
	public String gatherPlanKey(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "备货现品票采集确认", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
	    	Long xpp_id=json.getLong("xpp_id");
	    	Long pt_id=json.getLong("pt_id");
	    	String bhr=json.getString("bhr");
	    	PrintXppRecord r=this.commonDao.get(PrintXppRecord.class, xpp_id);
			if(r.getBeGather()!=null&&r.getBeGather()){
				errorMes="现品票已采集";
			}else{
    			r.setBeGather(Boolean.TRUE);
    			r.setGatherBy(bhr);
    			r.setGatherDate(new Date());
    			this.commonDao.store(r);
    			PickTicketDetail d =r.getPtd();
    			d.setPickedQuantity(d.getPickedQuantity()+r.getShsl());
    			this.commonDao.store(d);
    			PickTicket pt =d.getPickTicket();
    			pt.setPickedQuantity(pt.getPickedQuantity()+r.getShsl());
    			this.commonDao.store(pt);
    			if(pt.getPickedQuantity()==pt.getOrderQuantity()){
    				pt.setStatus(PickTicketStatus.PICK_ALL);
    				this.commonDao.store(pt);
    			}
    			obj.put("pick_qty", pt.getPickedQuantity());
    			List<PrintXppRecord> bhxs=this.commonDao.findByQuery("from PrintXppRecord r where r.ptd.pickTicket.id="+pt_id+" and r.beGather=1");
    			obj.put("ybhxs", bhxs.size());
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
    		log.setRequestContent(data);
			this.commonDao.store(log);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		log.setRequestContent(data);
		this.commonDao.store(log);
		return data;
	}
	
	public String scanKey(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "现品票扫描", json.toString(), null);
    	String key=json.getString("key");
    	String pId=json.getString("pId");
    	Long ptdId=null;
    	JSONObject obj= new JSONObject();
    	PlanPickTicketDetail pptd=null;
    	try{
	    	List<PlanPickTicketDetail> pptds=this.commonDao.findByQuery("from PlanPickTicketDetail d where d.psd.key='"+key+"' and d.pickTicket.id="+pId);
	    	if(pptds.size()<=0){
	    		errorMes="现品票不在该备货单";
	    	}else{
	    		pptd=pptds.get(0);
	    		if(pptd.getPickedQuantity()>0){
	    			errorMes="现品票已登记";
	    		}else{
	    			List<PickTicketDetail> ptds=this.commonDao.findByQuery("from PickTicketDetail d where d.pickTicket.id="+pId+" and d.item.id="+pptd.getItemKey().getItem().getId()
	    					+" and d.SOI='"+pptd.getPsd().getDdbh()+"'");
	    			if(ptds.size()<0){
	    				errorMes="找不到对应备货明细";
	    			}
	    			ptdId=ptds.get(0).getId();
	    		}
	    	}
	    	if(errorMes!=null&&!errorMes.equals("")){
	    		data=returnJson("400", errorMes, returnData);
				return data;
	    	}else{
	    		PlanShipDetail psd=pptd.getPsd();
				obj.put("pptdId", pptd.getId());
				obj.put("ptdId",ptdId);
				obj.put("key", psd.getKey());
				obj.put("ddbh", psd.getDdbh());
				obj.put("itemCode", psd.getItem().getCode());
				obj.put("itemName", psd.getItem().getName());
				obj.put("shsl", psd.getShsl());
	    	}
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String scanItemCode(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "其他发货单备货明细扫描", json.toString(), null);
    	String itemCode=json.getString("itemCode");
    	String pId=json.getString("pId");
    	Long ptdId=null;
    	JSONObject obj= new JSONObject();
    	PickTicketDetail ptd=null;
    	try{
    			List<PickTicketDetail> ptds=this.commonDao.findByQuery("from PickTicketDetail d where d.pickTicket.id="+pId+" and d.item.code='"+itemCode+"'");
    			if(ptds.size()<0){
    				errorMes="找不到对应备货明细";
    			}else{
    				ptd=ptds.get(0);
    				if(ptd.getPickedQuantity()>0){
    					errorMes=itemCode+"已备货完成";
    				}
    			}
    		
	    	if(errorMes!=null&&!errorMes.equals("")){
	    		data=returnJson("400", errorMes, returnData);
				return data;
	    	}else{
				obj.put("ptdId",ptd.getId());
				obj.put("itemCode", ptd.getItem().getCode());
				obj.put("itemName", ptd.getItem().getName());
				obj.put("ddsl", ptd.getOrderQtyOfMasterUnit());
				obj.put("shsl",  ptd.getOrderQtyOfMasterUnit());
	    	}
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String confirmPick(JSONObject json) {
		String data="";
		String returnMsg="操作成功";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "现品票拣货确认", json.toString(), null);
    	Long pId=Long.parseLong(json.getString("pId"));
   
    	Long ptdId=Long.parseLong(json.getString("ptdId"));
    	String key="";//json.getString("key");
    	String ckType=json.getString("ckType");
    	String errorMes="";
    	String itemCode="";
    	double shsl=0;
    	PlanPickTicketDetail pptd=null;
    	try {
    		PickTicketDetail ptd=this.commonDao.get(PickTicketDetail.class, ptdId);;
    		PickTicket pt=this.commonDao.get(PickTicket.class, pId);
    		if(ckType.equals("XPP")){
    		 	Long pptdId=Long.parseLong(json.getString("pptdId"));
    			pptd=this.commonDao.get(PlanPickTicketDetail.class, pptdId);
    			key=json.getString("key");
    			shsl=pptd.getOrderQuantity();
    			if(pt==null||!pt.getStatus().equals(PickTicketStatus.ALLOCATE)){
    	    		errorMes="备货单状态不对";
    	    	}else{
		    		if(pptd.getPickedQuantity()>0){
		    			errorMes="现品票已登记";
		    		}else{
	    				if(ptd.getOrderQuantity()-ptd.getPickedQuantity()<pptd.getOrderQuantity()){
	    					returnMsg="待备货数量不足";
	    				}
		    		}
    	    	}
    		}else{
    			itemCode=json.getString("itemCode");
    			shsl=json.getDouble("shsl");
    			//根据出库明细找现品票
    			if(pt==null||!pt.getStatus().equals(PickTicketStatus.ALLOCATE)){
    	    		errorMes="备货单状态不对";
    	    	}else{
    				if(ptd.getOrderQuantity()-ptd.getPickedQuantity()<shsl){
    					returnMsg="待备货数量不足";
    				}
    	    	}
    		}
    		if(errorMes!=null&&!errorMes.equals("")){
    			data=returnJson("400", errorMes, returnData);
    			return data;
    		}else{
    			if(ckType.equals("XPP")){
    				//更新记录
    				pptd.setShipTime(new Date());
    				pptd.setBePick(Boolean.TRUE);
    				pptd.setPickedQuantity(shsl);
    				this.commonDao.store(pptd);
    				//如果ptd下所有pptd都备货完成，则备货完成
        			ptd.setPickedQuantity(ptd.getPickedQuantity()+shsl);
        			if(ptd.getPickedQuantity()-ptd.getOrderQtyOfMasterUnit()==0){
	    				ptd.setShipTime(new Date());
	    				ptd.setOldRecord(Boolean.TRUE);
        			}
    				this.commonDao.store(ptd);
    				pt.setPickedQuantity(pt.getPickedQuantity()+shsl);
    				if(pt.getPickedQuantity()-pt.getOrderQuantity()==0){
    					pt.setStatus(PickTicketStatus.PICK_ALL);
    					pt.setShipDateStart(new Date());
    				}
    				this.commonDao.store(pt);
    			}else{
    				ptd.setPickedQuantity(ptd.getPickedQuantity()+shsl);
    				ptd.setShipTime(new Date());
    				ptd.setOldRecord(Boolean.TRUE);
    				this.commonDao.store(ptd);
    				pt.setPickedQuantity(pt.getPickedQuantity()+shsl);
    				if(pt.getPickedQuantity()-pt.getOrderQuantity()==0){
    					pt.setStatus(PickTicketStatus.PICK_ALL);
    					pt.setShipDateStart(new Date());
    				}
    				this.commonDao.store(pt);
    			}
    			//库存扣减
				List<Inventory> invs=this.commonDao.findByQuery("from Inventory inv where inv.itemKey.id="+ptd.getItemKey().getId()+" and inv.zone.id="+ptd.getZone().getId()
						+" and inv.location.id="+ptd.getLocation().getId());
				if(invs.size()>0){
					double kjQty=shsl;
					double syqty=shsl;
					for(Inventory inv:invs){
						if(syqty>0){
							ItemKey itemkey=inv.getItemKey();
							if(inv.getQueuedQuantity()>=kjQty){
								kjQty=kjQty;
							}else{
								kjQty=inv.getQueuedQuantity();
							}
							syqty=syqty-kjQty;
							String billCode=ptd.getPickTicket().getRelatedBill2()==null?ptd.getPickTicket().getCode():ptd.getPickTicket().getRelatedBill2();
							InventoryLog log1 = new InventoryLog(InventoryLogType.PICK_DOWN, -1, billCode, null, inv.getLocation(), null, itemkey, 
									new Date(), pt.getCode(), kjQty, null, null, ptd.getId(), null,inv.getZone());
							commonDao.store(log1);
							inv.setQueuedQuantity(inv.getQueuedQuantity()-shsl);
							inv.remove(kjQty,0,0);
							this.commonDao.store(inv);
							//增加库存
							Inventory inventoryNew =null;
							Location loc=this.commonDao.get(Location.class, 525l);
							List<Inventory> news=this.commonDao.findByQuery("from Inventory inv where inv.itemKey.id="+itemkey.getId()+" "
									+ " and inv.location.id="+loc.getId()+" and inv.SOI='"+billCode+"'");
							if(news.size()>0){
								inventoryNew=news.get(0);
							}else{
								inventoryNew=EntityFactory.getEntity(Inventory.class);
								inventoryNew.setLocation(loc);
								inventoryNew.setItemKey(itemkey);
								inventoryNew.setStatus(inv.getStatus());
								inventoryNew.setZone(loc.getZone());
								inventoryNew.setSOI(billCode);
								itemkey.getInventories().add(inventoryNew);
								this.commonDao.store(inventoryNew);
							}
							inventoryNew.add(kjQty,0,0);
							this.commonDao.store(inventoryNew);
							InventoryLog log2 = new InventoryLog(InventoryLogType.PICK_IN, 1, billCode, null, loc, null, itemkey, 
							new Date(), pt.getCode(), kjQty, null, null, ptd.getId(), null,inventoryNew.getZone());
							commonDao.store(log2);
						}else{
							break;
						}
						
					}
				}
				//全部发运完成需要更新状态
				JSONObject obj = new JSONObject();
				obj.put("pickedQuantity", pt.getPickedQuantity());
				obj.put("status", pt.getStatus());
				returnData=obj.toString();
    		}
    	} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String confirmPickALL(JSONObject json) {
		String data="";
		String returnMsg="操作成功";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "整单拣货确认", json.toString(), null);
    	Long pId=Long.parseLong(json.getString("pId"));
    	String errorMes="";
    	try {
    		PickTicket pt=this.commonDao.get(PickTicket.class, pId);
    		List<PickTicketDetail> ptds=this.commonDao.findByQuery("from PickTicketDetail d where d.pickTicket.id="+pId);
			if(pt==null||!pt.getStatus().equals(PickTicketStatus.ALLOCATE)){
	    		errorMes="备货单状态不对";
	    	}else{
    			for(PickTicketDetail ptd:ptds){
					//判断库存是否足够
					List<Inventory> invs=this.commonDao.findByQuery("from Inventory inv where inv.itemKey.id="+ptd.getItemKey().getId()+" and inv.zone.id="+ptd.getZone().getId()
							+" and inv.location.id="+ptd.getLocation().getId());
					double countinvQty=0;
					for(Inventory inv:invs){
						countinvQty+=inv.getQueuedQuantity();
					}
					double shsl=0;
					//如果为0
					if(ptd.getPickedQuantity()==0&&(ptd.getNote()!=null&&ptd.getNote().equals("APP"))){
						//释放现品票及库存
					}else{
						//判断扣除的库存是否满足
						shsl=ptd.getPickedQuantity()>0?ptd.getPickedQuantity():ptd.getOrderQuantity();
					}
					if(shsl>countinvQty){
						errorMes="库存不足";
					}
				}
	    	}
    		if(errorMes!=null&&!errorMes.equals("")){
    			data=returnJson("400", errorMes, returnData);
    			return data;
    		}else{
				for(PickTicketDetail ptd:ptds){
					List<Inventory> invs=this.commonDao.findByQuery("from Inventory inv where inv.itemKey.id="+ptd.getItemKey().getId()+" and inv.zone.id="+ptd.getZone().getId()
							+" and inv.location.id="+ptd.getLocation().getId());
					double shsl=0;
					//如果为0
					if(ptd.getPickedQuantity()==0&&(ptd.getNote()!=null&&ptd.getNote().equals("APP"))){
						//释放现品票及库存
						List<PlanPickTicketDetail> pptds=this.commonDao.findByQuery("from PlanPickTicketDetail d where d.psd.item.id="+ptd.getItem().getId()
								+" and d.psd.ddbh='"+ptd.getSOI()+"'");
						if(invs.size()>0){
							Inventory inv=invs.get(0);
							inv.setQueuedQuantity(inv.getQueuedQuantity()-ptd.getOrderQtyOfMasterUnit());
//							inv.remove(0,0,0);
							this.commonDao.store(inv);
						}
						for(PlanPickTicketDetail pptd:pptds){
							if(0>=pptd.getAllocatedQuantity()){
								pptd.setPickedQuantity(0);
								pptd.setShipTime(new Date());
								pptd.setBePick(Boolean.TRUE);
								this.commonDao.store(pptd);
							}
						}
						ptd.setPickedQuantity(0d);
						ptd.setShipTime(new Date());
						ptd.setOldRecord(Boolean.TRUE);
						this.commonDao.store(ptd);
						if(pt.getRelatedBill3()==null){
							pt.setRelatedBill3(this.bussinessCodeManager.getCodeForYm("CK",5));
							pt.setShipDateEnd(new Date());
							this.commonDao.store(pt);
						}
					}else{
						//判断扣除的库存是否满足
						shsl=ptd.getPickedQuantity()>0?ptd.getPickedQuantity():ptd.getOrderQuantity();
					}
					ptd.setPickedQuantity(shsl);
					ptd.setShipTime(new Date());
					ptd.setOldRecord(Boolean.TRUE);
					//库存扣减
					if(invs.size()>0){
						double kjQty=shsl;
						double syqty=shsl;
						for(Inventory inv:invs){
							if(syqty>0){
								ItemKey itemkey=inv.getItemKey();
								if(inv.getQueuedQuantity()>=kjQty){
									kjQty=kjQty;
								}else{
									kjQty=inv.getQueuedQuantity();
								}
								syqty=syqty-kjQty;
								String billCode=ptd.getPickTicket().getRelatedBill2()==null?ptd.getPickTicket().getCode():ptd.getPickTicket().getRelatedBill2();
								InventoryLog log1 = new InventoryLog(InventoryLogType.PICK_DOWN, -1, billCode, null, inv.getLocation(), null, itemkey, 
										new Date(), pt.getCode(), kjQty, null, null, ptd.getId(), null,inv.getZone());
								commonDao.store(log1);
								inv.setQueuedQuantity(inv.getQueuedQuantity()-shsl);
								inv.remove(kjQty,0,0);
								this.commonDao.store(inv);
								//增加库存
								Inventory inventoryNew =null;
								Location loc=this.commonDao.get(Location.class, 525l);
								List<Inventory> news=this.commonDao.findByQuery("from Inventory inv where inv.itemKey.id="+itemkey.getId()+" "
										+ " and inv.location.id="+loc.getId()+" and inv.SOI='"+billCode+"'");
								if(news.size()>0){
									inventoryNew=news.get(0);
								}else{
									inventoryNew=EntityFactory.getEntity(Inventory.class);
									inventoryNew.setLocation(loc);
									inventoryNew.setItemKey(itemkey);
									inventoryNew.setStatus(inv.getStatus());
									inventoryNew.setZone(loc.getZone());
									inventoryNew.setSOI(billCode);
									itemkey.getInventories().add(inventoryNew);
									this.commonDao.store(inventoryNew);
								}
								inventoryNew.add(kjQty,0,0);
								this.commonDao.store(inventoryNew);
								InventoryLog log2 = new InventoryLog(InventoryLogType.PICK_IN, 1, billCode, null, loc, null, itemkey, 
								new Date(), pt.getCode(), kjQty, null, null, ptd.getId(), null,inventoryNew.getZone());
								commonDao.store(log2);
							}else{
								break;
							}
						}
						List<PlanPickTicketDetail> pptds=this.commonDao.findByQuery("from PlanPickTicketDetail d where d.psd.item.id="+ptd.getItem().getId()+" "
								+ " and d.psd.ddbh='"+ptd.getSOI()+"' and d.pickTicket.id="+pt.getId() 
								+ " order by d.allocatedQuantity asc");
						double shipQty=shsl;
						for(PlanPickTicketDetail pptd:pptds){
							if(pptd.getAllocatedQuantity()==0||shipQty==0){
								pptd.setBePick(Boolean.TRUE);
								pptd.setShipTime(new Date());
							}else if(shipQty>=pptd.getAllocatedQuantity()){
								pptd.setShipTime(new Date());
								pptd.setBePick(Boolean.TRUE);
								pptd.setPickedQuantity(pptd.getOrderQuantity());
								this.commonDao.store(pptd);
								shipQty=shipQty-pptd.getOrderQuantity();
							}else{
								throw new BusinessException(ptd.getItem().getCode()+"登记数量不满足现品票");
							}
							this.commonDao.store(pptd);
						}
						if(pt.getRelatedBill3()==null){
							pt.setRelatedBill3(this.bussinessCodeManager.getCodeForYm("CK",5));
							pt.setShipDateEnd(new Date());
						}
						pt.setPickedQuantity(pt.getPickedQuantity()+shsl);
						pt.setStatus(PickTicketStatus.PICK_ALL);
						pt.setShipDateStart(new Date());
						this.commonDao.store(pt);		
					}
				}
				//全部发运完成需要更新状态
				JSONObject obj = new JSONObject();
				obj.put("pickedQuantity", pt.getPickedQuantity());
				obj.put("status", pt.getStatus());
				returnData=obj.toString();
    		}
    	} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String confirmPickForDetail(JSONObject json) {
		String data="";
		String returnMsg="操作成功";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "按明细拣货确认", json.toString(), null);
    	Long pId=Long.parseLong(json.getString("pId"));
    	Long ptdId=Long.parseLong(json.getString("ptdId"));
    	double bhQty=Double.parseDouble(json.getString("bhQty"));
    	try {
    		PickTicketDetail ptd=this.commonDao.get(PickTicketDetail.class, ptdId);;
    		PickTicket pt=this.commonDao.get(PickTicket.class, pId);
    		if(pt==null||!pt.getStatus().equals(PickTicketStatus.ALLOCATE)){
    			returnMsg="备货单状态不对";
    			returnCode="400";
	    	}else{
	    		ptd.setPickedQuantity(bhQty);
	    		ptd.setNote("APP");
	    		this.commonDao.store(ptd);
	    	}
				//全部发运完成需要更新状态
				JSONObject obj = new JSONObject();
				obj.put("pickedQuantity", pt.getPickedQuantity());
				obj.put("status", pt.getStatus());
				returnData=obj.toString();
    	} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	
	
	
	public String confirmShip(JSONObject json) {
		String data="";
		String returnMsg="操作成功";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "发运确认", json.toString(), null);
    	Long pId=Long.parseLong(json.getString("pId"));
    	try {
    		PickTicket pt=this.commonDao.get(PickTicket.class, pId);
			if(pt.getStatus().equals(PickTicketStatus.PICK_ALL)){
				pt.setShipDate(new Date());
				pt.setStatus(PickTicketStatus.SHIP_ALL);
				pt.setShippedQuantity(pt.getPickedQuantity());
				this.commonDao.store(pt);
				//扣库存
				Location loc=this.commonDao.get(Location.class, 525l);
				String billCode=pt.getRelatedBill2()==null?pt.getCode():pt.getRelatedBill2();
				List<Inventory> news=this.commonDao.findByQuery("from Inventory inv where  inv.location.id="+loc.getId()+" and inv.SOI='"+billCode+"'");
				for(Inventory inv:news){
					InventoryLog log2 = new InventoryLog(InventoryLogType.SHIPPING, -1, billCode, null, loc, null, inv.getItemKey(), 
							new Date(), pt.getCode(), inv.getQuantity(), null, null, inv.getId(), null,inv.getZone());
					commonDao.store(log2);
					this.commonDao.delete(inv);
				}
			}else{
				errorMes="出库单状态不对";
			}
			if(errorMes!=null&&!errorMes.equals("")){
    			data=returnJson("400", errorMes, returnData);
    			return data;
			}else{
				List<PickTicketDetail> pds=this.commonDao.findByQuery("from PickTicketDetail d where d.pickTicket.id="+pt.getId());
				for(PickTicketDetail pd:pds){
					pd.setShippedQuantity(pd.getPickedQuantity());
					this.commonDao.store(pd);
				}
				List<PlanPickTicketDetail> ppds=this.commonDao.findByQuery("from PlanPickTicketDetail d where d.pickTicket.id="+pt.getId());
				for(PlanPickTicketDetail d:ppds){
					PlanShipDetail psd=d.getPsd();
					if(d.getPickedQuantity()==0&&pt.getOrderQuantity()<pt.getShippedQuantity()){
						psd.setPtd(null);
						this.commonDao.store(psd);
					}
					d.setShippedQuantity(d.getPickedQuantity());
					d.setBeShip(Boolean.TRUE);
					this.commonDao.store(d);
				}
				//全部发运完成需要更新状态
				JSONObject obj = new JSONObject();
				obj.put("shippedQuantity", pt.getShippedQuantity());
				obj.put("status", pt.getStatus());
				returnData=obj.toString();
			}
    	} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String queryBhDetailList(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "备货单明细查询", json.toString(), null);
    	Long pId=json.getLong("pId");
    	PickTicket pt=this.commonDao.get(PickTicket.class, pId);
    	//String queryType=json.getString("queryType");
    	String hql="select d.id,d.psd.mh,d.item.code,d.item.name,d.SOI,"
    			+ " DATE_FORMAT(d.psd.nrzsr,'%Y-%m-%d '),"
    			+ " cast(d.orderQuantity,int),cast(d.pickedQuantity,int) "
    			+ " from PickTicketDetail d left join d.psd  where d.pickTicket.id="+pId;
    	if(pt.getBillType().getCode().equals("QTCK")){
	    	hql="select d.id,'',d.item.code,d.item.name,'',"
	    			+ " '',"
	    			+ " cast(d.orderQuantity,int),cast(d.pickedQuantity,int) "
	    			+ " from PickTicketDetail d left join d.psd  where d.pickTicket.id="+pId;
    	}
    	List<Object[]> objs=this.commonDao.findByQuery(hql);
    	JSONArray  array = new JSONArray();
    	JSONObject h = new JSONObject();
    	h.put("ptdId", "序号");
    	h.put("mh", "门号");
    	h.put("itemCode", "品番");
    	h.put("itemName", "名称");
		h.put("ddbh", "订单编号");
		h.put("nrzsr", "纳入指示日");
		h.put("ddQty", "订单数量");
		h.put("bhQty", "备货数量");
		array.add(h);
		for(Object[] o:objs) {
			try {
				int j=0;
				String ptdId=o[j++].toString();
				String mh=o[j++].toString();
				String itemCode=o[j++].toString();
				String itemName=o[j++].toString();
				String ddbh=o[j++].toString();
				String nrzsr=o[j++].toString();
				String ddQty=o[j++].toString();
				String bhQty=o[j++].toString();
				JSONObject d = new JSONObject();
				PickTicketDetail ptd=this.commonDao.get(PickTicketDetail.class,Long.parseLong(ptdId));
				d.put("ptdId", ptdId);
				d.put("mh", mh);
				d.put("itemCode", itemCode);
				d.put("itemName", itemName);
				d.put("ddbh", ddbh);
				d.put("nrzsr", nrzsr);
				bhQty=(int)ptd.getAllocatedQuantity()+"";//Integer.parseInt(ptd.getAllocatedQuantity()+"")+"";
				if(ptd.getPickedQuantity()>0){
					bhQty=(int)ptd.getPickedQuantity()+"";
				}
				d.put("ddQty", bhQty);//ddQty);
				d.put("bhQty", bhQty);
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String queryBhXppDetailList(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "备货单明细查询", json.toString(), null);
    	Long pId=json.getLong("pId");
    	List<Object[]> objs=this.commonDao.findByQuery("select d.psd.mh,d.psd.item.code,d.psd.item.name,d.psd.ddbh,"
    			+ " DATE_FORMAT(d.psd.nrzsr,'%Y-%m-%d '),"
    			+ " cast(d.allocatedQuantity,int),cast(d.pickedQuantity,int),d.psd.key "
    			+ " from PlanPickTicketDetail d where d.pickTicket.id="+pId);
    	JSONArray  array = new JSONArray();
    	JSONObject h = new JSONObject();
    	h.put("mh", "门号");
    	h.put("itemCode", "品番");
    	h.put("itemName", "名称");
		h.put("ddbh", "订单编号");
		h.put("nrzsr", "纳入指示日");
		h.put("ddQty", "订单数量");
		h.put("bhQty", "备货数量");
		h.put("key", "现品票");
		array.add(h);
		for(Object[] o:objs) {
			try {
				int j=0;
				String mh=o[j++].toString();
				String itemCode=o[j++].toString();
				String itemName=o[j++].toString();
				String ddbh=o[j++].toString();
				String nrzsr=o[j++].toString();
				String ddQty=o[j++].toString();
				String bhQty=o[j++].toString();
				String key=o[j++].toString();
				JSONObject d = new JSONObject();
				d.put("mh", mh);
				d.put("itemCode", itemCode);
				d.put("itemName", itemName);
				d.put("ddbh", ddbh);
				d.put("nrzsr", nrzsr);
				d.put("ddQty", ddQty);
				d.put("bhQty", bhQty);
				d.put("key", key);
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	public String queryFhList(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "发货单列表查询", json.toString(), null);
    	String queryInfo=json.getString("queryInfo");
    	String hql="select p.id,p.company.name, p.relatedBill2,DATE_FORMAT(p.orderDate,'%Y-%m-%d '),"
    			+ "cast(p.orderQuantity,int),cast(p.pickedQuantity,int),cast(p.shippedQuantity,int) from PickTicket p where p.status='PICK_ALL'";
    	if(queryInfo!=null&&!queryInfo.equals("")){
    		hql+=" and p.relatedBill2='"+queryInfo+"'";
    	}
    	List<Object[]> objs=this.commonDao.findByQuery(hql);
    	JSONArray  array = new JSONArray();
    	JSONObject h = new JSONObject();
    	h.put("pId", "序号");
    	h.put("companyName", "供应商");
		h.put("relatedBill2", "单号");
		h.put("orderDate", "订单日期");
		h.put("orderQuantity", "订单数量");
		h.put("pickedQuantity", "备货数量");
		h.put("shippedQuantity", "发货数量");
		array.add(h);
		for(Object[] o:objs) {
			try {
				int j=0;
				String pId=o[j++].toString();
				String companyName=o[j++].toString();
				String relatedBill2=o[j++].toString();
				String orderDate=o[j++].toString();
				String orderQuantity=o[j++].toString();
				String pickedQuantity=o[j++].toString();
				String shippedQuantity=o[j++].toString();
				JSONObject d = new JSONObject();
				d.put("pId", pId);
				d.put("companyName", companyName);
				d.put("relatedBill2", relatedBill2);
				d.put("orderDate", orderDate);
				d.put("orderQuantity", orderQuantity);
				d.put("pickedQuantity", pickedQuantity);
				d.put("shippedQuantity", shippedQuantity);
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String queryFhDetailList(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "发货单明细查询", json.toString(), null);
    	Long pId=json.getLong("pId");
    	List<Object[]> objs=this.commonDao.findByQuery("select d.item.code,d.item.name,cast(d.orderQuantity,int),"
    			+ "cast(d.pickedQuantity,int)  "
    			+ " from PickTicketDetail d where d.pickTicket.id="+pId);
    	JSONArray  array = new JSONArray();
    	JSONObject h = new JSONObject();
    	//品番	名称	订单数量	备货数量
    	h.put("itemCode", "品番");
    	h.put("itemName", "名称");
		h.put("ddQty", "订单数量");
		h.put("bhQty", "备货数量");
		array.add(h);
		for(Object[] o:objs) {
			try {
				int j=0;
				String itemCode=o[j++].toString();
				String itemName=o[j++].toString();
				String ddQty=o[j++].toString();
				String bhQty=o[j++].toString();
				JSONObject d = new JSONObject();
				d.put("itemCode", itemCode);
				d.put("itemName", itemName);
				d.put("ddQty", ddQty);
				d.put("bhQty", bhQty);
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String queryBhDetailKams(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "备货单明细查询", json.toString(), null);
    	Long pt_id=json.getLong("pt_id");
    	List<Object[]> objs=this.commonDao.findByQuery("select d.item.code,d.item.name,cast(d.allocatedQuantity,int),"
    			+ "cast(d.pickedQuantity,int),d.itemKey.pinfan.location.code  "
    			+ " from PickTicketDetail d where d.pickTicket.id="+pt_id+" and d.allocatedQuantity>0");
    	JSONArray  array = new JSONArray();
		for(Object[] o:objs) {
			try {
				int j=0;
				String item_code=o[j++].toString();
				String item_name=o[j++].toString();
				String dd_qty=o[j++].toString();
				String bh_qty=o[j++].toString();
				String src_loc=o[j++].toString();
				JSONObject d = new JSONObject();
				d.put("item_code", item_code);
				d.put("item_name", item_name);
				d.put("dd_qty", dd_qty);
				d.put("bh_qty", bh_qty);
				d.put("src_loc", src_loc);
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	public String queryPlanBhDetailKams(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "预备货单明细查询", json.toString(), null);
    	Long pt_id=json.getLong("pt_id");
    	List<Object[]> objs=this.commonDao.findByQuery("select d.id, d.item.code,d.item.name,cast(d.allocatedQuantity,int),"
    			+ "cast(d.pickedQuantity,int),d.pf.location.code  "
    			+ " from PickTicketDetail d where d.pickTicket.id="+pt_id);
    	JSONArray  array = new JSONArray();
		for(Object[] o:objs) {
			try {
				int j=0;
				String id=o[j++].toString();
				String item_code=o[j++].toString();
				String item_name=o[j++].toString();
				String dd_qty=o[j++].toString();
				String bh_qty=o[j++].toString();
				String src_loc=o[j++].toString();
				JSONObject d = new JSONObject();
				d.put("id", id);
				d.put("item_code", item_code);
				d.put("item_name", item_name);
				d.put("dd_qty", dd_qty);
				d.put("bh_qty", bh_qty);
				d.put("src_loc", src_loc);
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	public String queryFhDetailKams(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "发货明细查询", json.toString(), null);
    	Long pt_id=json.getLong("pt_id");
    	String hql="select l.srcLPCode,l.itemKey.item.code,l.itemKey.item.name,cast(sum(l.movedQuantity),int),"
    			+ "cast(sum(l.shipQuantity),int),cast(sum(l.xbQuantity),int)   "
    			+ " from TaskLog l where l.task.pickTicketDetail.pickTicket.id="+pt_id+" "
    			+ " group by l.srcLPCode,l.itemKey.item.code";
    	List<Object[]> objs=this.commonDao.findByQuery(hql);
    	JSONArray  array = new JSONArray();
		for(Object[] o:objs) {
			try {
				int j=0;
				String tph=o[j++].toString();
				String item_code=o[j++].toString();
				String item_name=o[j++].toString();
				String bh_qty=o[j++].toString();
				String fh_qty=o[j++].toString();
				String xb_qty=o[j++].toString();
				JSONObject d = new JSONObject();
				d.put("tph", tph);
				d.put("item_code", item_code);
				d.put("item_name", item_name);
				d.put("bh_qty", bh_qty);
				d.put("fh_qty", fh_qty);
				d.put("xb_qty", xb_qty);
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	public String queryRkList(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "入库清单列表查询", json.toString(), null);
    		String queryInfo=json.getString("queryInfo");
    		Session session = ((HibernateDaoSupport)this.commonDao).getSessionFactory().openSession();
	    	String sql="select asn.RELATED_BILL2,asn.RELATED_BILL1,DATE_FORMAT(asn.ORDER_DATE,'%Y-%m-%d') dhr,sum(asn.EXPECTED_QUANTITY),sum(asn.RECEIVED_QUANTITY) "
	    			+ " from ASN asn where 1=1 and asn.status not in ('OPEN','CLOSE') and asn.bill_type_id=1 and asn.RELATED_BILL1 is not null ";
	    	if(queryInfo!=null&&!queryInfo.equals("")){
	    		sql+=" and (asn.RELATED_BILL2='"+queryInfo+"' or asn.RELATED_BILL1='"+queryInfo+"') ";
	    	}
//	    	sql+=" GROUP BY relatedBill2";
	    	sql+=" GROUP BY CASE WHEN asn.RELATED_BILL2 is not null  THEN asn.RELATED_BILL2 else asn.RELATED_BILL1 end";
	    	sql+=" ORDER BY asn.id";
	    	System.out.println(sql);
	    	Query   query   =   session.createSQLQuery(sql); 
		List<Object> list= (List<Object>)query.list();
		JSONArray  array = new JSONArray();
		for (int i=0;i<list.size();i++) {
			try {
				Object[] objarr = (Object[])list.get(i);
				String tps=objarr[3]==null?"":objarr[3].toString();
				String put_ts=objarr[4]==null?"":objarr[4].toString();
				if(tps.equals(put_ts)){
					continue;
				}
				JSONObject obj = new JSONObject();
				String jzxh=objarr[0]==null?"":objarr[0]+"";
				String fph=objarr[1]==null?"":objarr[1]+"";
				String jzfp=jzxh;
				if(jzfp.equals("")){
					jzfp=fph;
				}
				obj.put("jzfp", jzfp);
				obj.put("jzxh", jzxh);
				obj.put("fph", fph);
				obj.put("tps",tps);
				obj.put("put_ts", put_ts);
//				int tps=getTotalTs(jzfp);
//				obj.put("tps", tps);
				obj.put("dhr",objarr[2]==null?"":objarr[2]+"");
//				obj.put("put_ts",tps-getNoPutTs(jzfp));
				array.add(obj);
			} catch (Exception e) {
				session.close();
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		session.close();
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	public String scanRkLp(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "入库托盘扫描", json.toString(), null);
	 	String jzfp=json.getString("jzfp");
	    	String tph=json.getString("tph");
	    	if(tph.substring(0, 2).equals("98")){
	    		tph=tph.replaceFirst("98", "");
	    	}else if(tph.substring(0, 2).equals("90")){
	    		tph=tph.replaceFirst("90", "");
	    	}
	    	JSONObject obj= new JSONObject();
	    	try{
	    		String sql=" from ASNDetail d where 1=1 and (d.asn.relatedBill2='"+jzfp+"' or d.asn.relatedBill1='"+jzfp+"') and d.lotInfo.propC2='"+tph+"'";
	    		List<ASNDetail> ds=this.commonDao.findByQuery(sql);
		    	if(ds.size()<=0){
		    		errorMes=tph+"托盘号不存在";
		    	}else{
		    		ASNDetail d=ds.get(0);
		    		LotInfo info=d.getLotInfo();
					obj.put("tph",info==null?"":info.getPropC2());
					obj.put("asn_id",d.getAsn().getId());
					int tps=getTotalTs(jzfp);
					obj.put("total_ts", tps);
					obj.put("put_ts",tps-getNoPutTs(jzfp));
					List<Object[]> qty=this.commonDao.findByQuery("select sum(d.expectedQuantity),sum(d.receivedQuantity) from ASNDetail d where 1=1 and d.lotInfo.propC2='"+tph+"'");
					Double tp_qty=(Double) qty.get(0)[0];
					Double tp_put_qty=(Double) qty.get(0)[1];
					Double sysl=tp_qty-tp_put_qty;
					obj.put("tp_qty",tp_qty);
					obj.put("tp_put_qty",tp_put_qty);
					obj.put("tp_sysl",sysl);
					obj.put("put_xs",getPutXs(tph));
		    	}
		    	if(errorMes!=null&&!errorMes.equals("")){
	    			data=returnJson("400", errorMes, returnData);
	    			return data;
	    		}
	    
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	public String scanRkKey(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "入库现品票扫描", json.toString(), null);
		String xpp =json.has("xpp")?json.getString("xpp"):"";
		String tph =json.has("tph")?json.getString("tph"):"";
		String jzfp =json.has("jzfp")?json.getString("jzfp"):"";
		String asn_id =json.has("asn_id")?json.getString("asn_id"):"";
		if(tph.substring(0, 2).equals("98")){
    		tph=tph.replaceFirst("98", "");
    	}else if(tph.substring(0, 2).equals("90")){
    		tph=tph.replaceFirst("90", "");
    	}
	    	JSONObject obj= new JSONObject();
	    	try{
			if(xpp!=null&&!xpp.equals("")&&xpp.length()==82){
				 String pfh=xpp.substring(3, 13);
				 String ddh=xpp.substring(29, 37);
				 String sql="from ASNDetail d where 1=1 and (d.asn.relatedBill2='"+jzfp+"' or d.asn.relatedBill1='"+jzfp+"')";
				 sql+=" and lotInfo.propC2='"+tph+"' and d.item.code='"+pfh+"' and REPLACE(d.PO,'-','')='"+ddh+"'" ;
	    		List<ASNDetail> ds=this.commonDao.findByQuery(sql);
			    if(ds.size()<=0){
			    	 errorMes=tph+"托盘号下"+ddh+"订单无该品番"+pfh;
				 }else{
					 ASNDetail d=ds.get(0);
					 if(d.getReceivedQuantity()==d.getExpectedQuantity()){
			    			errorMes=ddh+"订单号已全部收货";
			    		}else{
			    			LotInfo info=d.getLotInfo();
			    			String tax="保税";
			    			if(info.getPropC1()!=null&&info.getPropC1().equals("一般贸易")){
			    				tax="课税";
			    			}
			    			Item it=commonDao.get(Item.class, d.getItem().getId());
			    			obj.put("d_id", d.getId());
			    			obj.put("item_code", it.getCode());
						    obj.put("item_name", it.getName());
						    obj.put("tax", tax);
						    String s=xpp.substring(45, 52);
							char nbsp=0x00a0;
							 String sl=s.replace(nbsp,' ');
							 int shsl=Integer.parseInt(sl.trim());
						    obj.put("qty",shsl);
						    obj.put("put_xs", getPutXs(tph,d.getId(),it.getCode()));
			    			List<Pinfan> pfs=this.commonDao.findByQuery("from Pinfan pf where pf.item.id="+d.getItem().getId()+" and pf.bonded='"+tax+"'");
			    			if(pfs.size()<=0){
			    				throw new BusinessException("请维护品番关联");
			    			}else{
			    				Pinfan pf=pfs.get(0);
			    				Location loc=pf.getLocation();
//			    				double srs=pf.getQuantity()==0?1:pf.getQuantity();
//			    				List<Double> qty=this.commonDao.findByQuery("select sum(d.expectedQuantity) from ASNDetail d where 1=1 and lotInfo.propC2='"+tph+"' and d.item.code='"+pfh+"'");
//				    			int tp_xs=(int) (qty.get(0)/srs);
//								obj.put("srs", srs);
//								obj.put("tp_xs", tp_xs);
								if(loc!=null){
									String str[]= loc.getCode().split("/");
									if(str.length>1){
										obj.put("src_loc", str[1]);
									}else{
										obj.put("src_loc", str[0]);
									}
									obj.put("src_loc2", loc.getCode());
								}
						
								obj.put("pf_id", pf.getId());
				    			}
			    		}
				 }
			}else{
				errorMes = "现品票不能为空或长度不对";
			}
	    	if(errorMes!=null&&!errorMes.equals("")){
    			data=returnJson("400", errorMes, returnData);
    			return data;
    		}
	    
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	
	public String queryRkDetailList(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "入库清单明细查询", json.toString(), null);
		String jzfp =json.has("jzfp")?json.getString("jzfp"):"";
		String sql="from ASNDetail d where 1=1 and (d.asn.relatedBill2='"+jzfp+"' or d.asn.relatedBill1='"+jzfp+"')";
		sql+=" order by d.lotInfo.propC2 asc ";
		List<ASNDetail> ds=this.commonDao.findByQuery(sql);
	    	JSONArray  array = new JSONArray();
		for(ASNDetail ad:ds) {
			try {
				JSONObject d = new JSONObject();
				int j=0;
				LotInfo info=ad.getLotInfo();
    				String tax="保税";
	    			if(info.getPropC1()!=null&&info.getPropC1().equals("一般贸易")){
	    				tax="课税";
	    			}
	    			Item it=commonDao.get(Item.class, ad.getItem().getId());
	    			d.put("d_id", ad.getId());
	    			d.put("item_code", it.getCode());
				d.put("item_name", it.getName());
				d.put("tph",info.getPropC2());
				d.put("tax", tax);
				d.put("quantity", (int)ad.getExpectedQuantity());
				d.put("put_qty",  (int)ad.getReceivedQuantity());
//				d.put("pt_xs", getPutXs(ad.getId()));
				array.add(d);
			} catch (Exception e) {
				data=returnJson("400", "操作失败", e.getMessage());
			    return data;
			}
		}
		returnData=array.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	public String scanCkTph(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "出库托盘号扫描", json.toString(), null);
    	String tph=json.getString("tph");
    	String pId=json.getString("pId");
    	Long ptdId=null;
    	JSONObject obj= new JSONObject();
    	try{
			obj.put("order_xs","");
			obj.put("pick_xs","");
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		System.out.println(data);
		return data;
	}
	
	public String scanFhCode(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "发货出库单号扫描", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
			String pt_code=json.getString("pt_code");
	    	List<PickTicket> pts=this.commonDao.findByQuery("from PickTicket p where (p.code='"+pt_code+"' or p.relatedBill2='"+pt_code+"') and p.relatedBill1='日出库单'");
	    	if(pts.size()>0){
	    		PickTicket pt=pts.get(0);
	    		if(pt.getShippedQuantity()>=pt.getPickedQuantity()){
	    			errorMes = "备货单已全部出库";
	    		}else{
	    			obj.put("pt_id", pt.getId());
	    		}
	    	}else{
	    		errorMes = "备货单不存在";
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	public String scanFhTph(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "发货托盘号扫描", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
//			Long pt_id=json.getLong("pt_id");
			String tph=json.getString("tph");
			//已完成，支持查看
			List<TaskLog> logs=this.commonDao.findByQuery("from TaskLog l where (l.task.pickTicketDetail.pickTicket.code='"+tph+"' or l.task.pickTicketDetail.pickTicket.relatedBill2='"+tph+"'  or l.srcLPCode='"+tph+"') ");
	    	//List<TaskLog> logs=this.commonDao.findByQuery("from TaskLog l where (l.task.pickTicketDetail.pickTicket.code='"+tph+"' or l.srcLPCode='"+tph+"') and l.shipQuantity<=0");
	    	if(logs.size()>0){
	    		TaskLog l=logs.get(0);
	    		PickTicket pt=l.getTask().getPickTicketDetail().getPickTicket();
	    		Long pt_id=pt.getId();
	    		String ship_type="tph";
	    		if(pt.getCode().equals(tph)||pt.getRelatedBill2().equals(tph)){
	    			tph=null;
	    			ship_type="ckd";
	    		}
	    		obj.put("sku_qty", getPickTpSku(pt_id, tph));
	    		obj.put("tp_xs", getPickTpxs(pt_id, tph));
	    		obj.put("pt_id", pt_id);
	    		obj.put("ship_type", ship_type);
	    	}else{
	    		errorMes = "发货/托盘号不存在或已出库";
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	public String scanXbjs(JSONObject json) {
		String data="";
		String returnMsg="";
		String returnCode="200";
		String returnData="";
		String errorMes="";
		InterfaceLog log=storeInterfaceLog(null,"Android", "WMS", "线边接收-发货托盘号扫描", json.toString(), null);
		JSONObject obj= new JSONObject();
		try {
//			Long pt_id=json.getLong("pt_id");
			String tph=json.getString("tph");
	    	List<TaskLog> logs=this.commonDao.findByQuery("from TaskLog l where (l.task.pickTicketDetail.pickTicket.code='"+tph+"' or l.srcLPCode='"+tph+"') and l.xbQuantity<=0");
	    	if(logs.size()>0){
	    		TaskLog l=logs.get(0);
	    		PickTicket pt=l.getTask().getPickTicketDetail().getPickTicket();
	    		Long pt_id=pt.getId();
	    		String ship_type="tph";
	    		if(pt.getCode().equals(tph)){
	    			tph=null;
	    			ship_type="ckd";
	    		}
	    		obj.put("sku_qty", getPickTpSku(pt_id, tph));
	    		obj.put("tp_xs", getPickTpxs(pt_id, tph));
	    		obj.put("pt_id", pt_id);
	    		obj.put("ship_type", ship_type);
	    	}else{
	    		errorMes = "发货/托盘号不存在或已接收";
	    	}
    	
		} catch (Exception e) {
			data=returnJson("400", "操作失败", e.getMessage());
		    return data;
		}
		if(errorMes!=null&&!errorMes.equals("")){
    		data=returnJson("400", errorMes, returnData);
			return data;
    	}
		returnData=obj.toString();
		data=returnJson(returnCode, returnMsg, returnData);
		return data;
	}
	
	
	
	public int getPickXs(Long pt_id){
		return 0;
//		String sql="select sum(CEILING(d.allocatedQuantity/d.quantity)) from PickTicketDetail d where 1=1 d.pickTicket.id="+pt_id;
//		List<Task> ts=this.commonDao.findByQuery(sql);
//		return ts.size();
	}
	//托盘箱数
	public int getPickTpxs(Long pt_id,String tph){
		String sql="select t.id from TaskLog t where  t.task.pickTicketDetail.pickTicket.id="+pt_id;
		if(tph!=null){
			sql+=" and t.srcLPCode='"+tph+"'";
		}
		List<Task> ts=this.commonDao.findByQuery(sql);
		return ts.size();
	}
	//托盘SKU品种数
	public int getPickTpSku(Long pt_id,String tph){
		String sql="select distinct(t.itemKey.id) from TaskLog t where  t.task.pickTicketDetail.pickTicket.id="+pt_id;
		if(tph!=null){
			sql+=" and t.srcLPCode='"+tph+"'";
		}
		List<Task> ts=this.commonDao.findByQuery(sql);
		return ts.size();
	}
	
	public int getPickBhxs(Long pt_id){
		String sql="select t.id from TaskLog t where  t.task.pickTicketDetail.pickTicket.id="+pt_id;
		List<Task> ts=this.commonDao.findByQuery(sql);
		return ts.size();
	}
	public int getNoPutTs(String jzfp){
		String sql="select d.lotInfo.propC2 from ASNDetail d where 1=1 and (d.asn.relatedBill2='"+jzfp+"' or d.asn.relatedBill1='"+jzfp+"')";
		List<ASNDetail> pts=this.commonDao.findByQuery(sql+" and d.receivedQuantity<expectedQuantity group by d.lotInfo.propC2");
		return pts.size();
	}
	public int getTotalTs(String jzfp){
		String sql="select d.lotInfo.propC2 from ASNDetail d where 1=1 and (d.asn.relatedBill2='"+jzfp+"' or d.asn.relatedBill1='"+jzfp+"')";
		List<ASNDetail> tts=this.commonDao.findByQuery(sql+" group by d.lotInfo.propC2");
		return tts.size();
	}
	public int getPutXs(String tph){
		List<ReceivedRecord> rds=this.commonDao.findByQuery("from ReceivedRecord r where  r.asnDetail.lotInfo.propC2='"+tph+"'");
		return rds.size();
	}
	public int getPutXs(String tph,Long d_id,String item_code){
		List<ReceivedRecord> rds=this.commonDao.findByQuery("from ReceivedRecord r where  r.asnDetail.lotInfo.propC2='"+tph+"' and r.asnDetail.item.code='"+item_code+"'");
		return rds.size();
	}
	
	@Override
	public InterfaceLog storeInterfaceLog(String request,String fromSys,String toSys,String type,String content,Long id) {
		InterfaceLog log=EntityFactory.getEntity(InterfaceLog.class);
		log.setRequest(request);
		log.setFromSYS(fromSys);
		log.setToSYS(toSys);
		log.setType(type);
		log.setRequestContent(content);
		log.setRequestTime(new Date());
		log.setLogID(id);
		log.setStatus("OPEN");
		commonDao.store(log);
		return log;
	}
	
	private String  returnJson(String returnCode,String returnMsg,String returnData){
		JSONObject returnJson = new JSONObject();
		if(returnCode.equals("200")){
			returnCode="1";
		}else{
			returnCode="0";
		}
		returnJson.put("status",returnCode);
		returnJson.put("msg",returnMsg);
		returnJson.put("data",returnData);
		String data=returnJson.toString();
		return data;
	}
	
	private String conventNullStr(Object obj){
		if(obj==null){
			return null;
		}else{
			String str=obj.toString();
			if(str.equals("null")){
				return null;
			}else{
				return str;
			}
		}
		
	}
	
	private Date conventNullDate(Object obj){
		if(obj==null){
			return null;
		}else{
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String str=obj.toString();
			Date date=null;
			if(str.equals("null")){
				return null;
			}else{
				try {
					date= sdf.parse(str);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return date;
		}
		
	}
	
	private double conventNullStrDouble(Object obj){
		String str=obj.toString();
		if(str.equals("null")){
			return 0;
		}else{
			return Double.parseDouble(str);
		}
	}
	 
}
