package com.baosight.sts.st.wl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.exception.PlatException;
import com.baosight.iplat4j.core.soa.SoaManager;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ee.service.CascadeSelect;
import com.baosight.iplat4j.ep.util.MethodParamConstants;
import com.baosight.sts.st.wl.domain.STWL1901;
import com.baosight.sts.st.yy.domain.STYY0203;
import com.baosight.sts.util.BizConstants;
import com.baosight.sts.util.CodeValueUtil;

public class ServiceSTWL2002 extends CascadeSelect {

	public EiInfo initLoad(EiInfo inInfo) {
		String segNo = String.valueOf(UserSession.getInSessionProperty(BizConstants.ORG_ID));
		String userId = String.valueOf(UserSession.getInSessionProperty(BizConstants.USER_ID));
		
		inInfo = CodeValueUtil.procEiInfo(inInfo, "CLIENT_FEE_FLAG", segNo);

		Map param = new HashMap();
		param.put("segNo", segNo);
		inInfo = CodeValueUtil.procEiInfoOther(inInfo, "SALE_USER_ID", "SALE_USER_IDCode", param);
		
		inInfo.set("edit_status-0-segNo", segNo);
		inInfo.set("edit_status-0-userId", userId);
		inInfo.addBlock("result").setRows(null);
		inInfo.set(EiConstant.renderStr, "STWL2002");
		return inInfo;
	}

	// 查询
	public EiInfo queryEiInfo(List list, Map map) {
		String segNo = String.valueOf(UserSession.getInSessionProperty(BizConstants.ORG_ID));
		EiInfo info = new EiInfo();
		String blockName = list.get(list.size()-1).toString();
	
		if (blockName == null || "".equals(blockName)) {
			info.setStatus(-1);
			info.setMsg("错误的配置参数，请联系管理员");
		}
		// UserSession.getInSessionProperty(BizConstants.ORG_ID)
		map.put("segNo", segNo);
		int offset = Integer.parseInt(map.get(EiConstant.offsetStr).toString());
		int limit = Integer.parseInt(map.get(EiConstant.limitStr).toString());
		List aa = null;
	
		if(blockName.equals("edit_status-0-voucherNum"))
		{
			//map.put("providerId", map.get("p_contractId"));
			aa = dao.query("STYY0203.queryMgr", map, offset, limit);
			info.addBlock(blockName);
			EiBlock block = info.getBlock(blockName);
			EiBlockMeta meta = this.metaMgr();
			block.setBlockMeta(meta);
			block.setRows(aa);
			Integer countc = (Integer)(dao.query("STYY0203.countMgr", map).get(0));
			info.set("count", countc);
		}
		info.setMsg("查询成功.\n");
		return info;
	}

	public EiInfo save(EiInfo inInfo){
		EiInfo outInfo = new EiInfo();
		String segNo = String.valueOf(UserSession.getInSessionProperty(BizConstants.ORG_ID));
		try {
			inInfo.set(EiConstant.serviceName,"STWL2002");
			inInfo.set(EiConstant.methodName, "saveProvider");
			inInfo.setMethodParam(MethodParamConstants.inDataBlock, "exam_info");
			inInfo.setMethodParam(MethodParamConstants.outDataBlock, "exam_info");
			outInfo = SoaManager.call(inInfo);
			outInfo.set("onSuccess", "true");
			outInfo.setMsg("保存成功！");
		} catch (RuntimeException e) {
			e.printStackTrace();
			outInfo.setStatus(-1);
			outInfo.set("onSuccess", "false");
			outInfo.setMsg("保存失败！"+e.getMessage());
		}
		return outInfo;
	}
	
