/**
   * ** 更新系统客户 信息,返回json给客户端
   *
   * @param _id
   * @param data
   * @param user_id
   * @param request
   * @param part_flg
   * @return
   */
  @RequestMapping(value = "/{_id}/update_part", method = RequestMethod.POST)
  @ResponseBody
  public Object update_part(
      @PathVariable String _id, Client client, HttpServletRequest request, String part_flg) {

    String user_id = client.getOwner_user_id();
    if (!this.isValidObjId(_id)) {
      return this.handleValidateFalse("非法的客户主键");
    }

    if (!this.isValidObjId(user_id)) {
      return this.handleValidateFalse("非法的用户");
    }

    if (!PartFlgEnum.isValidPartFlg(part_flg)) {
      return this.handleValidateFalse("非法的更新参数part_flg");
    }

    client.set_id_m(_id);

    String phone_info = request.getParameter("phone_info");
    String address_info = request.getParameter("address_info");
    String interesting_services = request.getParameter("interesting_service");

    List<Phone> phones =
        JsonUtil.getGson().fromJson(phone_info, new TypeToken<List<Phone>>() {}.getType());

    List<Address> addresses =
        JsonUtil.getGson().fromJson(address_info, new TypeToken<List<Address>>() {}.getType());

    List<String> interesting_service =
        JsonUtil.getGson()
            .fromJson(interesting_services, new TypeToken<List<String>>() {}.getType());

    client.setInteresting_service(interesting_service);
    client.setPhone_info(phones);
    client.setAddress_info(addresses);

    logger.debug("传入的用户对象\n{}", client);

    try {
      IModifyClientInfoService modifyClientService = getModifyService(part_flg);
      DBObject updateResult = modifyClientService.updatePart(null, client);

      logger.debug("更新后的结果[{}]", updateResult);

      RequestResult rr = new RequestResult();
      rr.setSuccess(true);
      rr.setMessage(_id);
      return rr;
    } catch (Exception e) {
      return this.handleException(e);
    }
  }
  public static Map<String, Object> doPostToWx(String url, String outstr) {

    // 创建HttpClientBuilder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // HttpClient
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    HttpPost httpPost = new HttpPost(url);

    Map<String, Object> rtnMap = null;
    try {
      httpPost.setEntity(new StringEntity(outstr, "UTF-8"));
      HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
      // 获取响应消息实体
      HttpEntity entity = httpResponse.getEntity();
      // 响应状态
      logger.debug("status:{}", httpResponse.getStatusLine());
      // 判断响应实体是否为空
      if (entity != null) {
        logger.debug("contentEncoding:" + entity.getContentEncoding());
        String responseStr = EntityUtils.toString(entity, "UTF-8");
        logger.debug("response content\n", responseStr);

        rtnMap = JsonUtil.fromJson(responseStr, Map.class);

        logger.debug("返回的Map:\n{}", rtnMap);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        // 关闭流并释放资源
        closeableHttpClient.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return rtnMap;
  }
  public static AccessToken getAccessToken() {

    AccessToken accessToken = null;

    boolean isExist = false;
    String _id = null;

    // 1. 从全局配置中取ACCESSTOKEN
    CommonDaoMongo commonDaoMongo = new CommonDaoMongo();
    Criteria criteria = Criteria.where("APPID").is(APPID);
    criteria.and("APPSECRET").is(APPSECRET);
    Query query = new Query(criteria);
    accessToken = commonDaoMongo.findOne(query, AccessToken.class);

    logger.debug("数据库中的ACCESS_TOKEN\n{}", accessToken);

    if (accessToken != null) {
      isExist = true;
      _id = accessToken.get_id_m();

      String last_op_time = accessToken.getLast_op_time();
      String now = DateUtil.getCurrentTimsmp();

      logger.debug("\n{}\n{}", last_op_time, now);
      int between_seconds = DateUtil.getIntervalTimeStampSecond(now, last_op_time);
      logger.debug("距上次获取时间差[{}]秒", between_seconds);

      int expires_in = accessToken.getExpires_in() - 200;
      logger.debug("expires_in[{}]", expires_in);

      if (between_seconds <= expires_in) {
        return accessToken;
      }
    }

    // 2.远程获取AccessToken
    String tokenUrl = getTokenUrl.replace("APPID", APPID).replace("APPSECRET", APPSECRET);

    Map<String, Object> result = doGet(tokenUrl);
    if (result == null) {
      return null;
    }

    accessToken = JsonUtil.fromJson(JsonUtil.toJsonStr(result), AccessToken.class);

    // 3.设置到全局数据库中

    if (isExist) {
      // 更新
      DBObject update = new BasicDBObject();
      DBObject updateSet = new BasicDBObject();
      updateSet.put("access_token", accessToken.getAccess_token());
      updateSet.put("expires_in", accessToken.getExpires_in());
      CommonDomainInfoSetter.setModifyInfo(updateSet);
      update.put("$set", updateSet);
      update.put("$inc", new BasicDBObject("times", 1));

      DBObject upResult = commonDaoMongo.updateOneById(_id, null, update, AccessToken.class);
      logger.info("更新成功,更新后的结果[{}]", JsonUtil.getPrettyJsonStr(upResult));
    } else {
      // 插入
      accessToken.setAPPID(APPID);
      accessToken.setAPPSECRET(APPSECRET);
      accessToken.setTimes(1);
      CommonDomainInfoSetter.setCreateInfo(accessToken);

      _id = commonDaoMongo.insertOne(accessToken);

      logger.info("首次获取token,插入数据库成功!_id[{}]", _id);
    }

    return accessToken;
  }
  /**
   * ** 添加
   *
   * @return
   */
  @RequestMapping(value = "/add", method = RequestMethod.POST)
  @ResponseBody
  public Object add(@Validated Client client, BindingResult br, HttpServletRequest request) {

    HttpServletRequestUtil.debugParams(request);

    String phone_info = request.getParameter("phone_info");
    String address_info = request.getParameter("address_info");
    String interesting_services = request.getParameter("interesting_service");

    List<Phone> phones =
        JsonUtil.getGson().fromJson(phone_info, new TypeToken<List<Phone>>() {}.getType());

    List<Address> addresses =
        JsonUtil.getGson().fromJson(address_info, new TypeToken<List<Address>>() {}.getType());

    List<String> interesting_service =
        JsonUtil.getGson()
            .fromJson(interesting_services, new TypeToken<List<String>>() {}.getType());

    client.setInteresting_service(interesting_service);
    client.setPhone_info(phones);
    client.setAddress_info(addresses);

    logger.debug("传入的用户对象\n{}", client);

    // String userId = this.getUserId();
    // if (StringUtil.isEmpty(userId)) {
    // return this.handleValidateFalse("所属用户id不能为空");
    // }
    //
    // client.setOwner_user_id(userId);

    if (br.hasErrors()) {
      return ErrorHandler.getRequestResultFromBindingResult(br);
    }
    try {
      // 1.校验是否已存在相同的类型码
      // boolean isExist = this.clientService
      // .isExistSameTypecode(client.getTypecode());
      // if (isExist) {
      // RequestResult rr = new RequestResult();
      // rr.setSuccess(false);
      // rr.setMessage("已经存在类型码【" + client.getTypecode().trim()
      // + "】的用户!");
      // return rr;
      // }

      // 2.新增
      client.setUseflg("1");
      String _id = this.clientService.add(client);

      RequestResult rr = new RequestResult();
      rr.setSuccess(true);
      rr.setMessage(_id);

      return rr;
    } catch (Exception e) {
      return this.handleException(e);
    }
  }