package com.house365.web.wechat;

import com.google.common.base.Strings;
import com.house365.common.constant.CacheConst;
import com.house365.common.util.DateTimeUtils;
import com.house365.common.util.RestTemplateBean;
import com.house365.config.HgsProp;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Description
 * <p>
 * Created by 365 on 2017/3/7 0007.
 */
@Controller
@EnableAutoConfiguration
@RequestMapping("/customer")
public class CustomerController {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HgsProp hgsProp;
    private Integer pageSize = 10;

    @RequestMapping("/sea")
    public String customerIndex(Model model, HttpServletRequest request) {
        List<NameValuePair> params = getParams("getCustomerOceanSummary", request);

        try {

            Date now = new Date();
            Date begin = DateTimeUtils.getDaysAgo(now, 30);

            String startDate = DateTimeUtils.parseDate(begin, DateTimeUtils.DEFAULT_DATE_FORMAT_PATTERN_SHORT);
            String endDate = DateTimeUtils.parseDate(now, DateTimeUtils.DEFAULT_DATE_FORMAT_PATTERN_SHORT);

            model.addAttribute("startDate", startDate);
            model.addAttribute("endDate", endDate);

            String usersInfo = "";
            if (!redisTemplate.hasKey(CacheConst.USERSKEY)) {
                List<NameValuePair> userParams = getParams("getManagers", request);
                usersInfo = RestTemplateBean.doGet(hgsProp.getHgsUrl(), userParams);
                redisTemplate.opsForValue().set(CacheConst.USERSKEY, usersInfo, 30, TimeUnit.SECONDS);
            }

            String customers = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject data = getData(customers);
            if (data != null && data.containsKey("paraMap") && data.get("paraMap") != null) {
                JSONObject getYou = (JSONObject) data.get("paraMap");
                model.addAttribute("data", getYou);
            }

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }

        return "wechat/customerSea";
    }

