public PageResult payFrontMoney(String orderNo, String ip, PageResult result, String openid) {
    String hql = "from BabysitterOrder t where t.orderId = ?";
    BabysitterOrder order = dao.getSingleResultByHQL(BabysitterOrder.class, hql, orderNo);
    if (order == null) {
      result.setResult(ResultInfo.BABYSITTER_ORDER_NULL);
      return result;
    }
    if (order.getState() != Constants.NEW_ORDER) {
      result.setResult(ResultInfo.INVALID_ORDER);
      return result;
    }

    Map<String, String> params =
        RequestXMLCreater.getInstance()
            .getPayRequestMap(
                order.getGuid(),
                "月嫂定金",
                order.getOrderId() + BabysitterOrderService.FRONT,
                String.valueOf(order.getOrderFrontPrice() * 100),
                ip,
                openid);
    String xml =
        RequestXMLCreater.getInstance()
            .buildXmlString(params, Constants.WECHAT_OPENID_PAY_APPSECRET);
    System.out.println("======================>" + xml);
    sendPaymentToWeChatServer(result, order, xml);
    return result;
  }
 @Transactional
 public PageResult markBabysitter(String babysitterGuid, String orderGuid, PageResult result) {
   Babysitter babysitter = dao.getResultByGUID(Babysitter.class, babysitterGuid);
   ServiceOrder serviceOrder = dao.getResultByGUID(ServiceOrder.class, orderGuid);
   BabysitterOrder order = updateBabysitterOrder(orderGuid, babysitter, serviceOrder);
   result.setResult(ResultInfo.SUCCESS);
   result.put("result", order.view());
   return result;
 }
 public PageResult orderHistoryRecord(String orderGuid, PageResult result) {
   BabysitterOrder order = dao.getResultByGUID(BabysitterOrder.class, orderGuid);
   if (order == null) {
     result.setResult(ResultInfo.BABYSITTER_ORDER_NULL);
     return result;
   }
   result.setResult(ResultInfo.SUCCESS);
   result.put("result", order.getInfos());
   return result;
 }
 @Transactional
 public PageResult serviceEnd(String orderGuid, PageResult result) {
   BabysitterOrder order = dao.getResultByGUID(BabysitterOrder.class, orderGuid);
   if (order == null) {
     result.setResult(ResultInfo.BABYSITTER_ORDER_NULL);
     return result;
   }
   order.setState(Constants.OUT_ORDER);
   // 设置订单日志
   BabysitterOrderRecordInfo info = BabysitterOrderRecordInfo.getInstance();
   info.setBabysitterOrder(order);
   info.setState(order.getState());
   dao.add(info);
   result.setResult(ResultInfo.SUCCESS);
   return result;
 }
 public PageResult getBabysitterOrderInfo(String serviceOrderGuid, PageResult result) {
   ServiceOrder serviceOrder = dao.getResultByGUID(ServiceOrder.class, serviceOrderGuid);
   if (serviceOrder == null) {
     result.setResult(ResultInfo.SERVICE_ORDER_NULL);
     return result;
   }
   if (StringUtils.isEmpty(serviceOrder.getOrderGuid())) {
     result.setResult(ResultInfo.NO_BABYSITTER_SERVICE_ORDER);
     return result;
   }
   String orderGuid = serviceOrder.getOrderGuid();
   BabysitterOrder order = dao.getResultByGUID(BabysitterOrder.class, orderGuid);
   result.setResult(ResultInfo.SUCCESS);
   result.put("result", order.view());
   return result;
 }
 public PageResult endWechatOrder(String orderNo, String ip, PageResult result, String openid) {
   String hql = "from BabysitterOrder t where t.orderId = ?";
   BabysitterOrder order = dao.getSingleResultByHQL(BabysitterOrder.class, hql, orderNo);
   if (order == null) {
     result.setResult(ResultInfo.BABYSITTER_ORDER_NULL);
     return result;
   }
   if (order.getState() != Constants.EARNEST_MONEY) {
     result.setResult(ResultInfo.INVALID_ORDER);
     return result;
   }
   BigDecimal orderPrice = new BigDecimal(order.getOrderPrice());
   orderPrice = orderPrice.add(new BigDecimal(0 - order.getOrderFrontPrice()));
   orderPrice = orderPrice.multiply(new BigDecimal(100));
   Map<String, String> params =
       RequestXMLCreater.getInstance()
           .getPayRequestMap(
               order.getGuid(),
               "月嫂尾款",
               order.getOrderId() + BabysitterOrderService.END,
               orderPrice.toString(),
               ip,
               openid);
   String xml =
       RequestXMLCreater.getInstance()
           .buildXmlString(params, Constants.WECHAT_OPENID_PAY_APPSECRET);
   System.out.println("======================>" + xml);
   sendPaymentToWeChatServer(result, order, xml);
   return result;
 }
  @Transactional
  public ResultInfo addBabysitterOrderEvaluate(String orderGuid, String msg, String score) {
    BabysitterOrder order = dao.getResultByGUID(BabysitterOrder.class, orderGuid);
    if (order == null) return ResultInfo.BABYSITTER_ORDER_NULL;
    Babysitter babysitter = dao.getResultByGUID(Babysitter.class, order.getBabysitter().getGuid());
    if (babysitter == null) return ResultInfo.BABYSITTER_NULL;
    order.setScore(Integer.valueOf(score));
    order.setEvaluation(msg);
    order.setState(Constants.ORDER_OVER);
    dao.update(order);
    BabysitterOrderRecordInfo info = BabysitterOrderRecordInfo.getInstance();
    info.setBabysitterOrder(order);
    info.setState(order.getState());
    dao.add(info);

    // BabysitterEvaluate evaluate = BabysitterEvaluate.getInstance();
    // evaluate.setBabysitter(babysitter);
    // evaluate.setEmployer(employer);
    // evaluate.setOrder(order);
    // evaluate.setMsg(msg);
    // evaluate.setScore(Integer.valueOf(score));
    // dao.add(evaluate);
    int sumSocre = 0;
    int count = 0;
    List<BabysitterOrder> orders = babysitter.getOrders();

    for (BabysitterOrder o : orders) {
      if (o.getState() == Constants.ORDER_OVER) {
        sumSocre += o.getScore();
        count += 1;
      }
    }
    int avg = 0;
    if (count != 0) {
      avg = sumSocre / count;
    }
    babysitter.setScore(avg);
    dao.update(babysitter);
    return ResultInfo.SUCCESS;
  }
  @SuppressWarnings("resource")
  private void sendPaymentToWeChatServer(PageResult result, BabysitterOrder order, String xml) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(Constants.WECHAT_OPENID_PAY_URL);
    StringEntity myEntity = new StringEntity(xml, "UTF-8");
    httppost.addHeader("Content-Type", "text/xml");
    httppost.setEntity(myEntity);
    try {
      HttpResponse response = httpclient.execute(httppost);
      HttpEntity resEntity = response.getEntity();
      InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "UTF-8");
      StringBuffer resultStr = new StringBuffer();
      char[] buff = new char[1024];
      int length = 0;
      while ((length = reader.read(buff)) != -1) {
        resultStr.append(new String(buff, 0, length));
      }
      System.out.println(resultStr.toString());
      httpclient.getConnectionManager().shutdown();
      Map<String, String> responseResult =
          RequestXMLCreater.getInstance().getXmlMapResult(resultStr.toString());
      Map<String, String> resultMap;
      if (Constants.PAY_SUCCESS.equals(responseResult.get("return_code"))
          && Constants.PAY_MESSAGE_OK.equals(responseResult.get("return_msg"))
          && Constants.PAY_SUCCESS.equals(responseResult.get("result_code"))) {
        resultMap =
            RequestXMLCreater.getInstance()
                .getPayResponseMap(responseResult.get("prepay_id"), order.getGuid());

        result.setResult(ResultInfo.SUCCESS);
        result.put("result", resultMap);
      } else {
        result.put("code", responseResult.get("err_code"));
        result.put("msg", responseResult.get("err_code_des"));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  private BabysitterOrder updateBabysitterOrder(
      String orderGuid, Babysitter babysitter, ServiceOrder serviceOrder) {
    // 添加月嫂订单
    BabysitterOrder babysitterOrder = BabysitterOrder.getInstance();
    babysitterOrder.setBabysitter(babysitter);
    babysitterOrder.setEmployer(serviceOrder.getEmployer());
    babysitterOrder.setEmployerAddress(serviceOrder.getAddress());
    babysitterOrder.setEmployerName(serviceOrder.getEmployerName());
    babysitterOrder.setEmployerTelephone(serviceOrder.getMobilePhone());
    babysitterOrder.setOrderId(recordService.createOrderId());
    babysitterOrder.setOrderPrice(serviceOrder.getOrderPrice());
    long frontPrice = getFrontPrice(serviceOrder.getOrderPrice());
    babysitterOrder.setOrderFrontPrice(frontPrice);
    babysitterOrder.setServiceBeginDate(serviceOrder.getServiceBeginDate());
    babysitterOrder.setServiceEndDate(serviceOrder.getServiceEndDate());
    babysitterOrder.setState(Constants.NEW_ORDER);
    dao.add(babysitterOrder);
    // 添加月嫂订单变更信息
    BabysitterOrderRecordInfo recordInfo = BabysitterOrderRecordInfo.getInstance();
    recordInfo.setBabysitterOrder(babysitterOrder);
    recordInfo.setState(babysitterOrder.getState());
    dao.add(recordInfo);

    // 更新雇主订单
    serviceOrder.setOrderGuid(babysitterOrder.getGuid());
    dao.update(serviceOrder);

    // 更新月嫂抢单通知表中该订单的所有记录为不可用即订单已经被抢
    String hql =
        "from PanicBuyingBabysitterAdvice t where t.ovld = true and t.serviceOrder.guid = ?";
    List<PanicBuyingBabysitterAdvice> list =
        dao.getListResultByHQL(PanicBuyingBabysitterAdvice.class, hql, orderGuid);
    for (PanicBuyingBabysitterAdvice advice : list) {
      advice.setIsOver(true);
      dao.update(advice);
    }
    return babysitterOrder;
  }