基于javaweb+mysql的springboot+mybatis在线商城水果蔬菜商城果蔬商城(前台、后台)(java+springboot+ssm+freemarker+redis+mysql)

基于javaweb+mysql的springboot+mybatis在线商城水果蔬菜商城果蔬商城(前台、后台)(java+springboot+ssm+freemarker+redis+mysql)

运行环境

Java≥8、MySQL≥5.7

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

该项目分为前台用户和后台管理员两个角色,

用户角色的功能:登录(JWT的token验证)、注册浏览商品、修改个人信息(上传图片)、修改密码、评论商品添加商品到购物车(Redis缓存)、提交订单查看订单收藏商品等等功能。

管理员角色的功能:管理用户信息、管理用户评论信息、管理商品信息、管理订单信息等等功能。

前台:

后台:

技术框架

SpringBoot SpringMVC MyBatis FreeMarker JWT Redis

基于javaweb+mysql的SpringBoot+MyBatis在线商城水果蔬菜商城果蔬商城(前台、后台)(java+springboot+ssm+freemarker+redis+mysql)

					}
					return false;
				}
			}
		}
		return true;
	}
}
package com.demo.controller.admin;

/**
 * 后台管理系统首页控制器

 *
 */
@RequestMapping("/admin/index")
@Controller
public class IndexController {

	/**
	 * 角色添加表单处理
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> add(Role role){
		return roleService.add(role);
	}
	
	/**
	 * 角色编辑表单处理
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> edit(Role role){
		return roleService.edit(role);
	}
	
	/**
	 * 角色删除处理
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> delete(Integer id){
		return roleService.delete(id);
	}
	
	
}
package com.demo.controller.admin;

	 * @param admin
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> add(Admin admin){
		return adminService.add(admin);
	}
	
	/**
	 * 管理员编辑表单处理
	 * @param admin
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> edit(Admin admin,HttpServletRequest request){
		Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
		ResponseVo<Admin> editAdmin = adminService.edit(admin);
		if(editAdmin.getCode().intValue() == CodeMsg.SUCCESS.getCode()) {
			if(loginedAdmin.getId().intValue() == editAdmin.getData().getId().intValue()) {
				//更新权限
				request.getSession().setAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY, editAdmin.getData());
			}
			return ResponseVo.successByMsg(true, "编辑成功!");
		}else {
			CodeMsg codeMsg = new CodeMsg();
			codeMsg.setCode(editAdmin.getCode());
			codeMsg.setMsg(editAdmin.getMsg());
			return ResponseVo.errorByMsg(codeMsg);
		}
	}
	
	/**
	 * 管理员删除处理
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> delete(Integer id){
		return adminService.delete(id);
	}
	
	
	/**
	 * 管理员更改状态处理
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/change_state",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> chageState(Integer id){
        //把订单信息添加到数据库中
        if(orderMapper.insertSelective(order) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_ADD_ERROR);
        }
        //给订单详情对应的订单id赋值
        for(OrderItem or : orderItemList){
            or.setOrderId(order.getId());
        }
        //把订单详情信息添加到数据库中
        if(orderItemMapper.batchInsert(orderItemList) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_ITEM_ADD_ERROR);
        }
        return ResponseVo.success(order.getId());
    }

    @Override
    public Order selectByOrderIdAndUserId(Long userId, Long orderId) {
        return orderMapper.selectByOrderIdAndUserId(userId, orderId);
    }

    @Override
    @Transactional
    public ResponseVo<Boolean> submit(String remark, Long orderId, Long uid, String email) {
        //判断订单是否存在
        Order order = orderMapper.selectByOrderIdAndUserId(uid, orderId);
        if(order == null){
            return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //判断配送地址是否选择
        Address address = addressMapper.selectByUserIdAndFirstSelected(uid, AddressFirstSelectedEnum.YES.getCode());
        if(address == null){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_ADDRESS_EMPTY);
        }
        //判断订单留言长度是否合法
        if(remark.length() > 50){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_REMARK_EXCEED_LENGTH);
        }
        //声明一个自定义错误的CodeMsg
        CodeMsg codeMsg = CodeMsg.ORDER_ERROR;
        //声明一个商品id的Set集合
        Set<Long> productIdSet = new HashSet<>();
        //遍历订单中的商品
        for(OrderItem orderItem : order.getOrderItemList()){
            productIdSet.add(orderItem.getProductId());
        }
        //获取这些选中商品的详细信息
        List<Product> productList = productMapper.selectByProductIdSet(productIdSet);

/**
 * 后台管理系统管理员控制器

 *
 */
@RequestMapping("/admin/admin")
@Controller
public class AdminController {

