b0f57587776938d499a782988118e1e0f80ddabb.svn-base 12.8 KB
package com.thinkgem.jeesite.modules.interfaces.service;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.JsonUtil;
import com.thinkgem.jeesite.modules.reg.dao.bus.RegBusBdcqzsdjxxDao;
import com.thinkgem.jeesite.modules.reg.dao.bus.RegBusQlrDao;
import com.thinkgem.jeesite.modules.reg.dao.bus.RegBusSlsqDao;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseH;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseZdjbxx;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseHService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseZdjbxxService;
import com.thinkgem.jeesite.modules.reg.service.bus.RegBusSlsqService;
import com.thinkgem.jeesite.modules.reg.utils.RegUtils;
import com.thinkgem.jeesite.modules.sys.service.InterSiService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.thinkgem.jeesite.modules.wechat.entity.WxOrder;

import freemarker.core.ParseException;
import net.sf.json.JSON;
import net.sf.json.JSONObject;

@Service
public class WXService {

	private Logger logger = LoggerFactory.getLogger(InterSiService.class);
	@Autowired
	RegBusQlrDao qlrDao;
	@Autowired
	RegBusSlsqDao slsqDao;
	@Autowired
	RegBusBdcqzsdjxxDao regBusBdcqsdjxxDao;
	@Autowired
	RegBaseZdjbxxService zdjbxxService;
	@Autowired
	RegBaseHService hService;
	@Autowired
	RegBusSlsqService slsqService;
	
	

	// 进度查询
	public Map query_pro(HttpServletRequest request) {
		logger.info("入参:" + request);
		Map<String,String> res_map = slsqService.progress(request.getParameter("slglh"));
		if(res_map==null) {
			throw new ServiceException("无此业务相关信息");
		}
		Map res_data = new HashMap();
		String task = res_map.get("TASKDEFKEY");
		if (task.equals("reg_end")) {
			res_data.put("blzt", "3");
		} else if (task.equals("reg_audit56") || task.equals("reg_audit6")) {
			res_data.put("blzt", "2");
		} else {
			res_data.put("blzt", "1");
		}
		Map data = new HashMap();
		data.put("data", res_data);
		return res_data;
	}

	// 解析参数
	public Map params(HttpServletRequest request) {
		Map map_data = new HashMap();
		String param = request.getParameter("data");
		JSONObject jb = JSONObject.fromObject(param);
		Map map = (Map) jb;
		return map;
	}

	// 证明核验
	public Map check_ident(HttpServletRequest request) {
		Map<String,String> res_list = regBusBdcqsdjxxDao.queryIdent(request.getParameter("qlrmc"), request.getParameter("cardcode"),
				request.getParameter("year"), "登记证明", request.getParameter("zsbm"));
		Map res_data = new HashMap();
		if(res_list==null) {
			throw new ServiceException("无此证明相关信息");
		}
		if(StringUtils.isBlank(res_list.get("YWH"))) {
			throw new ServiceException("数据错误");
		}
		Map<String,String> qlr_map = qlrDao.querywxQlr(res_list.get("YWH"));
		res_list.put("qlrmc",qlr_map.get("QLRMC"));
		res_data.put("data", res_list);
		return res_data;
	}

	// 证书核验
	public Map check_cret(HttpServletRequest request) {
		Map<String,String> res_list = regBusBdcqsdjxxDao.queryIdent(request.getParameter("qlrmc"), request.getParameter("cardcode"),
				request.getParameter("year"), "不动产权", request.getParameter("zsbm"));
		Map res_data = new HashMap();
		if(res_list==null) {
			throw new ServiceException("无此证书相关信息");
		}
		if(StringUtils.isBlank(res_list.get("YWH"))) {
			throw new ServiceException("数据错误");
		}
		Map<String,String> qlr_map = qlrDao.querywxQlr(res_list.get("YWH"));
		res_list.put("qlrmc",qlr_map.get("QLRMC"));
		res_data.put("data", res_list);
		return res_data;
	}

