/**
  * 根据开销户日期查询返回客户信息
  *
  * @param req
  * @param resp
  * @return
  * @throws Exception
  */
 public PaginationSupport q2(String qDate, String paramFromDate, String paramToDate, int offset) {
   PaginationSupport page = null;
   try {
     if ("openDate".equals(qDate)) {
       page = clientAdminService.findClientsByOpenDate(paramFromDate, paramToDate, offset);
     } else if ("closeDate".equals(qDate)) {
       page = clientAdminService.findClientsByCloseDate(paramFromDate, paramToDate, offset);
     } else {
       page = clientAdminService.findAllClients(offset);
     }
   } catch (Exception e) {
     page = clientAdminService.findAllClients(offset);
   }
   return page;
 }
  /**
   * 更新客户资料信息
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ModelAndView updateClient(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    System.out.println("更新客户资料");
    // 获取表单数据
    String clientId = request.getParameter("id");
    String name = request.getParameter("name");
    String sex = request.getParameter("sex");
    sex = "1".equals(sex) ? "女" : "男";
    String post = request.getParameter("post");
    String brithday = request.getParameter("brithday");
    String tel = request.getParameter("tel");
    String address = request.getParameter("address");

    Client client = clientAdminService.findClientById(Integer.parseInt(clientId));
    System.out.println("client : " + client);
    Map<String, Object> errors = new HashMap<String, Object>();
    Map<String, Object> models = new HashMap<String, Object>();
    // 修改资料
    try {
      // 校验表单数据
      if (isEmptyParam(name)) {
        System.out.println("请输入姓名??");
        errors.put("name", "请输入姓名");
      }
      if (errors.size() > 0) {
        models.put("client", client);
        models.put("errors", errors);
        return new ModelAndView(getClientInfoUpdateView(), models);
      }
      client.setName(name);
      client.setSex(sex);
      client.setPost(post);
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date bdDate = brithday == null || brithday.trim().isEmpty() ? null : sdf.parse(brithday);
      client.setBrithday(bdDate);
      client.setTel(tel);
      client.setAddress(address);
      clientAdminService.updateInfo(client);
      models.put("message", "修改成功");
    } catch (Exception e) {
      errors.put("message", "修改失败");
      e.printStackTrace();
    }
    models.put("errors", errors);
    models.put("client", client);
    System.out.println(models.size() + "," + models);
    return new ModelAndView(getClientInfoUpdateView(), models);
  }
 /**
  * 业务报表
  *
  * @param req
  * @param resp
  * @return
  * @throws Exception
  */
 public ModelAndView busniessReport(HttpServletRequest request, HttpServletResponse response)
     throws Exception {
   String action = request.getParameter("action");
   String paramOffset = request.getParameter("offset");
   int offset =
       isEmptyParam(paramOffset) || !paramOffset.matches("\\d+")
           ? 0
           : Integer.valueOf(paramOffset);
   String selected1 = null;
   String selected2 = null;
   PaginationSupport page = null;
   Map<String, Object> models = new HashMap<String, Object>();
   if ("q1".equals(action)) {
     String qType = request.getParameter("qType"); // 查找类型
     String qValue = request.getParameter("qValue"); // 查找值
     String query =
         "action=q1&qType="
             + request.getParameter("qType")
             + "&qValue="
             + request.getParameter("qValue");
     page = q1(qType, qValue, offset);
     selected1 =
         "".equals(qType)
             ? "noselected1"
             : ("name".equals(qType))
                 ? "name"
                 : ("certificateNum".equals(qType)) ? "certificateNum" : "bankAccount";
     models.put(selected1, "selected='selected'");
     models.put("qValue", qValue);
     models.put("query", query);
   } else if ("q2".equals(action)) {
     String qDate = request.getParameter("qDate");
     String paramFromDate = request.getParameter("fromDate");
     String paramToDate = request.getParameter("toDate");
     String query =
         "action=q2&qDate="
             + request.getParameter("qDate")
             + "&fromDate="
             + request.getParameter("fromDate")
             + "&toDate="
             + request.getParameter("toDate");
     page = q2(qDate, paramFromDate, paramToDate, offset);
     selected2 =
         "".equals(qDate) ? "noseleted2" : ("openDate".equals(qDate)) ? "openDate" : "closeDate";
     models.put(selected2, "selected='selected'");
     models.put("fromDate", paramFromDate);
     models.put("toDate", paramToDate);
     models.put("query", query);
   } else {
     page = clientAdminService.findAllClients(offset);
   }
   models.put("page", page);
   return new ModelAndView(getBusinessReportView(), models);
 }
  /**
   * 根据姓名,证件号码,银行账号查询
   *
   * @param request
   * @return
   */
  private PaginationSupport q1(String qType, String qValue, int offset) {
    // 获取查找参数
    qType = qType == null || qType.trim().isEmpty() ? null : qType.trim();
    qValue = qValue == null || qValue.trim().isEmpty() ? null : qValue.trim();

    PaginationSupport page = null;
    if ("name".equals(qType) && qValue != null) {
      // 按照名字查询
      page = clientAdminService.findClientsByName(qValue, offset);
    } else if ("certificateNum".equals(qType) && qValue != null) {
      // 按照证件号码查询
      page = clientAdminService.findClientsByCerficateNum(qValue, offset);
    } else if ("bankAccount".equals(qType) && qValue != null) {
      page = clientAdminService.findClientsByBankAccount(qValue, offset);
    } else {
      // 没有查询条件,显示全部
      page = clientAdminService.findAllClients(offset);
    }
    return page;
  }
 /** 销户 */
 public ModelAndView closeAccount(HttpServletRequest request, HttpServletResponse response)
     throws Exception {
   System.out.println("销户xxx");
   String clientId = request.getParameter("id");
   int cid =
       clientId == null || clientId.trim().isEmpty() || !clientId.matches("\\d+")
           ? -1
           : Integer.parseInt(clientId);
   if (cid != -1) {
     try {
       clientAdminService.closeAccount(cid);
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return clientManage(request, response);
 }
 /**
  * 客户资料详情
  *
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ModelAndView clientInfo(HttpServletRequest request, HttpServletResponse response)
     throws Exception {
   System.out.println("clientInfo");
   String clientId = request.getParameter("id");
   String action = request.getParameter("action");
   int cid =
       clientId == null || clientId.trim().isEmpty() || !clientId.matches("\\d+")
           ? -1
           : Integer.parseInt(clientId);
   // 查找客户资料
   Client client = clientAdminService.findClientById(cid);
   String view = null;
   if ("edit".equals(action)) {
     // 返回编辑页面
     view = getClientInfoUpdateView();
   } else {
     // 返回详情页面
     view = getClientInfoDetailView();
   }
   return new ModelAndView(view, "client", client);
 }
  /**
   * 修改密码
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ModelAndView changePassword(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    String oldPassword = request.getParameter("oldPassword");
    String newPassword = request.getParameter("newPassword");
    String confirmPassword = request.getParameter("confirmPassword");
    oldPassword = oldPassword == null || oldPassword.trim().isEmpty() ? null : oldPassword.trim();
    newPassword = newPassword == null || newPassword.trim().isEmpty() ? null : newPassword.trim();
    confirmPassword =
        confirmPassword == null || confirmPassword.trim().isEmpty() ? null : confirmPassword.trim();

    Map<String, Object> errors = new HashMap<String, Object>();
    if (oldPassword == null) {
      errors.put("oldPassword", "请输入旧密码");
    }
    if (newPassword == null) {
      errors.put("newPassword", "请输入新密码");
    } else if (confirmPassword == null) {
      errors.put("confirmPassword", "请输入确认密码");
    } else if (!newPassword.equals(confirmPassword)) {
      errors.put("confirmPassword", "两次密码输入不一致");
    }
    if (errors.size() > 0) {
      return new ModelAndView(getChangePasswordFormView(), "errors", errors);
    }
    Administrator admin = (Administrator) request.getSession().getAttribute("admin");
    try {
      if (admin.getPassword().equals(MD5Utils.md532(oldPassword))) {
        clientAdminService.changePassword(admin.getId(), newPassword);
        admin.setPassword(MD5Utils.md532(newPassword));
        return new ModelAndView(getChangePasswordFormView(), "message", "修改成功");
      } else {
        errors.put("message", "旧密码错误");
        return new ModelAndView(getChangePasswordFormView(), "errors", errors);
      }
    } catch (Exception e) {
      errors.put("message", "操作失败");
      return new ModelAndView(getChangePasswordFormView(), "errors", errors);
    }
  }
  /**
   * 查询所有客户资料
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ModelAndView clientManage(HttpServletRequest request, HttpServletResponse response)
      throws Exception {

    String qType = request.getParameter("qType"); // 查找类型
    String qValue = request.getParameter("qValue"); // 查找值
    String action = request.getParameter("action");
    String paramOffset = request.getParameter("offset");
    // 当前查询类型
    System.out.println("qType:" + qType);
    int offset =
        isEmptyParam(paramOffset) || !paramOffset.matches("\\d+")
            ? 0
            : Integer.valueOf(paramOffset);

    PaginationSupport page = null;
    Map<String, Object> models = new HashMap<String, Object>();
    if ("q1".equals(action)) {
      page = q1(qType, qValue, offset);
      String query =
          "action=q1&qType="
              + request.getParameter("qType")
              + "&qValue="
              + request.getParameter("qValue");
      models.put("query", query);
      String selected =
          (qType == null || "".equals(qType))
              ? "noselected"
              : ("name".equals(qType)) ? "name" : "certificateNum";
      models.put(selected, "selected='selected'");
      models.put("qValue", qValue);
    } else {
      page = clientAdminService.findAllClients(offset);
    }
    models.put("page", page);
    System.out.println(models);
    return new ModelAndView(getClientManageView(), models);
  }
 /**
  * 根据开销户日期返回客户信息
  *
  * @param req
  * @param resp
  * @return
  * @throws Exception
  */
 public ModelAndView bankAccountReport(HttpServletRequest request, HttpServletResponse resp)
     throws Exception {
   String action = request.getParameter("action");
   String qDate = request.getParameter("qDate");
   String paramFromDate = request.getParameter("fromDate");
   String paramToDate = request.getParameter("toDate");
   String paramOffset = request.getParameter("offset");
   int offset =
       isEmptyParam(paramOffset) || !paramOffset.matches("\\d+")
           ? 0
           : Integer.valueOf(paramOffset);
   Map<String, Object> models = new HashMap<String, Object>();
   PaginationSupport page = null;
   if ("q2".equals(action)) {
     page = q2(qDate, paramFromDate, paramToDate, offset);
     String query =
         "action=q2&qDate="
             + request.getParameter("qDate")
             + "&fromDate="
             + request.getParameter("fromDate")
             + "&toDate="
             + request.getParameter("toDate");
     models.put("query", query);
     String selected =
         (qDate == null || "".equals(qDate))
             ? "noselected"
             : ("openDate".equals(qDate)) ? "openDate" : "closeDate";
     models.put(selected, "selected='selected'");
     models.put("fromDate", paramFromDate);
     models.put("toDate", paramToDate);
   } else {
     page = clientAdminService.findAllClientsOrderByBankAccountSize(offset);
   }
   models.put("page", page);
   return new ModelAndView(getBankAccountView(), models);
 }
  /**
   * 添加客户资料
   *
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  public ModelAndView addClient(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    System.out.println("添加客户资料");
    // 获取表单数据
    String name = request.getParameter("name");
    String sex = request.getParameter("sex");
    sex = isEmptyParam(sex) || "0".equals(sex) ? "男" : "女";
    String certificateType = request.getParameter("certificateType");
    String certificateNum = request.getParameter("certificateNum");
    String post = request.getParameter("post");
    String brithday = request.getParameter("brithday");
    String tel = request.getParameter("tel");
    String address = request.getParameter("address");
    String[] bankAccounts = request.getParameterValues("bankAccounts");

    Map<String, Object> errors = new HashMap<String, Object>();
    // 校验表单数据
    if (isEmptyParam(name)) {
      System.out.println("addClient请输入姓名");
      errors.put("name", "请输入姓名");
    }
    if (isEmptyParam(certificateType)) {
      errors.put("certificateType", "请选择证件类型");
    }
    if (isEmptyParam(certificateNum)) {
      errors.put("certificateNum", "请输入证件号码");
    }
    if (bankAccounts == null || bankAccounts.length < 1) {
      errors.put("bankAccounts", "请输入银行账号");
    }

    if (errors.size() > 0) {
      return new ModelAndView(getAddClientFormView(), "errors", errors);
    }
    // 开户
    try {
      Client client = new Client();
      client.setName(name);
      client.setSex(sex);
      client.setCertificateType(certificateType);
      client.setCertificateNum(certificateNum);
      client.setPost(post);
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date bdDate = brithday == null || brithday.trim().isEmpty() ? null : sdf.parse(brithday);
      client.setBrithday(bdDate);
      client.setTel(tel);
      client.setAddress(address);
      client.setState(Client.OPEN); // 开户状态
      client.setOpenDate(new Date()); // 开户日期
      // 银行卡号
      for (String bankAccount : bankAccounts) {
        BankAccount account = new BankAccount(client, bankAccount);
        client.getBankAccounts().add(account);
      }
      System.out.println(client.getBankAccounts().size());
      clientAdminService.openingAccount(client);
      return new ModelAndView(getAddClientFormView(), "message", "添加成功");
    } catch (Exception e) {
      errors.put("message", "添加失败");
      return new ModelAndView(getAddClientFormView(), "errors", errors);
    }
  }