|
|
package cn.itbaizhan.action;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import javax.annotation.Resource;
|
|
|
import org.springframework.context.annotation.Scope;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import cn.itbaizhan.action.*;
|
|
|
import cn.itbaizhan.po.Commodity;
|
|
|
import cn.itbaizhan.po.OrderForm;
|
|
|
import cn.itbaizhan.po.User;
|
|
|
import cn.itbaizhan.service.OrderFormService;
|
|
|
import cn.itbaizhan.service.UserService;
|
|
|
import com.opensymphony.xwork2.ActionContext;
|
|
|
import com.opensymphony.xwork2.ActionSupport;
|
|
|
@SuppressWarnings("serial")
|
|
|
@Component("orderFormAction")
|
|
|
@Scope("prototype")//多例,每个请求生成一个新的action
|
|
|
public class OrderFormAction extends ActionSupport {
|
|
|
// 订单表单对象
|
|
|
private OrderForm orderForm;
|
|
|
}
|
|
|
//要调用OrderFormService的方法,所以要声明,让spring把其实现类注入
|
|
|
// 定义了一个OrderFormAction类,用于处理订单表单相关的操作
|
|
|
public class OrderFormAction {
|
|
|
// 使用@Resource注解注入名为"orderFormServiceImpl"的OrderFormService实例
|
|
|
@Resource(name="orderFormServiceImpl")
|
|
|
OrderFormService service; // 声明一个OrderFormService类型的资源,指定资源名称为orderFormServiceImpl
|
|
|
// 声明了一个UserService类型的私有变量userservice
|
|
|
private UserService userservice;
|
|
|
// 获取OrderFormService实例的方法
|
|
|
public OrderFormService getService() {
|
|
|
// 返回服务实例
|
|
|
return service;
|
|
|
}
|
|
|
}
|
|
|
// 设置OrderFormService实例的方法
|
|
|
// 设置OrderFormService服务实例的方法
|
|
|
public void setService(OrderFormService service) {
|
|
|
this.service = service;
|
|
|
}
|
|
|
// 获取OrderForm实例的方法
|
|
|
public OrderForm getOrderForm() {
|
|
|
// 返回订单表单对象
|
|
|
return orderForm;
|
|
|
}
|
|
|
// 设置OrderForm实例的方法
|
|
|
public void setOrderForm(OrderForm orderForm) {
|
|
|
// 设置订单表单对象
|
|
|
this.orderForm = orderForm;
|
|
|
}
|
|
|
// 设置UserService实例的方法
|
|
|
@Resource
|
|
|
public void setUserservice(UserService userservice) {
|
|
|
this.userservice = userservice;
|
|
|
// 设置用户服务实例
|
|
|
}
|
|
|
// 获取UserService实例的方法
|
|
|
public UserService getUserservice() {
|
|
|
// 返回用户服务实例
|
|
|
return userservice;
|
|
|
}
|
|
|
@Override
|
|
|
// 执行订单提交操作,调用业务逻辑层保存订单表单,并返回成功状态
|
|
|
public String execute() throws Exception {
|
|
|
// 不要直接访问数据库,应该访问业务逻辑层service,而service去访问DAO,DAO去访问数据库
|
|
|
this.service.save(this.orderForm); // 调用服务将订单表单保存到数据库
|
|
|
System.out.println("orderForm:"+orderForm.getUsername()); // 打印订单表单的用户名
|
|
|
ActionContext.getContext().put("ordermessage", "您的订单已经提交!"); // 将成功消息放入ActionContext
|
|
|
return "success"; // 返回成功标识
|
|
|
}
|
|
|
//列表
|
|
|
@SuppressWarnings("unchecked")
|
|
|
// 列出所有的订单表单
|
|
|
public String listOrderForm(){
|
|
|
// 从当前的ActionContext中获取请求对象,并将其转换为Map类型
|
|
|
Map request = (Map) ActionContext.getContext().get("request");
|
|
|
// 将所有订单表单放入请求对象中
|
|
|
request.put("listOrderForms", service.findAllOrderForms());
|
|
|
// 打印字符串 "listOrderForms"
|
|
|
System.out.println("listOrderForms");
|
|
|
// 返回逻辑视图名 "listOrderForm"
|
|
|
return "listOrderForm";
|
|
|
}
|
|
|
// 查询订单表单的方法
|
|
|
@SuppressWarnings("unchecked")
|
|
|
// 查询订单表信息的方法
|
|
|
public String queryOrderForm(){
|
|
|
// 获取订单表单的ID并打印
|
|
|
int orderFormId= orderForm.getOrderFormId();
|
|
|
System.out.println("orderFormId:"+orderFormId);
|
|
|
// 获取用户名并打印
|
|
|
String username= orderForm.getUsername();
|
|
|
System.out.println("username:"+username);
|
|
|
// 获取提交时间并打印
|
|
|
String submitTime= orderForm.getSubmitTime();
|
|
|
System.out.println("submitTime:"+submitTime);
|
|
|
// 获取发货时间并打印
|
|
|
String consignmentTime= orderForm.getConsignmentTime();
|
|
|
System.out.println("consignmentTime:"+consignmentTime);
|
|
|
}
|
|
|
// 查询订单表单信息的方法
|
|
|
OrderForm orderForm=this.service.queryOrderForm(orderFormId);
|
|
|
// 获取请求对象并将其转换为Map类型
|
|
|
Map request=(Map) ActionContext.getContext().get("request");
|
|
|
// 将查询到的订单表单信息放入请求对象中
|
|
|
request.put("queryOrder", orderForm);
|
|
|
// 返回查询订单表单的结果标识
|
|
|
return "queryOrder";
|
|
|
public String deleteOrderForm(){
|
|
|
// 调用服务层方法删除订单表单
|
|
|
this.service.delete(orderForm);
|
|
|
// 返回删除操作的结果标识
|
|
|
return "deleteOrderForm";
|
|
|
}
|
|
|
public String findOrderFormById(){
|
|
|
// 根据订单表单ID查询订单表单,并将结果赋值给orderForm对象
|
|
|
orderForm = this.service.findOrderFormById(orderForm.getOrderFormId());
|
|
|
// 返回查询操作的结果标识
|
|
|
return "findOrderForm";
|
|
|
}
|
|
|
//付款
|
|
|
@SuppressWarnings("unchecked")
|
|
|
// 该方法用于处理订单支付请求,检查用户是否有足够的金额支付订单
|
|
|
public String payOrderForm(){
|
|
|
double totalprice=orderForm.getTotalPrice();//订单总金额
|
|
|
// 通过用户名查找用户信息
|
|
|
User user=userservice.findUserByName(orderForm.getUsername());
|
|
|
double money=user.getMoney();//用户所有金钱
|
|
|
Map request = (Map) ActionContext.getContext().get("request");
|
|
|
// 如果订单总金额大于用户现有金额,则将订单信息存入请求对象,并设置支付失败的消息
|
|
|
if(totalprice>money){
|
|
|
// 将订单表单对象放入请求对象中,键为"nowOrder"
|
|
|
request.put("nowOrder", orderForm);
|
|
|
// 将支付不足的消息放入ActionContext中,以便在视图层显示
|
|
|
ActionContext.getContext().put("payemessage", "您的金额不足!");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// 更新用户余额并保存用户信息
|
|
|
user.setMoney(money-totalprice);
|
|
|
userservice.update(user);
|
|
|
// 设置订单支付状态为“是”并保存订单信息
|
|
|
// 设置订单表的支付状态为“是”
|
|
|
orderForm.setIsPayoff("是");
|
|
|
// 更新订单表信息
|
|
|
this.service.update(orderForm);
|
|
|
// 打印订单表的ID
|
|
|
// 输出订单表单的ID
|
|
|
System.out.println("orderForm:"+orderForm.getOrderFormId());
|
|
|
// 将当前订单对象放入请求中
|
|
|
request.put("nowOrder", orderForm);
|
|
|
// 将支付成功信息放入ActionContext中
|
|
|
// 将付款成功消息放入ActionContext上下文中
|
|
|
ActionContext.getContext().put("payemessage", "付款成功!");
|
|
|
}
|
|
|
// 返回支付订单表单的标识符
|
|
|
return "payOrderForm";
|
|
|
}
|
|
|
//订单列表付款
|
|
|
@SuppressWarnings("unchecked")
|
|
|
// 支付订单方法
|
|
|
// 处理订单支付的方法
|
|
|
public String payOrder(){
|
|
|
double totalprice=orderForm.getTotalPrice();//订单总金额
|
|
|
// 从订单表单中获取用户名
|
|
|
String username = orderForm.getUsername();
|
|
|
// 根据订单表单中的用户名查找用户
|
|
|
User user=userservice.findUserByName(orderForm.getUsername());
|
|
|
// 根据订单表单中的用户名查找对应的用户信息
|
|
|
double money=user.getMoney();//用户所有金钱
|
|
|
}
|
|
|
// 获取当前请求的上下文中的请求对象
|
|
|
Map request = (Map) ActionContext.getContext().get("request");
|
|
|
// 判断总价是否大于用户提供的金额
|
|
|
if(totalprice>money){
|
|
|
// 如果金额不足,将该用户的订单信息存入请求对象
|
|
|
request.put("orderFormByUserName", service.findOrderFormByUserName(username));
|
|
|
// 设置支付消息,提示用户金额不足
|
|
|
ActionContext.getContext().put("payemessage", "您的金额不足!");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// 更新用户余额并保存用户信息
|
|
|
user.setMoney(money-totalprice);
|
|
|
userservice.update(user);
|
|
|
// 设置订单状态为已付款并保存订单信息
|
|
|
// 设置订单表的支付状态为“是”
|
|
|
orderForm.setIsPayoff("是");
|
|
|
// 更新订单表信息
|
|
|
this.service.update(orderForm);
|
|
|
// 打印订单ID
|
|
|
System.out.println("orderForm:"+orderForm.getOrderFormId());
|
|
|
// 将当前用户的所有订单信息存入request
|
|
|
request.put("orderFormByUserName", service.findOrderFormByUserName(username));
|
|
|
// 设置付款成功消息至ActionContext
|
|
|
ActionContext.getContext().put("payemessage", "付款成功!");
|
|
|
}
|
|
|
// 返回支付订单的字符串标识
|
|
|
return "payOrder";
|
|
|
}
|
|
|
// 返回输入订单表单的字符串标识
|
|
|
public String inputOrderForm(){
|
|
|
return "input";
|
|
|
}
|
|
|
//发货
|
|
|
@SuppressWarnings("unchecked")
|
|
|
// 方法 conOrder 用于设置订单为代销并更新订单信息,然后将所有订单信息放入请求中
|
|
|
public String conOrder(){
|
|
|
// 设置订单表的委托状态为“是”
|
|
|
orderForm.setIsConsignment("是");
|
|
|
// 调用服务层的方法更新订单表信息
|
|
|
this.service.update(orderForm);
|
|
|
// 获取请求对象
|
|
|
Map request = (Map) ActionContext.getContext().get("request");
|
|
|
// 将所有订单表信息放入请求对象中,键名为“listOrderForms”
|
|
|
request.put("listOrderForms", service.findAllOrderForms());
|
|
|
// 返回逻辑视图名“conOrder”
|
|
|
return "conOrder";
|
|
|
}
|
|
|
//提交订单
|
|
|
@SuppressWarnings("unchecked")
|
|
|
// 此方法用于添加订单表单并将购物车清空
|
|
|
public String addOrderForm(){
|
|
|
// 调用服务层的保存方法,将订单表单保存到数据库
|
|
|
this.service.save(this.orderForm);
|
|
|
// 获取当前会话对象
|
|
|
Map session = ActionContext.getContext().getSession();
|
|
|
// 从会话中获取购物车列表
|
|
|
List<Commodity> car = (List<Commodity>)session.get("car");
|
|
|
// 清空购物车
|
|
|
car.clear();
|
|
|
}
|
|
|
// 此方法用于向请求中添加订单表单信息,并返回相应的结果字符串
|
|
|
// 获取请求上下文中的 request 对象,并将其转换为 Map 类型
|
|
|
Map request = (Map) ActionContext.getContext().get("request");
|
|
|
// 将 orderForm 对象放入 request 中,键名为 "nowOrder"
|
|
|
request.put("nowOrder",orderForm);
|
|
|
// 返回字符串 "addOrderForm",该字符串可能用于后续的视图跳转或逻辑处理
|
|
|
return "addOrderForm";
|
|
|
@SuppressWarnings("unchecked")
|
|
|
// 根据用户名查找订单表单的方法
|
|
|
public String findOrderFormByUserName(){
|
|
|
// 获取请求对象
|
|
|
Map request = (Map) ActionContext.getContext().get("request");
|
|
|
// 将根据用户名查找的订单表单放入请求对象中
|
|
|
request.put("orderFormByUserName",this.service.findOrderFormByUserName(orderForm.getUsername()));
|
|
|
// 返回逻辑视图名
|
|
|
return "findOrderFormByUserName";
|
|
|
}
|
|
|
} |