小蜜锋 - 云代码空间
—— 技术宅拯救世界!
package com.gym.user.service; import java.util.List; import com.gym.model.UserModel; public interface UserService { /** * 根据用户id查询用户信息 * * @return */ public UserModel queryUserInfoById(String uId); /** * 根据用户名查询用户信息 * * @return */ public UserModel queryUserInfoByName(String uName); /** * 修改用户信息 * * @param userModel * @return */ public int alterUserInfo(UserModel userModel); /** * 修改用户密码 * * @param userModel * @return */ public int alterUserPwd(UserModel userModel, String oldPwd, String newPwd1, String newPwd2); /** * 查询我的预约 * * @return */ public List queryMyBook(UserModel userModel); /** * 查询我的租借 * * @param userModel * @return */ public List queryMyRent(UserModel userModel); }上面是UserService的接口,该接口具有根据用户id查询用户信息,根据用户名查询用户信息,修改用户信息,修改用户密码,查询我的预约,查询我的器材租借 6个功能。下面是这个接口的实现类:
package com.gym.user.service.impl; import java.util.List; import com.gym.dao.impl.UserDaoImpl; import com.gym.model.UserModel; import com.gym.user.service.UserService; import com.gym.utils.Constant; import com.gym.utils.Md5; public class UserServiceImpl implements UserService { public UserModel queryUserInfoById(String uId) { // TODO Auto-generated method stub UserDaoImpl userDaoImpl = new UserDaoImpl(); return userDaoImpl.queryUserById(uId); } public UserModel queryUserInfoByName(String uName) { // TODO Auto-generated method stub UserDaoImpl userDaoImpl = new UserDaoImpl(); return userDaoImpl.queryUserByName(uName); } public int alterUserInfo(UserModel userModel) { // TODO Auto-generated method stub UserDaoImpl userDaoImpl = new UserDaoImpl(); if (userDaoImpl.alterUser(userModel) == 1) { return Constant.SUCCESS; } else { return Constant.ERROR; } } /** * 修改用户密码 * * @param userModel * @param oldPwd * 旧密码 * @param newPwd1 * 新密码1 * @param newPwd2 * 新密码2 * @return */ public int alterUserPwd(UserModel userModel, String oldPwd, String newPwd1, String newPwd2) { // TODO Auto-generated method stub UserDaoImpl userDaoImpl = new UserDaoImpl(); // 验证旧密码 if (!Md5.encryption(oldPwd).equals( userDaoImpl.queryUserById(userModel.getuId()).getuPassword())) { return Constant.USERPWDERROR; } if (!newPwd1.equals(newPwd2)) { return Constant.PASSWORDDIFFER; } userModel.setuPassword(Md5.encryption(newPwd1)); if (userDaoImpl.alterUserPwd(userModel) == 1) { return Constant.SUCCESS; } else { return Constant.ERROR; } } public List queryMyBook(UserModel userModel) { // TODO Auto-generated method stub UserDaoImpl userDaoImpl = new UserDaoImpl(); return (List) userDaoImpl.queryMyBook(userModel); } public List queryMyRent(UserModel userModel) { // TODO Auto-generated method stub UserDaoImpl userDaoImpl = new UserDaoImpl(); return (List) userDaoImpl.queryMyRent(userModel); } }这里的增、删、改操作也用到了Constant常量工具类,能直观地传递操作状态;而查询操作一般是返回List对象,里面封装了查询数据集合,前端模版可以通过<c:foreach>标签遍历。
public int bookGround(GroundBookModel groundBookModel, GroundModel groundModel) { // TODO Auto-generated method stub CompareUtil compareUtil = new CompareUtil(); // 验证起始时间是否大于结束时间 if (compareUtil.compareDateTime(groundBookModel.getbStartTime(), groundBookModel.getbEndTime(), "yyyy-MM-dd HH:mm") >= 0) { return Constant.TIMEERROR; } // ------------------待开发(惩罚规则)-------------------- // 判断用户是否被冻结 // ------------------待开发(惩罚规则)-------------------- // 检测场地是否存在并有效 if (groundModel.getgId().equals("") || groundModel.getgStatus() == "0") { return Constant.GROUNDINVALID; } // 检测场地此时间段是否空闲 GroundbookDaoImpl groundbookDaoImpl = new GroundbookDaoImpl(); List groundBookList = groundbookDaoImpl.queryByTime( groundBookModel.getbStartTime(), groundBookModel.getbEndTime()); if (groundBookList.size() != 0) { return Constant.TIMECLASH; } // 计算费用 groundBookModel.setbFee(new CalculationUitl().calculaBookMoney( groundBookModel, groundModel) + ""); if (groundbookDaoImpl.addGroundbook(groundBookModel) == 1) { return Constant.SUCCESS; } else { return Constant.ERROR; } }时间比较器封装在CompareUtil工具类中,用于验证起始时间是否大于结束时间,防止用户输入错误。实现代码如下:
/** * 比较时间日期先后 格式例子:2012-12-30 11:30 * * @param begin * @param end * @return begin在end前返回负数;begin在end后返回正数;格式错误或时间相等返回0 */ public long compareDateTime(String begin, String end, String format) { DateFormat df = new SimpleDateFormat(format); try { Date d1 = df.parse(begin); Date d2 = df.parse(end); long diff = d1.getTime() - d2.getTime(); return diff; } catch (Exception e) { } return 0; }计算总金额方法封装在CalculationUitl工具类中,用于根据预定小时数计算金额,不足一小时按一小时计。实现代码如下:
/** * 按预定小时数计算金额,不足一小时按一小时计 * * @param groundBookModel * @param groundModel * @return */ public float calculaBookMoney(GroundBookModel groundBookModel, GroundModel groundModel) { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date begin = null; Date end = null; try { begin = dateFormat.parse(groundBookModel.getbStartTime()); end = dateFormat.parse(groundBookModel.getbEndTime()); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } long l = end.getTime() - begin.getTime(); long day = l / (24 * 60 * 60 * 1000); long hour = (l / (60 * 60 * 1000) + (l % (60 * 60 * 1000) > 0 ? 1 : 0)); // 不足一小时按一小时计 float fee = Float.parseFloat(groundModel.getgFee()) * hour; return fee; }
package com.gym.user.service.impl; import java.util.List; import com.gym.dao.impl.EquipmentDaoImpl; import com.gym.dao.impl.EquipmentRentDaoImpl; import com.gym.model.EquipmentModel; import com.gym.model.EquipmentRentModel; import com.gym.model.UserModel; import com.gym.user.service.EquipmentService; import com.gym.utils.CalculationUitl; import com.gym.utils.CompareUtil; import com.gym.utils.Constant; public class EquipmentServiceImpl implements EquipmentService { public List queryEquipment() { // TODO Auto-generated method stub EquipmentDaoImpl equipmentDaoImpl = new EquipmentDaoImpl(); return equipmentDaoImpl.queryEquipment(); } public EquipmentModel queryById(String eId) { // TODO Auto-generated method stub EquipmentDaoImpl equipmentDaoImpl = new EquipmentDaoImpl(); return equipmentDaoImpl.queryById(eId); } public EquipmentModel queryByName(String eName) { // TODO Auto-generated method stub return null; } public int rentEquipment(EquipmentRentModel equipmentRentModel, EquipmentModel equipmentModel, UserModel userModel) { // TODO Auto-generated method stub EquipmentServiceImpl equipmentServiceImpl = new EquipmentServiceImpl(); equipmentModel = equipmentServiceImpl.queryById(equipmentRentModel .geteId()); // 根据场地id查询 if (equipmentModel == null) { // 找不到此器材 return Constant.EQUIPMENTINVALID; } // 重新设置相关参数,防止某些参数被用户在前台篡改 equipmentRentModel.setBorrowFee(equipmentModel.geteFee()); equipmentRentModel.setStatus(equipmentModel.getStatus()); equipmentRentModel.setuId(userModel.getuId()); CompareUtil compareUtil = new CompareUtil(); // 验证起始时间是否大于结束时间 if (compareUtil.compareDateTime(equipmentRentModel.getBorrowBegin(), equipmentRentModel.getBorrowEnd(), "yyyy-MM-dd HH:mm") >= 0) { return Constant.TIMEERROR; } // ------------------待开发(惩罚规则)-------------------- // 判断用户是否被冻结 // ------------------待开发(惩罚规则)-------------------- // 检测器材此时间段是否空闲 EquipmentRentDaoImpl equipmentRentDaoImpl = new EquipmentRentDaoImpl(); List groundBookList = equipmentRentDaoImpl.queryByTime( equipmentRentModel.getBorrowBegin(), equipmentRentModel.getBorrowEnd()); if (groundBookList.size() != 0) { return Constant.TIMECLASH; } // 查询该用户此时间段内是否有其他预定 // 计算费用 equipmentRentModel.setBorrowFee(new CalculationUitl().calculaRentMoney( equipmentRentModel, equipmentModel) + ""); if (equipmentRentDaoImpl.addEquipmentRent(equipmentRentModel) == 1) { return Constant.SUCCESS; } else { return Constant.ERROR; } } }验证时间是否正确和计算费用同样封装在CompareUtil和CalculationUitl工具类中。