PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/penuel/mythopoet/controllers/OrderController.java

https://gitlab.com/tycoon/mythopoet
Java | 329 lines | 252 code | 30 blank | 47 comment | 37 complexity | f0eff396fc24f1806cdd0d310d3969b9 MD5 | raw file
  1. package com.penuel.mythopoet.controllers;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.penuel.mythopoet.annotation.LoginRequired;
  4. import com.penuel.mythopoet.constants.PoetConstants;
  5. import com.penuel.mythopoet.model.CartDetail;
  6. import com.penuel.mythopoet.model.Item;
  7. import com.penuel.mythopoet.model.ItemSku;
  8. import com.penuel.mythopoet.model.Orders;
  9. import com.penuel.mythopoet.modelView.OrderView;
  10. import com.penuel.mythopoet.service.*;
  11. import com.penuel.mythopoet.utils.ResponseUtil;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.apache.commons.lang3.math.NumberUtils;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.ui.Model;
  19. import org.springframework.util.CollectionUtils;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.servlet.http.HttpServletRequest;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. /**
  26. * OrderController Created with mythopoet.
  27. * User: penuel (penuel.leo@gmail.com)
  28. * Date: 15/4/1 上午8:51
  29. * Desc:
  30. */
  31. @Controller
  32. @RequestMapping( "order" )
  33. @LoginRequired
  34. public class OrderController {
  35. private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);
  36. @Autowired
  37. private OrderService orderService;
  38. @Autowired
  39. private OrderDetailService orderDetailService;
  40. @Autowired
  41. private ItemSkuService itemSkuService;
  42. @Autowired
  43. private CartDetailService cartDetailService;
  44. @Autowired
  45. private ItemService itemService;
  46. @Autowired
  47. private CneeAddressService cneeAddressService;
  48. @Autowired
  49. private UserService userService;
  50. @Autowired
  51. private TicketServcie ticketServcie;
  52. /**
  53. * 获取订单列表
  54. *
  55. * @param model
  56. * @param userId 用户ID
  57. * @param status 状态 {@see PoetConstants}
  58. * -1=全部,0=待支付 1=待收货 4=已完成 9=已取消
  59. * @param pageNum
  60. * @param pageSize
  61. * @return
  62. */
  63. @RequestMapping( value = "/list", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  64. public String list(Model model, @CookieValue( "userId" ) Long userId, @RequestParam( value = "status", defaultValue = "0" ) int status,
  65. @RequestParam( "pageNum" ) int pageNum, @RequestParam( "pageSize" ) int pageSize) {
  66. List<OrderView> orderViewList = null;
  67. try {
  68. List<Orders> orderList;
  69. LOGGER.info("OrderController.list userId=" + userId + ",status=" + status + ",pageNum=" + pageNum + ",pageSize=" + pageSize);
  70. if ( status == PoetConstants.Order_VIEW_WAIT_REC ) {
  71. orderList = orderService.getWaitRecOrderList(userId, (pageNum - 1) * pageSize, pageSize);
  72. } else if ( status == PoetConstants.Order_VIEW_ALL ) {
  73. orderList = orderService.getAllByUserId(userId, (pageNum - 1) * pageSize, pageSize);
  74. } else {
  75. orderList = orderService.listByUserIdAndStatus(userId, status, (pageNum - 1) * pageSize, pageSize);
  76. }
  77. if ( !CollectionUtils.isEmpty(orderList) ) {
  78. orderViewList = new ArrayList<OrderView>();
  79. OrderView orderView;
  80. for ( Orders orders : orderList ) {
  81. orderView = orderService.buildOrderViewByOrder(orders);
  82. orderViewList.add(orderView);
  83. }
  84. }
  85. LOGGER.info("OrderController.list Sucess: userId=" + userId + ",status=" + status + ",pageNum=" + pageNum + ",pageSize=" + pageSize+",orderViewList="+JSONObject.toJSONString(orderViewList));
  86. } catch ( Exception e ) {
  87. LOGGER.error("OrderController.list Error:userId=" + userId + ",status=" + status + ",pageNum=" + pageNum + ",pageSize=" + pageSize, e);
  88. return ResponseUtil.result(1, "获取订单列表失败", null);
  89. }
  90. return ResponseUtil.result(0, "OK", orderViewList);
  91. }
  92. @RequestMapping( value = "/{orderId}", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  93. public String detail(Model model, @CookieValue( "userId" ) Long userId, @PathVariable Long orderId) {
  94. OrderView orderView;
  95. try {
  96. LOGGER.info("OrderController.detail userId=" + userId + ",orderId=" + orderId);
  97. Orders order = orderService.getById(userId, orderId);
  98. orderView = orderService.buildOrderViewByOrder(order);
  99. } catch ( Exception e ) {
  100. LOGGER.error("OrderController.detail Error:userId=" + userId + ",orderId=" + orderId, e);
  101. return ResponseUtil.result(1, "获取订单详情失败", null);
  102. }
  103. return ResponseUtil.result(0, "OK", orderView);
  104. }
  105. @RequestMapping( value = "/{orderId}/delete", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  106. public String deleteOrder(Model model, @CookieValue( "userId" ) Long userId, @PathVariable Long orderId) {
  107. try {
  108. LOGGER.info("OrderController.delete userId=" + userId + ",orderId=" + orderId);
  109. int result = orderService.deleteById(userId, orderId);
  110. if ( result < 1 ){
  111. return ResponseUtil.result(1, "删除失败", null);
  112. }
  113. } catch ( Exception e ) {
  114. LOGGER.error("OrderController.detail Error:userId=" + userId + ",orderId=" + orderId, e);
  115. return ResponseUtil.result(1, "删除订单失败", null);
  116. }
  117. return ResponseUtil.result(0, "OK", null);
  118. }
  119. /**
  120. * 立即购买,生成订单
  121. *
  122. * @param model
  123. * @param userId
  124. * @param itemId
  125. * @param count
  126. * @param props
  127. * @param payType 0=微信支付 5=银行转账
  128. * @return
  129. */
  130. @RequestMapping( value = "/add", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  131. public String addOrder(Model model, @CookieValue( "userId" ) Long userId, @RequestParam( "itemId" ) long itemId,
  132. @RequestParam( value = "count", defaultValue = "1" ) int count, @RequestParam( "props" ) String props,
  133. @RequestParam( value = "amount", defaultValue = "" ) double amount,
  134. @RequestParam( value = "addressId", defaultValue = "0" ) long addressId, @RequestParam( value = "remark", defaultValue = "" ) String remark,
  135. @RequestParam( value = "payType", defaultValue = "-1" ) int payType,
  136. @RequestParam( value = "discountType", defaultValue = "0" ) int discountType,
  137. @RequestParam( value = "discountAmount", defaultValue = "0" ) double discountAmount) {
  138. OrderView orderView;
  139. try {
  140. LOGGER.info("OrderController.addOrder userId=" + userId + ",itemId=" + itemId + ",count=" + count + ",props=" + props + ",addressId=" + addressId
  141. + ",remark=" + remark);
  142. ItemSku itemSku = itemSkuService.getValidSkuByItemAndProps(itemId, props);
  143. if ( itemSku == null || itemSku.getCount() < count ) {
  144. int haveCount = itemSku == null ? 0 : itemSku.getCount();
  145. return ResponseUtil.result(1, "下手太慢了,商品已被抢光了,剩余" + haveCount + "件", null);
  146. }
  147. Orders order = orderService.addOrder(userId, itemSku, count, addressId, remark, payType,amount,discountType,discountAmount);
  148. orderView = orderService.buildOrderViewByOrder(order);
  149. LOGGER.info("OrderController.addOrder OK userId=" + userId + ",itemId=" + itemId + ",count=" + count + ",props=" + props + ",addressId=" + addressId
  150. + ",remark=" + remark + ",order=" + JSONObject.toJSONString(orderView));
  151. } catch ( Exception e ) {
  152. LOGGER.info("OrderController.addOrder userId=" + userId + ",itemId=" + itemId + ",count=" + count + ",props=" + props + ",addressId=" + addressId
  153. + ",remark=" + remark, e);
  154. return ResponseUtil.result(1, "添加订单失败", null);
  155. }
  156. return ResponseUtil.result(0, "OK", orderView);
  157. }
  158. /**
  159. * 选择购物车商品,提交订单
  160. *
  161. * @param model
  162. * @param userId
  163. * @param cartDetailIds 购物车条目IDs,用,分割
  164. * @param addressId
  165. * @param remark
  166. * @param payType 0=微信支付 5=银行转账
  167. * @return
  168. */
  169. @RequestMapping( value = "/submit", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  170. public String addOrder(Model model, @CookieValue( "userId" ) Long userId,
  171. @RequestParam( "cartDetailIds" ) String cartDetailIds,
  172. @RequestParam( value = "addressId", defaultValue = "0" ) long addressId,
  173. @RequestParam( value = "pword", defaultValue = "" ) String pword,
  174. @RequestParam( value = "remark", defaultValue = "" ) String remark,
  175. @RequestParam( value = "amount", defaultValue = "" ) double amount,
  176. @RequestParam( value = "payType", defaultValue = "-1" ) int payType,
  177. @RequestParam( value = "discountType", defaultValue = "0" ) int discountType,
  178. @RequestParam( value = "discountAmount", defaultValue = "0" ) double discountAmount) {
  179. OrderView orderView;
  180. try {
  181. List<String> cartDetailIdStringList = Arrays.asList(cartDetailIds.split(","));
  182. if ( CollectionUtils.isEmpty(cartDetailIdStringList) ) {
  183. return ResponseUtil.result(1, "未选择任何商品", "");
  184. }
  185. String errMsg = "";
  186. LOGGER.info("OrderController.submit userId=" + userId + ",cartDetailIds=" + cartDetailIds + ",addressId=" + addressId + ",remark=" + remark);
  187. List<CartDetail> cartDetailList = new ArrayList<CartDetail>();
  188. List<Long> cartDetailIdList = new ArrayList<Long>();
  189. for ( String cartDetailIdString : cartDetailIdStringList ) {
  190. CartDetail cartDetail = cartDetailService.getById(NumberUtils.toLong(cartDetailIdString), userId);
  191. if ( cartDetail == null ) {
  192. errMsg = "用户信息异常,请退出重新登陆";
  193. break;
  194. } else {
  195. ItemSku itemSku = itemSkuService.getValidSkuByItemAndProps(cartDetail.getItemId(), cartDetail.getProps());
  196. if ( itemSku == null || itemSku.getCount() < cartDetail.getCount() ) {
  197. int haveCount = itemSku == null ? 0 : itemSku.getCount();
  198. Item item = itemService.getById(cartDetail.getItemId());
  199. errMsg = "下手太慢了,商品【" + item.getName() + "】选择的类型已被抢光了,剩余" + haveCount + "件";
  200. break;
  201. } else {
  202. Item item = itemService.getById(itemSku.getItemId());
  203. cartDetail.setCurrentPrice(item.getPrice());
  204. cartDetailList.add(cartDetail);
  205. cartDetailIdList.add(NumberUtils.toLong(cartDetailIdString));
  206. }
  207. }
  208. }
  209. if ( StringUtils.isNotBlank(errMsg) ) {
  210. return ResponseUtil.result(2, errMsg, "");
  211. }
  212. Orders order = orderService.batchAddOrder(userId, cartDetailList, addressId, remark, payType,amount,discountType,discountAmount);
  213. orderView = orderService.buildOrderViewByOrder(order);
  214. if ( order != null && order.getId() > 0 ) {//下单成功,清空购物车
  215. cartDetailService.deleteByIds(cartDetailIdList);
  216. if(pword!=null||!pword.equals("")){
  217. ticketServcie.haveUsed(order.getId(),pword);//将券状态设置为已经使用
  218. LOGGER.info("OrderController.submit 订单号:"+order.getId()+"券密码:"+pword+" 已使用");
  219. }
  220. }
  221. LOGGER.info(
  222. "OrderController.submit userId=" + userId + ",cartDetailIds=" + cartDetailIds + ",addressId=" + addressId + ",remark=" + remark + ",order="
  223. + JSONObject.toJSONString(orderView));
  224. } catch ( Exception e ) {
  225. LOGGER.info("OrderController.submit userId=" + userId + ",cartDetailIds=" + cartDetailIds + ",addressId=" + addressId + ",remark=" + remark, e);
  226. return ResponseUtil.result(1, "获取订单详情失败", null);
  227. }
  228. return ResponseUtil.result(0, "OK", orderView);
  229. }
  230. @RequestMapping( value = "/{orderId}/updateAddress", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  231. public String updateAddress(Model model, @PathVariable Long orderId, @CookieValue( "userId" ) Long userId,
  232. @RequestParam( value = "addressId", defaultValue = "0" ) long addressId) {
  233. OrderView orderView;
  234. try {
  235. Orders order = orderService.getById(NumberUtils.toLong(userId + ""), NumberUtils.toLong(orderId + ""));
  236. if ( order != null ) {
  237. addressId = orderService.updateAddress(NumberUtils.toLong(userId + ""), NumberUtils.toLong(orderId + ""), addressId);
  238. order.setAddressId(addressId);
  239. }
  240. orderView = orderService.buildOrderViewByOrder(order);
  241. } catch ( Exception e ) {
  242. LOGGER.error("OrderController.updateAddress Error:userId=" + userId + ",orderId=" + orderId + ",addressId=" + addressId, e);
  243. return ResponseUtil.result(1, "更新订单地址失败", null);
  244. }
  245. return ResponseUtil.result(0, "OK", orderView);
  246. }
  247. @RequestMapping( value = "/{orderId}/updateRemark", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  248. public String updateRemark(Model model, @PathVariable Long orderId, @CookieValue( "userId" ) Long userId,
  249. @RequestParam( value = "remark", defaultValue = "" ) String remark) {
  250. OrderView orderView;
  251. try {
  252. LOGGER.info("OrderController.updateRemark userId=" + userId + ",orderId=" + orderId + ",remark=" + remark);
  253. Orders order = orderService.getById(NumberUtils.toLong(userId + ""), NumberUtils.toLong(orderId + ""));
  254. if ( order != null ) {
  255. orderService.updateRemark(NumberUtils.toLong(userId + ""), NumberUtils.toLong(orderId + ""), remark);
  256. order.setRemark(remark);
  257. }
  258. orderView = orderService.buildOrderViewByOrder(order);
  259. } catch ( Exception e ) {
  260. LOGGER.error("OrderController.updateRemark Error:userId=" + userId + ",orderId=" + orderId + ",remark=" + remark, e);
  261. return ResponseUtil.result(1, "更新订单备注失败", null);
  262. }
  263. return ResponseUtil.result(0, "OK", orderView);
  264. }
  265. /**
  266. *
  267. * @param model
  268. * @param orderId
  269. * @param userId
  270. * @param payType 0=微信支付 1=支付宝 5=银行转账
  271. * @return
  272. */
  273. @RequestMapping( value = "/{orderId}/updatePayType", method = RequestMethod.POST, produces = "application/json;charset=utf-8" ) @ResponseBody
  274. public String updatePayType(Model model, @PathVariable Long orderId, @CookieValue( "userId" ) Long userId,
  275. @RequestParam( value = "payType", defaultValue = "0" ) int payType) {
  276. try {
  277. LOGGER.info("OrderController.updatePayType userId=" + userId + ",orderId=" + orderId + ",payType=" + payType);
  278. if ( payType !=0 && payType!=1 && payType!=5 ){
  279. return ResponseUtil.result(1, "不支持的付款方式", null);
  280. }
  281. Orders order = orderService.getById(NumberUtils.toLong(userId + ""), NumberUtils.toLong(orderId + ""));
  282. if ( order != null ) {
  283. orderService.updatePayType(NumberUtils.toLong(userId + ""), NumberUtils.toLong(orderId + ""), payType);
  284. }
  285. } catch ( Exception e ) {
  286. LOGGER.error("OrderController.updatePayType Error:userId=" + userId + ",orderId=" + orderId + ",payType=" + payType, e);
  287. return ResponseUtil.result(1, "更新订单失败", null);
  288. }
  289. return ResponseUtil.result(0, "OK", null);
  290. }
  291. @RequestMapping( value = "/page" )
  292. public String myOrderPage(HttpServletRequest request, Model model) {
  293. return "mine/orders";
  294. }
  295. @RequestMapping("/refund")
  296. public String refund(HttpServletRequest request, Model model) {
  297. return "order/refund";
  298. }
  299. }