	//叫号推送
	public Map call(HttpServletRequest request) {
		String url = Global.getWxUrl() + "/start/appointmentProcess";
		String yyid = request.getParameter("yyid");
		Map params = new HashMap();
		params.put("yyid", yyid);
		String ckbh = UserUtils.getUser().getCkbh();
		params.put("blck", ckbh);
		if(StringUtils.isBlank(ckbh)) {
			throw new ServiceException("请先维护窗口编号");
		}
		String content = "";
		try {
			content = posturl(params, url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map map = JsonUtil.jsonToMap(content);
		return map;
	}

	// post方式向微信端传递数据
	private String posturl(Map params, String url) throws IOException {

		Integer URLOUTTIME = 1000 * 60;
		String res = "";
		HttpPost httppost = null;
		CloseableHttpClient client = null;
		CloseableHttpResponse response = null;
		Exception exception = null;
		try {
			client = HttpClientBuilder.create().build();
			httppost = new HttpPost(url);
			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(URLOUTTIME)
					.setConnectTimeout(URLOUTTIME).setSocketTimeout(URLOUTTIME).build();
			httppost.setConfig(requestConfig);

			List<NameValuePair> urlParameters = Lists.newArrayList();
			
			if(params != null && params.size()>0) {
				Iterator<String> it = params.keySet().iterator();
				while (it.hasNext()) {
					String key = it.next(); 
					urlParameters.add(new BasicNameValuePair(key,params.get(key).toString())); 
				}
				httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
			}
			httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		
			response = client.execute(httppost);
			res = EntityUtils.toString(response.getEntity(), "utf-8");
			logger.info("返回信息:" + res);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (httppost != null) {
				httppost.releaseConnection();
			}
			if (response != null) {
				response.close();
			}
			if (client != null) {
				client.close();
			}
		}
		return res;
	}

	public Map check_rig(HttpServletRequest request) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		List list = new ArrayList();
		List alist = Lists.newArrayList();
		String name = request.getParameter("name");
		String carcode = request.getParameter("carcode");
		List<Map<String, String>> res_data = regBusBdcqsdjxxDao.queryRights(name,carcode);
		if(res_data.size()>0) {
			for(int i=0;i<res_data.size();i++) {
				Map<String,String> res_map = res_data.get(i);
				res_map.put("qlrmc", name);//产权人
				res_map.put("carcode",carcode);//身份证号
				if(StringUtils.isBlank(res_map.get("YWH"))) {
					logger.info("业务号为空");
					continue;
				}
				Map<String,String> qlr_map = qlrDao.querywxGyr(res_map.get("YWH"),carcode);
				Integer sxh =1;
				if(qlr_map==null) {
					res_map.put("gyrmc","无");
				}else {
				sxh =qlrDao.getSxh(res_map.get("YWH"),name,carcode);
				if(sxh==null) {
					logger.info("权利人顺序号为空");
					continue;
				}
				res_map.put("gyrmc",qlr_map.get("GYRMC"));
				}
				String bdcqzh = res_map.get("BDCQZH");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				res_map.put("cxsj", sdf.format(new Date()));
				if(StringUtils.isNotBlank(bdcqzh)) {
				if(bdcqzh.indexOf(",") == -1) {
					res_map.put("cqzh", bdcqzh);
				}else {
				  String newbdcqzh = bdcqzh.substring(bdcqzh.indexOf("第")).split(",")[sxh-1];
				  logger.info(bdcqzh.substring(0,bdcqzh.indexOf("第")));
				  res_map.put("cqzh", bdcqzh.substring(0,bdcqzh.indexOf("第")+1)+newbdcqzh);
				}
				}
				String bdcdyh = res_map.get("BDCDYH");
				if(bdcdyh.indexOf("W")==-1) {//房权
					RegBaseH h = hService.getdy(bdcdyh);
					if(h==null) {
						logger.info("户信息为空");
						continue;
					}
					String zt="";
					 if(!(h.getIsdiya()).equals("0")) {
							zt = zt +"已抵押";
						}
					if(!(h.getIschafeng()).equals("0")) {
						zt = zt+" 已查封";
					}
                   
                    if(!(h.getIsyiyi()).equals("0")) {
						zt = zt +" 已异议";
					}
                    if(StringUtils.isBlank(zt)) {
						zt = "无";
					}
					res_map.put("bdczt",zt);
					res_map.put("MJ",res_map.get("MJ")+DictUtils.getDictLabel(h.getMjdw(), "reg_bus_mjdw", ""));
				}else {
					RegBaseZdjbxx zdjbxx = zdjbxxService.getbdcdyh(bdcdyh);
					if(zdjbxx==null) {
						logger.info("宗地信息为空");
						continue;
					}
					String zt="";
					if(!(zdjbxx.getIsdiya()).equals("0")) {
						zt = zt +"已抵押";
					}
					if(!(zdjbxx.getIschafeng()).equals("0")) {
						zt = zt+" 已查封";
					}
					if(!(zdjbxx.getIsyiyi()).equals("0")) {
						zt = zt +" 已异议";
					}
					if(StringUtils.isBlank(zt)){
						zt = "无";
					}
					res_map.put("bdczt",zt);
					res_map.put("MJ",res_map.get("MJ")+DictUtils.getDictLabel(zdjbxx.getMjdw(), "reg_bus_mjdw", ""));
				}
				alist.add(res_map);
			}
		}else {
				throw new ServiceException("无产权信息");
		}
		map.put("aaData", alist);
		return map;
	}

	//业务完结推送
	public Map send(HttpServletRequest request) {
		String url = Global.getWxUrl() + "/end/bussProcess";
		String ywh = request.getParameter("ywh");
		String djxl = request.getParameter("djxl");
		List<Map<String, String>> list = qlrDao.getXgry(ywh);
		Map params = Maps.newHashMap();
		params.put("ywh",ywh);
		params.put("djxl",DictUtils.getDictLabel(djxl, "reg_bus_djxl", ""));
		params.put("xgry", list);
		
		String content = "";
		try {
			content = posturl(params, url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map map = JsonUtil.jsonToMap(content);
		return map;
		
	}

	public Map sign(WxOrder wxOrder) {
		String url = Global.getWxUrl() + "/end/order";
		Map params = new HashMap();
		params.put("yyid", wxOrder.getYyid());
		params.put("sign", wxOrder.getSign());
		
		String content = "";
		try {
			content = posturl(params, url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map map = JsonUtil.jsonToMap(content);
		return map;
	}

	public Map getFlow() {
		Map map = Maps.newHashMap();
		//目前排除法定假节日还未实现,只是周内
		//获取前一个工作日
		Calendar now = Calendar.getInstance();
		int today = now.getTime().getDay();//取得今天的星期值
		if(today == now.getFirstDayOfWeek()){//今天在这个星期中是否为第一天
		now.roll(Calendar.DAY_OF_YEAR, -3);
		}else{
		now.roll(Calendar.DAY_OF_YEAR, -1);
		}
		System.out.println(now.getTime());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		List<Map<String,String>> ymap = slsqService.getYflow(sdf.format(now.getTime()));
		map.put("yData", ymap);
		logger.info("yData:"+JsonUtil.toJsonString(ymap));
		//获取前一周工作日,没法周一周五关联查询,所以循环吧、、
		 Date a = DateUtils.addDays(new Date(), -1);    
         Calendar cale = Calendar.getInstance();    
         cale.setTime(a);    
         cale.add(Calendar.WEEK_OF_YEAR, -1);// 一周    
         cale.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);    
        List<Map<String,String>> wmap = slsqService.getWflow(sdf.format(cale.getTime()));
        logger.info("日期"+sdf.format(cale.getTime()));
        String[] weekDays = {"一", "二", "三", "四", "五"};
        if(wmap.size()>0) {
        	for(int w=0;w<wmap.size();w++) {
        		SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd");
                
                Calendar cal = Calendar.getInstance(); // 获得一个日历
                Date datet = null;
                try {
                    datet = f.parse(wmap.get(w).get("SJ"));
                    cal.setTime(datet);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String week = "周"+weekDays[w]; 
                wmap.get(w).put("SJ", week);
        	}
        }
		map.put("wData", wmap);
		logger.info("wData:"+JsonUtil.toJsonString(ymap));
		return map;
	}

	
}