预算编报管理系统 Day10 业务逻辑 测试代码
程序员文章站
2022-03-30 21:12:12
...
1.业务逻辑代码:
写了一个doSomething 类,实现了一些业务逻辑
当servlet接收传入参数时,只需要简单获取参数并将参数传入我写的方法,就可以执行要处理的业务逻辑
我实现的业务逻辑有:
1)预算申请
2)预算查找
3)预算限制金额设置
4)预算查找
5)信息发送
6)信息获取
一下是dosomething类的代码:
package Service.Logic;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import Bean.AdBudget;
import Bean.Budget;
import Bean.Department;
import Bean.Message;
import Bean.User;
import DAO.Acadmy_DAO;
import DAO.AdBudget_DAO;
import DAO.Budget_DAO;
import DAO.Department_DAO;
import DAO.Message_DAO;
import DAO.User_DAO;
public class DoSomething {
// budget 的状态
public static final String BUDGET_STATE_NOTYETREAD = "n";
public static final String BUDGET_STATE_ALREADYREAD = "y";
public static final String BUDGET_STATE_READANDPASS = "p";
public static final String BUDGET_STATE_NOTPASSED = "np";
// message state
public static final String MESSAGE_STATE_NOTREAD = "nr";
public static final String MESSAGE_STATE_HAVEREAD = "hr";
// messageType
public static final String MESSAGE_TYPE_SCHOOL_TO_ACADMY = "1";
public static final String MESSAGE_TYPE_ACADMY_TO_DEP = "2";
public static final String MESSAGE_TYPE_DEP_TO_PERSON = "3";
// user jobs
public static final String USER_JOB_PUTONG = "1";
public static final String USER_JOB_DEP_MANAGER = "2";
public static final String USER_JOB_ACA_MANAGER = "3";
public static final String USER_JOB_ACA_LEADER = "4";
public static final String USER_JOB_SCH_MANAGER = "5";
private Budget_DAO budgetDao;
public Department_DAO getDepartmentDao() {
return departmentDao;
}
public void setDepartmentDao(Department_DAO departmentDao) {
this.departmentDao = departmentDao;
}
private Acadmy_DAO acadmyDao;
private Message_DAO messageDao;
private AdBudget_DAO adBudgetDao;
private User_DAO userDao;
private Department_DAO departmentDao;
public Budget_DAO getBudgetDao() {
return budgetDao;
}
public void setBudgetDao(Budget_DAO budgetDao) {
this.budgetDao = budgetDao;
}
public Message_DAO getMessageDao() {
return messageDao;
}
public void setMessageDao(Message_DAO messageDao) {
this.messageDao = messageDao;
}
public User_DAO getUserDao() {
return userDao;
}
public void setUserDao(User_DAO userDao) {
this.userDao = userDao;
}
public void setAcadmyDao(Acadmy_DAO acadmyDao) {
this.acadmyDao = acadmyDao;
}
public AdBudget_DAO getAdBudgetDao() {
return adBudgetDao;
}
public void setAdBudgetDao(AdBudget_DAO adBudgetDao) {
this.adBudgetDao = adBudgetDao;
}
public Acadmy_DAO getAcadmyDao() {
return acadmyDao;
}
public DoSomething() {
// TODO Auto-generated constructor stub
}
// 用户提交预算申请
public String commitBudget(String userId, String depId, String money, String typeId) {
// String budgetId = null;
String budgetId = getNewBudgetId();
String date = getNowDate();
String state = getNewBudgetState();
//
// private String budg_id;//id
// private String user_id;
// private String dep_id;
// private double money;
// private String type_id;
// private String state;
// private String date;
Budget b = new Budget();
b.setBudg_id(budgetId);
b.setDate(date);
b.setDep_id(depId);
b.setMoney(Double.parseDouble(money));
b.setState(state);
b.setType_id(typeId);
b.setUser_id(userId);
budgetDao.save(b);
return budgetId;
}
// 根据userid 和年份 获取其预算申请
public List<Budget> getUserBudgetByUserId(String userId, String year) {
return budgetDao.queryWhereSQL("user_id='" + userId + "' and year(date)='" + year + "'");
}
private String getNewBudgetId() {
String budgetId = "";
return budgetId;
}
// 获取现在的日期
private String getNowDate() {
Calendar cal = Calendar.getInstance();
int y = cal.get(Calendar.YEAR);
int m = cal.get(Calendar.MONTH) + 1;
int d = cal.get(Calendar.DATE);
return y + "-" + m + "-" + d;
}
// 获取现在budget的状态
private String getNewBudgetState() {
return BUDGET_STATE_NOTYETREAD;
}
private String getNewMessageState() {
return MESSAGE_STATE_NOTREAD;
}
// sendMessage 用户发送数据
// 即在message 中保存一条记录
// private String dep_id;//id
// private String msgtype;//id
// private String date;
// private String sch_name;//id
// private String aca_name;//id
// private String content;
// private String user_id;//id
// private String state;
// private String message_id;
public void sendMessage(String depId, String msgType, String schName, String acaName, String content,
String userId) {
String date = getNowDate();
String state = getNewMessageState();
String messageId = getNewMessageId();
Message msg = new Message();
// msg.setAca_name(acaName);
msg.setContent(content);
msg.setDate(date);
msg.setDep_id(depId);
msg.setMsgtype(msgType);
msg.setSch_name(schName);
msg.setState(state);
msg.setUser_id(userId);
msg.setAca_name(acaName);
msg.setMessage_id(messageId);
messageDao.save(msg);
}
private String getNewMessageId() {
return "2091000";
}
// 更新每个单位的limit_money
public void setDepLimitMoneyOneByOne(String date_year, String depId, String limitMoney) {
Double limitMoneyByDouble = Double.parseDouble(limitMoney);
String whereSql = "year(date)='" + date_year + "' and dep_id='" + depId + "'";
List<AdBudget> r = adBudgetDao.queryWhereSQL(whereSql);
if (r.size() != 1)
return;
AdBudget ab = (AdBudget) r.get(0);
ab.setLimit_money(limitMoneyByDouble);
adBudgetDao.update(ab);
// System.out.println(r.size());
// AdBudget adBudget = adBudgetDao.queryAdBudget(params);
}
// 更新多个单位的limitmoney 分别以逗号隔开
// date_year 只需要一个就好
public void setDepLimitMoneyByMany(String date_year, String depId, String limitMoney) {
String depIds[] = depId.split(",");
String limitMoneys[] = limitMoney.split(",");
for (int i = 0; i < depIds.length; i++) {
setDepLimitMoneyOneByOne(date_year, depIds[i], limitMoneys[i]);
}
}
// 按学院查询各个部门的预算表
public List selectByAcaName(String aca_name) {
return adBudgetDao.queryWhereSQL("aca_name='" + aca_name + "'");
}
public static void main(String args[]) {
ApplicationContext actx = new ClassPathXmlApplicationContext("applicationContext.xml");
AdBudget_DAO bd = (AdBudget_DAO) actx.getBean("adBudgetDao");
DoSomething ds = (DoSomething) actx.getBean("dosomething");
HibernateTransactionManager tranManager = (HibernateTransactionManager) actx.getBean("transactionManager");
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // 事物隔离级别,开启新事务
TransactionStatus status = tranManager.getTransaction(def);
// ds.setDepLimitMoneyOneByOne("2018", "090", "50.3");
Object[] l = ds.selectMessageByUserId("001");
//ds.sendMessage("090", MESSAGE_TYPE_ACADMY_TO_DEP, "sd", "good", "请修改金额", "");
tranManager.commit(status);
System.out.println(l.length);
}
// 按个人id查看消息列表
// 但是有人有不同身份,所以可能会有多张消息表分别是个人的,学院给部门的,学校给学院的
// 返回两种类型,一种是表示他是哪种职务,第二是他的消息列表
// 如果是 putong 则只有一个消息列表
// 如果是dep_manager 则有两个消息列表 第一个是个人的 , 第二个是学院发过来的
// 如果是aca _manager 则有两个 , 一个是个人的, 第二个是学校发来的
// 如果是aca_leader ,2个 个人 和本学院
// 如果是schManager , 2个 ,个人和下面学院
public Object[] selectMessageByUserId(String userId) {
// 首先获取userId 的job
List<User> user = userDao.queryWhereSQL("user_id='" + userId + "'");
if (user.size() != 1)
return null;
List<Message> messages1;
String job = user.get(0).getUser_job();
messages1 = messageDao
.queryWhereSQL("msgtype='" + MESSAGE_TYPE_DEP_TO_PERSON + "' and user_id='" + userId + "'");
if (job.equals(USER_JOB_PUTONG)) {
Object[] objs = { job, messages1 };
return objs;
} else {
List<Message> messages2 = null;
if (job.equals(USER_JOB_DEP_MANAGER)) {
String whereSQL = "msgtype='" + MESSAGE_TYPE_ACADMY_TO_DEP + "' and dep_id='" + user.get(0).getDep_id()
+ "'";
messages2 = messageDao.queryWhereSQL(whereSQL);
} else if (job.equals(USER_JOB_ACA_MANAGER)) {
String dep_id = user.get(0).getDep_id();
List<Department> department = departmentDao.queryWhereSQL("dep_id='" + dep_id + "'");
String aca_name = department.get(0).getAca_name();
String whereSQL = "msgtype='" + MESSAGE_TYPE_SCHOOL_TO_ACADMY + "' and aca_name='" + aca_name + "'";
messages2 = messageDao.queryWhereSQL(whereSQL);
}
Object objs[] = { job, messages1, messages2 };
return objs;
}
// return null;
}
// 设置是否已读,如果传入的state 不是规定的两种状态的代码,这将其默认设为已读
public void setMeeasgeReadState(String messageId, String state) {
if (!(state.equals(MESSAGE_STATE_HAVEREAD) || state.equals(MESSAGE_STATE_NOTREAD)))
state = MESSAGE_STATE_HAVEREAD;
List<Message> msg = messageDao.queryWhereSQL("message_id='" + messageId + "'");
if (msg.size() != 1)
return;
msg.get(0).setState(state);
messageDao.update(msg.get(0));
}
// 根据messageId获取message
// 用与用户点击一台消息查看详情
public Message selectMessageById(String messageId) {
List<Message> l = messageDao.queryWhereSQL("message_id='" + messageId + ",");
if (l.size() != 1)
return null;
return l.get(0);
}
}
2.测试代码遇到的问题
1)直接在DoSomething类里面的main方法调用可以
2)当启动tomcat 在servlet doget方法里执行1)中main方法的内容,就会出现数据库连接错误
猜想是spring配置问题
最终还没有解决,因此包装了一下hibernate,修改了一下dao类的方法,直接在dosomrthing中使用dao的类进行业务逻辑的实现
调用很简单:
如存储一个budget过程代下:
接收到浏览器端穿过的参数,为均为string类型有
userId ,depid,money typeid;
Dosonmething ds = new DoSomthing();
ds.commitBudget(userId,depId, money, typeIds);
这样就可以将该条记录存到budget表中