	public EiInfo saveProvider(EiInfo inInfo) {
		StringBuffer buffer = new StringBuffer();
		buffer.append(inInfo.getMsg() == null ? "" : inInfo.getMsg());
		String userId = String.valueOf(UserSession.getInSessionProperty(BizConstants.USER_ID));
		String segNo = String.valueOf(UserSession.getInSessionProperty(BizConstants.ORG_ID));
		try{
			// 出库单号
			String putoutId = "";
			Map pOutId = new HashMap();
			// 根据函数生成出库单号
			pOutId.put("outResult", "");
			pOutId.put("segNo", segNo);
			dao.query("STWL0301.getPutoutId", pOutId);
			putoutId = (String) pOutId.get("outResult");
			//插入 主表
			HashMap<String, Object> mapMain = new HashMap<String,Object>();
			mapMain=(HashMap<String, Object>) inInfo.getBlock("edit_status").getRow(0);
			mapMain.put("segNo", segNo);
			mapMain.put("userId", userId);
			mapMain.put("accsetNo", 1);
			mapMain.put("userSegNo", segNo);
			mapMain.put("putoutId", putoutId);
			mapMain.put("putoutStatus", "10");
			mapMain.put("putoutType", "50");
			mapMain.put("modiPerson", userId);
			dao.insert("STWL030101.insertPutoutM", mapMain);
			
			//插入 子表 捆包
			//String putoutId=(String) mapMain.get("putoutId");
			for (int i = 0; i < inInfo.getBlock("result").getRows().size(); i++) {
				Map paramD=(HashMap)inInfo.getBlock("result").getRows().get(i);
				//获取子表ID
				Map pSubId = new HashMap();
				pSubId.put("segNo", segNo);
				pSubId.put("putoutId", putoutId);
				pSubId.put("productId",paramD.get("productId"));
				List listSub = dao.query("STWL0301.getPutoutSubId", pSubId);
				String subId = String.valueOf(((Map) listSub.get(0)).get("subId"));;
				if (listSub.size() > 0) {
					if (subId != null && !subId.equals("") && !subId.equals("null")) {
						subId = String.valueOf(((Map) listSub.get(0)).get("subId"));
					} else {
						subId = "001";
					}
				} else {
					subId = "001";
				}
				String putoutSubid = putoutId + subId;
				
				paramD.put("segNo", segNo);
				// 出库单号
				paramD.put("putoutId", putoutId);
				paramD.put("putoutSubid", putoutSubid);
				paramD.put("modiPerson", userId); // 更新人
				dao.insert("STWL030101.insertPutoutD", paramD);
				
				//捆包
				dao.insert("STWL030101.insertPutoutPack", paramD);
			}
			
			//修改对应的异议捆包表(过程)
			Map pbuild = new HashMap();
			pbuild.put("segNo", segNo);
			pbuild.put("putoutId", putoutId);
			pbuild.put("finishMark", "1");
			pbuild.put("userId", userId);
			pbuild.put("outResult", "");
			dao.query("STWL0301.bill_putout_build", pbuild);
			String outResult = String.valueOf(pbuild.get("outResult"));
			if (!"1".equals(outResult)) {
				buffer.append("出库失败." + outResult.toString() + "\n");
				throw new PlatException(buffer.toString());
			}
			Map pbuild1 = new HashMap();
			pbuild1.put("segNo", segNo);
			pbuild1.put("putoutId", putoutId);
			pbuild1.put("outResult", "");
			dao.query("STWL0301.billPutoutBuild", pbuild1);
			String outResult1 = String.valueOf(pbuild1.get("outResult"));
			if (!"1".equals(outResult1)) {
				buffer.append("出库失败." + outResult1.toString() + "\n");
				throw new PlatException(buffer.toString());
			}
			
			inInfo.set("goinPutoutId", putoutId);
			buffer.append("出库成功.出库单号:"+putoutId);
			inInfo.setMsg(buffer.toString());
			inInfo.set("succeed", "true");
			
		}catch (Exception e) {
			throw new PlatException(e.getMessage());
		}
		return inInfo;
	}
	
	public EiBlockMeta metaMgr() {
		EiBlockMeta meta = new EiBlockMeta();
		EiColumn eiColumn = null;
		
		eiColumn = new EiColumn("dissentMgrId");
		eiColumn.setDescName("异议处理单号");
		meta.addMeta(eiColumn);
		eiColumn = new EiColumn("dissentId");
		eiColumn.setDescName("异议单号");
		meta.addMeta(eiColumn);
		eiColumn = new EiColumn("dissentSubid");
		eiColumn.setDescName("异议单子项号");
		meta.addMeta(eiColumn);
		eiColumn = new EiColumn("scontractid");
		eiColumn.setVisible(false);
		eiColumn.setDescName("销售合同");
		meta.addMeta(eiColumn);
		eiColumn = new EiColumn("pcontractid");
		eiColumn.setVisible(false);
		eiColumn.setDescName("采购合同");
		meta.addMeta(eiColumn);
		
		meta.addMeta(eiColumn);
		eiColumn = new EiColumn("productId");
		eiColumn.setVisible(false);
		eiColumn.setDescName("资源号");
		meta.addMeta(eiColumn);

		return meta;
	}
}