    /**
     * 异步加载客户数据
     *
     * @param day
     * @param page
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/customerCell/{day}/{page}")
    public String customerCell(
            @PathVariable(value = "day", required = false) String day,
            @PathVariable(value = "page", required = false) String page, Model model, HttpServletRequest request
    ) {
        List<NameValuePair> params = getParams("getCustomerInOcean", request);
        params.add(new BasicNameValuePair("page", Strings.isNullOrEmpty(page) ? "1" : page));
        params.add(new BasicNameValuePair("pageSize", "" + pageSize));

        try {

            String startDate = request.getParameter("startDate");
            String endDate = request.getParameter("endDate");
            if (!Strings.isNullOrEmpty(startDate)) {
                params.add(new BasicNameValuePair("createTimeStart", startDate));
            }
            if (!Strings.isNullOrEmpty(endDate)) {
                params.add(new BasicNameValuePair("createTimeEnd", endDate));
            }

            //组装查询条件
            String buyIntention = request.getParameter("buyIntention") == null ? "" : request.getParameter(
                    "buyIntention");
            if (!Strings.isNullOrEmpty(buyIntention)) {
                params.add(new BasicNameValuePair("buyIntention", buyIntention));
            }

            //get customer list
            JSONArray customers = getCustomerList(day, params);

            model.addAttribute("list", customers);

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }

        return "wechat/customerSeaCell";
    }

    /**
     * 数据公海页面
     *
     * @param day
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/seal/{day}")
    public String customerInSeaList(
            @PathVariable(value = "day", required = false) String day, Model model, HttpServletRequest request
    ) {
        String page = request.getParameter("page");
        List<NameValuePair> params = getParams("getCustomerInOcean", request);
        params.add(new BasicNameValuePair("page", Strings.isNullOrEmpty(page) ? "1" : page));
        params.add(new BasicNameValuePair("pageSize", "" + pageSize));

        try {
            String startDate = request.getParameter("startDate");
            String endDate = request.getParameter("endDate");
            if (!Strings.isNullOrEmpty(startDate)) {
                params.add(new BasicNameValuePair("createTimeStart", startDate));
                model.addAttribute("startDate", startDate);
            }
            if (!Strings.isNullOrEmpty(endDate)) {
                params.add(new BasicNameValuePair("createTimeEnd", endDate));
                model.addAttribute("endDate", endDate);
            }
            //save page var
            String buyIntention = request.getParameter("buyIntention");
            if (!Strings.isNullOrEmpty(buyIntention)) {
                params.add(new BasicNameValuePair("buyIntention", buyIntention));
                model.addAttribute("buyIntention", buyIntention);
            }

            //get user list by cache
            JSONArray users = getUserData(request);
            model.addAttribute("users", users);

            //get customer list
            JSONArray customers = getCustomerList(day, params);

            model.addAttribute("list", customers);
            model.addAttribute("day", day);

            switch (day) {
                case "1":
                    model.addAttribute("title", "数据公海-今日数据");
                    break;
                case "7":
                    model.addAttribute("title", "数据公海-最近7天");
                    break;
                default:
                    model.addAttribute("title", "数据公海");
                    break;
            }

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }

        return "wechat/data-seas";
    }

    //公海查询页面
    @RequestMapping("/search")
    public String findsea(
            Model model, HttpServletRequest request
    ) {

        return "wechat/findSea";
    }

    //公海查询
    @RequestMapping(value = "/querySea")
    public String querySea(
            Model model, HttpServletRequest request
    ) {
        List<NameValuePair> params = getParams("getCustomerInOcean", request);
        params.add(new BasicNameValuePair("page", "1"));
        params.add(new BasicNameValuePair("pageSize", "" + pageSize));
        try {
            String startDate = request.getParameter("startDate");
            String endDate = request.getParameter("endDate");
            if (!Strings.isNullOrEmpty(startDate)) {
                params.add(new BasicNameValuePair("createTimeStart", startDate));
            }
            if (!Strings.isNullOrEmpty(endDate)) {
                params.add(new BasicNameValuePair("createTimeEnd", endDate));
            }

            String buyIntention = request.getParameter("buyIntention");
            if (!Strings.isNullOrEmpty(buyIntention)) {
                params.add(new BasicNameValuePair("buyIntention", buyIntention));
                model.addAttribute("buyIntention", buyIntention);
            }

            //get user list by cache
            JSONArray users = getUserData(request);
            model.addAttribute("users", users);

            //get customer list
            JSONArray customers = getCustomerList("2", params);

            model.addAttribute("list", customers);
            model.addAttribute("day", "2");
            model.addAttribute("title", "数据公海");
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }

        return "wechat/data-seas";
    }

    @RequestMapping(value = "/callclient", method = RequestMethod.GET)
    public ModelAndView callClient(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/call-client");
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getHotLineCount"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject result = JSONObject.fromObject(resultStr);

            view.addObject("counts", result.getJSONObject("data").getJSONObject("entity"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return view;
    }

    @RequestMapping(value = "/evaluationCell", method = RequestMethod.GET)
    public String evaluationCell(Model model, HttpServletRequest request) {
        String checkStatus = request.getParameter("checkStatus");
        String page = request.getParameter("page");
        if (checkStatus == null || "".equals(checkStatus)) {
            checkStatus = "0";
        }
        model.addAttribute("checkStatus", checkStatus);
        if (page == null || "".equals(page)) {
            page = "0";
        }
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Hgs"));
            params.add(new BasicNameValuePair("serviceName", "getEvaluationList"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("checkStatus", checkStatus));
            params.add(new BasicNameValuePair("page", page));
            params.add(new BasicNameValuePair("pageSize", "2"));
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject result = JSONObject.fromObject(resultStr);
            JSONArray array = result.getJSONObject("data").getJSONArray("list");
            if (array != null && array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    JSONObject tags = array.getJSONObject(i);
                    String temp = tags.getString("tagsArr");
                    String[] s = temp.split(" ");
                    if (temp != null && temp.length() > 2) {
                        String temp2 = temp.substring(2, temp.length() - 2);
                        String[] tt = temp2.split(",");
                        array.getJSONObject(i).put("tagsArr", temp2);
                    }
                }
            }
            model.addAttribute("evaluationList", array);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "wechat/evaluationCell";
    }

    /**
     * evaluation评价列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/evaluation", method = RequestMethod.GET)
    public String evaluation(Model model, HttpServletRequest request) {
//        ModelAndView view = new ModelAndView();
        String checkStatus = request.getParameter("checkStatus");
        String page = request.getParameter("page");
        if (checkStatus == null || "".equals(checkStatus)) {
            checkStatus = "0";
        }
        if (page == null || "".equals(page)) {
            page = "0";
        }
//        view.setViewName("wechat/evaluation-list");
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Hgs"));
            params.add(new BasicNameValuePair("serviceName", "getEvaluationList"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("checkStatus", checkStatus));
            params.add(new BasicNameValuePair("page", page));
            params.add(new BasicNameValuePair("pageSize", "2"));
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject result = JSONObject.fromObject(resultStr);

//            view.addObject("evaluationList", result.getJSONObject("data").getJSONObject("entity"));
            JSONArray array = result.getJSONObject("data").getJSONArray("list");
            if (array != null && array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    JSONObject tags = array.getJSONObject(i);
                    String temp = tags.getString("tagsArr");
                    String[] s = temp.split(" ");
                    if (temp != null && temp.length() > 2) {
                        String temp2 = temp.substring(2, temp.length() - 2);
                        String[] tt = temp2.split(",");
                        JSONObject jo = array.getJSONObject(i);
                        for (int j = 0; j < tt.length; j++) {
                            tt[j] = tt[j].replaceAll("\"", "");
                            jo.put("tag" + j, tt[j]);
                        }
                    }
                }
            }
//            JSONArray array = JSONArray.fromObject(result.getJSONObject("data"));
            model.addAttribute("evaluationList", array);
            model.addAttribute("checkStatus", checkStatus);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
//        return view;
        return "wechat/evaluation-list";
    }

    /**
     * 审核
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/review", method = RequestMethod.GET)
    @ResponseBody
    public String review(HttpServletRequest request) {
        String result = "fail";
//        List<NameValuePair> params = getParams("reviewEvaluation", request);
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Hgs"));
            params.add(new BasicNameValuePair("serviceName", "reviewEvaluation"));
            String evid = request.getParameter("evid");
            params.add(new BasicNameValuePair("id", evid));
            RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            result = "success";
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
//        model.addAttribute("checkStatus", checkStatus);
        return result;
    }

    /**
     * 审核
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delEvaluation", method = RequestMethod.GET)
    @ResponseBody
    public String delEvaluation(HttpServletRequest request) {
        String result = "fail";
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Hgs"));
            params.add(new BasicNameValuePair("serviceName", "delEvaluation"));
            String evid = request.getParameter("evid");
            params.add(new BasicNameValuePair("id", evid));
            RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            result = "success";
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/hotlinequery", method = RequestMethod.GET)
    public ModelAndView hotLineQuery(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/query-condition");
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getManagers"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject result = JSONObject.fromObject(resultStr);

            view.addObject("managers", result.getJSONObject("data").getJSONArray("list"));

            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-8:00"));
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -30);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.MILLISECOND, 1);

            view.addObject("startTime", DateTimeUtils.parseDate(calendar.getTime(), "yyyy-MM-dd"));
            view.addObject("endTime", DateTimeUtils.parseDate(new Date(), "yyyy-MM-dd"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return view;
    }

    @RequestMapping(value = "/hotline", method = RequestMethod.GET)
    public ModelAndView hotLine(
            HttpServletRequest request, String timeFilter, String startTime, String endTime, String managerFilter,
            String customerStatus
    ) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/today-call");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        if (StringUtils.isNotBlank(timeFilter)) {
            switch (timeFilter) {
                case "1":
                    view.addObject("endTime", DateFormatUtils.format(calendar, "yyyy-MM-dd HH:mm:ss"));
                    view.addObject("startTime", DateFormatUtils.format(calendar, "yyyy-MM-dd 00:00:00"));
                    break;
                case "2":
                    view.addObject("endTime", DateFormatUtils.format(calendar, "yyyy-MM-dd HH:mm:ss"));
                    calendar.add(Calendar.DATE, -Calendar.DAY_OF_WEEK);
                    view.addObject("startTime", DateFormatUtils.format(calendar, "yyyy-MM-dd 00:00:00"));
                    break;
                default:
                    break;
            }
            view.addObject("timeFilter", timeFilter);
        } else {
            view.addObject("startTime", startTime);
            view.addObject("endTime", endTime);
        }

        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getManagers"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject result = JSONObject.fromObject(resultStr);

            view.addObject("managers", result.getJSONObject("data").getJSONArray("list"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        if (StringUtils.isNotBlank(managerFilter)) {
            view.addObject("managerFilter", managerFilter);
        }
        if (StringUtils.isNotBlank(customerStatus)) {
            view.addObject("customerStatus", customerStatus);
        }
        return view;
    }

    @RequestMapping(value = "/hotline", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getHotLineCustomers(
            HttpServletRequest request, String currentPage, String managerFilter, String customerStatus, String startTime,
            String endTime
    ) {
        String result = "fail";
        List<NameValuePair> params = new ArrayList<>();
        try {
            logger.debug(
                    "==doGetHotLineCustomer>>currentPage:{}==managerFilter:{}==startTime:{}==" + "endTime:{}==customerStatus:{}==",
                    currentPage, managerFilter, startTime, endTime, customerStatus);
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getHotLineCustomers"));
            if (!Strings.isNullOrEmpty(managerFilter)) {
                params.add(new BasicNameValuePair("managerFilter", managerFilter));
            }
            params.add(new BasicNameValuePair("pageNo", currentPage));
            if (!Strings.isNullOrEmpty(customerStatus)) {
                params.add(new BasicNameValuePair("customerStatus", customerStatus));
            }
            if (!Strings.isNullOrEmpty(startTime)) {
                params.add(new BasicNameValuePair("startTime", startTime));
            }
            if (!Strings.isNullOrEmpty(endTime)) {
                params.add(new BasicNameValuePair("endTime", endTime));
            }
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.debug("===>>getHotLineCustomers>>==result:{}====", result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;
    }

    @RequestMapping(value = "/addvisit", method = RequestMethod.GET)
    public ModelAndView addVisit(String customerId, String phone, HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.addObject("customerId", customerId);
        view.addObject("phone", phone);

        try {
            // 获取回访内容模板
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getCustomerCallbackTemplateList"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject data = JSONObject.fromObject(result).getJSONObject("data");
            if (data != null) {
                JSONArray list = data.getJSONArray("list");
                if (list != null) {
                    view.addObject("callbackTemplateList", list);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        view.setViewName("wechat/add-visit");
        return view;
    }

    @RequestMapping(value = "/addvisit", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addVisit(HttpServletRequest request, String customerId, String callContent) {
        JSONObject result = new JSONObject();
        try {
            List<NameValuePair> customerProjectParams = new ArrayList<>();
            customerProjectParams.add(new BasicNameValuePair("serviceCode", "Wechat"));
            customerProjectParams.add(new BasicNameValuePair("serviceName", "addVisitCallContent"));
            customerProjectParams.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            customerProjectParams.add(new BasicNameValuePair("customerId", customerId));
            customerProjectParams.add(new BasicNameValuePair("callContent", callContent));

            String buyIntention = request.getParameter("buyIntention") == null ? "99" : request.getParameter(
                    "buyIntention");
            if (!Strings.isNullOrEmpty(buyIntention)) {
                customerProjectParams.add(new BasicNameValuePair("buyIntention", buyIntention));
            }

            String customerProjectResult = RestTemplateBean.doGet(hgsProp.getHgsUrl(), customerProjectParams);
            result = JSONObject.fromObject(customerProjectResult);
            logger.info("=======addVisitCallContent======result:{}", result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result.toString();
    }

    @RequestMapping(value = "/deletehotlinecustomer", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteHotLineCustomer(HttpServletRequest request, String customerId) {
        JSONObject result = new JSONObject();
        try {
            if (Strings.isNullOrEmpty(customerId)) {
                return result.toString();
            }
            List<NameValuePair> customerProjectParams = new ArrayList<>();
            customerProjectParams.add(new BasicNameValuePair("serviceCode", "Wechat"));
            customerProjectParams.add(new BasicNameValuePair("serviceName", "deleteHotLineCustomer"));
            customerProjectParams.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            customerProjectParams.add(new BasicNameValuePair("customerId", customerId));

            String customerProjectResult = RestTemplateBean.doGet(hgsProp.getHgsUrl(), customerProjectParams);
            result = JSONObject.fromObject(customerProjectResult);
            logger.debug("===>>deleteHotLineCustomer>>==result:{}====", result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result.toString();
    }

    //派工
    @RequestMapping(value = "/bindManager")
    @ResponseBody
    public String bindManager(
            HttpServletRequest request
    ) {
        String result = "fail";
        List<NameValuePair> params = getParams("bindManager", request);
        try {
            String customerId = request.getParameter("customerId");
            String assignFrom = request.getParameter("assignFrom");
            //置业顾问ID
            String userId = request.getParameter("userId");
            params.add(new BasicNameValuePair("customerIds", customerId));
            params.add(new BasicNameValuePair("userIds", userId));
            params.add(new BasicNameValuePair("assignFrom", assignFrom));
            //操作者手机号
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            result = "success";
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    //删除无效客户
    @RequestMapping(value = "/deleteFormOcean")
    @ResponseBody
    public String deleteFormOcean(
            HttpServletRequest request
    ) {
        String result = "fail";
        List<NameValuePair> params = getParams("deleteFormOcean", request);
        try {
            String customerId = request.getParameter("customerId");
            params.add(new BasicNameValuePair("customerId", customerId));
            RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            result = "success";
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    //我的客户首页
    @RequestMapping(value = "/myCustomers")
    public String myCustomers(HttpServletRequest request, Model model) {
        List<NameValuePair> params = getParams("myCustomerIndex", request);
        List<NameValuePair> params2 = getParams("myCustomerUserIndex", request);
        try {
            //定位tab
            String viewTab = request.getParameter("viewTab") == null ? "1" : request.getParameter("viewTab");
            model.addAttribute("viewTab", viewTab);

            //操作者手机号
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String summary = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);

            params2.add(new BasicNameValuePair("page", "1"));
            params2.add(new BasicNameValuePair("pageSize", "" + pageSize));
            params2.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String list = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params2);

            JSONObject summaryData = getData(summary);
            JSONObject listData = getData(list);

            JSONArray customerList = new JSONArray();

            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
            }

            model.addAttribute("summary", summaryData);
            model.addAttribute("listData", customerList);

            Date now = new Date();
            Date begin = DateTimeUtils.getDaysAgo(now, 30);

            String startDate = DateTimeUtils.parseDate(begin, DateTimeUtils.DEFAULT_DATE_FORMAT_PATTERN_SHORT);
            String endDate = DateTimeUtils.parseDate(now, DateTimeUtils.DEFAULT_DATE_FORMAT_PATTERN_SHORT);

            model.addAttribute("startDate", startDate);
            model.addAttribute("endDate", endDate);

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/myCustomers";
    }

    //我的客户首页-客户经理视图-cell
    @RequestMapping(value = "/myCustomersUser")
    public String myCustomersUser(
            HttpServletRequest request, Model model
    ) {
        List<NameValuePair> params = getParams("myCustomerUserIndex", request);
        String page = request.getParameter("page");
        params.add(new BasicNameValuePair("pageNo", Strings.isNullOrEmpty(page) ? "2" : page));
        params.add(new BasicNameValuePair("pageSize", "" + pageSize));
        try {
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String list = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject listData = getData(list);

            JSONArray customerList = new JSONArray();

            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
            }

            model.addAttribute("listData", customerList);

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/myCustomerCell";
    }

    @RequestMapping(value = "/myCustomerList")
    public String myCustomerList(HttpServletRequest request, Model model) {
        List<NameValuePair> params = getParams("getMyCustomerList", request);
        String labelId = request.getParameter("labelId");
        String page = request.getParameter("page");
        params.add(new BasicNameValuePair("pageNo", Strings.isNullOrEmpty(page) ? "0" : page));
        params.add(new BasicNameValuePair("pageSize", "" + pageSize));
        if(StringUtils.isNotBlank(labelId)){
            params.add(new BasicNameValuePair("labelId", labelId));
        }

        try {
            warpSearchCustomer(request, model, params);

            String list = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject listData = getData(list);
            JSONArray customerList = new JSONArray();
            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
                if (customerList.size() > 0) {
                    model.addAttribute("currentSystemId", customerList.getJSONObject(0).getString("currentSystemId"));
//                    for(int i=0;i<customerList.size();i++){
//                        JSONObject jo = customerList.getJSONObject(i);
//                        List<Object> lids = (List<Object>)jo.get("lids");
//                        if(lids.size()>0){
//
//                        }
//                    }
                }
            }
            model.addAttribute("listData", customerList);
            model.addAttribute("labelId", labelId);

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/myCustomerList";
    }

    /**
     * 待回访客户
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/myWaitCallCustomer")
    public String myWaitCallCustomer(HttpServletRequest request, Model model) {
        List<NameValuePair> params = getParams("getMyWaitCallCustomerList", request);
        String page = request.getParameter("page");
        params.add(new BasicNameValuePair("pageNo", Strings.isNullOrEmpty(page) ? "0" : page));
        params.add(new BasicNameValuePair("pageSize", "20"));
        try {
//            warpSearchCustomer(request, model, params);
            String list = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject listData = getData(list);
            JSONArray customerList = new JSONArray();
            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
            }
            if (CollectionUtils.isNotEmpty(customerList)) {
                for (int i = 0; i < customerList.size(); i++) {
                    JSONObject customer = (JSONObject) customerList.get(i);
                    String bindTime = customer.get("bindTime").toString();
                    JSONObject newbindTime = JSONObject.fromObject(bindTime);
                    String time = newbindTime.get("time").toString();
                    long lt = new Long(time);
                    Date date = new Date(lt);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    calendar.add(Calendar.HOUR_OF_DAY, -1);
                    Date newdate = calendar.getTime();
                    long l = newdate.getTime();
                    newbindTime.put("time", l);
                    customer.put("bindTime", newbindTime);
                }
            }

            model.addAttribute("listData", customerList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取我的待回访列表报错", e);
        }
        return "wechat/myWaitCalllist";
    }

    @RequestMapping(value = "/myWaitCallCustomerCell")
    public String myWaitCallCustomerCell(HttpServletRequest request, Model model) {
        List<NameValuePair> params = getParams("getMyWaitCallCustomerList", request);
        String page = request.getParameter("page");
        params.add(new BasicNameValuePair("pageNo", Strings.isNullOrEmpty(page) ? "0" : page));
        params.add(new BasicNameValuePair("pageSize", "20"));
        try {
            String list = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject listData = getData(list);
            JSONArray customerList = new JSONArray();
            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
            }
            model.addAttribute("listData", customerList);
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/myWaitCallCustomerCell";
    }

    @RequestMapping(value = "/waitCustomerDetail", method = RequestMethod.GET)
    public ModelAndView waitCustomerDetail(String customerId, String customerName, String buyIntention, String customerPhone, String managerName, String singupTime, String bindTime,
                                           String latestSource, String actSource, String isPrivate, int privateId) {
        ModelAndView view = new ModelAndView();
        view.addObject("customerId", customerId);
        view.addObject("customerName", customerName);
        view.addObject("customerPhone", customerPhone);
        view.addObject("managerName", managerName);
        view.addObject("bindTime", bindTime);
        view.addObject("singupTime", singupTime);
        view.addObject("latestSource", latestSource);
        view.addObject("actSource", actSource);
        view.addObject("buyIntention", buyIntention);
        view.addObject("isPrivate", isPrivate);
        view.addObject("privateId", privateId);
        view.setViewName("wechat/waitCustomerDetail");
        return view;
    }

    @RequestMapping(value = "/abandonCustomer", method = RequestMethod.GET)
    @ResponseBody
    public String abandonCustomer(HttpServletRequest request, String customerId) {
        ModelAndView view = new ModelAndView();
        JSONObject resultJson = new JSONObject();
        String result = "";
        List<NameValuePair> params = getParams("abandonCustomer", request);
        try {
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("customerId", customerId));
            String list = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            resultJson.put("result", "1");
            result = "success";
        } catch (Exception e) {
            e.printStackTrace();
            resultJson.put("result", "0");
        }
        return result;
    }

    /**
     * 约看楼盘
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/getBindProject", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object getBindProject(HttpServletRequest request, Model model) {
        JSONArray pList = new JSONArray();
        try {
            List<NameValuePair> params = getParams("getAssignProjects", request);
            String customerId = request.getParameter("customerId") == null ? "" : request.getParameter("customerId");
            String projectName = request.getParameter("projectName") == null ? "" : request.getParameter("projectName");
            projectName = URLEncoder.encode(projectName, "UTF-8");
            if (!Strings.isNullOrEmpty(projectName)) {
                params.add(new BasicNameValuePair("projectName", projectName));
            }
            if (!Strings.isNullOrEmpty(customerId)) {
                params.add(new BasicNameValuePair("customerId", customerId));
                String projectList = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
                JSONObject projectListData = getData(projectList);
                if (projectListData.containsKey("list") && projectListData.get("list") != null) {
                    pList = (JSONArray) projectListData.get("list");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return pList;
    }

    private void warpSearchCustomer(HttpServletRequest request, Model model, List<NameValuePair> params) {
        String type = request.getParameter("type") == null ? "" : request.getParameter("type");
        String buyIntention = request.getParameter("buyIntention") == null ? "" : request.getParameter("buyIntention");
        String startDate = request.getParameter("startDate") == null ? "" : request.getParameter("startDate");
        String endDate = request.getParameter("endDate") == null ? "" : request.getParameter("endDate");
        String traceStatus = request.getParameter("traceStatus") == null ? "" : request.getParameter("traceStatus");
        String projectStatus = request.getParameter("projectStatus") == null ? "" : request.getParameter(
                "projectStatus");
        String keyword = request.getParameter("searchKey") == null ? "" : request.getParameter("searchKey");

        String viewTab = "1";
        //置业顾问视图
        String userPhone = request.getParameter("userPhone") == null ? "" : request.getParameter("userPhone");

        //选择过滤某个置业顾问
        if (!Strings.isNullOrEmpty(userPhone)) {
            viewTab = "2";

            Iterator<NameValuePair> sListIterator = params.iterator();
            while (sListIterator.hasNext()) {
                NameValuePair e = sListIterator.next();
                if ("phone".equalsIgnoreCase(e.getName())) {
                    sListIterator.remove();
                }
            }

            model.addAttribute("userPhone", userPhone);
        }
        //返回定位视图
        model.addAttribute("viewTab", viewTab);

        params.add(new BasicNameValuePair("phone",
                !Strings.isNullOrEmpty(userPhone) ? userPhone : getPhoneFromCookie(request)));
        if (!Strings.isNullOrEmpty(type)) {
            params.add(new BasicNameValuePair("queryType", type));

            model.addAttribute("type", type);
        }
        if (!Strings.isNullOrEmpty(buyIntention)) {
            params.add(new BasicNameValuePair("buyIntention", buyIntention));
            model.addAttribute("buyIntention", buyIntention);
        }
        if (!Strings.isNullOrEmpty(startDate)) {
            params.add(new BasicNameValuePair("startDate", startDate));
            model.addAttribute("startDate", startDate);
        }
        if (!Strings.isNullOrEmpty(endDate)) {
            params.add(new BasicNameValuePair("endDate", endDate));
            model.addAttribute("endDate", endDate);
        }
        if (!Strings.isNullOrEmpty(traceStatus)) {
            params.add(new BasicNameValuePair("traceStatus", traceStatus));
            model.addAttribute("traceStatus", traceStatus);
        }
        if (!Strings.isNullOrEmpty(projectStatus)) {
            params.add(new BasicNameValuePair("projectStatus", projectStatus));
            model.addAttribute("projectStatus", projectStatus);
        }
        if (!Strings.isNullOrEmpty(keyword)) {
            params.add(new BasicNameValuePair("keyword", keyword));
            model.addAttribute("searchKey", keyword);
        }
    }

    @RequestMapping(value = "/myCustomerListCell")
    public String myCustomerListCell(
            HttpServletRequest request, Model model
    ) {
        List<NameValuePair> params = getParams("getMyCustomerList", request);
        String page = request.getParameter("page");
        String labelId = request.getParameter("labelId");
        params.add(new BasicNameValuePair("pageNo", Strings.isNullOrEmpty(page) ? "2" : page));
        params.add(new BasicNameValuePair("pageSize", "" + pageSize));
        if(StringUtils.isNotBlank(labelId)){
            params.add(new BasicNameValuePair("labelId", labelId));
        }

        try {
            warpSearchCustomer(request, model, params);

            String list = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject listData = getData(list);
            JSONArray customerList = new JSONArray();
            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
            }
            model.addAttribute("labelId", labelId);
            model.addAttribute("listData", customerList);

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/myCustomerListCell";
    }

    @RequestMapping(value = "/saveBindProject", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveBindProject(HttpServletRequest request) {
        String result = "0";
        try {
            List<NameValuePair> params = getParams("bindToProjects", request);
            String customerId = request.getParameter("customerId") == null ? "" : request.getParameter("customerId");
            String bindProjectId = request.getParameter("bindProjectId") == null ? "" : request.getParameter(
                    "bindProjectId");
            params.add(new BasicNameValuePair("customerIds", customerId));
            params.add(new BasicNameValuePair("projectIds", bindProjectId));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            if (Strings.isNullOrEmpty(bindProjectId) || Strings.isNullOrEmpty(customerId)) {
                logger.error("parameter is empty b[{}],c[{}]", bindProjectId, customerId);
                return result;
            }
            result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject bindResult = getData(result);
            if ("true".equalsIgnoreCase(String.valueOf(bindResult.get("entity")))) {
                result = "1";
            }
            logger.debug("===>>saveBindProject>>==result:{}====", result);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/usercenter")
    public String statusChangeLogs(HttpServletRequest request, Model model) {
        List<NameValuePair> params = getParams("getUserInfo", request);
        String result = null;
        try {
            result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.info(result);
            JSONObject obj = getData(result);
            model.addAttribute("user", obj.get("entity"));
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/personalCenter";
    }

    @RequestMapping(value = "/saveUserInfo", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String saveUserInfo(HttpServletRequest request, HttpServletResponse response) {
        String result = "0";
        try {
            List<NameValuePair> params = getParams("saveUserInfo", request);
            String id = request.getParameter("id") == null ? "" : request.getParameter("id");
            String prop = request.getParameter("prop") == null ? "" : request.getParameter("prop");
            String newValue = request.getParameter("newValue") == null ? "" : request.getParameter("newValue");
            params.add(new BasicNameValuePair("id", id));
            params.add(new BasicNameValuePair("prop", prop));
            params.add(new BasicNameValuePair("newValue", newValue));
            if (Strings.isNullOrEmpty(id) || Strings.isNullOrEmpty(prop) || Strings.isNullOrEmpty(newValue)) {
                logger.error("parameter is empty id[{}],prop[{}],value[{}]", id, prop, newValue);
                return result;
            }
            result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject bindResult = getData(result);

            if (bindResult != null && "0".equalsIgnoreCase(String.valueOf(bindResult.get("result")))) {
                result = String.valueOf(bindResult.get("msg"));
            } else {
                if ("true".equalsIgnoreCase(String.valueOf(bindResult.get("entity")))) {
                    result = "1";

                    if ("mobile".equalsIgnoreCase(prop)) {
                        Cookie phone = new Cookie("phone", newValue);
                        phone.setDomain("qyhgs.house365.com");
                        phone.setPath("/");
                        phone.setMaxAge(DateTimeUtils.ONE_YEAR_SECONDS);
                        response.addCookie(phone);
                    }
                }
            }
            logger.debug("===>>saveUserInfo>>==result:{}====", result);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/editUserInfo")
    public String editUserInfo(HttpServletRequest request, Model model) {
        try {
            String id = request.getParameter("id") == null ? "" : request.getParameter("id");
            String text = request.getParameter("text") == null ? "" : request.getParameter("text");
            String prop = request.getParameter("prop") == null ? "" : request.getParameter("prop");

            String propName = "";
            switch (prop) {
                case "realName":
                    propName = "姓名";
                    break;
                case "mobile":
                    propName = "手机号";
                    break;
                case "position":
                    propName = "职位";
                    break;
                default:
                    break;
            }

            model.addAttribute("id", id);
            model.addAttribute("text", text);
            model.addAttribute("prop", prop);
            model.addAttribute("propName", propName);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return "wechat/editUserInfo";
    }

    @RequestMapping("/customerdetail")
    public ModelAndView customerDetail(HttpServletRequest request, String customerId) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/client-details");
        view.addObject("customerId", customerId);
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getCustomerDetail"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("customerId", customerId));
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.info("=======getCustomerDetail======result:{}", result);
            JSONObject data = JSONObject.fromObject(result).getJSONObject("data");
            JSONObject jsonData = data.getJSONObject("entity");
            if (jsonData != null) {
                view.addObject("customer", jsonData.get("customer"));
                view.addObject("oldList", jsonData.get("oldList"));
                view.addObject("backLogList", jsonData.get("backLogList"));
                view.addObject("cpList", jsonData.get("cpList"));
                view.addObject("logShowList", jsonData.get("logShowList"));
                view.addObject("labs", jsonData.get("labs"));
            } else {
                view.setViewName("redirect:/error/405?title=customer");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return view;
    }

    @RequestMapping("/customerDetailOld")
    public ModelAndView customerDetailOld(HttpServletRequest request, String customerId) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/client-details-old");
        view.addObject("customerId", customerId);
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getCustomerDetail"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("customerId", customerId));
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.info("=======customerDetailOld======result:{}", result);
            JSONObject data = JSONObject.fromObject(result).getJSONObject("data");
            JSONObject jsonData = data.getJSONObject("entity");
            if (jsonData != null) {
                view.addObject("customer", jsonData.get("customer"));
                view.addObject("oldList", jsonData.get("oldList"));
                view.addObject("backLogList", jsonData.get("backLogList"));
                view.addObject("cpList", jsonData.get("cpList"));
                view.addObject("logShowList", jsonData.get("logShowList"));
            } else {
                view.setViewName("redirect:/error/405?title=customer");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return view;
    }

    @RequestMapping("/toUpdateCustomer")
    public ModelAndView toUpdateCustomer(HttpServletRequest request, String customerId) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/customerEdit");
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getCustomerDetail"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("customerId", customerId));
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.info("=======getCustomerDetail======result:{}", result);
            JSONObject data = JSONObject.fromObject(result).getJSONObject("data");
            JSONObject jsonData = data.getJSONObject("entity");
            if (jsonData != null) {
                view.addObject("customer", jsonData.get("customer"));
                JSONArray jsonArray = (JSONArray)jsonData.get("labs");
                if(jsonArray != null && !"".equals(jsonArray) && jsonArray.size()>0){
                    view.addObject("labs", jsonData.get("labs"));
                }else{
                    view.addObject("noLabel", "0");
                }
            } else {
                view.setViewName("redirect:/error/405?title=customer");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return view;
    }


    /**
     * 编辑客户标签页
     * @param request
     * @param customerId
     * @return
     */
    @RequestMapping("/editCustomerLabel")
    public ModelAndView editCustomerLabel(HttpServletRequest request,String customerId) {
        ModelAndView view = new ModelAndView();
        String noLabel = request.getParameter("noLabel");
//        String customerId = request.getParameter("customerId");
        view.setViewName("wechat/customerLabelEdit");
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getCustomerLabel"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("customerId", customerId));
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.info("=======getCustomerDetail======result:{}", result);
            JSONObject data = JSONObject.fromObject(result).getJSONObject("data");
            JSONObject jsonData = data.getJSONObject("entity");//标签管理
            if (jsonData != null) {
                view.addObject("customer", jsonData.get("customer"));
                JSONArray jsonArray = (JSONArray)jsonData.get("colorLabel");//标签管理
                if(jsonArray != null && !"".equals(jsonArray) && jsonArray.size()>0){//标签管理有标签noLabel:1
                    view.addObject("noLabel", "1");
                    view.addObject("labs", jsonData.get("colorLabel"));//标签管理已添加标签
                    view.addObject("customerLabelList", jsonData.get("labelColorList"));//客户已得标签
                }else{
                    view.addObject("noLabel", "0");//标签管理无标签:noLabel:0
                }
                view.addObject("customerId", customerId);//
            } else {
                view.setViewName("redirect:/error/405?title=customer");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return view;
    }

    /**
     * 编辑标签保存
     * @param request
     * @param customerId
     * @return
     */
    @RequestMapping("/saveCustomerLabel")
    @ResponseBody
    public String saveCustomerLabel(HttpServletRequest request,String customerId,String labelId) {
//        ModelAndView view = new ModelAndView();

//        view.setViewName("wechat/customerLabelEdit");
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "saveCustomerLabel"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("customerId", customerId));
            if(!StringUtils.isEmpty(labelId)){
                params.add(new BasicNameValuePair("labelId", labelId));
            }
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.info("=======getCustomerDetail======result:{}", result);
            JSONObject data = JSONObject.fromObject(result);;
            String resultdata = (String)data.get("result");
            return resultdata;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "";
    }

