/** * 查询数据库操作记录 * * @param error * @return */ public static PageBean<v_db_operations> queryOperations( int currPage, int pageSize, ErrorInfo error) { error.clear(); if (currPage < 1) { currPage = 1; } if (pageSize < 1) { pageSize = 10; } StringBuffer sql = new StringBuffer(""); sql.append(SQLTempletes.PAGE_SELECT); sql.append(SQLTempletes.V_DB_OPERATIONS); List<v_db_operations> page = null; int count = 0; try { EntityManager em = JPA.em(); Query query = em.createNativeQuery(sql.toString(), v_db_operations.class); query.setFirstResult((currPage - 1) * pageSize); query.setMaxResults(pageSize); page = query.getResultList(); count = QueryUtil.getQueryCount(em); } catch (Exception e) { Logger.error(e.getMessage()); error.code = -1; error.msg = "数据库异常"; return null; } PageBean<v_db_operations> bean = new PageBean<v_db_operations>(); bean.pageSize = pageSize; bean.currPage = currPage; bean.page = page; bean.totalCount = count; error.code = 0; return bean; }
/** * 产品管理,产品列表 * * @param error 产品名称 * @param error 信息值 * @return List<Product> */ public static List<Product> queryProduct( PageBean<Product> pageBean, String name, ErrorInfo error) { error.clear(); int count = 0; String condition = "1=1"; if (StringUtils.isNotBlank(name)) { condition += name == null ? "" : " AND name LIKE '%" + name + "%'"; pageBean.conditions = new HashMap<String, Object>(); pageBean.conditions.put("name", name); } try { /* 得到总记录数 */ count = (int) t_products.count(condition); } catch (Exception e) { e.printStackTrace(); Logger.error("标->产品管理,产品列表,查询总记录数:" + e.getMessage()); error.msg = error.FRIEND_INFO + "产品列表加载失败!"; return null; } List<Product> products = new ArrayList<Product>(); if (count < 0) return products; pageBean.totalCount = count; List<t_products> tproducts = null; String hql = "select new t_products" + "(id, name, time, credit_id, min_interest_rate, max_interest_rate, min_amount, max_amount, is_agency, is_use, _order)" + " from t_products where " + condition + " order by _order, time"; try { tproducts = t_products.find(hql).fetch(pageBean.currPage, pageBean.pageSize); } catch (Exception e) { e.printStackTrace(); Logger.error("产品->管理,产品列表部分字段查询:" + e.getMessage()); error.msg = error.FRIEND_INFO + "产品列表加载失败!"; return null; } if (null == tproducts) return products; Product product = null; String _creditImageFilename = null; long _creditId = -1; long _pid = -1; for (t_products pro : tproducts) { product = new Product(); _creditId = pro.credit_id; /* 最低信用积分图标 */ _creditImageFilename = CreditLevel.queryImageFilename(_creditId, error); _pid = pro.id; // 审核资料数量 count = ProductAuditItem.queryAuditCount(_pid, error); product.name = pro.name; product.time = pro.time; product.minInterestRate = pro.min_interest_rate; product.maxInterestRate = pro.max_interest_rate; product.minAmount = pro.min_amount; product.maxAmount = pro.max_amount; product.isAgency = pro.is_agency; product.isUse = pro.is_use; product.order = pro._order; product._id = _pid; product.creditId = _creditId; product.auditCount = count; product.creditImageFilename = _creditImageFilename; products.add(product); } error.code = 0; return products; }
/** * 合作结构列表 * * @param pageBean 分页对象 * @param error 信息值 * @return List<v_agencies> */ public static List<v_agencies> queryAgencies( PageBean<v_agencies> pageBean, ErrorInfo error, String condition, String keyword) { error.clear(); int count = -1; StringBuffer conditions = new StringBuffer(" where 1 = 1"); List<Object> values = new ArrayList<Object>(); Map<String, Object> conditionmap = new HashMap<String, Object>(); /* 组装条件 */ if (NumberUtil.isNumericInt(condition)) { switch (Integer.parseInt(condition)) { /* 编号搜索 */ case Constants.AGENCY_SEARCH_ID: conditions.append(" AND no LIKE ?"); values.add("%" + keyword + "%"); break; /* 名称搜索 */ case Constants.AGENCY_SEARCH_NAME: conditions.append(" AND name LIKE ?"); values.add("%" + keyword + "%"); break; /* 全部搜索 */ case Constants.SEARCH_ALL: if (StringUtils.isBlank(keyword)) break; conditions.append(" AND (no LIKE ? OR name LIKE ?)"); values.add("%" + keyword + "%"); values.add("%" + keyword + "%"); break; } conditionmap.put("condition", condition); conditionmap.put("keyword", keyword); } pageBean.conditions = conditionmap; StringBuffer sql = new StringBuffer(); sql.append("select count(t.id) from ("); sql.append(SQLTempletes.V_AGENCIES); sql.append(")"); sql.append(SQLTempletes.TABLE_NAME); sql.append(conditions); EntityManager em = JPA.em(); Query query = em.createNativeQuery(sql.toString()); for (int n = 1; n <= values.size(); n++) { query.setParameter(n, values.get(n - 1)); } List<?> list = null; try { list = query.getResultList(); } catch (Exception e) { Logger.error("合作机构->合作结构列表,查询总记录数:" + e.getMessage()); error.msg = error.FRIEND_INFO + "加载合作结构列表失败!"; } count = list == null ? 0 : Integer.parseInt(list.get(0).toString()); if (count < 1) return new ArrayList<v_agencies>(); pageBean.totalCount = count; sql = new StringBuffer(); sql.append("select t.* from ("); sql.append(SQLTempletes.V_AGENCIES); sql.append(")"); sql.append(SQLTempletes.TABLE_NAME); sql.append(conditions); sql.append(" order by t.id desc"); query = em.createNativeQuery(sql.toString(), v_agencies.class); for (int n = 1; n <= values.size(); n++) { query.setParameter(n, values.get(n - 1)); } query.setFirstResult((pageBean.currPage - 1) * pageBean.pageSize); query.setMaxResults(pageBean.pageSize); try { return query.getResultList(); } catch (Exception e) { e.printStackTrace(); Logger.error("标->合作结构列表:" + e.getMessage()); error.msg = error.FRIEND_INFO + "加载合作结构列表失败!"; return null; } }