	@Autowired
	private IMenuService menuService;
	
	@Autowired 
	private MenuMapper menuMapper;
	
	@Autowired
	private AdminMapper adminMapper;
	
	@Autowired
	private IAdminService adminService;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private AuthorityMapper authorityMapper;
	
	/**
	 * 管理员列表页面
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index(Model model,Integer id,String name,HttpServletRequest request,
			@RequestParam(required = false, defaultValue = "1") Integer pageNum,

    @Override
    public ResponseVo<Boolean> add(Address address, HttpServletRequest request) {
        if(address == null){
            return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //获取当前登录用户的id
        String id = (String) request.getAttribute("id");
        address.setUserId(Long.valueOf(id));
        //统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(address);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            return ResponseVo.errorByMsg(validate);
        }
        //判断用户是否已存三个地址,若是,则不让添加
        List<Address> addressByUserId = addressMapper.findAddressByUserId(Long.valueOf(id));
        if(addressByUserId.size() >= 3){
            return ResponseVo.errorByMsg(CodeMsg.ADDRESS_NUM_EXCEED_LIMIT);
        }
        //把地址信息添加到数据库
        if(addressMapper.insertSelective(address) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ADDRESS_ADD_ERROR);
        }
        return ResponseVo.successByMsg(true, "添加地址成功!");
    }

    @Override
    public List<Address> findAddressByUserId(Long userId) {
        return addressMapper.findAddressByUserId(userId);
    }

    @Override
    public ResponseVo<Boolean> setFirstSelected(Long id, HttpServletRequest request) {
        if(id == null){
            return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
        }
        String userId = (String) request.getAttribute("id");
        //先把当前用户所有地址都设置为不是首选
        if(addressMapper.updateFirstSelectedByUserId(Long.valueOf(userId), AddressFirstSelectedEnum.NO.getCode()) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ADDRESS_SET_FIRST_SELECTED_ERROR);
        }
        //根据要设置为订单首选的地址id从数据库中获取地址信息
        Address address = addressMapper.selectByPrimaryKey(id);
        address.setFirstSelected(AddressFirstSelectedEnum.YES.getCode());
        //更新数据库中的信息
        if(addressMapper.updateByPrimaryKeySelective(address) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ADDRESS_SET_FIRST_SELECTED_ERROR);
        }
        return ResponseVo.successByMsg(true, "成功设置该地址为订单首选!");
    }
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if(order == null){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_NOT_EXIST);
        }
        if(orderMapper.updateIsDeletedByOrderId(orderId, isDeleted) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_DELETE_ERROR);
        }
        return ResponseVo.successByMsg(true, "成功删除该订单!");
    }

    @Override
    public ResponseVo<PageInfo> selectByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderMapper.selectAll();
        PageInfo pageInfo = new PageInfo<>(orderList);
        pageInfo.setList(orderList);
        return ResponseVo.success(pageInfo);
    }

    @Override
    public ResponseVo<PageInfo> selectByPageAndContent(Long orderNo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderMapper.selectBySearchContent(orderNo);
        PageInfo pageInfo = new PageInfo<>(orderList);
        pageInfo.setList(orderList);
        return ResponseVo.success(pageInfo);
    }

    @Override
    public Order selectByPrimaryKey(Long id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderItem> getOrderItemByOrderId(Long orderId) {
        return orderMapper.getOrderItemByOrderId(orderId);
    }

    @Override
    public ResponseVo<Boolean> deleteOrder(Long orderId) {
        if(orderId == null){
    }

    @Override
    public ResponseVo<Boolean> updateUserPasswd(String passwd, Long userId) {
        if(StringUtil.isEmpty(passwd) || userId == null){
            return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
        }
        User user = userMapper.selectByPrimaryKey(userId);
        user.setPassword(passwd);
        //统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(user);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            return ResponseVo.errorByMsg(validate);
        }
        //修改数据库中的用户信息
        if(userMapper.updateByPrimaryKeySelective(user) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.USER_PASSWORD_EDIT_ERROR);
        }
        return ResponseVo.successByMsg(true, "修改用户密码成功!");
    }

    @Override
    public ResponseVo<Boolean> deleteUser(Long userId) {
        if(userId == null){
            return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
        }
        User user = userMapper.selectByPrimaryKey(userId);
        if(user == null){
            return ResponseVo.errorByMsg(CodeMsg.USER_NOT_EXIST);
        }
        if(userMapper.deleteByPrimaryKey(userId) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.USER_DELETE_ERROR);
        }
        return ResponseVo.successByMsg(true, "删除用户成功!");
    }

}
package com.demo.controller.common;


    /**
     * 修改密码页面
     * @param model
     * @return
     */
    @RequestMapping(value="/update_passwd",method=RequestMethod.GET)
    public String updatePasswd(Model model) {
        return "home/user/update_passwd";
    }

