package nc.itf.fa.byzitf;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import nc.bs.framework.adaptor.IHttpServletAdaptor;
import nc.bs.framework.common.NCLocator;
import nc.bs.framework.server.ISecurityTokenCallback;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;

public class QueryDefdocInfoService implements IHttpServletAdaptor{
	
	@Override
	public void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//后台模拟用户登录
		NCLocator.getInstance().lookup(ISecurityTokenCallback.class).token("NCSystem".getBytes(), "pfxx".getBytes());
		JSONObject resultjson=new JSONObject();
		String requestJson = requestJson(request);
		try{
//			String requestJson = requestJson(request);
//			JSONObject jsonObject = JSONObject.parseObject(requestJson);
//			if(!jsonObject.containsKey("listcode")||jsonObject.getString("listcode")==null||"".equals(jsonObject.getString("listcode")))
//				throw new Exception("请输入档案类型编码，进行查询！");
//			List<Map<String,String>> maplist=new ArrayList<Map<String,String>>();
//			maplist=queryDeocInfo(jsonObject);
//			if(maplist==null||maplist.size()==0)
//				throw new Exception("系统没有找到对应的档案信息！");	
//			resultjson.put("resultcode", "1");
//			resultjson.put("resultdescription", "查询成功");
//			JSONArray deatilarray=new JSONArray();
//			for(int i=0;i<maplist.size();i++) {
//				JSONObject deatiljson = new JSONObject();
//				Map<String,String> corpmap=maplist.get(i);
//				if(corpmap!=null) {
//					deatiljson.put("listcode",corpmap.get("listcode"));
//					deatiljson.put("listname",corpmap.get("listname"));
//					deatiljson.put("name",corpmap.get("name"));
//					deatiljson.put("orgcode",corpmap.get("orgcode"));
//					deatiljson.put("orgname",corpmap.get("orgname"));
//					deatiljson.put("code",corpmap.get("code"));
//					deatiljson.put("name",corpmap.get("name"));
//				}
//				deatilarray.add(deatiljson);
//			}
//			resultjson.put("content",deatilarray );
		} catch (Exception e) {
			 resultjson.put("resultcode", "0");
			 resultjson.put("resultdescription", "查询失败"+e.getMessage());
			 resultjson.put("content", "");
	    } finally {
			responseWriter(response, requestJson);
		}
	}
	
	public String requestJson(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        try{
        	request.setCharacterEncoding("utf-8");
        	BufferedReader reader = request.getReader();
            char[] buff = new char[1024];
            int len;
            while ((len = reader.read(buff)) != -1) {
                sb.append(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
	
	public void responseWriter(HttpServletResponse response, String returnJson) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "application/json; charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.write(returnJson);
		writer.close();
	}
	
	private static IUAPQueryBS queryBS;
	public static IUAPQueryBS getQueryBS() {
		if (queryBS == null) {
			queryBS = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		}
		return queryBS;
	}
	
	public List<Map<String,String>> queryDeocInfo(JSONObject jsonObject) throws BusinessException{
		List<Map<String,String>> maplist=new ArrayList<Map<String,String>>();
		StringBuilder deocquery=new StringBuilder();
		String listcode=jsonObject.getString("listcode");
		String orgcode=jsonObject.getString("orgcode");
		String codename=jsonObject.getString("codename");
		String code=jsonObject.getString("code");
		String name=jsonObject.getString("name");
		deocquery.append("select dl.code listcode,"
				+ "dl.name listname,"
				+ "os.code orgcode,"
				+ "os.name orgname,"
				+ "d.code,"
				+ "d.name"
				+ " from bd_defdoclist dl"
				+ " join bd_defdoc d on d.pk_defdoclist=dl.pk_defdoclist"
				+ " join org_orgs os on os.pk_org=d.pk_org"
				+ " where nvl(d.dr,0)=0 and d.enablestate=2 and dl.code ='"+listcode+"'");
				if(orgcode!=null&&!orgcode.equals(""))
					deocquery.append(" and os.code ='"+orgcode+"'");
				if(code!=null&&!code.equals(""))
					deocquery.append(" and d.code ='"+code+"'");
				if(name!=null&&!name.equals(""))
					deocquery.append(" and d.name ='"+name+"'");
				if(codename!=null&&!codename.equals(""))
					deocquery.append(" and d.code||d.name like '%"+codename+"%'");
				maplist=(List<Map<String, String>>) getQueryBS().executeQuery(deocquery.toString(), new MapListProcessor());		
		return maplist;
	}
}
