/** * 重置(还原出厂设置) * * @param error * @return */ public static int reset(ErrorInfo error) { error.clear(); String backupFileName = backup(false, error); if (null == backupFileName) { return error.code; } if (0 != createOperation(DBOperationType.RESET, backupFileName, error)) { return error.code; } DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.DB_RESET, "还原出厂初始数据", error); if (error.code < 0) { JPA.setRollbackOnly(); return error.code; } if (0 != MySQLTool.executeSqlFile( username, password, host, port, database, resetFileName, error)) { JPA.setRollbackOnly(); return error.code; } error.code = 0; error.msg = "还原出厂设置成功"; return error.code; }
/** * 添加操作记录 * * @param type * @param fileName * @param error * @return */ private static int createOperation(int type, String fileName, ErrorInfo error) { error.clear(); t_db_operations op = new t_db_operations(); op.supervisor_id = Supervisor.currSupervisor().id; op.time = new Date(); op.ip = DataUtil.getIp(); op.type = type; op.filename = fileName; try { op.save(); } catch (Exception e) { e.printStackTrace(); Logger.info(e.getMessage()); error.code = -1; error.msg = "数据库异常"; JPA.setRollbackOnly(); return error.code; } error.code = 0; return error.code; }
/** * 还原数据库 * * @param fileName * @param error * @return */ public static int recover(String fileName, ErrorInfo error) { error.clear(); if (StringUtils.isBlank(fileName)) { error.code = -1; error.msg = "恢复文件不能为空"; return error.code; } String backupFileName = backup(false, error); if (null == backupFileName) { return error.code; } if (0 != createOperation(DBOperationType.RECOVER, backupFileName, error)) { return error.code; } DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.DB_RECOVER, "还原运营数据", error); if (error.code < 0) { JPA.setRollbackOnly(); return error.code; } String decryptFileName = Constants.SQL_PATH + UUID.randomUUID().toString() + ".sql"; if (!FileEncrypt.decrypt(fileName, decryptFileName, Constants.ENCRYPTION_KEY)) { error.code = -1; error.msg = "还原数据库失败"; return error.code; } if (0 != MySQLTool.executeSqlFile( username, password, host, port, database, decryptFileName, error)) { JPA.setRollbackOnly(); return error.code; } if (!new File(decryptFileName).delete()) { error.code = -1; error.msg = "还原数据库失败"; return error.code; } error.code = 0; error.msg = "还原数据库成功"; return error.code; }
/** * 上架/下架 * * @param pid 产品ID * @param isUse 状态值 * @param error 信息值 * @return ? > 0 : success; ? < 0 : fail */ public static void editStatus(long pid, boolean isUse, ErrorInfo error) { error.clear(); String hql = "update t_products set is_use=? where id=?"; Query query = JPA.em().createQuery(hql); query.setParameter(1, isUse); query.setParameter(2, pid); int rows = 0; try { rows = query.executeUpdate(); } catch (Exception e) { e.printStackTrace(); Logger.error("产品->上架/下架:" + e.getMessage()); error.msg = error.FRIEND_INFO + "上架/下架失败!"; return; } if (rows == 0) { JPA.setRollbackOnly(); error.code = -1; error.msg = "设置失败!"; return; } /* 添加事件 */ if (isUse) { DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.ENABLE_PRODUCT, "启用借款标产品", error); } else { DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.ENABLE_PRODUCT, "暂停借款标产品", error); } if (error.code < 0) { JPA.setRollbackOnly(); error.msg = "设置失败!"; return; } }
/** * 改变合作机构状态(正常/暂停) * * @param aid 机构ID * @param isUse 正常/暂停 * @param error 信息值 */ public static void editStatus(long aid, boolean isUse, ErrorInfo error) { error.clear(); String hql = "update t_agencies set is_use=? where id=?"; Query query = JPA.em().createQuery(hql); query.setParameter(1, isUse); query.setParameter(2, aid); int rows = 0; try { rows = query.executeUpdate(); } catch (Exception e) { e.printStackTrace(); Logger.error("合作机构->正常/暂停:" + e.getMessage()); error.msg = error.FRIEND_INFO + "设置失败!"; return; } if (rows == 0) { JPA.setRollbackOnly(); error.code = -1; error.msg = "设置失败!"; return; } /* 添加事件 */ if (isUse) DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.ENABLE_AGENCY, "启用合作机构", error); else DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.NOT_ENABLE_AGENCY, "暂停合作机构", error); if (error.code < 0) { JPA.setRollbackOnly(); error.msg = "设置失败!"; return; } error.code = 0; }
/** * 添加 * * @param info 信息值 * @return ? > 0 : success; ? < 0 : fail */ public void create(ErrorInfo error) { error.clear(); t_products product = new t_products(); product.is_use = Constants.ENABLE; product.is_agency = Constants.NOT_IS_AGENCY; // product.fee_description = this.feeDescription; // 手续费描述(常量值读取拼接,无需编辑) /* 添加基本信息 */ error.code = this.addOrEdit(product); if (error.code < 0) { error.code = -1; error.msg = error.FRIEND_INFO + "添加基本信息失败!"; return; } /* 添加对应的审核资料 */ error.code = this.addProductAudit(product.id, product.mark); if (error.code < 0) { error.code = -2; error.msg = error.FRIEND_INFO + "添加审核资料失败!"; return; } /* 添加标签和字段 */ error.code = this.addProductLableAndFiled(product.id, error); if (error.code < 0) { error.code = -3; error.msg = error.FRIEND_INFO + "添加产品标签/字段失败!"; return; } /* 添加事件 */ DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.CREATE_PRODUCT, "添加产品", error); if (error.code < 0) { JPA.setRollbackOnly(); error.msg = "保存失败!"; return; } }
/** 添加合作机构 */ public void createAgency(ErrorInfo error) { t_agencies agency = new t_agencies(); agency.time = new Date(); // 当前时间 agency.name = this.name; agency.credit_level = this.creditLevel; agency.introduction = this.introduction; agency.id_number = this.id_number; agency.imageFilenames = this.imageFilenames; // 借款图片 agency.is_use = Constants.ENABLE; // 默认为启动 try { agency.save(); } catch (Exception e) { e.printStackTrace(); Logger.error("合作机构->添加合作机构:" + e.getMessage()); error.msg = "添加失败!"; this._id = -1; return; } if (agency.id < 0) { error.msg = "添加失败!"; return; } /* 添加事件 */ DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.CREATE_AGENCY, "添加合作机构", error); if (error.code < 0) { JPA.setRollbackOnly(); error.msg = "添加失败!"; return; } error.code = 0; }
/** * 备份数据库 * * @param isVisual 是否在数据库操作记录表中可见 * @param error * @return */ public static String backup(boolean isVisual, ErrorInfo error) { error.clear(); String fileName = Constants.SQL_PATH + UUID.randomUUID().toString(); FileUtil.getStore(Constants.SQL_PATH); if (0 != MySQLTool.dumpSqlFile(username, password, host, port, database, fileName, error)) { return null; } if (!FileEncrypt.encrypt(fileName, Constants.ENCRYPTION_KEY)) { error.code = -1; error.msg = "备份数据库失败"; return null; } if (isVisual) { if (0 != createOperation(DBOperationType.BACKUP, fileName, error)) { return null; } DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.DB_BACKUP, "备份数据", error); if (error.code < 0) { JPA.setRollbackOnly(); return null; } } error.code = 0; error.msg = "备份数据库成功"; return fileName; }
/** * 编辑 * * @param id 产品ID * @param error 信息值 * @return ? > 0 : success; ? < 0 : fail */ public void edit(long id, ErrorInfo error) { error.clear(); t_products product = null; try { product = t_products.findById(id); } catch (Exception e) { e.printStackTrace(); Logger.error("产品->编辑:" + e.getMessage()); error.code = -1; error.msg = error.FRIEND_INFO + "编辑失败!"; return; } if (null == product) { error.code = -2; error.msg = error.FRIEND_INFO + "编辑失败!"; return; } /* 添加基本信息 */ error.code = this.addOrEdit(product); if (error.code < 0) { error.code = -3; error.msg = error.FRIEND_INFO + "编辑失败!"; return; } /* 删除作废的审核资料 */ error.code = ProductAuditItem.deleteProductAudit(id); if (error.code < 0) { error.code = -4; error.msg = error.FRIEND_INFO + "编辑失败!"; return; } /* 添加对应的审核资料 */ error.code = this.addProductAudit(id, product.mark); if (error.code < 0) { error.code = -5; error.msg = error.FRIEND_INFO + "编辑失败!"; return; } /* 编辑标签和字段 */ error.code = this.editFiledConten(); if (error.code < 0) { error.code = -6; error.msg = error.FRIEND_INFO + "编辑失败!"; return; } /* 添加事件 */ DealDetail.supervisorEvent( Supervisor.currSupervisor().id, SupervisorEvent.EDIT_PRODUCT, "编辑产品", error); if (error.code < 0) { JPA.setRollbackOnly(); error.msg = "保存失败!"; return; } }