    @RequestMapping(value = "/modifytextarea", method = RequestMethod.GET)
    public ModelAndView modifyRemark(String customerId, String key, String value) {
        ModelAndView view = new ModelAndView();
        view.addObject("customerId", customerId);
        view.addObject("key", key);
        String keyName = "";
        switch (key) {
            case "customerName":
                keyName = "姓名";
                break;
            case "bakPhone1":
                keyName = "其他号码1";
                break;
            case "bakPhone2":
                keyName = "其他号码2";
                break;
            case "bakPhone3":
                keyName = "其他号码3";
                break;
        }
        view.addObject("keyName", keyName);
        view.addObject("value", value);
        view.setViewName("wechat/modify-textarea");
        return view;
    }

    @RequestMapping(value = "/modifytextarea", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String modifyRemarkPost(HttpServletRequest request, String customerId, String key, String value) {
        String result = "";
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "modifyCustomerInfo"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("customerId", customerId));
            params.add(new BasicNameValuePair("key", key));
            if (StringUtils.isBlank(value)) {
                value = "null";
            }
            params.add(new BasicNameValuePair("value", value));
            String uri = RestTemplateBean.getUri(hgsProp.getHgsUrl(), params);
            result = RestTemplateBean.doPost(uri, new JSONObject());
            logger.info("=======modifyCustomer======result:{}", result);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result;
    }


