package com.sino.rrs.servlet;

import com.alibaba.fastjson.JSONObject;
import com.sino.rrs.business.RescueCompanyDispatchMessage;
import com.sino.rrs.entity.RescueCompanyDispatchPO;
import com.sino.weixin.oauth.util.Constants;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ThreadPoolExecutor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WeixinClientImplServlet extends HttpServlet
{
  private static final long serialVersionUID = 1L;
  private static Log log = LogFactory.getLog("WeixinClientImplServlet");

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    log.info(request.getRemoteAddr() + "向服务器发出请求：" + request.getQueryString());
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Cache-Control", "no-cache,must-revalidate");
    PrintWriter out = response.getWriter();
    JSONObject responseMap = new JSONObject();

    int action = Integer.parseInt(request.getParameter("action"));
    switch (action){
    case 1:
      String orderId = request.getParameter("orderId");
      String orderCode = request.getParameter("orderCode");
      int flowId = Integer.parseInt(request.getParameter("flowId"));
      String serviceName = request.getParameter("serviceName");

      log.info("可以走到订单调度处");
      log.info("调度处=====订单编号1====" + serviceName);
      String license = "";
      String style = "";

      String licenseAndStyle = request.getParameter("license");
      if ((licenseAndStyle != null) && (!licenseAndStyle.equals(""))) {
        if (licenseAndStyle.indexOf("style=") > 0) {
          String[] dd = licenseAndStyle.split("style=");
          license = dd[0].toString();
          if (dd.length == 2)
            style = dd[1].toString();
        }
        else {
          license = licenseAndStyle;
        }
      }
      log.info("可以走到订单调度处");

      String userName = request.getParameter("userName");
      String userPhone = request.getParameter("userPhone");
      double lon = Double.parseDouble(request.getParameter("lon"));
      double lat = Double.parseDouble(request.getParameter("lat"));
      String address = request.getParameter("address");
      if ((address != null) && (address.length() > 80)) {
        address = address.substring(0, 80);
      }
      String addressAdd = request.getParameter("addressAdd");
      if ((addressAdd != null) && (addressAdd.length() > 200)) {
        addressAdd = addressAdd.substring(0, 200);
      }
      String destAddress = request.getParameter("destAddress");
      if ((destAddress != null) && (destAddress.length() > 100)) {
        destAddress = destAddress.substring(0, 100);
      }
      String remark = request.getParameter("remark");
      String rescueCompanyCode = request.getParameter("rescueCompanyCode");
      String dispatchUser = request.getParameter("dispatchUser");
      String ADDRESS_PROPERTY = request.getParameter("ADDRESS_PROPERTY");
      String CONTRACTING_NAME = request.getParameter("CONTRACTING_NAME");

      log.info("可以走到订单调度处");
      log.info("调度处=====订单编号====" + serviceName);

      RescueCompanyDispatchPO rescueCompanyDispatchPO = new RescueCompanyDispatchPO();
      rescueCompanyDispatchPO.setOrder_id(Integer.parseInt(orderId));
      rescueCompanyDispatchPO.setOrder_code(orderCode);
      rescueCompanyDispatchPO.setFlow_id(flowId);
      rescueCompanyDispatchPO.setService_name(serviceName);
      rescueCompanyDispatchPO.setLicense(license);
      rescueCompanyDispatchPO.setStyle(style);
      rescueCompanyDispatchPO.setUserName(userName);
      rescueCompanyDispatchPO.setUserPhone(userPhone);
      rescueCompanyDispatchPO.setLon(lon);
      rescueCompanyDispatchPO.setLat(lat);
      rescueCompanyDispatchPO.setAddress(address);
      rescueCompanyDispatchPO.setAddressAdd(addressAdd);
      rescueCompanyDispatchPO.setDest_address(destAddress);
      rescueCompanyDispatchPO.setRemark(remark);
      rescueCompanyDispatchPO.setRESCUE_COMPANY_CODE(rescueCompanyCode);
      rescueCompanyDispatchPO.setDispatchUser(dispatchUser);
      rescueCompanyDispatchPO.setADDRESS_PROPERTY(ADDRESS_PROPERTY);
      rescueCompanyDispatchPO.setCONTRACTING_NAME(CONTRACTING_NAME);
      Constants.poolExecutor.execute(new RescueCompanyDispatchMessage(rescueCompanyDispatchPO));
      break;
    case 2:
      int orderIdCancel = Integer.parseInt(request.getParameter("orderId"));
      String orderCodeCancel = request.getParameter("orderCode");
      int orderStyle = Integer.parseInt(request.getParameter("orderStyle"));
      String remarkCancel = request.getParameter("remark");
      log.info("订单被取消====" + orderIdCancel);
      WeixinClientRequestService.rescueCompanyUpdate(orderIdCancel, orderCodeCancel, orderStyle, remarkCancel);

      break;
    case 3:
      String orderIdFinish = request.getParameter("orderId");
      String state = request.getParameter("state");
      log.info("处理完成状态===========" + orderIdFinish);
      WeixinClientRequestService.updateStateToFinish(Integer.parseInt(orderIdFinish), Integer.parseInt(state));
      break;
    case 4:
      String orderIdAcceptOrRefuse = request.getParameter("orderId");
      String acceptFlag = request.getParameter("acceptFlag");
      String rescueCode = request.getParameter("rescueCode");
      log.info("系统接单或拒单===" + orderIdAcceptOrRefuse);
      WeixinClientRequestService.updateDispatchStatus(Integer.parseInt(orderIdAcceptOrRefuse), rescueCode, Integer.parseInt(acceptFlag));
      break;
    }

    log.info("服务器答 " + request.getRemoteAddr() + " ：" + responseMap.toJSONString());
    out.print(responseMap.toJSONString());
    out.flush();
    out.close();
  }

  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  {
    doGet(req, resp);
  }
}