在cart-1.html中javascript
<form action="alipayConfirm" th:object="${cartForm}" method="post">html
AND<table>java
<tbody th:each="cartsInfo,status:${cartList}">web
<tr>算法
<td class="chk">sql
<input type="hidden" th:name="${#strings.concat('listBean[').concat(status.index).concat('].commodityId')}" th:value="${cartsInfo.commodityId}"/>apache
<input type="hidden" th:name="${#strings.concat('listBean[').concat(status.index).concat('].cartId')}" th:value="${cartsInfo.cartId}"/>session
<input type="checkbox" th:name="${#strings.concat('listBean[').concat(status.index).concat('].checkArray')}" onclick="check();"/></td>app
<td><div class="cont cf">ide
<img th:src="@{showImage(pictureId=${cartsInfo.pictureId})}"
alt="購物車詳細狀況" class="pic" />
<h4>
<span th:text="${cartsInfo.commodityName}"></span>
</h4>
<p class=" mt10">規格:每<span
th:text="${#strings.concat(cartsInfo.unit).concat(cartsInfo.specifications)}"></span></p>
<p>品牌:<span th:text="${cartsInfo.brandName}"></span></p>
</div></td>
<td><p class="price yh">
<span th:id="${#strings.concat('single').concat(cartsInfo.commodityId)}" th:text="${cartsInfo.retailPrice}"></span>元
</p></td>
<td>
<div class="chooseAmount">
<a href="javascript:void(0);"
th:onclick="${#strings.concat('subNum(').concat(cartsInfo.commodityId).concat(')')}"></a>
<input th:id="${cartsInfo.commodityId}" type="text" class="fl inp-t" th:name="${#strings.concat('listBean[').concat(status.index).concat('].countArray')}" th:value="${cartsInfo.count}" value="1" />
<a href="javascript:void(0);"
th:onclick="${#strings.concat('addNum(').concat(cartsInfo.commodityId).concat(')')}"></a>
</div></td>
<td><p class="price yh">
<span name="xiaoji" th:id="${#strings.concat('sum').concat(cartsInfo.commodityId)}" th:text="${cartsInfo.smallSumPrice}"></span>元
</p></td>
<td><a class="button"
th:href="@{delCart(cartId=${cartsInfo.cartId},count=${cartsInfo.count},commodityId=${cartsInfo.commodityId})}"><span>刪除</span></a></td>
</tr>
</tbody>
<p class="fr">
<button class="btn btn-success yh" name="delChoosedCart"><span>刪除選中的商品</span</button>
已選擇<i id="sumCount" class="yh">0</i>種商品 合計¥<i id="sumMoney" class="yh">0</i>元
<button class="btn btn-success yh" name="Go">結 算</button>
</p>
</table></form>
在controller中的代碼:
@RequestMapping(value = "alipayConfirm", method = RequestMethod.POST,params="Go")
public String alipayConfirm(Model model, HttpSession session,CartForm cartForm, Device device) throws SQLException {
GoodsForm goodsForm = new GoodsForm();
List<GoodsForm> commodityType = goodsService.getType();
goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId());
model.addAttribute("goodsForm", goodsForm);
model.addAttribute("commodityType", commodityType);
log.info("確認支付");
UVO uvo = (UVO) session.getAttribute("UVO");
if (uvo == null || StringUtils.isEmpty(uvo.getGuestId())) {
return "redirect:/initGuestLogin";
}//用來判斷用戶是否登陸了
List<ListBean> listBean = cartForm.getListBean();//經過傳來的選定的商品的list來一個id修改一條記錄
int b = listBean.size();
String cartIds = "";
for (int i = 0; i < b; i++) {
cartForm.setUpdateUser(uvo.getGuestName());
Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
cartForm.setUpdateTime(dateformat.format(date));
cartForm.setCommodityId(listBean.get(i).getCommodityId());
cartForm.setCount(listBean.get(i).getCountArray());
cartForm.setGuestId(uvo.getGuestId());
String check = listBean.get(i).getCheckArray();
if (check != null) {
cartForm.setCartId(listBean.get(i).getCartId());
boolean result = cartService.editStockByCart(cartForm);//用選定商品的id來修改庫存
if (!result) {
throw new SQLException("庫存不足!");
}
boolean hisResult = cartService.updateCart(cartForm);//用選定商品的id來修改購物車
if (!hisResult) {
throw new SQLException("添加支付寶失敗");
}
cartIds = cartIds + ",'" + listBean.get(i).getCartId() + "'";
}
}
cartForm.setCartId(cartIds.substring(1));
// cartForm.setGuestId(uvo.getGuestId());
List<CartForm> list = cartService.searchCartListForCartId(cartForm);
AlipayForm alipayForm = new AlipayForm();
String body = "您購買的商品以下:";
Double price = 0d;
for (CartForm item : list) {
body = body
+ "品名:"
+ item.getCommodityName()
+ ", 數量:"
+ item.getCount()
+ ", 總價:"
+ String.valueOf(Double.valueOf(item.getCount())
* Double.valueOf(item.getRetailPrice())) + ";";
price = price + Double.valueOf(item.getCount())
* Double.valueOf(item.getRetailPrice());
}
alipayForm.setBody(body);
alipayForm.setOutTradeNo(list.get(0).getCartId());
// 不滿88元加8元郵費
if (price < 88) {
price = price + 8;
body = body + "(因爲本次訂單未滿88元,加收您郵費8元)";
}
alipayForm.setCartFormList(list);
alipayForm.setPrice(price.toString());
GuestForm guestForm=new GuestForm();
guestForm.setGuestId(uvo.getGuestId());
String addressDefault=guestService.searchAddressId(guestForm).getAddressId();//設置默認地址
model.addAttribute("addressDefault", addressDefault);//蔣默認地址傳到前臺
ReceiveForm receiveForm=new ReceiveForm();
receiveForm.setGuestId(uvo.getGuestId());
List<ReceiveForm> list1=receiveservice.searchlist(receiveForm);//搜索出用戶所對應的全部address_id
model.addAttribute("list", list1);//傳到前臺,來實現下一步去支付寶的結算
String host = env.getProperty("host.web");
alipayForm.setShowUrl(host + "/initCart");
alipayForm.setSubject(body);
model.addAttribute("alipayForm", alipayForm);//將去支付頁面所須要的其餘東西傳到前臺
cartForm.setGuestId(uvo.getGuestId());
model.addAttribute("cartList", cartService.searchCartList(cartForm));
if (device.isNormal()) {
return "shop/cart/cart-2";
} else {
return "mobile/alipay/cart-2";
}
}
用到的文件還有goodService.java CartService receiveservice ListBean cartForm alipayForm receiveForm uvo
cartSqlMap.xml
等,詳細文件在項目裏
package cn.agriculture.web.service;
@Service
@PropertySource("classpath:system.properties")
public class CartService {
public List<CartForm> searchCartList(CartForm frm) {
frm.setStatus("未付款");
List<CartForm> result = queryDao.executeForObjectList("Cart.selectCartList", frm);
return result;
}
public boolean editStockByCart(CartForm frm) {
frm.setStatus("未付款");
CartForm cartResult = queryDao.executeForObject("Cart.selectCart", frm,
CartForm.class);
if (cartResult == null) {
;
} else {
// String tempCount = frm.getCount();
Integer count = Integer.valueOf(frm.getCount())
- Integer.valueOf(cartResult.getCount());
// frm.setCount(String.valueOf(count));
frm.setCartId(cartResult.getCartId());
frm.setStatus("未付款");
// int result = updateDao.execute("Cart.editCart", frm);
// if (result == 1) {
Integer stock = queryDao.executeForObject("Cart.selectStock", frm,
Integer.class);
// if (stock == null) {
// 新商品沒有庫存
// return false;
// }
stock = stock - Integer.valueOf(count);//購物車算法
if (stock < 0) {
return false;
}
frm.setStock(stock.toString());
if (updateDao.execute("Cart.editStockAgain", frm) == 1) {
return true;
}
// return true;
}
return false;
}
public boolean updateCart(CartForm frm) {
frm.setStatus("未付款");
int result = updateDao.execute("Cart.updateCart", frm);
if (result != 0) {
return true;
}
return false;
}
public List<CartForm> searchCartListForCartId(CartForm frm) {
frm.setStatus("未付款");
List<CartForm> result = queryDao.executeForObjectList(
"Cart.selectCartListForCartId", frm);
return result;
}
}
public class GuestService {
@Autowired
QueryDAO queryDao;
@Autowired
UpdateDAO updateDao;
public GuestForm searchAddressId(GuestForm guestForm){
return queryDao.executeForObject("Guest.selectAddressId", guestForm,GuestForm.class);
}
}
sql
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<!-- SqlMap定義 -->
<sqlMap namespace="Cart">
<select id="selectCart" parameterClass="cn.agriculture.web.form.CartForm"
resultClass="cn.agriculture.web.form.CartForm">
SELECT cart.cart_id as cartId,
cart.guest_id as guestId,
cart.count as count,
commodity.commodity_id as commodityId,
commodity.type as type,
supplier.supplier_name as supplierName,
brand.brand_name as brandName,
commodity.commodity_name as commodityName,
commodity.weight as weight,
commodity.is_gift as isGift,
commodity.specifications as specifications,
commodity.unit as unit,
commodity.benchmark_price as benchmarkPrice,
commodity.guide_price as guidePrice,
commodity.retail_price as retailPrice,
commodity.competition_level as competitionLevel,
commodity.note as note,
cart.update_time as updateTime,
cart.update_user as updateUser,
commodity.picture_id as pictureId
FROM cart, commodity, supplier, brand
WHERE cart.commodity_id = commodity.commodity_id
AND commodity.supplier_id = supplier.supplier_id
AND commodity.brand_id = brand.brand_id
AND cart.status = #status#
AND cart.commodity_id = #commodityId#
AND cart.guest_id = #guestId#
</select>
<select id="selectCartList" parameterClass="cn.agriculture.web.form.CartForm"
resultClass="cn.agriculture.web.form.CartForm">
SELECT cart.cart_id as cartId,
cart.guest_id as guestId,
cart.count as count,
commodity.commodity_id as commodityId,
commodity.type as type,
supplier.supplier_name as supplierName,
brand.brand_name as brandName,
commodity.commodity_name as commodityName,
commodity.weight as weight,
commodity.is_gift as isGift,
commodity.specifications as specifications,
commodity.unit as unit,
commodity.benchmark_price as benchmarkPrice,
commodity.guide_price as guidePrice,
commodity.retail_price as retailPrice,
commodity.competition_level as competitionLevel,
commodity.note as note,
cart.update_time as updateTime,
cart.update_user as updateUser,
commodity.picture_id as pictureId
FROM cart, commodity, supplier, brand
WHERE cart.commodity_id = commodity.commodity_id
AND commodity.supplier_id = supplier.supplier_id
AND commodity.brand_id = brand.brand_id
AND cart.status = #status#
AND cart.guest_id = #guestId#
</select>
<select id="selectStock" parameterClass="cn.agriculture.web.form.CartForm" resultClass="java.lang.Integer">
SELECT stock FROM stock WHERE commodity_id = #commodityId#
</select>
<update id="editStock" parameterClass="cn.agriculture.web.form.CartForm">
UPDATE stock SET stock = #count#,
update_time = #updateTime#,
update_user = #updateUser#
WHERE commodity_id = #commodityId#
</update>
<update id="editStockAgain" parameterClass="cn.agriculture.web.form.CartForm">
UPDATE stock SET stock = #stock#,
update_time = #updateTime#,
update_user = #updateUser#
WHERE commodity_id = #commodityId#
</update>
<select id="selectCartListForCartId" parameterClass="cn.agriculture.web.form.CartForm"
resultClass="cn.agriculture.web.form.CartForm">
SELECT cart.cart_id as cartId,
cart.guest_id as guestId,
cart.count as count,
commodity.commodity_id as commodityId,
commodity.type as type,
supplier.supplier_name as supplierName,
brand.brand_name as brandName,
commodity.commodity_name as commodityName,
commodity.weight as weight,
commodity.is_gift as isGift,
commodity.specifications as specifications,
commodity.unit as unit,
commodity.benchmark_price as benchmarkPrice,
commodity.guide_price as guidePrice,
commodity.retail_price as retailPrice,
commodity.competition_level as competitionLevel,
commodity.note as note,
cart.update_time as updateTime,
cart.update_user as updateUser,
commodity.picture_id as pictureId
FROM cart, commodity, supplier, brand
WHERE cart.commodity_id = commodity.commodity_id
AND commodity.supplier_id = supplier.supplier_id
AND commodity.brand_id = brand.brand_id
AND cart.status = #status#
AND cart.cart_id in($cartId$)
</select>
<update id="updateCart" parameterClass="cn.agriculture.web.form.CartForm">
UPDATE cart SET count = #count#
WHERE commodity_id = #commodityId#
AND guest_id = #guestId#
</update>
<sqlMap namespace="Receive">
<select id="selectReceiveAddresslist"
parameterClass="cn.agriculture.web.form.ReceiveForm"
resultClass="cn.agriculture.web.form.ReceiveForm">
SELECT address_id as addressId,
guest_id as guestId,
receive_address as addressName,
receive_name as receiveName,
receive_phone as phone,
receive_post as post
FROM address
WHERE guest_id=#guestId#
</select>
<select id="selectAddressId"
parameterClass="cn.agriculture.web.form.GuestForm"
resultClass="cn.agriculture.web.form.GuestForm">
SELECT
address_id as addressId
FROM
guest
WHERE
guest_id=#guestId#
</select>
cart-2.html
<form name="alipayForm" action="alipaySubmit" th:object="${alipayForm}" method="post" class="form-horizontal form-address mt20">
<input type="hidden" name="outTradeNo" value="" th:value="${alipayForm.outTradeNo}"/>
<input type="hidden" name="subject" value="" th:value="${alipayForm.subject}"/>
<input type="hidden" name="body" value="" th:value="${alipayForm.body}"/>
<input type="hidden" name="price" value="" th:value="${alipayForm.price}"/>
<input type="hidden" name="showUrl" value="" th:value="${alipayForm.showUrl}"/>
<ul>
<li th:each="cartForm,status:${alipayForm.cartFormList}">
<input type="hidden" th:name="${#strings.concat('cartFormList[').concat(status.index).concat('].cartId')}"
th:value="${cartForm.cartId}"/>
//實現id拼接
</li>
</ul>
<tbody>
<tr th:each="receiveInfo,status:${list}">
<td class="chk"><input type="radio" name="addressId" class="vm" th:value="${receiveInfo.addressId}"/>
//實現id拼接
<span th:text="${receiveInfo.receiveName}"></span></td>
<td><span th:text="${receiveInfo.addressName}"></span></td>
<td><span th:text="${receiveInfo.post}"></span></td>
<td><span th:text="${receiveInfo.phone}"></span></td>
</tr>
</tbody>
<p class="yellow f14 mt30">支付方式</p>
<p class="payWay mt10"><input type="checkbox" class="vm"/> 支付寶支付</p>
<p class="tr mt20"><button class="btnBlue yh">去支付</button></p>
</form>
cart2提交到的controller
@RequestMapping(value = "alipaySubmit", method = RequestMethod.POST)
public String executeAlipaySubmit(Model model, HttpSession session, @Valid @ModelAttribute("alipayForm") AlipayForm alipayForm, BindingResult results, Device device, ReceiveService receiveservice) throws SQLException {
log.info("修改購物車信息爲已付款");
UVO uvo = (UVO)session.getAttribute("UVO");
if (uvo == null || StringUtils.isEmpty(uvo.getGuestId())) {
return "redirect:/initGuestLogin";
}
GoodsForm goodsForm=new GoodsForm();
// goodsForm.setType("糧食");
// model.addAttribute("goodsForm", goodsForm);
List<GoodsForm> commodityType = goodsService.getType();
goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId());
model.addAttribute("goodsForm", goodsForm);
model.addAttribute("commodityType", commodityType);
alipayForm.setUpdateUser(uvo.getGuestName());
ReceiveForm receiveForm=new ReceiveForm();
receiveForm.setAddressId(alipayForm.getAddressId());//這個直接將選定的radio的值(addressId)傳來使用也就肯定了那個單選按鈕就選選定了
receiveForm=receiveService.searchReceive(receiveForm);
alipayForm.setReceiveAddress(receiveForm.getAddressName());
alipayForm.setReceiveMobile(receiveForm.getMobile());
alipayForm.setReceiveName(receiveForm.getReceiveName());
alipayForm.setReceivePhone(receiveForm.getPhone());
alipayForm.setReceiveZip(receiveForm.getPost());
Date date = new Date();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
alipayForm.setUpdateTime(dateformat.format(date));
alipayForm.setGuestId(uvo.getGuestId());
model.addAttribute("AlipayForm", alipayForm);
boolean hisResult = cartService.addAlipayHistory(alipayForm);
if(!hisResult) {
throw new SQLException("添加支付寶帳單失敗!");
}
CartForm cartForm = new CartForm();
cartForm.setUpdateUser(uvo.getGuestName());
cartForm.setUpdateTime(dateformat.format(date));
cartForm.setGuestId(uvo.getGuestId());
int b = alipayForm.getCartFormList().size();
for (int k = 0; k < b;k++) {
cartForm.setCartId(alipayForm.getCartFormList().get(k).getCartId());
boolean result = cartService.editStatus(cartForm);
if(!result) {
throw new SQLException("修改支付狀態失敗!");
}
}
if(device.isNormal()) {
model.addAttribute("sHtmlText", alipayRequestWeb(alipayForm));
} else {
model.addAttribute("sHtmlText", alipayRequestMobile(alipayForm));
}
return "manager/charge/alipay";
}