基于javaweb+mysql的springboot化妆品商城系统(java+springboot+ssm+mysql+jsp+maven)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot化妆品商城系统(java+springboot+ssm+mysql+jsp+maven)
一、项目简述
本系统主要实现的功能有: 网上商城系统,前台+后台管理,用户注册,登录,上架展示,分组展示,搜索,收货地址管理,购物车管理,添加,购买,个人信息修改。订单查询等等,后台商品管理,分类管理,库存管理,订单管理,评论管理,用户管理,信息修改等等。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: Springboot+ SpringMVC + MyBatis + Jsp + Html+ JavaScript + JQuery + Ajax + maven等等。
/**
* 订单列表页
*/
@Controller
public class ForeOrderController extends BaseController {
@Resource(name = "productService")
private ProductService productService;
@Resource(name = "userService")
private UserService userService;
@Resource(name = "productOrderItemService")
private ProductOrderItemService productOrderItemService;
@Resource(name = "addressService")
private AddressService addressService;
@Resource(name = "categoryService")
private CategoryService categoryService;
@Resource(name = "productImageService")
private ProductImageService productImageService;
@Resource(name = "productOrderService")
private ProductOrderService productOrderService;
@Resource(name = "reviewService")
private ReviewService reviewService;
@Resource(name = "lastIDService")
private LastIDService lastIDService;
//转到前台天猫-订单列表页
@RequestMapping(value = "order", method = RequestMethod.GET)
public String goToPageSimple() {
return "redirect:/order/0/10";
}
@RequestMapping(value = "order/{index}/{count}", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map,
@RequestParam(required = false) Byte status,
@PathVariable("index") Integer index/* 页数 */,
@PathVariable("count") Integer count/* 行数*/) {
logger.info("检查用户是否登录");
Object userId = checkUser(session);
User user;
if (userId != null) {
logger.info("获取用户信息");
user = userService.get(Integer.parseInt(userId.toString()));
map.put("user", user);
/**
* 后台管理-打赏信息页
*/
@Controller
public class RewardController extends BaseController {
@Resource(name = "rewardService")
private RewardService rewardService;
@Resource(name = "reviewService")
private ReviewService reviewService;
@Resource(name = "lastIDService")
private LastIDService lastIDService;
@Resource(name = "userService")
private UserService userService;
@Resource(name = "productService")
private ProductService productService;
//转到后台管理-打赏信息页-ajax
@RequestMapping(value = "admin/reward", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map) {
logger.info("获取前10条打赏信息列表");
PageUtil pageUtil = new PageUtil(0, 10);
List<Review> rewardList = reviewService.getList( null, pageUtil);
for (int i = 0; i < rewardList.size(); i++) {
User use = userService.get(rewardList.get(i).getReview_user().getUser_id());
rewardList.get(i).setReview_user(use);
Product pro = productService.get(rewardList.get(i).getReview_product().getProduct_id());
rewardList.get(i).setReview_product(pro);
}
map.put("rewardList", rewardList);
logger.info("获取评论总数量");
map.put("jsonObject", getChartData(null,null,7));
map.put("productTotal", productTotal);
map.put("userTotal", userTotal);
map.put("orderTotal", orderTotal);
logger.info("转到后台管理-主页");
return "admin/homePage";
}
/**
* 转到后台管理-主页(ajax方式)
* @param session session对象
* @param map 前台传入的Map
* @return 响应数据
* @throws ParseException 转换异常
*/
@RequestMapping(value = "admin/home", method = RequestMethod.GET)
public String goToPageByAjax(HttpSession session, Map<String, Object> map) throws ParseException {
logger.info("获取管理员信息");
Object adminId = checkAdmin(session);
if (adminId == null) {
return "admin/include/loginMessage";
}
Admin admin = adminService.get(null, Integer.parseInt(adminId.toString()));
map.put("admin", admin);
logger.info("获取统计信息");
Integer productTotal = productService.getTotal(null, new Byte[]{0, 2});
Integer userTotal = userService.getTotal(null);
Integer orderTotal = productOrderService.getTotal(null, new Byte[]{3});
logger.info("获取图表信息");
map.put("jsonObject", getChartData(null, null,7));
logger.info("获取图表信息");
map.put("jsonObject", getChartData(null,null,7));
map.put("productTotal", productTotal);
map.put("userTotal", userTotal);
map.put("orderTotal", orderTotal);
logger.info("转到后台管理-主页-ajax方式");
return "admin/homeManagePage";
}
/**
* 按日期查询图表数据(ajax方式)
* @param beginDate 开始日期
* @param endDate 结束日期
* @return 响应数据
* @throws ParseException 转换异常
beginDate = time.parse(time.format(beginDate));
endDate = timeSpecial.parse(time.format(endDate) + " 23:59:59");
}
logger.info("根据订单状态分类");
//未付款订单数统计数组
int[] orderUnpaidArray = new int[7];
//未发货订单数统计叔祖
int[] orderNotShippedArray = new int[7];
//未确认订单数统计数组
int[] orderUnconfirmedArray = new int[7];
//交易成功订单数统计数组
int[] orderSuccessArray = new int[7];
//总交易订单数统计数组
int[] orderTotalArray = new int[7];
logger.info("从数据库中获取统计的订单集合数据");
List<OrderGroup> orderGroupList = productOrderService.getTotalByDate(beginDate, endDate);
//初始化日期数组
JSONArray dateStr = new JSONArray(days);
//按指定的天数进行循环
for (int i = 0; i < days; i++) {
//格式化日期串(MM/dd)并放入日期数组中
Calendar cal = Calendar.getInstance();
cal.setTime(beginDate);
cal.add(Calendar.DATE, i);
String formatDate = time2.format(cal.getTime());
dateStr.add(formatDate);
//该天的订单总数
int orderCount = 0;
//循环订单集合数据的结果集
for(int j = 0; j < orderGroupList.size(); j++){
OrderGroup orderGroup = orderGroupList.get(j);
//如果该订单日期与当前日期一致
if(orderGroup.getProductOrder_pay_date().equals(formatDate)){
//从结果集中移除数据
orderGroupList.remove(j);
//根据订单状态将统计结果存入对应的订单状态数组中
switch (orderGroup.getProductOrder_status()) {
case 0:
//未付款订单
orderUnpaidArray[i] = orderGroup.getProductOrder_count();
break;
case 1:
//未发货订单
orderNotShippedArray[i] = orderGroup.getProductOrder_count();
break;
case 2:
//未确认订单
orderUnconfirmedArray[i] = orderGroup.getProductOrder_count();
break;
case 3:
//交易成功订单
orderSuccessArray[i] = orderGroup.getProductOrder_count();
object.put("success", false);
return object.toJSONString();
}
JSONObject orderItemString = JSON.parseObject(orderItemMap);
Set<String> orderItemIDSet = orderItemString.keySet();
if (orderItemIDSet.size() > 0) {
logger.info("更新产品订单项数量");
for (String key : orderItemIDSet) {
ProductOrderItem productOrderItem = productOrderItemService.get(Integer.valueOf(key));
if (productOrderItem == null || !productOrderItem.getProductOrderItem_user().getUser_id().equals(userId)) {
logger.warn("订单项为空或用户状态不一致!");
object.put("success", false);
return object.toJSONString();
}
if (productOrderItem.getProductOrderItem_order() != null) {
logger.warn("用户订单项不属于购物车,回到购物车页");
return "redirect:/cart";
}
Short number = Short.valueOf(orderItemString.getString(key));
if (number <= 0 || number > 500) {
logger.warn("订单项产品数量不合法!");
object.put("success", false);
return object.toJSONString();
}
double price = productOrderItem.getProductOrderItem_price() / productOrderItem.getProductOrderItem_number();
Boolean yn = productOrderItemService.update(new ProductOrderItem().setProductOrderItem_id(Integer.valueOf(key)).setProductOrderItem_number(number).setProductOrderItem_price(number * price));
if (!yn) {
throw new RuntimeException();
}
}
Object[] orderItemIDArray = orderItemIDSet.toArray();
object.put("success", true);
object.put("orderItemIDArray", orderItemIDArray);
return object.toJSONString();
} else {
logger.warn("无订单项可以处理");
object.put("success", false);
return object.toJSONString();
}
}
//创建新订单-单订单项-ajax
@ResponseBody
@RequestMapping(value = "order", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
public String createOrderByOne(HttpSession session, Map<String, Object> map, HttpServletResponse response,
@RequestParam String addressId,
@RequestParam String cityAddressId,
@RequestParam String districtAddressId,
@RequestParam String productOrder_detail_address,
}
logger.info("订单地址字符串:{}", builder);
order.setProductOrder_detail_address(builder.toString());
logger.info("获取订单详情-用户信息");
order.setProductOrder_user(userService.get(order.getProductOrder_user().getUser_id()));
logger.info("获取订单详情-订单项信息");
List<ProductOrderItem> productOrderItemList = productOrderItemService.getListByOrderId(oid, null);
if (productOrderItemList != null) {
logger.info("获取订单详情-订单项对应的产品信息");
for (ProductOrderItem productOrderItem : productOrderItemList) {
Integer productId = productOrderItem.getProductOrderItem_product().getProduct_id();
logger.info("获取产品ID为{}的产品信息", productId);
Product product = productService.get(productId);
if (product != null) {
logger.info("获取产品ID为{}的第一张预览图片信息", productId);
product.setSingleProductImageList(productImageService.getList(productId, (byte) 0, new PageUtil(0, 1)));
}
productOrderItem.setProductOrderItem_product(product);
}
}
order.setProductOrderItemList(productOrderItemList);
map.put("order", order);
logger.info("转到后台管理-订单详情页-ajax方式");
return "admin/include/orderDetails";
}
//更新订单信息-ajax
@ResponseBody
@RequestMapping(value = "admin/order/{order_id}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
public String updateOrder(@PathVariable("order_id") String order_id) {
JSONObject jsonObject = new JSONObject();
logger.info("整合订单信息");
ProductOrder productOrder = new ProductOrder()
.setProductOrder_id(Integer.valueOf(order_id))
.setProductOrder_status((byte) 2)
.setProductOrder_delivery_date(new Date());
logger.info("更新订单信息,订单ID值为:{}", order_id);
boolean yn = productOrderService.update(productOrder);
if (yn) {
logger.info("更新成功!");
jsonObject.put("success", true);
} else {
logger.info("更新失败!事务回滚");
jsonObject.put("success", false);
/**
* 订单列表页
*/
@Controller
public class ForeOrderController extends BaseController {
@Resource(name = "productService")
private ProductService productService;
@Resource(name = "userService")
private UserService userService;
@Resource(name = "productOrderItemService")
private ProductOrderItemService productOrderItemService;
@Resource(name = "addressService")
private AddressService addressService;
@Resource(name = "categoryService")
private CategoryService categoryService;
@Resource(name = "productImageService")
private ProductImageService productImageService;
@Resource(name = "productOrderService")
private ProductOrderService productOrderService;
@Resource(name = "reviewService")
private ReviewService reviewService;
@Resource(name = "lastIDService")
private LastIDService lastIDService;
//转到前台天猫-订单列表页
@RequestMapping(value = "order", method = RequestMethod.GET)
public String goToPageSimple() {
return "redirect:/order/0/10";
}
@RequestMapping(value = "order/{index}/{count}", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map,
@RequestParam(required = false) Byte status,
@PathVariable("index") Integer index/* 页数 */,
@PathVariable("count") Integer count/* 行数*/) {
logger.info("检查用户是否登录");
}
//更新管理员信息
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@ResponseBody
@RequestMapping(value = "admin/account/{admin_id}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
public String updateAdmin(HttpSession session, @RequestParam String admin_nickname/*管理员昵称*/,
@RequestParam(required = false) String admin_password/*管理员当前密码*/,
@RequestParam(required = false) String admin_newPassword/*管理员新密码*/,
@RequestParam(required = false) String admin_profile_picture_src/*管理员头像路径*/,
@PathVariable("admin_id") String admin_id/*管理员编号*/) {
logger.info("获取管理员信息");
Object adminId = checkAdmin(session);
if (adminId == null) {
return "admin/include/loginMessage";
}
JSONObject jsonObject = new JSONObject();
Admin putAdmin = new Admin();
putAdmin.setAdmin_id(Integer.valueOf(admin_id));
putAdmin.setAdmin_nickname(admin_nickname);
if (admin_password != null && !"".equals(admin_password) && admin_newPassword != null && !"".equals(admin_newPassword)) {
logger.info("获取需要修改的管理员信息");
Admin admin = adminService.get(null, Integer.valueOf(adminId.toString()));
if (adminService.login(admin.getAdmin_name(), admin_password) != null) {
logger.info("原密码正确");
putAdmin.setAdmin_password(admin_newPassword);
} else {
logger.info("原密码错误,返回错误信息");
jsonObject.put("success", false);
jsonObject.put("message", "原密码输入有误!");
return jsonObject.toJSONString();
}
}
if (admin_profile_picture_src != null && !"".equals(admin_profile_picture_src)) {
logger.info("管理员头像路径为{}", admin_profile_picture_src);
putAdmin.setAdmin_profile_picture_src(admin_profile_picture_src.substring(admin_profile_picture_src.lastIndexOf("/") + 1));
}
logger.info("更新管理员信息,管理员ID值为:{}", admin_id);
Boolean yn = adminService.update(putAdmin);
if (yn) {
logger.info("更新成功!");
jsonObject.put("success", true);
session.removeAttribute("adminId");
session.invalidate();
logger.info("登录信息已清除");
} else {
jsonObject.put("success", false);
logger.warn("更新失败!事务回滚");
@RequestParam(required = false) Double product_sale_price/* 产品促销价 */,
@RequestParam(required = false) Double product_price/* 产品原价 */,
@RequestParam(required = false) Byte[] product_isEnabled_array/* 产品状态数组 */,
@RequestParam(required = false) String orderBy/* 排序字段 */,
@RequestParam(required = false,defaultValue = "true") Boolean isDesc/* 是否倒序 */,
@PathVariable Integer index/* 页数 */,
@PathVariable Integer count/* 行数 */) throws UnsupportedEncodingException {
//移除不必要条件
if (product_isEnabled_array != null && (product_isEnabled_array.length <= 0 || product_isEnabled_array.length >=3)) {
product_isEnabled_array = null;
}
if (category_id != null && category_id == 0) {
category_id = null;
}
if (product_name != null) {
//如果为非空字符串则解决中文乱码:URLDecoder.decode(String,"UTF-8");
product_name = "".equals(product_name) ? null : URLDecoder.decode(product_name, "UTF-8");
}
if (orderBy != null && "".equals(orderBy)) {
orderBy = null;
}
//封装查询条件
Product product = new Product()
.setProduct_name(product_name)
.setProduct_category(new Category().setCategory_id(category_id))
.setProduct_price(product_price)
.setProduct_sale_price(product_sale_price);
OrderUtil orderUtil = null;
if (orderBy != null) {
logger.info("根据{}排序,是否倒序:{}",orderBy,isDesc);
orderUtil = new OrderUtil(orderBy, isDesc);
}
JSONObject object = new JSONObject();
logger.info("按条件获取第{}页的{}条产品", index + 1, count);
PageUtil pageUtil = new PageUtil(index, count);
List<Product> productList = productService.getList(product, product_isEnabled_array, orderUtil, pageUtil);
object.put("productList", JSONArray.parseArray(JSON.toJSONString(productList)));
logger.info("按条件获取产品总数量");
Integer productCount = productService.getTotal(product, product_isEnabled_array);
object.put("productCount", productCount);
logger.info("获取分页信息");
pageUtil.setTotal(productCount);
object.put("totalPage", pageUtil.getTotalPage());
object.put("pageUtil", pageUtil);
Object userId = checkUser(session);
User user;
if (userId != null) {
logger.info("获取用户信息");
user = userService.get(Integer.parseInt(userId.toString()));
map.put("user", user);
} else {
return "redirect:/login";
}
logger.info("获取订单项信息");
ProductOrderItem orderItem = productOrderItemService.get(orderItem_id);
if (orderItem == null) {
logger.warn("订单项不存在,返回订单页");
return "redirect:/order/0/10";
}
if (!orderItem.getProductOrderItem_user().getUser_id().equals(userId)) {
logger.warn("订单项与用户不匹配,返回订单页");
return "redirect:/order/0/10";
}
if (orderItem.getProductOrderItem_order() == null) {
logger.warn("订单项状态有误,返回订单页");
return "redirect:/order/0/10";
}
ProductOrder order = productOrderService.get(orderItem.getProductOrderItem_order().getProductOrder_id());
if (order == null || order.getProductOrder_status() != 3) {
logger.warn("订单项状态有误,返回订单页");
return "redirect:/order/0/10";
}
if (reviewService.getTotalByOrderItemId(orderItem_id) > 0) {
logger.warn("订单项所属商品已被评价,返回订单页");
return "redirect:/order/0/10";
}
logger.info("获取订单项所属产品信息和产品评论信息");
Product product = productService.get(orderItem.getProductOrderItem_product().getProduct_id());
product.setSingleProductImageList(productImageService.getList(product.getProduct_id(), (byte) 0, new PageUtil(0, 1)));
orderItem.setProductOrderItem_product(product);
map.put("orderItem", orderItem);
logger.info("转到前台天猫-评论添加页");
return "fore/addReview";
}
//添加一条评论
@RequestMapping(value = "review", method = RequestMethod.POST)
public String addReview(HttpSession session, Map<String, Object> map,
@RequestParam Integer orderItem_id,
@RequestParam String review_content) throws UnsupportedEncodingException {
logger.info("检查用户是否登录");
Object userId = checkUser(session);
public String orderSuccess(HttpSession session, @PathVariable("order_code") String order_code) {
JSONObject object = new JSONObject();
logger.info("检查用户是否登录");
Object userId = checkUser(session);
if (userId == null) {
object.put("success", false);
object.put("url", "/login");
return object.toJSONString();
}
logger.info("------验证订单信息------");
logger.info("查询订单是否存在");
ProductOrder order = productOrderService.getByCode(order_code);
if (order == null) {
logger.warn("订单不存在,返回订单列表页");
object.put("success", false);
object.put("url", "/order/0/10");
return object.toJSONString();
}
logger.info("验证订单状态");
if (order.getProductOrder_status() != 2) {
logger.warn("订单状态不正确,返回订单列表页");
object.put("success", false);
object.put("url", "/order/0/10");
return object.toJSONString();
}
logger.info("验证用户与订单是否一致");
if (order.getProductOrder_user().getUser_id() != Integer.parseInt(userId.toString())) {
logger.warn("用户与订单信息不一致,返回订单列表页");
object.put("success", false);
object.put("url", "/order/0/10");
return object.toJSONString();
}
logger.info("更新订单信息");
ProductOrder productOrder = new ProductOrder()
.setProductOrder_id(order.getProductOrder_id())
.setProductOrder_status((byte) 3)
.setProductOrder_confirm_date(new Date());
boolean yn = productOrderService.update(productOrder);
if (yn) {
object.put("success", true);
} else {
object.put("success", false);
}
return object.toJSONString();
}
//更新订单信息为交易关闭-ajax
productList = productService.getList(product, new Byte[]{0, 2}, null, pageUtil);
logger.info("按条件获取产品总数量");
productCount = productService.getTotal(product, new Byte[]{0, 2});
}
logger.info("获取商品列表的对应信息");
for (Product p : productList) {
p.setSingleProductImageList(productImageService.getList(p.getProduct_id(), (byte) 0, null));
p.setProduct_category(categoryService.get(p.getProduct_category().getCategory_id()));
}
logger.info("获取分类列表");
List<Category> categoryList = categoryService.getList(null, new PageUtil(0, 5));
logger.info("获取分页信息");
pageUtil.setTotal(productCount);
map.put("categoryList", categoryList);
map.put("totalPage", pageUtil.getTotalPage());
map.put("pageUtil", pageUtil);
map.put("productList", productList);
map.put("searchValue", searchValue);
map.put("searchType", searchType);
logger.info("转到前台天猫-产品搜索列表页");
return "fore/productListPage";
}
//产品高级查询
@RequestMapping(value = "product/{index}/{count}", method = RequestMethod.GET)
public String searchProduct(HttpSession session, Map<String, Object> map,
@PathVariable("index") Integer index/* 页数 */,
@PathVariable("count") Integer count/* 行数*/,
@RequestParam(value = "category_id", required = false) Integer category_id/* 分类ID */,
@RequestParam(value = "product_name", required = false) String product_name/* 产品名称 */,
@RequestParam(required = false) String orderBy/* 排序字段 */,
@RequestParam(required = false, defaultValue = "true") Boolean isDesc/* 是否倒序 */) {
logger.info("整合搜索信息");
Product product = new Product();
OrderUtil orderUtil = null;
String searchValue = null;
Integer searchType = null;
if (category_id != null) {
product.setProduct_category(new Category().setCategory_id(category_id));
searchType = category_id;
}
if (product_name != null) {
.setProductOrder_status((byte) 4);
boolean yn = productOrderService.update(productOrder);
if (yn) {
object.put("success", true);
} else {
object.put("success", false);
}
return object.toJSONString();
}
//更新购物车订单项数量-ajax
@ResponseBody
@RequestMapping(value = "orderItem", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
public String updateOrderItem(HttpSession session, Map<String, Object> map, HttpServletResponse response,
@RequestParam String orderItemMap) {
JSONObject object = new JSONObject();
logger.info("检查用户是否登录");
Object userId = checkUser(session);
if (userId == null) {
object.put("success", false);
return object.toJSONString();
}
JSONObject orderItemString = JSON.parseObject(orderItemMap);
Set<String> orderItemIDSet = orderItemString.keySet();
if (orderItemIDSet.size() > 0) {
logger.info("更新产品订单项数量");
for (String key : orderItemIDSet) {
ProductOrderItem productOrderItem = productOrderItemService.get(Integer.valueOf(key));
if (productOrderItem == null || !productOrderItem.getProductOrderItem_user().getUser_id().equals(userId)) {
logger.warn("订单项为空或用户状态不一致!");
object.put("success", false);
return object.toJSONString();
}
if (productOrderItem.getProductOrderItem_order() != null) {
logger.warn("用户订单项不属于购物车,回到购物车页");
return "redirect:/cart";
}
Short number = Short.valueOf(orderItemString.getString(key));
product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
product.setSingleProductImageList(productImageService.getList(product.getProduct_id(), (byte) 0, new PageUtil(0, 1)));
orderItem.setProductOrderItem_product(product);
orderTotalPrice += orderItem.getProductOrderItem_price();
}
String addressId = "110000";
String cityAddressId = "110100";
String districtAddressId = "110101";
String detailsAddress = null;
String order_post = null;
String order_receiver = null;
String order_phone = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String cookieName = cookie.getName();
String cookieValue = cookie.getValue();
switch (cookieName) {
case "addressId":
addressId = cookieValue;
break;
case "cityAddressId":
cityAddressId = cookieValue;
break;
case "districtAddressId":
districtAddressId = cookieValue;
break;
case "order_post":
order_post = URLDecoder.decode(cookieValue, "UTF-8");
break;
case "order_receiver":
order_receiver = URLDecoder.decode(cookieValue, "UTF-8");
break;
case "order_phone":
order_phone = URLDecoder.decode(cookieValue, "UTF-8");
break;
case "detailsAddress":
detailsAddress = URLDecoder.decode(cookieValue, "UTF-8");
break;
}
}
}
logger.info("获取省份信息");
List<Address> addressList = addressService.getRoot();
logger.info("获取addressId为{}的市级地址信息", addressId);
List<Address> cityAddress = addressService.getList(null, addressId);
logger.info("获取cityAddressId为{}的区级地址信息", cityAddressId);
List<Address> districtAddress = addressService.getList(null, cityAddressId);
map.put("orderItemList", orderItemList);
map.put("addressList", addressList);
map.put("cityList", cityAddress);
map.put("districtList", districtAddress);
}
package com.xq.tmall.controller.admin;
/**
* 后台管理-产品页
*/
@Controller
public class ProductController extends BaseController{
@Resource(name = "categoryService")
private CategoryService categoryService;
@Resource(name = "productService")
private ProductService productService;
@Resource(name = "productImageService")
private ProductImageService productImageService;
@Resource(name = "propertyService")
private PropertyService propertyService;
@Resource(name = "propertyValueService")
private PropertyValueService propertyValueService;
@Resource(name = "lastIDService")
private LastIDService lastIDService;
//转到后台管理-产品页-ajax
@RequestParam String productOrder_receiver,
@RequestParam String productOrder_mobile,
@RequestParam String orderItemJSON) throws UnsupportedEncodingException {
JSONObject object = new JSONObject();
logger.info("检查用户是否登录");
Object userId = checkUser(session);
if (userId == null) {
object.put("success", false);
object.put("url", "/login");
return object.toJSONString();
}
JSONObject orderItemMap = JSONObject.parseObject(orderItemJSON);
Set<String> orderItem_id = orderItemMap.keySet();
List<ProductOrderItem> productOrderItemList = new ArrayList<>(3);
if (orderItem_id.size() > 0) {
for (String id : orderItem_id) {
ProductOrderItem orderItem = productOrderItemService.get(Integer.valueOf(id));
if (orderItem == null || !orderItem.getProductOrderItem_user().getUser_id().equals(userId)) {
logger.warn("订单项为空或用户状态不一致!");
object.put("success", false);
object.put("url", "/cart");
return object.toJSONString();
}
Integer product_stock_count = 0;
try {
product_stock_count = this.productService.get(orderItem.getProductOrderItem_product().getProduct_id()).getProduct_stock_count();
} catch (Exception e) {
e.printStackTrace();
}
// if(product_stock_count
// object.put("success", false);
// object.put("url", "/not");
// return object.toJSONString();
// }
if (orderItem.getProductOrderItem_order() != null) {
logger.warn("用户订单项不属于购物车,回到购物车页");
object.put("success", false);
object.put("url", "/cart");
return object.toJSONString();
}
boolean yn = productOrderItemService.update(new ProductOrderItem().setProductOrderItem_id(Integer.valueOf(id)).setProductOrderItem_userMessage(orderItemMap.getString(id)));
if (!yn) {
throw new RuntimeException();
}
orderItem.setProductOrderItem_product(productService.get(orderItem.getProductOrderItem_product().getProduct_id()));
productOrderItemList.add(orderItem);
}
} else {
object.put("success", false);
object.put("url", "/cart");
return object.toJSONString();
}
logger.info("将收货地址等相关信息存入Cookie中,便于下次使用");
@RequestParam Double product_price/* 产品原价 */,
@RequestParam Byte product_isEnabled/* 产品状态 */,
@RequestParam String propertyAddJson/* 产品添加属性JSON */,
@RequestParam String propertyUpdateJson/* 产品更新属性JSON */,
@RequestParam Integer product_stock_count/* 产品库存 */,
@RequestParam(required = false) Integer[] propertyDeleteList/* 产品删除属性ID数组 */,
@RequestParam(required = false) String[] productSingleImageList/*产品预览图片名称数组*/,
@RequestParam(required = false) String[] productDetailsImageList/*产品详情图片名称数组*/,
@PathVariable("product_id") Integer product_id/* 产品ID */) {
JSONObject jsonObject = new JSONObject();
logger.info("整合产品信息");
Product product = new Product()
.setProduct_id(product_id)
.setProduct_name(product_name)
.setProduct_title(product_title)
.setProduct_category(new Category().setCategory_id(product_category_id))
.setProduct_sale_price(product_sale_price)
.setProduct_price(product_price)
.setProduct_isEnabled(product_isEnabled)
.setProduct_create_date(new Date());
product.setProduct_stock_count(product_stock_count);
logger.info("更新产品信息,产品ID值为:{}", product_id);
boolean yn = productService.update(product);
if (!yn) {
logger.info("产品信息更新失败!事务回滚");
jsonObject.put("success", false);
throw new RuntimeException();
}
logger.info("产品信息更新成功!");
JSONObject object = JSON.parseObject(propertyAddJson);
Set<String> propertyIdSet = object.keySet();
if (propertyIdSet.size() > 0) {
logger.info("整合产品子信息-需要添加的产品属性");
List<PropertyValue> propertyValueList = new ArrayList<>(5);
for (String key : propertyIdSet) {
String value = object.getString(key);
PropertyValue propertyValue = new PropertyValue()
.setPropertyValue_value(value)
.setPropertyValue_property(new Property().setProperty_id(Integer.valueOf(key)))
.setPropertyValue_product(product);
propertyValueList.add(propertyValue);
}
logger.info("共有{}条需要添加的产品属性数据", propertyValueList.size());
String extension = originalFileName.substring(originalFileName.lastIndexOf('.'));
//生成随机名
String fileName = UUID.randomUUID() + extension;
//获取上传路径
String filePath = session.getServletContext().getRealPath("/") + "res/images/item/adminProfilePicture/" + fileName;
logger.info("文件上传路径:{}", filePath);
JSONObject jsonObject = new JSONObject();
try {
logger.info("文件上传中...");
file.transferTo(new File(filePath));
logger.info("文件上传成功!");
jsonObject.put("success", true);
jsonObject.put("fileName", fileName);
} catch (IOException e) {
logger.warn("文件上传失败!");
e.printStackTrace();
jsonObject.put("success", false);
}
return jsonObject.toJSONString();
}
//更新管理员信息
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@ResponseBody
@RequestMapping(value = "admin/account/{admin_id}", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
public String updateAdmin(HttpSession session, @RequestParam String admin_nickname/*管理员昵称*/,
@RequestParam(required = false) String admin_password/*管理员当前密码*/,
@RequestParam(required = false) String admin_newPassword/*管理员新密码*/,
@RequestParam(required = false) String admin_profile_picture_src/*管理员头像路径*/,
@PathVariable("admin_id") String admin_id/*管理员编号*/) {
logger.info("获取管理员信息");
Object adminId = checkAdmin(session);
if (adminId == null) {
return "admin/include/loginMessage";
}
JSONObject jsonObject = new JSONObject();
Admin putAdmin = new Admin();
putAdmin.setAdmin_id(Integer.valueOf(admin_id));
putAdmin.setAdmin_nickname(admin_nickname);
if (propertyDeleteList != null && propertyDeleteList.length > 0) {
logger.info("整合产品子信息-需要删除的产品属性");
logger.info("共有{}条需要删除的产品属性数据", propertyDeleteList.length);
yn = propertyValueService.deleteList(propertyDeleteList);
if (yn) {
logger.info("产品属性删除成功!");
} else {
logger.warn("产品属性删除失败!事务回滚");
jsonObject.put("success", false);
throw new RuntimeException();
}
}
if (productSingleImageList != null && productSingleImageList.length > 0) {
logger.info("整合产品子信息-产品预览图片");
List<ProductImage> productImageList = new ArrayList<>(5);
for (String imageName : productSingleImageList) {
productImageList.add(new ProductImage()
.setProductImage_type((byte) 0)
.setProductImage_src(imageName.substring(imageName.lastIndexOf("/") + 1))
.setProductImage_product(product)
);
}
logger.info("共有{}条产品预览图片数据", productImageList.size());
yn = productImageService.addList(productImageList);
if (yn) {
logger.info("产品预览图片添加成功!");
} else {
logger.warn("产品预览图片添加失败!事务回滚");
jsonObject.put("success", false);
throw new RuntimeException();
}
}
if (productDetailsImageList != null && productDetailsImageList.length > 0) {
logger.info("整合产品子信息-产品详情图片");
List<ProductImage> productImageList = new ArrayList<>(5);
for (String imageName : productDetailsImageList) {
productImageList.add(new ProductImage()
.setProductImage_type((byte) 1)
.setProductImage_src(imageName.substring(imageName.lastIndexOf("/") + 1))
.setProductImage_product(product)
);
JSONObject object = new JSONObject();
logger.info("获取productImage_id为{}的产品图片信息",productImage_id);
ProductImage productImage = productImageService.get(productImage_id);
logger.info("删除产品图片");
Boolean yn = productImageService.deleteList(new Integer[]{productImage_id});
if (yn) {
logger.info("删除图片成功!");
object.put("success", true);
} else {
logger.warn("删除图片失败!事务回滚");
object.put("success", false);
throw new RuntimeException();
}
return object.toJSONString();
}
//上传产品图片-ajax
@ResponseBody
@RequestMapping(value = "admin/uploadProductImage", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
public String uploadProductImage(@RequestParam MultipartFile file, @RequestParam String imageType, HttpSession session) {
String originalFileName = file.getOriginalFilename();
logger.info("获取图片原始文件名:{}", originalFileName);
String extension = originalFileName.substring(originalFileName.lastIndexOf('.'));
String filePath;
String fileName = UUID.randomUUID() + extension;
if ("single".equals(imageType)) {
filePath = session.getServletContext().getRealPath("/") + "res/images/item/productSinglePicture/" + fileName;
} else {
filePath = session.getServletContext().getRealPath("/") + "res/images/item/productDetailsPicture/" + fileName;
}
logger.info("文件上传路径:{}", filePath);
JSONObject object = new JSONObject();
try {
logger.info("文件上传中...");
file.transferTo(new File(filePath));
logger.info("文件上传完成");
object.put("success", true);
object.put("fileName", fileName);
} catch (IOException e) {
logger.warn("文件上传失败!");
e.printStackTrace();
object.put("success", false);
}
return object.toJSONString();
}
}
package com.xq.tmall.controller.fore;
* 按日期查询图表数据(ajax方式)
* @param beginDate 开始日期
* @param endDate 结束日期
* @return 响应数据
* @throws ParseException 转换异常
*/
@ResponseBody
@RequestMapping(value = "admin/home/charts", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public String getChartDataByDate(@RequestParam(required = false) String beginDate, @RequestParam(required = false) String endDate) throws ParseException {
if (beginDate != null && endDate != null) {
//转换日期格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return getChartData(simpleDateFormat.parse(beginDate), simpleDateFormat.parse(endDate),7).toJSONString();
} else {
return getChartData(null, null,7).toJSONString();
}
}
/**
* 按日期获取图表数据
* @param beginDate 开始日期
* @param endDate 结束日期
* @param days 天数
* @return 图表数据的JSON对象
* @throws ParseException 转换异常
*/
private JSONObject getChartData(Date beginDate,Date endDate,int days) throws ParseException {
JSONObject jsonObject = new JSONObject();
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
SimpleDateFormat time2 = new SimpleDateFormat("MM/dd", Locale.UK);
SimpleDateFormat timeSpecial = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.UK);
//如果没有指定开始和结束日期
if (beginDate == null || endDate == null) {
//指定一周前的日期为开始日期
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1-days);
beginDate = time.parse(time.format(cal.getTime()));
//指定当前日期为结束日期
cal = Calendar.getInstance();
endDate = cal.getTime();
} else {
beginDate = time.parse(time.format(beginDate));
endDate = timeSpecial.parse(time.format(endDate) + " 23:59:59");
}
}
logger.info("创建用户对象");
User user = new User()
.setUser_name(user_name)
.setUser_nickname(user_nickname)
.setUser_password(user_password)
.setUser_gender(Byte.valueOf(user_gender))
.setUser_birthday(new SimpleDateFormat("yyyy-MM-dd").parse(user_birthday))
.setUser_address(new Address().setAddress_areaId(user_address))
.setUser_homeplace(new Address().setAddress_areaId("130000"));
logger.info("用户注册");
if (userService.add(user)) {
logger.info("注册成功");
JSONObject object = new JSONObject();
object.put("success", true);
return object.toJSONString();
} else {
throw new RuntimeException();
}
}
}
package com.xq.tmall.controller.fore;
/**
* 打赏信息管理
*/
@Controller
product = productService.get(productOrderItem.getProductOrderItem_product().getProduct_id());
if (product != null) {
product.setSingleProductImageList(productImageService.getList(product.getProduct_id(), (byte) 0, new PageUtil(0, 1)));
}
}
}
map.put("orderItem", productOrderItem);
}
map.put("product", product);
logger.info("转到前台天猫-订单完成页");
return "fore/orderSuccessPage";
}
//转到前台天猫-购物车页
@RequestMapping(value = "cart", method = RequestMethod.GET)
public String goToCartPage(Map<String, Object> map, HttpSession session) {
logger.info("检查用户是否登录");
Object userId = checkUser(session);
User user;
if (userId != null) {
logger.info("获取用户信息");
user = userService.get(Integer.parseInt(userId.toString()));
map.put("user", user);
} else {
return "redirect:/login";
}
logger.info("获取用户购物车信息");
List<ProductOrderItem> orderItemList = productOrderItemService.getListByUserId(Integer.valueOf(userId.toString()), null);
Integer orderItemTotal = 0;
if (orderItemList.size() > 0) {
logger.info("获取用户购物车的商品总数");
orderItemTotal = productOrderItemService.getTotalByUserId(Integer.valueOf(userId.toString()));
logger.info("获取用户购物车内的商品信息");
for (ProductOrderItem orderItem : orderItemList) {
Integer product_id = orderItem.getProductOrderItem_product().getProduct_id();
Product product = productService.get(product_id);
product.setSingleProductImageList(productImageService.getList(product_id, (byte) 0, null));
product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
orderItem.setProductOrderItem_product(product);
}
}
map.put("orderItemList", orderItemList);
map.put("orderItemTotal", orderItemTotal);
logger.info("转到前台天猫-购物车页");
return "fore/productBuyCarPage";
}
//更新订单信息为已支付,待发货-ajax
@ResponseBody
logger.warn("用户订单项获取失败,回到购物车页");
return "redirect:/cart";
}
for (ProductOrderItem orderItem : orderItemList) {
if (orderItem.getProductOrderItem_user().getUser_id() != userId) {
logger.warn("用户订单项与用户不匹配,回到购物车页");
return "redirect:/cart";
}
if (orderItem.getProductOrderItem_order() != null) {
logger.warn("用户订单项不属于购物车,回到购物车页");
return "redirect:/cart";
}
}
logger.info("验证通过,获取订单项的产品信息");
double orderTotalPrice = 0.0;
for (ProductOrderItem orderItem : orderItemList) {
Product product = productService.get(orderItem.getProductOrderItem_product().getProduct_id());
product.setProduct_category(categoryService.get(product.getProduct_category().getCategory_id()));
product.setSingleProductImageList(productImageService.getList(product.getProduct_id(), (byte) 0, new PageUtil(0, 1)));
orderItem.setProductOrderItem_product(product);
orderTotalPrice += orderItem.getProductOrderItem_price();
}
String addressId = "110000";
String cityAddressId = "110100";
String districtAddressId = "110101";
String detailsAddress = null;
String order_post = null;
String order_receiver = null;
String order_phone = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
String cookieName = cookie.getName();
String cookieValue = cookie.getValue();
switch (cookieName) {
case "addressId":
addressId = cookieValue;
break;
case "cityAddressId":
cityAddressId = cookieValue;
break;
case "districtAddressId":
districtAddressId = cookieValue;
break;
case "order_post":
order_post = URLDecoder.decode(cookieValue, "UTF-8");
break;
/**
* 主页
*/
@Controller
public class ForeHomeController extends BaseController {
@Resource(name = "userService")
private UserService userService;
@Resource(name="categoryService")
private CategoryService categoryService;
@Resource(name="productService")
private ProductService productService;
@Resource(name="productImageService")
private ProductImageService productImageService;
//转到前台天猫-主页
@RequestMapping(value = "/", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map) {
logger.info("检查用户是否登录");
Object userId = checkUser(session);
if (userId != null) {
logger.info("获取用户信息");
}
}
package com.xq.tmall.controller.fore;
/**
* 评论信息管理
*/
@Controller
public class ForeReviewController extends BaseController {
@Resource(name = "reviewService")
private ReviewService reviewService;
@Resource(name = "userService")
private UserService userService;
@Resource(name = "productOrderItemService")
private ProductOrderItemService productOrderItemService;
@Resource(name = "productOrderService")
private ProductOrderService productOrderService;
@Resource(name = "productService")
private ProductService productService;
@Resource(name = "productImageService")
private ProductImageService productImageService;
//转到前台天猫-评论添加页
@RequestMapping(value = "review/{orderItem_id}", method = RequestMethod.GET)
public String goToPage(HttpSession session, Map<String, Object> map,
@PathVariable("orderItem_id") Integer orderItem_id) {
logger.info("检查用户是否登录");
Object userId = checkUser(session);
User user;
if (userId != null) {
logger.info("获取用户信息");
user = userService.get(Integer.parseInt(userId.toString()));
map.put("user", user);
} else {
return "redirect:/login";
}
logger.info("获取订单项信息");
ProductOrderItem orderItem = productOrderItemService.get(orderItem_id);