Example #1
0
  public static void imp(File file) {
    String result = "";
    String profiles = "";
    int imported = 0;
    int missed = 0;

    if (file != null) {

      Config config = Config.find("1=1").first();

      FileInputStream fileInputStream = null;
      HSSFWorkbook workbook = null;
      try {
        fileInputStream = new FileInputStream(file);
        workbook = new HSSFWorkbook(fileInputStream);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }

      Profile profile = null;
      User user = null;
      String value = "";
      Material material = null;

      SendMessage m = new SendMessage();
      for (int x = 0; x < workbook.getNumberOfSheets(); x++) {

        HSSFSheet worksheet = workbook.getSheetAt(x);
        HSSFRow row = null;
        HSSFCell cell = null;
        DecimalFormat df = new DecimalFormat("#");
        df.setMaximumFractionDigits(0);

        for (int i = 1; i < worksheet.getLastRowNum(); i++) {
          String unit = "万元";
          String business = "0";
          try {
            row = worksheet.getRow(i);
            if (row.getLastCellNum() < 22) {
              continue;
            }
            cell = row.getCell(0);
            value = cell.getStringCellValue();
            user = User.find("username=?", value).first();
            if (user == null) {
              user = new User(value, getRandomPwd(), ApplicationRole.getByName("user"));
              user.save();
            }

            profile = Profile.find("user.id=?", user.id).first();
            if (profile == null) {
              profile = new Profile();
              profile.user = user;
            }

            profile.is_audit = 1;
            cell = row.getCell(1);
            value = cell.getStringCellValue();
            profile.name = value;

            cell = row.getCell(2);
            value = cell.getStringCellValue();
            material = Material.find("name=?", value).first();
            if (material == null) {
              material = new Material();
              material.name = value;
              material.save();
            }
            if (!profile.materials.contains(material)) profile.materials.add(material);

            cell = row.getCell(3);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }

            cell = row.getCell(4);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }

            cell = row.getCell(5);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.registration_number = value;

            cell = row.getCell(6);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              df.setMaximumFractionDigits(2);
              value = String.valueOf(df.format(cell.getNumericCellValue()));
            } else {
              value = cell.getStringCellValue();
            }
            if (value.contains("美元")) {
              value = value.replace("(美元)", "").trim();
              unit = "万美元";
            }
            profile.registration_assets = value;
            profile.registration_assets_unit = unit;

            df.setMaximumFractionDigits(0);
            cell = row.getCell(7);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.registration_address = value;

            cell = row.getCell(8);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.bank_name = value;

            cell = row.getCell(9);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.account_name = value;

            cell = row.getCell(10);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.tfn = value;

            cell = row.getCell(11);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.factory_name = value;

            cell = row.getCell(12);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.factory_address = value;

            cell = row.getCell(13);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(df.format(cell.getNumericCellValue()));
            } else {
              value = cell.getStringCellValue();
            }
            profile.first_supply = value.replaceAll("年", "");

            cell = row.getCell(14);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.legal_person = value;

            cell = row.getCell(15);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.contact_name = value;

            cell = row.getCell(16);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.contact_job = value;

            cell = row.getCell(17);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(df.format(cell.getNumericCellValue()));
            } else {
              value = cell.getStringCellValue();
            }
            profile.contact_phone = value;

            cell = row.getCell(18);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.sales_name = value;

            cell = row.getCell(19);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            profile.sales_job = value;

            cell = row.getCell(20);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(df.format(cell.getNumericCellValue()));
            } else {
              value = cell.getStringCellValue();
            }
            profile.sales_phone = value;

            cell = row.getCell(21);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
              value = String.valueOf(cell.getNumericCellValue());
            } else {
              value = cell.getStringCellValue();
            }
            if (value.equals("自营")) {
              business = "1";
            } else if (value.equals("经销")) {
              business = "2";
            } else if (value.equals("挂靠")) {
              business = "3";
            } else {
              business = "0";
            }
            profile.business_model = business;
            profile.save();
            imported += 1;

            if (profile.contact_phone != null) {
              String message =
                  "您的信息已导入,用户名:" + user.username + ",密码:" + user.password + ",请登录比价平台上传资质文件";
              if (config.msg_import != null && !"".equals(config.msg_import)) {
                message =
                    config
                        .msg_import
                        .replace("{username}", user.username)
                        .replace("{passowrd}", user.password);
              }
              m.sendSms(profile.contact_phone, message, "0000001");
              if (profile.contact_email != null && !"".equals(profile.contact_phone)) {
                m.sendMail(
                    profile.contact_email,
                    "[" + Messages.get("application.name") + "]信息导入",
                    message);
              }
            }
          } catch (Exception e) {
            missed += 1;
            if (!"".equals(profiles)) {
              profiles += "," + profile.name;
            } else {
              profiles += profile.name;
            }
            e.printStackTrace();
          }
        }
      }

      result = "成功导入" + imported + "条记录,丢失" + missed + "条记录。";
      if (!"".equals(profiles)) {
        result += "丢失导入的供应商为:" + profiles;
      }
    }
    redirect("/admin/profiles?result=" + URLEncoder.encode(result));
  }