    //时间搜索页
    @RequestMapping(value = "/myCustomerSearch")
    public String myCustomerSearch(HttpServletRequest request, Model model) {
        //查询所创建标签
        List<NameValuePair> params = getParams("getLabelByPhone", request);
        params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
        try {
            String labelObj = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject listData = getData(labelObj);
            JSONArray labelList = new JSONArray();
            if (listData.containsKey("list") && listData.get("list") != null) {
                labelList = (JSONArray) listData.get("list");
                if (labelList.size() > 0) {
//                    for(int i=0;i<labelList.size();i++){
                        model.addAttribute("labelList", labelList);
//                    }
                }
            }
            logger.info("call --{}--", labelObj);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return "wechat/findMyCustomer";
    }

    //收藏
    @RequestMapping(value = "/starToggle", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String starToggle(HttpServletRequest request) {
        String result = "fail";
        String customerId = request.getParameter("customerId") == null ? "" : request.getParameter("customerId");
        String flag = request.getParameter("flag") == null ? "" : request.getParameter("flag");

        if (Strings.isNullOrEmpty(customerId) || Strings.isNullOrEmpty(flag)) {
            return result;
        }

        List<NameValuePair> params = getParams("starToggle", request);
        params.add(new BasicNameValuePair("flag", flag));
        params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
        params.add(new BasicNameValuePair("customerId", customerId));

        try {
            String obj = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.info("call --{}--", obj);
            result = "success";

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }

        return result;
    }

    //公告
    @RequestMapping(value = "/notice")
    public String notice(HttpServletRequest request, Model model) {
        List<NameValuePair> params = getParams("noticeList", request);
        try {

            String page = request.getParameter("page");
            params.add(new BasicNameValuePair("pageNo", Strings.isNullOrEmpty(page) ? "0" : page));
            params.add(new BasicNameValuePair("pageSize", "20"));

            //操作者手机号
            String notice = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);

            JSONObject listData = getData(notice);
            JSONArray customerList = new JSONArray();

            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
            }

            model.addAttribute("listData", customerList);
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/notice";
    }

    @RequestMapping(value = "/noticeCell")
    public String noticeCell(HttpServletRequest request, Model model) {
        List<NameValuePair> params = getParams("noticeList", request);
        try {

            String page = request.getParameter("page");
            params.add(new BasicNameValuePair("pageNo", Strings.isNullOrEmpty(page) ? "2" : page));
            params.add(new BasicNameValuePair("pageSize", "20"));

            //操作者手机号
            String notice = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);

            JSONObject listData = getData(notice);
            JSONArray customerList = new JSONArray();

            if (listData.containsKey("list") && listData.get("list") != null) {
                customerList = (JSONArray) listData.get("list");
            }

            model.addAttribute("listData", customerList);
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/noticeCell";
    }

    //公告
    @RequestMapping(value = "/noticeDetail/{id}")
    public String noticeDetail(
            HttpServletRequest request, Model model, @PathVariable(value = "id", required = false) String id
    ) {
        List<NameValuePair> params = getParams("noticeDetail", request);
        try {
            if (Strings.isNullOrEmpty(id)) {
                return "redirect:wechat/notice";
            }

            //操作者手机号
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            params.add(new BasicNameValuePair("id", id));
            String notice = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);

            JSONObject listData = getData(notice);

            JSONObject noticeObj = listData.containsKey("entity") ? (JSONObject) listData.get(
                    "entity") : new JSONObject();
            if (noticeObj.getInt("id") == 0) {
                return "redirect:/error/405?title=notice";
            }

            model.addAttribute("notice", noticeObj);
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        return "wechat/notice-details";
    }

    @RequestMapping(value = "/addcustomer", method = RequestMethod.GET)
    public ModelAndView addCustomer(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/add-client");
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getCustomerSources"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.debug("=======getCustomerSources:{}======", result);
            JSONObject data = JSONObject.fromObject(result).getJSONObject("data");
            view.addObject("resources", data.getJSONArray("list"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return view;
    }

    @RequestMapping(value = "/addSalesReport", method = RequestMethod.GET)
    public String addSalesReport(Model model, HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-8:00"));
        view.setViewName("wechat/salesReport");
        view.addObject("startTime", DateTimeUtils.parseDate(calendar.getTime(), "yyyy-MM-dd"));
        try {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("serviceCode", "Wechat"));
            params.add(new BasicNameValuePair("serviceName", "getSalesReport"));
            params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            String result = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            logger.debug("=======getCustomerSources:{}======", result);
            if (result != null) {
                JSONObject data = JSONObject.fromObject(result).getJSONObject("data");
                JSONArray customerList = new JSONArray();
                JSONObject jo = new JSONObject();
                if (data != null && data.getJSONArray("list") != null && data.getJSONArray("list").size() > 0) {
//                view.addObject("salesReport", data.getJSONArray("list"));
                    jo = data.getJSONArray("list").getJSONObject(0);
                    String callCount = jo.getString("callCount");
                    String connectCount = jo.getString("connectCount");
                    String intentBuyCount = jo.getString("intentBuyCount");
                    String visitedCount = jo.getString("visitedCount");
                    String twiceVisitedCount = jo.getString("twiceVisitedCount");
                    String identificationChipsCount = jo.getString("identificationChipsCount");
                    String subscriptionCount = jo.getString("subscriptionCount");
                    String source = jo.getString("source");

                    String visitedProject = jo.getString("visitedProject");
                    String twiceVisitedProject = jo.getString("twiceVisitedProject");
                    String identificationChipsProject = jo.getString("identificationChipsProject");
                    String subscriptionProject = jo.getString("subscriptionProject");
                    model.addAttribute("callCount", callCount);
                    model.addAttribute("connectCount", connectCount);
                    model.addAttribute("intentBuyCount", intentBuyCount);
                    model.addAttribute("visitedCount", visitedCount);
                    model.addAttribute("twiceVisitedCount", twiceVisitedCount);
                    model.addAttribute("identificationChipsCount", identificationChipsCount);
                    model.addAttribute("subscriptionCount", subscriptionCount);
                    model.addAttribute("source", source);

                    model.addAttribute("visitedProject", visitedProject);
                    model.addAttribute("twiceVisitedProject", twiceVisitedProject);
                    model.addAttribute("identificationChipsProject", identificationChipsProject);
                    model.addAttribute("subscriptionProject", subscriptionProject);
//                customerList =  data.getJSONArray("list");
                }
                model.addAttribute("salesReport", jo);
            }

            model.addAttribute("startTime", DateTimeUtils.parseDate(calendar.getTime(), "yyyy-MM-dd"));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "wechat/salesReport";
    }

    @RequestMapping(value = "/addSalesReport", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addSalesReport(
            HttpServletRequest request, String callCount, String connectCount, String intentBuyCount, String source,
            String visitedCount, String visitedProject, String twiceVisitedCount, String twiceVisitedProject, String identificationChipsCount, String identificationChipsProject,
            String subscriptionCount, String subscriptionProject
    ) {
        JSONObject result = new JSONObject();
        try {
            JSONObject salesreport = new JSONObject();
            salesreport.put("callCount", callCount);
            salesreport.put("connectCount", connectCount);
            salesreport.put("intentBuyCount", intentBuyCount);
            salesreport.put("source", source);
            salesreport.put("visitedCount", visitedCount);
            salesreport.put("visitedProject", visitedProject);
            salesreport.put("twiceVisitedCount", twiceVisitedCount);
            salesreport.put("twiceVisitedProject", twiceVisitedProject);
            salesreport.put("identificationChipsCount", identificationChipsCount);
            salesreport.put("identificationChipsProject", identificationChipsProject);
            salesreport.put("subscriptionCount", subscriptionCount);
            salesreport.put("subscriptionProject", subscriptionProject);
            List<NameValuePair> customerProjectParams = new ArrayList<>();
            customerProjectParams.add(new BasicNameValuePair("serviceCode", "Wechat"));
            customerProjectParams.add(new BasicNameValuePair("serviceName", "addSalesReport"));
            customerProjectParams.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));

            String customerProjectResult = RestTemplateBean.doPost(
                    RestTemplateBean.getUri(hgsProp.getHgsUrl(), customerProjectParams), salesreport);
            result = JSONObject.fromObject(customerProjectResult);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result.toString();
    }

    @RequestMapping(value = "/checkcustomer", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String checkCustomer(HttpServletRequest request, String customerPhone) {
        JSONObject checkResult = new JSONObject();
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("serviceCode", "Wechat"));
        params.add(new BasicNameValuePair("serviceName", "checkCustomerPhone"));
        params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
        params.add(new BasicNameValuePair("customerPhone", customerPhone));

        try {
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject result = getData(resultStr).getJSONObject("paraMap");
            if ("0".equals(result.getString("result"))) {
                return result.toString();
            }
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        checkResult.put("result", "1");
        checkResult.put("msg", "该手机号可以注册！");
        return checkResult.toString();
    }


    /**
     * 放弃400客户
     * @param request
     * @param customerId
     * @return
     */
    @RequestMapping(value = "/abandonHotlineCustomer", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String abandonHotlineCustomer(HttpServletRequest request, String customerId) {
        JSONObject checkResult = new JSONObject();
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("serviceCode", "Wechat"));
        params.add(new BasicNameValuePair("serviceName", "abandonHotlineCustomer"));
        params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
        params.add(new BasicNameValuePair("customerId", customerId));
        System.out.println("****************customerId*****************"+customerId);
        try {
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            System.out.println("****************处理结果*****************"+resultStr);
            JSONObject result = getData(resultStr).getJSONObject("paraMap");
            if ("0".equals(result.getString("result"))) {
                return result.toString();
            }
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        checkResult.put("result", "1");
        checkResult.put("msg", "已放弃!");
        return checkResult.toString();
    }

    @RequestMapping(value = "/checkCustomerPhoneByCustomerIds", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String checkCustomerPhoneByCustomerIds(HttpServletRequest request, String customerId, String userId) {
        JSONObject checkResult = new JSONObject();
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("serviceCode", "Wechat"));
        params.add(new BasicNameValuePair("serviceName", "checkCustomerPhoneByCustomerIds"));
        params.add(new BasicNameValuePair("userId", userId));
        params.add(new BasicNameValuePair("customerId", customerId));

        try {
            String resultStr = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
            JSONObject result = getData(resultStr).getJSONObject("paraMap");
            if ("0".equals(result.getString("result"))) {
                return result.toString();
            }
        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        }
        checkResult.put("result", "1");
        checkResult.put("msg", "该手机号可以注册！");
        return checkResult.toString();
    }

    @RequestMapping(value = "/addcustomer", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addCustomer(
            HttpServletRequest request, String customerName, String customerPhone, String customerSex, String actSource,
            String buyIntention, String traceStatus, String bakPhone1, String bakPhone2, String bakPhone3, String remark
    ) {
        JSONObject result = new JSONObject();
        try {
            JSONObject customerProject = new JSONObject();
            customerProject.put("name", StringUtils.isBlank(customerName) ? "" : customerName);
            customerProject.put("phone", StringUtils.isBlank(customerPhone) ? "" : customerPhone);
            //            customerProject.put("cityCode", StringUtils.isBlank(cityCode) ? "" : cityCode);
            //            customerProject.put("cityName", StringUtils.isBlank(cityName) ? "" : cityName);
            customerProject.put("sex", StringUtils.isBlank(customerSex) ? "" : customerSex);
            customerProject.put("buyIntention", StringUtils.isBlank(buyIntention) ? "" : buyIntention);
            customerProject.put("actSource", StringUtils.isBlank(actSource) ? "" : actSource);
            customerProject.put("traceStatus", StringUtils.isBlank(traceStatus) ? "1" : traceStatus);
            customerProject.put("bakPhone1", bakPhone1);
            customerProject.put("bakPhone2", bakPhone2);
            customerProject.put("bakPhone3", bakPhone3);
            customerProject.put("remark", remark);
            List<NameValuePair> customerProjectParams = new ArrayList<>();
            customerProjectParams.add(new BasicNameValuePair("serviceCode", "Wechat"));
            customerProjectParams.add(new BasicNameValuePair("serviceName", "addCustomer"));
            customerProjectParams.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));

            String customerProjectResult = RestTemplateBean.doPost(
                    RestTemplateBean.getUri(hgsProp.getHgsUrl(), customerProjectParams), customerProject);
            result = JSONObject.fromObject(customerProjectResult);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result.toString();
    }


    @RequestMapping(value = "/rushcustomerlist", method = RequestMethod.GET)
    public ModelAndView rushCustomer(
            HttpServletRequest request, String timeType
    ) {
        ModelAndView view = new ModelAndView();
        view.setViewName("wechat/client-rush");
        try {
            view.addObject("timeType", timeType);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return view;
    }

    @RequestMapping(value = "/getrushcustomerlist", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getRushCustomerList(
            HttpServletRequest request, String timeType, String currentPage
    ) {
        JSONObject result = new JSONObject();
        try {
            List<NameValuePair> rushCustomerParams = new ArrayList<>();
            rushCustomerParams.add(new BasicNameValuePair("serviceCode", "Wechat"));
            rushCustomerParams.add(new BasicNameValuePair("serviceName", "rushCustomerList"));
            rushCustomerParams.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            rushCustomerParams.add(new BasicNameValuePair("timeType", timeType));
            rushCustomerParams.add(new BasicNameValuePair("page", currentPage));
            rushCustomerParams.add(new BasicNameValuePair("pageSize", "" + pageSize));

            String rushCustomerResult = RestTemplateBean.doGet(
                    RestTemplateBean.getUri(hgsProp.getHgsUrl(), rushCustomerParams));
            result = JSONObject.fromObject(rushCustomerResult).getJSONObject("data");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result.toString();
    }

    @RequestMapping(value = "/rushcustomer", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String rushcustomer(
            HttpServletRequest request, String rushCustomerId) {
        JSONObject result = new JSONObject();
        try {
            List<NameValuePair> customerProjectParams = new ArrayList<>();
            customerProjectParams.add(new BasicNameValuePair("serviceCode", "Wechat"));
            customerProjectParams.add(new BasicNameValuePair("serviceName", "rushCustomer"));
            customerProjectParams.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
            customerProjectParams.add(new BasicNameValuePair("rushCustomerId", rushCustomerId));

            String customerProjectResult = RestTemplateBean.doGet(
                    RestTemplateBean.getUri(hgsProp.getHgsUrl(), customerProjectParams));
            result = JSONObject.fromObject(customerProjectResult).getJSONObject("data").getJSONObject("paraMap");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return result.toString();
    }

    /**
     * 从HGS获取公海客户信息
     *
     * @param day
     * @param params
     * @return
     * @throws URISyntaxException
     */
    private JSONArray getCustomerList(
            String day, List<NameValuePair> params
    ) throws URISyntaxException {

        String createTimeStart = "";
        String createTimeEnd = "";
        if ("1".equalsIgnoreCase(day)) {
            Date today = DateTimeUtils.getToday();
            createTimeStart = DateTimeUtils.parseDate(today, DateTimeUtils.DEFAULT_DATE_FORMAT_PATTERN_FULL);
        } else if ("7".equalsIgnoreCase(day)) {
            Date date = DateTimeUtils.getDaysAgo(new Date(), 7);
            createTimeStart = DateTimeUtils.parseDate(date, DateTimeUtils.DEFAULT_DATE_FORMAT_PATTERN_FULL);
        } else {
            //历史数据
            Date today = DateTimeUtils.getToday();
            createTimeEnd = DateTimeUtils.parseDate(today, DateTimeUtils.DEFAULT_DATE_FORMAT_PATTERN_FULL);
        }

        if (!Strings.isNullOrEmpty(createTimeStart)) {
            params.add(new BasicNameValuePair("createTimeStart", createTimeStart));
        }
        if (!Strings.isNullOrEmpty(createTimeEnd)) {
            params.add(new BasicNameValuePair("createTimeEnd", createTimeEnd));
        }

        String customers = RestTemplateBean.doGet(hgsProp.getHgsUrl(), params);
        JSONObject data = Strings.isNullOrEmpty(customers) ? null : getData(customers);

        JSONArray customerList = new JSONArray();

        if (data != null && data.containsKey("list") && data.get("list") != null) {
            customerList = (JSONArray) data.get("list");
        }

        return customerList;

    }


    /**
     * 获取置业顾问信息
     *
     * @return
     * @throws URISyntaxException
     */
    private JSONArray getUserData(HttpServletRequest request) throws URISyntaxException {
        String usersInfo = "";
        //        if (!redisTemplate.hasKey(CacheConst.USERSKEY)) {
        List<NameValuePair> userParams = getParams("getManagers", request);
        usersInfo = RestTemplateBean.doGet(hgsProp.getHgsUrl(), userParams);
        redisTemplate.opsForValue().set(CacheConst.USERSKEY, usersInfo, 30, TimeUnit.SECONDS);
        //        }

        JSONArray users = new JSONArray();
        JSONObject usersData = null;
        if (!Strings.isNullOrEmpty(usersInfo)) {
            usersData = getData(usersInfo);

            if (usersData.containsKey("list") && usersData.get("list") != null) {
                users = (JSONArray) usersData.get("list");
            }
        }

        return users;
    }

    private List<NameValuePair> getParams(String serviceName, HttpServletRequest request) {
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("serviceCode", "Wechat"));
        params.add(new BasicNameValuePair("serviceName", serviceName));
        params.add(new BasicNameValuePair("phone", getPhoneFromCookie(request)));
        return params;
    }

    private JSONObject getData(String json) {
        if (Strings.isNullOrEmpty(json)) {
            return new JSONObject();
        }

        JSONObject data = null;
        JSONObject jsonObject = JSONObject.fromObject(json);
        if ("1".equalsIgnoreCase(String.valueOf(jsonObject.get("result")))) {
            data = (JSONObject) jsonObject.get("data");
        } else {
            data = jsonObject;
        }
        return data;
    }

    /**
     * 从cookie获取用户phone
     *
     * @param request
     * @return
     */
    private String getPhoneFromCookie(HttpServletRequest request) {
        logger.debug("====getPhoneFromCookie:currentUrl:{}======", request.getServletPath());
        String phone = "";
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            if (cookie.getName().equals("phone")) {
                phone = cookie.getValue();
            }
        }
//        return "15195854561";
        return phone;
    }
}