    /**
     * 我的评论页面
     * @param model
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/comment",method=RequestMethod.GET)
    public String comment(Model model,HttpServletRequest request,
                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                          @RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
    ){
        String uid = (String) request.getAttribute("id");
        model.addAttribute("PageInfo", commentService.selectByPageAndUserId(pageNum, pageSize, Long.valueOf(uid)).getData());
        return "home/user/comment";
    }

    /**
     * 我的订单页面
     * @param model
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/order",method=RequestMethod.GET)
    public String order(Model model, HttpServletRequest request,
                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(required = false, defaultValue = "1") Integer pageSize //每页1个数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String edit(Model model,Integer id) {
		Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
		if(selectByPrimaryKey == null) {
			return "error/404";
		}
		List<Menu> allMenus = menuMapper.selectAll();
		model.addAttribute("FirstMenus",menuService.getFirstMenus(allMenus).getData());
		model.addAttribute("SecondMenus",menuService.getSecondMenus(allMenus).getData());
		model.addAttribute("editMenu",selectByPrimaryKey);
		return "admin/menu/edit";
	}
	
	/**
	 * 菜单添加按钮页面
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/add_button",method=RequestMethod.GET)
	public String add_button(Model model,Integer id) {
		Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
		if(selectByPrimaryKey == null) {
			return "error/404";
		}
		model.addAttribute("secondMenu",selectByPrimaryKey);
		return "admin/menu/add_button";
	}
	
	/**
	 * 菜单添加表单处理
	 * @param menu
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> add(Menu menu){
		return menuService.add(menu);
	}
	
	/**
	 * 菜单编辑表单处理
	 * @param menu
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public ResponseVo<Boolean> edit(Menu menu){
		return menuService.edit(menu);
	}

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ICommentService commentService;

    /**
     * 注册页面
     * @param model
     * @return
     */
    @RequestMapping(value="/register",method=RequestMethod.GET)
    public String register(Model model) {
        return "home/user/register";
    }

    /**
     * 登录页面
     * @param model
     * @return
     */
    @RequestMapping(value="/login",method=RequestMethod.GET)
    public String login(Model model) {
        return "home/user/login";
    }

    /**
     * 个人信息页面
     * @param model
     * @return
     */
    @RequestMapping(value="/info",method=RequestMethod.GET)
    public String info(Model model) {
        return "home/user/info";
    }

    /**
     * 修改密码页面
     * @param model

/**
 * 公用的上传类
 */
@RequestMapping("/upload")
@Controller
public class UploadController {

    private String uploadPhotoSufix = ".jpg,.png,.gif,.jpeg";

    private long uploadPhotoMaxSize = 10240;   //大小1024KB

    private String uploadPhotoPath = System.getProperty("user.dir") + "/src/main/resources/upload/photo/";

    private long uploadAttachmentMaxSize = 204800;   //大小204800KB

    private String uploadAttachmentPath = System.getProperty("user.dir") + "/src/main/resources/upload/attachment/";

    private Logger log = LoggerFactory.getLogger(UploadController.class);

    @Autowired
    private AttachmentMapper attachmentMapper;