Example #2
0
  public static void save() throws Throwable {
    String username = params.get("username");
    String name = params.get("name");
    String password = params.get("password");
    String[] materials = params.getAll("material");
    String registration_number = params.get("registration_number");
    String registration_assets = params.get("registration_assets");
    String registration_assets_unit = params.get("registration_assets_unit");
    String registration_address = params.get("registration_address");
    String bank_name = params.get("bank_name");
    String account_name = params.get("account_name");
    String tfn = params.get("tfn");
    String legal_person = params.get("legal_person");
    String factory_name = params.get("factory_name");
    String factory_address = params.get("factory_address");
    String first_supply = params.get("first_supply");
    String business_model = params.get("business_model");
    String contact_name = params.get("contact_name");
    String contact_job = params.get("contact_job");
    String contact_phone = params.get("contact_phone");
    String contact_email = params.get("contact_email");
    String sales_name = params.get("sales_name");
    String sales_job = params.get("sales_job");
    String sales_phone = params.get("sales_phone");
    String[] files = params.getAll("files");

    User user = null;
    Material m = null;
    Files file = null;
    if (username != null && password != null && !"".equals(username) && !"".equals(password)) {
      user = User.find("username=?", username).first();
      if (user == null) {
        user = new User(username, password, ApplicationRole.getByName("user"));
        user.save();
      }

      Profile profile = Profile.find("user.id=?", user.id).first();
      if (profile == null) {
        profile = new Profile();
        profile.user = user;
      }

      if (materials != null) {
        for (String material_id : materials) {
          m = Material.find("id=?", Long.valueOf(material_id.trim())).first();
          if (m != null) profile.materials.add(m);
        }
      }
      profile.name = name;
      profile.registration_number = registration_number;
      profile.registration_assets = registration_assets;
      profile.registration_address = registration_address;
      profile.registration_assets_unit = registration_assets_unit;
      profile.bank_name = bank_name;
      profile.account_name = account_name;
      profile.tfn = tfn;
      profile.legal_person = legal_person;
      profile.factory_name = factory_name;
      profile.factory_address = factory_address;
      profile.first_supply = first_supply;
      profile.business_model = business_model;
      profile.contact_name = contact_name;
      profile.contact_job = contact_job;
      profile.contact_phone = contact_phone;
      profile.contact_email = contact_email;
      profile.sales_name = sales_name;
      profile.sales_job = sales_job;
      profile.sales_phone = sales_phone;

      if (files != null) {
        for (String f : files) {
          file = Files.find("id=?", Long.valueOf(f)).first();
          if (file != null) {
            profile.files.add(file);
          }
        }
      }

      profile.save();
    }

    session.put("username", username);

    Secure.redirectToOriginalURL();
  }