/**
   * 根据单元折扣id获取项目折扣的显示名称
   *
   * @param unitDiscountId
   * @return
   */
  public static String getProjectDiscountShowByUnitDiscountId(int unitDiscountId) {

    StringBuffer sb = new StringBuffer();

    UnitDiscount unitDiscount = unitDiscountServices.findUnitDiscountById(unitDiscountId);

    try {

      List<UnitDiscountDetail> detailList =
          unitDiscountDetailServices.findDetailByDiscountId(unitDiscountId);

      if (!CommonUtils.isCollectionEmpty(detailList)) {
        for (UnitDiscountDetail detail : detailList) {

          sb.append(detail.getDiscountPercent()).append("*");
        }
      }

      List<ProjectDiscount> proList =
          projectDiscountServices.findProjectDiscountByUnitDiscountId(unitDiscountId);

      if (!CommonUtils.isCollectionEmpty(proList)) {
        for (ProjectDiscount pro : proList) {

          sb.append(pro.getDiscountPercent()).append("*");
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
      sb.delete(0, sb.length());
      sb.append(unitDiscount.getDiscountName());
    }

    String ret = sb.toString();
    if (ret.endsWith("*")) {

      ret = ret.substring(0, ret.length() - 1);

      // 增加折扣对应的说明
      String computeWay = unitDiscount.getComputeWay();
      String wayValue = computeWayMap.get(computeWay);
      if (!CommonUtils.isStrEmpty(wayValue)) {

        ret += wayValue;
      }
    }

    if (CommonUtils.isStrEmpty(ret)) {
      ret = "查看折扣";
    }

    return ret;
  }
  /**
   * 获取project_discount(楼盘折扣)的table map String
   *
   * @param payWayDiscountList
   * @return
   */
  public static String projectDiscountListJson(List<ProjectDiscount> projectDiscountList) {

    if (CommonUtils.isCollectionEmpty(projectDiscountList)) {

      return "[]";
    }

    List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
    Map<String, String> map = null;

    for (ProjectDiscount detail : projectDiscountList) {

      map = new HashMap<String, String>();

      map.put("typeId", detail.getDiscountType());
      map.put("percent", detail.getDiscountPercent().toString());
      map.put("remark", detail.getRemark());

      listMap.add(map);
    }

    String out = CommonUtils.getListMapJsonAnd(listMap);

    return out;
  }
  /**
   * 获取单元折扣dialog关闭要设置的值
   *
   * @param request
   * @return
   */
  public static Map<String, String> getUnitDiscountManagerCloseShowAndMultiplyByDiscountId(
      HttpServletRequest request) {

    int discountId = Integer.parseInt(request.getParameter("unitDiscountId"));

    Map<String, String> retMap = new HashMap<String, String>();

    StringBuffer sb = new StringBuffer();
    BigDecimal multiply = new BigDecimal(1); // 具体的折扣

    UnitDiscount unitDiscount = unitDiscountServices.findUnitDiscountById(discountId);

    try {

      List<PayWayDiscount> discountList =
          payWayDiscountServices.findPayWayDiscountByUnitDiscountId(discountId);

      if (!CommonUtils.isCollectionEmpty(discountList)) {
        for (PayWayDiscount discount : discountList) {

          sb.append(discount.getDiscountPercent()).append("*");
          multiply = multiply.multiply(discount.getDiscountPercent().divide(new BigDecimal(100)));
        }
      } else {

        sb.append(unitDiscount.getDiscountName());
      }

      retMap = initSumMoneyAndContractMoney(request, discountId, multiply, retMap); // 设置其他的相关金额

    } catch (Exception e) {
      e.printStackTrace();
    }

    String detail = sb.toString();
    if (detail.endsWith("*")) {
      detail = detail.substring(0, detail.length() - 1);
    }

    // 增加折扣对应的说明
    String computeWay = unitDiscount.getComputeWay();
    String wayValue = computeWayMap.get(computeWay);
    if (!CommonUtils.isStrEmpty(wayValue)) {

      detail += wayValue;
    }

    retMap.put("detail", detail); // 折扣显示
    retMap.put("multiply", multiply.toString()); // 具体的折扣

    return retMap;
  }
  /**
   * 根据单元折扣id获取对应的项目折扣id
   *
   * @param unitDiscountId
   * @return
   */
  public static Integer[] getProjectDiscountIdsByUnitDiscountId(int unitDiscountId) {

    List<ProjectDiscount> changeList =
        projectDiscountServices.findProjectDiscountByUnitDiscountId(unitDiscountId);

    List<Integer> idList = new ArrayList<Integer>();

    if (!CommonUtils.isCollectionEmpty(changeList)) {

      for (ProjectDiscount change : changeList) {

        idList.add(change.getId());
      }
    }

    if (CommonUtils.isCollectionEmpty(idList)) return new Integer[0];

    Integer[] ret = new Integer[idList.size()];
    for (int i = 0; i < idList.size(); i++) {
      ret[i] = idList.get(i);
    }

    return ret;
  }
  /**
   * 根据payWayId获取具体的折扣(百分比)多选框
   *
   * @param payWayId
   * @return
   */
  public static Map<String, String> initSelProjectDiscountByPayWayId(int payWayId) {

    List<ProjectDiscount> list = projectDiscountServices.findProjectDiscountByPayWayId(payWayId);

    Map<String, String> map = new HashMap<String, String>();
    if (CommonUtils.isCollectionEmpty(list)) return map;

    for (ProjectDiscount discount : list) {

      String key = discount.getId() + "";
      String value = discount.getDescDiscountType() + "(" + discount.getDiscountPercent() + ")";

      map.put(key, value);
    }

    return map;
  }
  /**
   * 跳到批量修改项目的页面 dtc 2013.11.29
   *
   * @return
   * @throws Exception
   */
  public String toBatchModifyProject() throws Exception {

    Map<String, String> map = new LinkedHashMap<String, String>();

    List<Company> companyList = companyServices.findCompany(new CompanyCond());
    if (!CommonUtils.isCollectionEmpty(companyList)) {

      for (Company com : companyList) {

        map.put(com.getId() + "", com.getCompanyName());
      }
    }

    companyOption = CommonUtils.getSelectContent(map, SessionUser.getCompanyId() + "", true);

    return "toBatchModifyProject";
  }
  /**
   * 判断是否唯一包含公司项目,如果返回的list size不等于1就表示有问题
   *
   * @param proList
   * @param companyProjectName
   * @return
   */
  private List<CompanyProject> isIncludeOnlyCompanyProject(
      List<CompanyProject> proList, String companyProjectName) {

    List<CompanyProject> retList = new ArrayList<CompanyProject>();

    if (CommonUtils.isCollectionEmpty(proList)) {

      return retList;
    }

    for (CompanyProject pro : proList) {

      if (companyProjectName.equals(pro.getProjectName())) {

        retList.add(pro);
      }
    }

    return retList;
  }
  /**
   * 返回数据
   *
   * @return
   * @throws Exception
   */
  public String xsgjqkReportAjax() throws Exception {
    ActionTemplate.executeAjaxPage(
        this,
        cond,
        new ActionAjaxPageCallback() {

          @Override
          public int initTotal() throws Exception {
            return value;
          }

          @Override
          public List<Map<String, String>> initRows() throws Exception {
            List<Map<String, String>> retList = new ArrayList<Map<String, String>>();
            List<Map> list = null;
            if (!"".equals(cond.getFollowUserId()))
              list = customerFollowServices.findCustomerListByCustomerFollowUser(cond);
            else if (!"".equals(cond.getCompanyId()))
              list = customerFollowServices.findCustomerListByCustomer(cond);
            else if (!"".equals(cond.getProjectId()))
              list = customerFollowServices.findCustomerListByProject(cond);
            if (!CommonUtils.isCollectionEmpty(list)) {
              for (Map obj : list) {
                Map<String, String> map = new HashMap<String, String>();

                map.put("name", obj.get("customerName").toString());
                if (PermissionUtils.hasPermission(
                    EnumPrivCode.PRECUSTOMER_RETRIEVE_TEL, EnumDevFlag.GUANGZHOU)) {
                  map.put("phone1", obj.get("homePhone").toString());
                  map.put("phone2", obj.get("phone").toString());
                } else {
                  map.put("phone1", "********");
                  map.put("phone2", "********");
                }
                map.put("area", obj.get("areaNum").toString());
                map.put("price", obj.get("priceNum").toString());
                map.put("pro", obj.get("projectName").toString());
                map.put("followUserName", obj.get("userId").toString());
                map.put(
                    "createdTime",
                    obj.get("createdTime")
                        .toString()
                        .substring(0, obj.get("createdTime").toString().length() - 2));
                retList.add(map);
              }
            }
            return retList;
          }
        });
    cond.pageSize = 0;
    List<Map<String, String>> downList = new ArrayList<Map<String, String>>();
    List<Map> list = null;
    if (!"".equals(cond.getFollowUserId()))
      list = customerFollowServices.findCustomerListByCustomerFollowUser(cond);
    else if (!"".equals(cond.getCompanyId()))
      list = customerFollowServices.findCustomerListByCustomer(cond);
    else if (!"".equals(cond.getProjectId()))
      list = customerFollowServices.findCustomerListByProject(cond);
    if (!CommonUtils.isCollectionEmpty(list)) {
      for (Map obj : list) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name", obj.get("customerName").toString());
        map.put("phone1", obj.get("phone").toString());
        map.put("phone2", obj.get("homePhone").toString());
        map.put("area", obj.get("areaNum").toString());
        map.put("price", obj.get("priceNum").toString());
        map.put("pro", obj.get("projectName").toString());
        map.put("followUserName", obj.get("userId").toString());
        map.put(
            "createdTime",
            obj.get("createdTime")
                .toString()
                .substring(0, obj.get("createdTime").toString().length() - 2));
        downList.add(map);
      }
    }
    setDownloadData(downList);

    return null;
  }
  /**
   * 返回null表示修改成功
   *
   * @param cell
   * @param proList
   * @param companyId
   * @return
   */
  private BatchExceptionData modifyUserAccountProject(
      Cell[] cell, List<CompanyProject> proList, int companyId) {

    String message = "";

    try {

      String projectName = cell[0].getContents().trim();
      int projectId = 0;
      try {
        projectId = Integer.parseInt(cell[1].getContents().trim());
      } catch (Exception e) {
      }

      String jobNumber = cell[2].getContents().trim();

      String realName = cell[3].getContents().trim();

      if (projectId > 0) {
        // 表示填了项目id

        CompanyProject project = companyProjectServices.findCompanyProjectById(projectId);
        if (project == null || !projectName.equals(project.getProjectName())) {

          message = "项目名称与id不一致";
          throw new Exception();
        }
      } else {
        // 没有填项目id

        List<CompanyProject> inclueProList = isIncludeOnlyCompanyProject(proList, projectName);
        if (CommonUtils.isCollectionEmpty(inclueProList) || inclueProList.size() != 1) {

          message = "选择的公司该项目不合法";
          throw new Exception();
        }

        projectId = inclueProList.get(0).getId();
      }

      UserAccount userAccount = new UserAccount(); // 参数
      userAccount.setCompanyId(companyId);
      userAccount.setRealName(realName);
      if (!CommonUtils.isStrEmpty(jobNumber)) {
        userAccount.setJobNumber(jobNumber);
      }

      List<UserAccount> userList =
          userAccountServices.findUserAccountByCompanyIdAndRealNameOrJobNumberIncludeDelete(
              userAccount);
      if (CommonUtils.isCollectionEmpty(userList) || userList.size() != 1) {

        message = "用户姓名不能唯一确定一个用户";
        throw new Exception();
      }

      UserAccount user = userList.get(0);

      user.setProjectId(projectId);

      user.setModId(SessionUser.getUserId());
      user.setModTime(new Date());

      userAccountServices.updateUserAccount(user);

    } catch (Exception e) {

      if (CommonUtils.isStrEmpty(message)) {

        message = "数据格式不合法\n" + e.getMessage();
      }
    }

    if (CommonUtils.isStrEmpty(message)) {
      // 表示修改成功

      return null;
    }

    BatchExceptionData retPojo = new BatchExceptionData();

    retPojo.setProjectName(cell[0].getContents());
    retPojo.setProjectId(cell[1].getContents());
    retPojo.setJobNumber(cell[2].getContents());
    retPojo.setRealName(cell[3].getContents());
    retPojo.setExceptionData(message);

    return retPojo;
  }