    /**
     * 图片统一上传类
     *
     * @param photo
     * @return
     */
    @RequestMapping(value = "/upload_photo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseVo<String> uploadPhoto(@RequestParam(name = "photo", required = true) MultipartFile photo) {
        //判断文件类型是否是图片
        String originalFilename = photo.getOriginalFilename();

/**

 */

/**
 * 用户service接口实现类

 *
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseVo<Boolean> isUsernameExist(User user, Long id) {
        User findByUsername = userMapper.findUserByUsername(user.getUsername());
        if(findByUsername != null) {
            if(!findByUsername.getId().equals(id)) {
                return ResponseVo.success(true); //出现重名
            }

 */

/**
 * 后台管理系统用户控制器

 *
 */
@RequestMapping("/admin/user")
@Controller
public class UserController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @Autowired
    private IUserService userService;

    @Autowired
    private ICommentService commentService;

    /**
     * 用户列表页面
     * @param model
     * @param id
     * @param request
     * @param content
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/index",method= RequestMethod.GET)
    public String index(Model model, Integer id, HttpServletRequest request, String content,
                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
    )  {
        //获取列表展示有关信息
        if(StringUtil.isEmpty(content)) {
            //如果查询信息为空
	@Autowired
	private IAnnouncementService announcementService;
	
	@Autowired 
	private MenuMapper menuMapper;
	
	@Autowired
	private AdminMapper adminMapper;
	
	@Autowired
	private AnnouncementMapper announcementMapper;
	
	@Autowired
	private MailMapper mailMapper;

	@Autowired
	private AttachmentMapper attachmentMapper;
	
	@Autowired
	private AuthorityMapper authorityMapper;

	@Autowired
	private IOrderService orderService;
	
	
	/**
	 * 系统首页页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index(Model model,HttpServletRequest request) {
		Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
		List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
		Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
		List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
		model.addAttribute("FirstMenus",menuService.getFirstMenus(allMenusByStateAndPrimaryKeys).getData());
		model.addAttribute("SecondMenus",menuService.getSecondMenus(allMenusByStateAndPrimaryKeys).getData());
		model.addAttribute("ThirdMenus",menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
		return "admin/index/index";
	}
	
	/**
	 * 系统欢迎页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/welcome",method=RequestMethod.GET)
	public String welcome(Model model){
		model.addAttribute("TodayOrder", orderService.getOrderByDay().size());
		model.addAttribute("announcementTotal", announcementMapper.getTotal());
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            return ResponseVo.errorByMsg(validate);
        }
        //修改数据库中用户信息
        if(userMapper.updateByPrimaryKeySelective(user) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.USER_INFO_EDIT_ERROR);
        }
        Map<String,String> map = new HashMap<>();
        map.put("id",user.getId().toString());
        map.put("username",user.getUsername());
        map.put("email",user.getEmail());
        map.put("phone",user.getPhone());
        map.put("headPic",user.getHeadPic());
        String token = JWTUtil.getToken(map);

        return ResponseVo.successByMsg(token,"修改个人信息成功!");
    }

    @Override
    public ResponseVo<Boolean> updatePasswd(String prePassword, String newPassword, String reNewPassword, HttpServletRequest request) {
        //对用户输入的数据进行非空验证
        if(StringUtil.isEmpty(prePassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_PREPASSWORD_EMPTY);
        }
        if(StringUtil.isEmpty(newPassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_NEWPASSWORD_EMPTY);
        }
        if(StringUtil.isEmpty(reNewPassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_EMPTY);
        }
        //获取当前登录用户的id
        String id = (String) request.getAttribute("id");
        //判断用户输入的旧密码是否正确
        User user = userMapper.selectByPrimaryKey(Long.valueOf(id));
        if(!prePassword.equals(user.getPassword())){
            return ResponseVo.errorByMsg(CodeMsg.USER_PREPASSWORD_ERROR);
        }
        //判断用户输入的新密码是否符合规范
        user.setPassword(newPassword);
        CodeMsg validate = ValidateEntityUtil.validate(user);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            return ResponseVo.errorByMsg(validate);
        }
        //判断用户输入的新密码和确认新密码是否一致
        if(!newPassword.equals(reNewPassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_ERROR);
        }
        //修改数据库中的用户密码信息
        if(userMapper.updateByPrimaryKeySelective(user) <= 0){
        return ResponseVo.success(order.getId());
    }

    @Override
    public Order selectByOrderIdAndUserId(Long userId, Long orderId) {
        return orderMapper.selectByOrderIdAndUserId(userId, orderId);
    }

    @Override
    @Transactional
    public ResponseVo<Boolean> submit(String remark, Long orderId, Long uid, String email) {
        //判断订单是否存在
        Order order = orderMapper.selectByOrderIdAndUserId(uid, orderId);
        if(order == null){
            return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
        }
        //判断配送地址是否选择
        Address address = addressMapper.selectByUserIdAndFirstSelected(uid, AddressFirstSelectedEnum.YES.getCode());
        if(address == null){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_ADDRESS_EMPTY);
        }
        //判断订单留言长度是否合法
        if(remark.length() > 50){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_REMARK_EXCEED_LENGTH);
        }
        //声明一个自定义错误的CodeMsg
        CodeMsg codeMsg = CodeMsg.ORDER_ERROR;
        //声明一个商品id的Set集合
        Set<Long> productIdSet = new HashSet<>();
        //遍历订单中的商品
        for(OrderItem orderItem : order.getOrderItemList()){
            productIdSet.add(orderItem.getProductId());
        }
        //获取这些选中商品的详细信息
        List<Product> productList = productMapper.selectByProductIdSet(productIdSet);
        //分别比较订单中商品购买数量是否大于库存
        for(OrderItem orderItem : order.getOrderItemList()){
            for(Product product : productList){
                if(orderItem.getProductId().equals(product.getId())){
                    if(orderItem.getQuantity() > product.getStock()){
                        codeMsg.setMsg("商品<"+product.getProductName()+">库存不足了,请减少购买数量!");
                        return ResponseVo.errorByMsg(codeMsg);
    private MenuMapper menuMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @Autowired
    private IProductService productService;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 商品列表页面
     * @param model
     * @param id
     * @param request
     * @param content
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/index",method= RequestMethod.GET)
    public String index(Model model, Integer id, HttpServletRequest request, String content,
                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
    )  {
        //获取列表展示有关信息
        if(StringUtil.isEmpty(content)) {
            //如果查询信息为空
            model.addAttribute("PageInfo", productService.getProductByPage(pageNum, pageSize).getData());
        }else {
            model.addAttribute("PageInfo", productService.getProductByPageAndContent(pageNum, pageSize, content).getData());
            model.addAttribute("content",content);
        }
        //获取路径上有关信息
        Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
        if(selectByPrimaryKey == null) {
            return "error/404";
        }
        Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
        List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
        Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
        List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
        model.addAttribute("allAdmins", adminMapper.selectAll());
        model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
        model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));

/**

 */

/**
 * 后台管理系统订单控制器

 *
 */
@RequestMapping("/admin/order")
@Controller
public class OrderController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @Autowired
    private IOrderService orderService;

    /**
     * 订单列表页面
     * @param model
        }
        //准备保存文件
        File filePath = new File(uploadAttachmentPath);
        if (!filePath.exists()) {
            //若不存在文件夹,则创建一个文件夹
            filePath.mkdir();
        }
        filePath = new File(uploadAttachmentPath + "/" + StringUtil.getFormatterDate(new Date(), "yyyyMMdd"));
        //判断当天日期的文件夹是否存在,若不存在,则创建
        if (!filePath.exists()) {
            //若不存在文件夹,则创建一个文件夹
            filePath.mkdir();
        }
        String filename = StringUtil.getFormatterDate(new Date(), "yyyyMMdd") + "/" + System.currentTimeMillis() + suffix;
        try {
            attachment.transferTo(new File(uploadAttachmentPath + "/" + filename));   //把文件上传
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //把附件信息写入数据库
        BigDecimal size = new BigDecimal((double) attachment.getSize() / 1024);
        size = size.setScale(2, BigDecimal.ROUND_HALF_EVEN);
        Attachment saveAttachment = new Attachment(null, loginedAdmin.getId(), filename, originalFilename, size); //id,senderid,url,name
        CodeMsg validate = ValidateEntityUtil.validate(saveAttachment);
        if (!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            return ResponseVo.errorByMsg(validate);
        }
        if (attachmentMapper.insertSelective(saveAttachment) <= 0) {
            return ResponseVo.errorByMsg(CodeMsg.UPLOAD_ATTACHMENT_ERROR);
        }
        log.info("附件上传成功,保存位置:" + uploadAttachmentPath + filename);
        return ResponseVo.success(saveAttachment);

    }

    /**
     * 文件统一下载类
     *
     * @param id
     * @param response
     * @return

/**

 */

/**
 * 后台管理系统商品种类控制器

 *
 */
@RequestMapping("/admin/product_category")
@Controller
public class ProductCategoryController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @Autowired
    private IProductCategoryService productCategoryService;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 商品种类列表页面
     * @param model
     * @param id
     * @param request
     * @param content
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/index",method= RequestMethod.GET)

/**

 */

/**
 * 后台管理系统订单控制器

 *
 */
@RequestMapping("/admin/order")
@Controller
public class OrderController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private AdminMapper adminMapper;

        if(userMapper.updateByPrimaryKeySelective(user) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.USER_INFO_EDIT_ERROR);
        }
        Map<String,String> map = new HashMap<>();
        map.put("id",user.getId().toString());
        map.put("username",user.getUsername());
        map.put("email",user.getEmail());
        map.put("phone",user.getPhone());
        map.put("headPic",user.getHeadPic());
        String token = JWTUtil.getToken(map);

