最近一段时间刚刚学习了一些新的知识,就想拿个项目练练手,然后就在网上找了一个培训的电商项目练习了一下,做了之后学到了很多,学到了项目的很多流程,在这里总结一下。
一、项目介绍:
网上商城项目,用于建立网上终端、营销案在线销售及相关辅助功能,后台包含商品管理、订单管理、类目管理、客户管理、合作商管理、客服管理、支付平台、内容管理等,很大程度上分担了人工的压力,前台包括个人中心,购物车,商城首页,频道页,商品详情页,提交订单页,支付页面等页面构成,对提高客户服务效率和客户满意度能够起到较好的作用。
二、项目所用技术:
1、Jsp,freemarker,jquery,css+div,jstl标签,fckEditor, struts-menu
2、Springmvc,spring,mybatis
3、Webservice框架cxf
4、Jersey搭建图片服务器
5、Maven,svn,hudson
6、Oracle
三、开发环境:
1、Eclipse4.3
2、Tomcat7.0
3、Jdk1.7
4、Oracle10g
5、Maven3.1.5
6、Svn
7、hudson
接下来,我把这里面比较核心的模块分析一下,进行总结,里面包括:
后台:
1、品牌管理:包括简单的增删改查
2、商品管理:里面有一个比较核心的商品添加到前台的功能,主要的业务包括操作商品表、商品大字段表、商品属性表、商品的特殊属性表
3、订单管理:订单的增删改查,商品的上架,商品的发布(通过webService)。
前台:
1、商品的首页展示:通过多表查询进行分页查询显示
2、商品的单品页展示:利用freemaker技术对页面进行静态化处理,在后台进行静态化的发布,这样的好处是查询的时候只需查询一次数据库,静态化之后不需要再查询,大大的提高了系统的效率。
3、购物车:使用cookie技术,好处:用户不需要登录即可操作购物车。
4、订单流程:这里使用工作流activiti,这里对工作流的使用进行了加强。
五、核心模块的总结:
6.1、商品的添加:
操作商品表、商品大字段表、商品属性表、商品的特殊属性表
aaction层:
@RequestMapping("/addItem.do")
public String addItem(EbItem item, EbItemClob itemClob, HttpServletRequest request,Integer divNum){
item.setItemNo(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
List commList = featureService.selectCommFeature();
List paraList = new ArrayList();
for(EbFeature feature: commList){
//获得属性的id也就是普通属性在页面上的name
Long featureId = feature.getFeatureId();
if(feature.getInputType() == 3){
String [] paraArr = request.getParameterValues(featureId+"");
if(paraArr != null && paraArr.length > 0){
String paraVals = "";
for(String val : paraArr){
paraVals = paraVals + val + ",";
}
paraVals = paraVals.substring(0, paraVals.length() - 1);
EbParaValue pv = new EbParaValue();
pv.setFeatureId(featureId);
pv.setParaValue(paraVals);
paraList.add(pv);
}
}else{
String paraVal = request.getParameter(featureId+"");
if(StringUtils.isNotBlank(paraVal)){
EbParaValue pv = new EbParaValue();
pv.setFeatureId(featureId);
pv.setParaValue(paraVal);
paraList.add(pv);
}
}
}
List skuList = new ArrayList();
List specList = featureService.selectSpecFeature();
//skuType1 showStatus1 sort1 skuPrice1 marketPrice1 stockInventory1 skuUpperLimit1 sku1 location1
//遍历div数量
for(int i = 1; i <= divNum; i++){
//获得商城价和库存,他们是必填的字段
String skuPrice = request.getParameter("skuPrice"+i);
String stock = request.getParameter("stockInventory"+i);
//如果上面的必填字段不是空说明数据有效
if(StringUtils.isNotBlank(skuPrice) && StringUtils.isNotBlank(stock)){
String skuType = request.getParameter("skuType"+i);
String showStatus = request.getParameter("showStatus"+i);
String sort = request.getParameter("sort"+i);
String marketPrice = request.getParameter("marketPrice"+i);
String skuUpperLimit = request.getParameter("skuUpperLimit"+i);
String sku = request.getParameter("sku"+i);
String location = request.getParameter("location"+i);
//创建最小销售单元的对象,并且赋值
EbSku skuObj = new EbSku();
skuObj.setSkuPrice(new BigDecimal(skuPrice));
skuObj.setStockInventory(new Integer(stock));
if(StringUtils.isNotBlank(skuType) && !StringUtils.equals(skuType, "")){
skuObj.setSkuType(new Short(skuType));
}
if(StringUtils.isNotBlank(showStatus) && !StringUtils.equals(showStatus, "")){
skuObj.setShowStatus(new Short(showStatus));
}
if(StringUtils.isNotBlank(sort) && !StringUtils.equals(sort, "")){
skuObj.setSkuSort(new Integer(sort));
}
if(StringUtils.isNotBlank(marketPrice) && !StringUtils.equals(marketPrice, "")){
skuObj.setMarketPrice(new BigDecimal(marketPrice));
}
if(StringUtils.isNotBlank(skuUpperLimit) && !StringUtils.equals(skuUpperLimit, "")){
skuObj.setSkuUpperLimit(new Integer(skuUpperLimit));
}
skuObj.setSku(sku);
skuObj.setLocation(location);
List specValList = new ArrayList();
//获得每个最小销售单元所拥有的规格属性值,
//遍历规格属性
for(EbFeature feature : specList){
Long featureId = feature.getFeatureId();
//获得所选规格属性的值
String specVal = request.getParameter(featureId+"specradio"+i);
//创建规格对象
EbSpecValue spec = new EbSpecValue();
spec.setFeatureId(featureId);
spec.setSpecValue(specVal);
specValList.add(spec);
}
skuObj.setSpecList(specValList);
skuList.add(skuObj);
}
}
itemService.saveItem(item, itemClob, paraList, skuList);
return "redirect:listItem.do?showStatus=1&auditStatus=1";
}
service层:
public void saveItem(EbItem item, EbItemClob itemClob,
List paraList, List skuList) {
itemDao.saveItem(item);
paraValueDao.saveParaValue(paraList, item.getItemId());
itemClobDao.saveItemClob(itemClob, item.getItemId());
skuDao.saveSku(skuList, item.getItemId());
}
6.2、商品分页sql
<select id="selectItemByCondition" parameterType="com.rl.ecps.model.QueryCondition" resultMap="BaseResultMap">
select *
from (select rownum rw, a.*
from (
select * from eb_item t
<where>
<if test="brandId != null">
t.brand_id = #{brandId}
if>
<if test="auditStatus != null">
and t.audit_status = #{auditStatus}
if>
<if test="showStatus != null">
and t.show_status = #{showStatus}
if>
<if test="itemName != null">
and t.item_name like '%${itemName}%'
if>
where>
order by t.item_id desc
#{startNum}
]]>
select>
6.3、商品的首页查询筛选
6.3.1、sql
7、商品页静态化
主要将jsp改为静态的html页面,利用freemaker语法。
8、购物车
购物车利用cookie存储在浏览器,在后台拿到cookie进行数据操作。
购物车模块:
接口:
/**
* 查询购物车所有商品
* @param request
* @param response
* @return
*/
public List listCart(HttpServletRequest request, HttpServletResponse response);
/**
* 添加购物车
* @param request
* @param response
* @param skuId 最小销售单元id
* @param quantity 商品数量
*/
public void addCart(HttpServletRequest request, HttpServletResponse response, Long skuId, Integer quantity);
/**
* 根据商品id商品数量加一
* @param request
* @param response
* @param skuId 最小销售单元id
*/
public void addNum(HttpServletRequest request, HttpServletResponse response, Long skuId);
/**
* 根据商品id商品数量减一
* @param request
* @param response
* @param skuId 最小销售单元id
*/
public void reduceNum(HttpServletRequest request, HttpServletResponse response, Long skuId);
public void deleteCart(HttpServletRequest request, HttpServletResponse response, Long skuId);
public void clearCart(HttpServletRequest request, HttpServletResponse response);
public String validCookie(HttpServletRequest request, HttpServletResponse response);
public String validCar(HttpServletRequest request, HttpServletResponse response);
实现类:
public List listCart(HttpServletRequest request,
HttpServletResponse response) {
List cartList = new ArrayList();
//获取浏览器所有cookie
Cookie[] cookies = request.getCookies();
//[{skuId:1,quantity:2},{}]
if(cookies != null && cookies.length > 0){
for (Cookie cookie : cookies) {
String code = ECPSUtils.readProp("cookie_ecps_code");
String name = cookie.getName();
if(StringUtils.equals(name, code)){
String result = cookie.getValue();
//对其进行解码,防止中文乱码
result = URLDecoder.decode(result);
JSONArray ja = JSONArray.fromObject(result);
JsonConfig jc = new JsonConfig();
//设置要转换的类
jc.setRootClass(EbCart.class);
//排除类里面的属性
jc.setExcludes(new String[]{"sku"});
cartList = (List) JSONSerializer.toJava(ja, jc);
//根据skuid设置购物车对象的EbSku对象
for (EbCart cart : cartList) {
EbSku sku = skuDao.getSkuDetailById(cart.getSkuId());
cart.setSku(sku);
}
}
}
}
return cartList;
}
public void addCart(HttpServletRequest request,
HttpServletResponse response, Long skuId, Integer quantity) {
List cartList = new ArrayList();
//json的配置对象
JsonConfig jc = new JsonConfig();
//设置要转换的类
jc.setRootClass(EbCart.class);
//设置不需要转换的属性
jc.setExcludes(new String[]{"sku"});
Cookie[] cookies = request.getCookies();
if(cookies != null&&cookies.length > 0){
for(Cookie cookie : cookies){
String name = cookie.getName();
String cookieKey = ECPSUtils.readProp("cookie_ecps_code");
if(StringUtils.equals(name, cookieKey)){
//获得cookie的值
String result = cookie.getValue();
//[{skuId:1002, quantity:2}, {skuId:1003, quantity:3},....]
result = URLDecoder.decode(result);
//把json格式的字符串转换成json数组对象
JSONArray ja = JSONArray.fromObject(result);
//把json的数组转换成java集合
cartList = (List) JSONSerializer.toJava(ja, jc);
boolean isExsits = false;
//1.如果存在对应商品,则在基础上数量加一
for(EbCart cart: cartList){
if(cart.getSkuId().longValue() == skuId.longValue()){
cart.setQuantity(cart.getQuantity() + quantity);
isExsits = true;
}
}
//2.如果不存在,则创建新的对象
if(!isExsits){
EbCart cartObj = new EbCart();
cartObj.setSkuId(skuId);
cartObj.setQuantity(quantity);
cartList.add(cartObj);
}
}
}
}
//3.如果没有创建过cookie对象,则创建
if(cartList.size() == 0){
EbCart cartObj = new EbCart();
cartObj.setSkuId(skuId);
cartObj.setQuantity(quantity);
cartList.add(cartObj);
}
//将java对象再次转换为字符串存到cookie中
JSONArray ja = JSONArray.fromObject(cartList, jc);
String result = ja.toString();
result = URLEncoder.encode(result);
Cookie cookie = new Cookie("cookie_ecps_code", result);
cookie.setMaxAge(Integer.MAX_VALUE);
cookie.setPath("/");
response.addCookie(cookie);
}
public void addNum(HttpServletRequest request,
HttpServletResponse response, Long skuId) {
List cartList = new ArrayList();
//json的配置对象
JsonConfig jc = new JsonConfig();
//设置要转换的类
jc.setRootClass(EbCart.class);
//设置不需要转换的属性
jc.setExcludes(new String[]{"sku"});
Cookie[] cookies = request.getCookies();
if(cookies != null&&cookies.length > 0){
for(Cookie cookie : cookies){
String name = cookie.getName();
String cookieKey = ECPSUtils.readProp("cookie_ecps_code");
if(StringUtils.equals(name, cookieKey)){
//获得cookie的值
String result = cookie.getValue();
//[{skuId:1002, quantity:2}, {skuId:1003, quantity:3},....]
result = URLDecoder.decode(result);
//把json格式的字符串转换成json数组对象
JSONArray ja = JSONArray.fromObject(result);
//把json的数组转换成java集合
cartList = (List) JSONSerializer.toJava(ja, jc);
for(EbCart cart: cartList){
if(cart.getSkuId().longValue() == skuId.longValue()){
cart.setQuantity(cart.getQuantity() + 1);
}
}
}
}
}
//将java对象再次转换为字符串存到cookie中
JSONArray ja = JSONArray.fromObject(cartList, jc);
String result = ja.toString();
result = URLEncoder.encode(result);
Cookie cookie = new Cookie("cookie_ecps_code", result);
cookie.setMaxAge(Integer.MAX_VALUE);
cookie.setPath("/");
response.addCookie(cookie);
}
@SuppressWarnings("deprecation")
public void reduceNum(HttpServletRequest request,
HttpServletResponse response, Long skuId) {
List cartList = new ArrayList();
Cookie[] cookies = request.getCookies();
JsonConfig jc = new JsonConfig();
jc.setRootClass(EbCart.class);
jc.setExcludes(new String[]{"sku"});
if(cookies != null && cookies.length > 0){
for (Cookie cookie : cookies) {
String name = cookie.getName();
String code = ECPSUtils.readProp("cookie_ecps_code");
if(StringUtils.equals(name, code)){
String result = cookie.getValue();
//将html编码解码
result = URLDecoder.decode(result);
JSONArray ja = JSONArray.fromObject(result);
cartList = (List) JSONSerializer.toJava(ja, jc);
for(EbCart cart : cartList){
if(cart.getSkuId().longValue() == skuId.longValue()){
cart.setQuantity(cart.getQuantity() - 1);
}
}
}
}
}
//将java对象再次转换为字符串存到cookie中
JSONArray ja = JSONArray.fromObject(cartList, jc);
String result = ja.toString();
result = URLEncoder.encode(result);
Cookie cookie = new Cookie("cookie_ecps_code", result);
cookie.setMaxAge(Integer.MAX_VALUE);
cookie.setPath("/");
response.addCookie(cookie);
}
public void deleteCart(HttpServletRequest request,
HttpServletResponse response, Long skuId) {
List cartList = new ArrayList();
//json的配置对象
JsonConfig jc = new JsonConfig();
//设置要转换的类
jc.setRootClass(EbCart.class);
//设置不需要转换的属性
jc.setExcludes(new String[]{"sku"});
Cookie[] cookies = request.getCookies();
if(cookies != null&&cookies.length > 0){
for(Cookie cookie : cookies){
String name = cookie.getName();
String cookieKey = ECPSUtils.readProp("cookie_ecps_code");
if(StringUtils.equals(name, cookieKey)){
//获得cookie的值
String result = cookie.getValue();
//[{skuId:1002, quantity:2}, {skuId:1003, quantity:3},....]
result = URLDecoder.decode(result);
//把json格式的字符串转换成json数组对象
JSONArray ja = JSONArray.fromObject(result);
//把json的数组转换成java集合
cartList = (List) JSONSerializer.toJava(ja, jc);
for(EbCart cart: cartList){
if(cart.getSkuId().longValue() == skuId.longValue()){
cartList.remove(cart);
}
}
}
}
}
//将java对象再次转换为字符串存到cookie中
JSONArray ja = JSONArray.fromObject(cartList, jc);
String result = ja.toString();
result = URLEncoder.encode(result);
Cookie cookie = new Cookie("cookie_ecps_code", result);
cookie.setMaxAge(Integer.MAX_VALUE);
cookie.setPath("/");
response.addCookie(cookie);
}
public void clearCart(HttpServletRequest request,
HttpServletResponse response) {
List cartList = new ArrayList();
//json的配置对象
JsonConfig jc = new JsonConfig();
//设置要转换的类
jc.setRootClass(EbCart.class);
//设置不需要转换的属性
jc.setExcludes(new String[]{"sku"});
Cookie[] cookies = request.getCookies();
if(cookies != null&&cookies.length > 0){
for(Cookie cookie : cookies){
String name = cookie.getName();
String cookieKey = ECPSUtils.readProp("cookie_ecps_code");
if(StringUtils.equals(name, cookieKey)){
//获得cookie的值
String result = cookie.getValue();
//[{skuId:1002, quantity:2}, {skuId:1003, quantity:3},....]
result = URLDecoder.decode(result);
//把json格式的字符串转换成json数组对象
JSONArray ja = JSONArray.fromObject(result);
//把json的数组转换成java集合
cartList = (List) JSONSerializer.toJava(ja, jc);
cartList.clear();
}
}
}
JSONArray ja = JSONArray.fromObject(cartList, jc);
String result = ja.toString();
result = URLEncoder.encode(result);
Cookie cookie = new Cookie("cookie_ecps_code", result);
cookie.setMaxAge(Integer.MAX_VALUE);
cookie.setPath("/");
response.addCookie(cookie);
}
public String validCookie(HttpServletRequest request,
HttpServletResponse response) {
/**
* 1.创建cookie设置到浏览器
* 2.取cookie,看是否能拿到,如果拿到,说明浏览器cookie正常,否则,关闭了
*/
Cookie cookie = new Cookie("test", "test");
response.addCookie(cookie);
Cookie[] cookies = request.getCookies();
String result = "yes";//默认为cookie可用
if(cookies != null && cookies.length > 0){
for (Cookie cookie2 : cookies) {
String name = cookie2.getName();
String value = cookie2.getValue();
System.out.println(name+"="+value);
if((StringUtils.equals("test", name) && StringUtils.equals("test", value))){
result = "no";
}
}
}
System.out.println(result);
return "yes";
}
public String validCar(HttpServletRequest request,
HttpServletResponse response) {
String result1 = "success";
List cartList = new ArrayList();
//获得当前网站的cookie
Cookie[] cookies = request.getCookies();
if(cookies != null&&cookies.length > 0){
for(Cookie cookie : cookies){
String name = cookie.getName();
String cookieKey = ECPSUtils.readProp("cart_key");
if(StringUtils.equals(name, cookieKey)){
//获得cookie的值
String result = cookie.getValue();
//[{skuId:1002, quantity:2}, {skuId:1003, quantity:3},....]
result = URLDecoder.decode(result);
//把json格式的字符串转换成json数组对象
JSONArray ja = JSONArray.fromObject(result);
//json的配置对象
JsonConfig jc = new JsonConfig();
//设置要转换的类
jc.setRootClass(EbCart.class);
//设置不需要转换的属性
jc.setExcludes(new String[]{"sku"});
//把json的数组转换成java集合
cartList = (List) JSONSerializer.toJava(ja, jc);
for (EbCart cart : cartList) {
//判断库存商品数量是否大于购买数量
EbSku sku = skuDao.getSkuDetailById(cart.getSkuId());
if(sku.getStockInventory().intValue() < cart.getQuantity().intValue()){
//提示信息:哪个商品什么规格不足多少
result1 = sku.getItem().getItemName();//哪个商品
for(EbSpecValue spec : sku.getSpecList()){
result1 = result1 + spec.getSpecValue();//哪个规格
}
result1 = result1 + "不足" + cart.getQuantity();//不足多少
break;//循环到库存不足的商品就跳出
}
}
}
}
}
return result1;
}
9、订单提交
订单提交的时候,注意每一次提交都要验证库存是否足够
页面:
//订单提交
function trueBuy(){
$.ajax({
url:"${path}/user/getUser.do",
type:"post",
dataType:"text",
success:function(responseText){
var userObj = $.parseJSON(responseText);
if(userObj.user != null){
var result = validCar();
if(result == "success"){
window.location.href = "${path}/order/toSubmitOrder.do";
}else{
alert(result);
}
}else{
tipShow("#loginAlert");
}
},
error:function(){
alert("系统错误");
}
})
}
//购买数量减一
function reduceNum(skuId, quantity){
var jsonObj = validStock(skuId, quantity);
if(jsonObj.result == "no"){
var newQuantity = quantity - 1;
alert("当前的库存数量不足"+newQuantity+"个,仅有"+jsonObj.stock+"个");
return;
}
if(quantity == 1){
if(confirm("是否把该商品从购物车中删除?")){
window.location.href = "${path}/cart/deleteCart.do?skuId="+skuId;
}
}else{
window.location.href = "${path}/cart/reduceNum.do?skuId="+skuId;
}
}
//验证库存数量
function validStock(skuId, quantity){
quantity--;
var jsonObj = null;
$.ajax({
url:"${path}/cart/validStockCar.do",
type:"post",
dataType:"text",
async:false,
data:{
skuId:skuId,
quantity:quantity
},
success:function(responseText){
jsonObj = $.parseJSON(responseText);
},
error:function(){
alert("系统错误");
}
})
return jsonObj;
}
function changeImage(){
var path = "${path}/user/getImage.do?date="+new Date();
$("#captchaImage").attr("src", path);
}
//提交订单异步登录
function loginAjax(){
var username = $("#username").val();
var password = $("#password").val();
var captcha = $("#captcha").val();
$.ajax({
url:"${path}/user/loginAjax.do",
type:"post",
dataType:"text",
data:{
username:username,
password:password,
captcha:captcha
},
success:function(responseText){
if(responseText == "caperror"){
$("#errorName").html("验证码错误");
$("#errorName").show(500);
}else if(responseText == "userpasserror"){
$("#errorName").html("用户名或者密码错误");
$("#errorName").show(500);
}else if(responseText == "success"){
$("#loginAlertIs").html(username);
tipHide("#loginAlert");
//校验库存
var result = validCar();
if(result == "success"){
window.location.href = "${path}/order/toSubmitOrder.do";
}else{
alert(result);
}
}
},
error:function(){
alert("系统错误");
}
})
}
//验证库存是否足够
function validCar(){
var result = "success";
$.ajax({
url:"${path}/cart/validCar.do",
type:"post",
dataType:"text",
async:false,
success:function(responseText){
result = responseText;
},
error:function(){
alert("系统错误");
}
})
return result;
}
action:
//订单提交
@RequestMapping("/submitOrder.do")
public String submitOrder(HttpServletResponse response, HttpServletRequest request,EbOrder order,
HttpSession session, String address, Model model) throws Exception{
TsPtlUser user = (TsPtlUser) session.getAttribute("user");
if(user != null){
order.setPtlUserId(user.getPtlUserId());
order.setUsername(user.getUsername());
}
order.setOrderNum(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
//1.如果不是新添加的地址,則通過地址的id查詢當前的地址
//2.新添加的地址,會自動的賦值給order對象
if(!StringUtils.equals("add", address)){
EbShipAddr addr = shipAddrService.selectAddrByShipAddrId(new Long(address));
BeanUtils.copyProperties(order, addr);
}
List cartList = cartService.listCart(request, response);
List detailList = new ArrayList();
for(EbCart cart:cartList){
EbOrderDetail detail = new EbOrderDetail();
detail.setItemId(cart.getSku().getItem().getItemId());
detail.setItemName(cart.getSku().getItem().getItemName());
detail.setItemNo(cart.getSku().getItem().getItemNo());
detail.setSkuId(cart.getSkuId());
String specVal = "";
List specList = cart.getSku().getSpecList();
for(EbSpecValue spec : specList){
specVal = specVal + spec.getSpecValue()+",";
}
specVal = specVal.substring(0, specVal.length() - 1);
detail.setSkuSpec(specVal);
detail.setQuantity(cart.getQuantity());
detail.setSkuPrice(cart.getSku().getSkuPrice());
detail.setMarketPrice(cart.getSku().getMarketPrice());
detailList.add(detail);
}
try {
String processInstanceId = orderService.saveOrder(response, request, order, detailList);
model.addAttribute("order", order);
model.addAttribute("processInstanceId", processInstanceId);
} catch (Exception e) {
if(e instanceof EbStockException){
model.addAttribute("tip", "stock_error");
}
}
return "shop/confirmProductCase2";
}
/**
* 订单支付
*/
@RequestMapping("/pay.do")
public void pay(String processInstanceId, Long orderId, PrintWriter out){
orderService.pay(processInstanceId, orderId);
out.write("success");
}
service:
public String saveOrder(HttpServletResponse response, HttpServletRequest request,
EbOrder order, List<EbOrderDetail> detailList)throws EbStockException {
orderDao.saveOrder(order);
Map<String,Object> map = new HashMap<String,Object>();
for(EbOrderDetail detail : detailList){
//給訂單詳情設置訂單id
detail.setOrderId(order.getOrderId());
detailDao.saveOrderDetail(detail);
/*EbSku sku = skuDao.getSkuById(detail.getSkuId());
sku.setStockInventory(sku.getStockInventory() - detail.getQuantity());
skuDao.update(sku);*/
map.put("skuId", detail.getSkuId());
map.put("quantity", detail.getQuantity());
//為了防止并发问题,这里可以使用:
//1.悲观锁:对查询语句进行锁,即for update
//2.乐观锁:对修改语句进行锁,即条件加判断条件:会返回影响数量
int flag = skuDao.updateStock(map);
if(flag == 0){
throw new EbStockException("库存不足");
}
}
//保存订单时,开启一个流程实例
String processInstanceId = flowService.startProcess(order.getOrderId());
cartService.clearCart(request, response);
return processInstanceId;
}
public void pay(String processInstanceId, Long orderId){
//修改订单支付状态
EbOrder order = new EbOrder();
order.setOrderId(orderId);
order.setIsPaid((short)1);
orderDao.updateOrder(order);
//完成订单支付节点
flowService.completeTaskByPId(processInstanceId, "支付");
}
public List<TaskBean> listNoPayOrder(Short isCall, String assignee) {
/**
* 1.根据办理人查询到task和businesskey
* 2.根据businesskey查询订单
* 3.根据iscall查询未支付的订单
*/
List<TaskBean> tbList1 = new ArrayList<TaskBean>();
List<TaskBean> tbList = flowService.selectTaskBeanByAssignee(assignee);
for (TaskBean tb : tbList) {
EbOrder order = orderDao.selectOrderById(tb.getBusinessKey());
//查询没有付款的订单
if(order.getIsCall().shortValue() == isCall.shortValue()){
tb.setOrder(order);
tbList1.add(tb);
}
}
return tbList1;
}
public EbOrder selectOrderAndDetailById(Long orderId) {
return orderDao.selectOrderAndDetailById(orderId);
}
public void updateOrder(Long orderId) {
EbOrder order = new EbOrder();
order.setOrderId(orderId);
order.setIsCall((short)1);
orderDao.updateOrder(order);
}
public List<TaskBean> listPaidOrder(String assignee) {
/**
* 1.根据办理人查询到task和businesskey
* 2.根据businesskey查询订单
* 3.根据iscall查询未支付的订单
*/
List<TaskBean> tbList = flowService.selectTaskBeanByAssignee(assignee);
for (TaskBean tb : tbList) {
EbOrder order = orderDao.selectOrderById(tb.getBusinessKey());
//查询没有付款的订单
tb.setOrder(order);
}
return tbList;
}
public TaskBean selectTaskBeanByOrderIdAndTaskId(Long orderId, String taskId) {
EbOrder order = orderDao.selectOrderAndDetailById(orderId);
TaskBean tb = flowService.selectTaskBeanByTaskId(taskId);
tb.setOrder(order);
return tb;
}
public void compeleTask(String taskId, String outcome, Long orderId) {
EbOrder order = new EbOrder();
order.setOrderId(orderId);
order.setUpdateTime(new Date());
flowService.compeleTaskByTaskId(taskId, outcome);
}
10、订单流程
这里采用工作流activiti整合ssm
10.1、业务流程图
10.3、service层
public void deployFlow() {
DeploymentBuilder db = repositoryService.createDeployment();
db.addClasspathResource("com/sihai/ecps/diagrams/OrderFlow.bpmn")
.addClasspathResource("com/sihai/ecps/diagrams/OrderFlow.png");
db.deploy();
}
public String startProcess(Long orderId) {
ProcessInstance pi = runtimeService.startProcessInstanceByKey("OrderFlow", orderId+"");
return pi.getId();
}
public void completeTaskByPId(String processInstanceId, String outcome) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("outcome", outcome);
Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
taskService.complete(task.getId(), map);
}
public List<TaskBean> selectTaskBeanByAssignee(String assignee) {
//根据办理人查询任务列表
List<Task> tList = taskService.createTaskQuery()
.processDefinitionKey("OrderFlow")
.taskAssignee(assignee)
.orderByTaskCreateTime()
.desc()
.list();
List<TaskBean> tbList = new ArrayList<TaskBean>();
for (Task task : tList) {
//设置任务和businesskey
TaskBean tb = new TaskBean();
tb.setTask(task);
ProcessInstance pi = runtimeService.createProcessInstanceQuery()
.processDefinitionKey("OrderFlow")
.processInstanceId(task.getProcessInstanceId())
.singleResult();
String businessKey = pi.getBusinessKey();
tb.setBusinessKey(businessKey);
tbList.add(tb);
}
return tbList;
}
public TaskBean selectTaskBeanByTaskId(String taskId) {
Task task = taskService.createTaskQuery().processDefinitionKey("OrderFlow").taskId(taskId).singleResult();
TaskBean tb = new TaskBean();
tb.setTask(task);
List<String> outcomes = this.getOutcomes(task);
tb.setOutcomes(outcomes);
return tb;
}
public List<String> getOutcomes(Task task){
List<String> outcomes = new ArrayList<String>();
//获得流程定义的对象
ProcessDefinitionEntity pe = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(task.getProcessDefinitionId());
//获得流程实例对象
ProcessInstance pi = runtimeService.createProcessInstanceQuery().processDefinitionKey("OrderFlow")
.processInstanceId(task.getProcessInstanceId()).singleResult();
ActivityImpl ai = pe.findActivity(pi.getActivityId());
//获得往外面走的线路的对象
List<PvmTransition> ptList = ai.getOutgoingTransitions();
for(PvmTransition pt : ptList){
String name = (String) pt.getProperty("name");
outcomes.add(name);
}
return outcomes;
}
public void compeleTaskByTaskId(String taskId, String outcome) {
Map<String,Object> map = new HashMap<String,Object>();
map.put("outcome", outcome);
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
taskService.complete(task.getId(), map);
}
最后,写了好久,就把这些核心的写出来,总结总结一下,以后好看看