        return ResponseVo.successByMsg(token,"修改个人信息成功!");
    }

    @Override
    public ResponseVo<Boolean> updatePasswd(String prePassword, String newPassword, String reNewPassword, HttpServletRequest request) {
        //对用户输入的数据进行非空验证
        if(StringUtil.isEmpty(prePassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_PREPASSWORD_EMPTY);
        }
        if(StringUtil.isEmpty(newPassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_NEWPASSWORD_EMPTY);
        }
        if(StringUtil.isEmpty(reNewPassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_EMPTY);
        }
        //获取当前登录用户的id
        String id = (String) request.getAttribute("id");
        //判断用户输入的旧密码是否正确
        User user = userMapper.selectByPrimaryKey(Long.valueOf(id));
        if(!prePassword.equals(user.getPassword())){
            return ResponseVo.errorByMsg(CodeMsg.USER_PREPASSWORD_ERROR);
        }
        //判断用户输入的新密码是否符合规范
        user.setPassword(newPassword);
        CodeMsg validate = ValidateEntityUtil.validate(user);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            return ResponseVo.errorByMsg(validate);
        }
        //判断用户输入的新密码和确认新密码是否一致
        if(!newPassword.equals(reNewPassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_ERROR);
            return ResponseVo.errorByMsg(validate);
        }
        //判断用户输入的新密码和确认新密码是否一致
        if(!newPassword.equals(reNewPassword)){
            return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_ERROR);
        }
        //修改数据库中的用户密码信息
        if(userMapper.updateByPrimaryKeySelective(user) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.USER_PASSWORD_EDIT_ERROR);
        }
        return ResponseVo.successByMsg(true, "修改密码成功!");
    }

    @Override
    public ResponseVo<PageInfo> getUserByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectAll();
        PageInfo pageInfo = new PageInfo<>(userList);
        pageInfo.setList(userList);
        return ResponseVo.success(pageInfo);
    }

    @Override
    public ResponseVo<PageInfo> getUserByPageAndContent(Integer pageNum, Integer pageSize, String content) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userMapper.selectBySearchContent(content);
        PageInfo pageInfo = new PageInfo<>(userList);
        pageInfo.setList(userList);
        return ResponseVo.success(pageInfo);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResponseVo<Boolean> updateUserPasswd(String passwd, Long userId) {
        if(StringUtil.isEmpty(passwd) || userId == null){
            return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
        }
        User user = userMapper.selectByPrimaryKey(userId);
        user.setPassword(passwd);
        //统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(user);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
            return ResponseVo.errorByMsg(validate);
        for(Map.Entry<String,String> entry : entries.entrySet()){
            Cart cart = gson.fromJson(entry.getValue(), Cart.class);
            for(Product product : productList){
                if(product.getId().equals(cart.getProductId())){
                    orderItem = new OrderItem(product.getId(), cart.getQuantity(),
                            product.getProductName(),
                            product.getProductPic(),
                            product.getPrice(),
                            product.getPrice().multiply(BigDecimal.valueOf(cart.getQuantity())));
                    orderItemList.add(orderItem);
                    orderTotalPrice = orderTotalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(cart.getQuantity())));
                }
            }
        }
        //声明一个订单对象
        Order order = new Order(new SnowFlake(2,3).nextId(),
                Long.valueOf(uid),
                OrderStateEnum.NO_PAY.getCode(),
                orderTotalPrice);
        //把订单信息添加到数据库中
        if(orderMapper.insertSelective(order) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_ADD_ERROR);
        }
        //给订单详情对应的订单id赋值
        for(OrderItem or : orderItemList){
            or.setOrderId(order.getId());
        }
        //把订单详情信息添加到数据库中
        if(orderItemMapper.batchInsert(orderItemList) <= 0){
            return ResponseVo.errorByMsg(CodeMsg.ORDER_ITEM_ADD_ERROR);
        }
        return ResponseVo.success(order.getId());
    }

    @Override
    public Order selectByOrderIdAndUserId(Long userId, Long orderId) {
        return orderMapper.selectByOrderIdAndUserId(userId, orderId);
    }

    @Override
    @Transactional

/**

 */

/**
 * 后台管理系统商品种类控制器

 *
 */
@RequestMapping("/admin/product_category")
@Controller
public class ProductCategoryController {

    @Autowired
    private IMenuService menuService;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AuthorityMapper authorityMapper;

    @Autowired

请添加图片描述

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值