utils.JdbcUtils.java
package com.wzc.utils;
import java.sql.*;
public class JdbcUtils {
// 连接数据库的工具类
// mysql-connector-java-xxx.jar 的作用是为了提供Java连接MySQL的能力--数据库加载驱动
// 1、创建数据库连接对象
public static Connection conn;
// 2、创建连接方法
public static Connection getConnection() {
try {
// 2.1 加载数据库驱动 (8.0的版本com.mysql.cj.jdbc.Driver )
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.2 配置访问数据库的信息,包含url, username, password
// 2.3 mysql 8.0以上需要配置全球时差 serverTimezone=UTC
String url = "jdbc:mysql://localhost:3306/shopping_manager?userSSL=false";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
// 3、关闭数据库 (连接数据库的时候,会开启一个结果集ResultSet(查询的结果集合,更新-增-删-改 0或n) ,
// 数据声明PrepareStatement (where id = 1001 或者条件后面赋值)
// 连接对象Conn)
public static void closeConnection(ResultSet rs, PreparedStatement ps, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
bean.Role.java
package com.xsj.bean;
public class Role {
// 实体--角色
private int r_id;
private String r_name;
private String r_username;
private String r_password;
private int power_id;
private String r_address;
public String getR_address() {
return r_address;
}
public void setR_address(String r_address) {
this.r_address = r_address;
}
// 构造函数 getter/setter
public Role() {
}
public Role(int r_id, String r_name, String r_username, String r_password, int power_id, String r_address) {
this.r_id = r_id;
this.r_name = r_name;
this.r_username = r_username;
this.r_password = r_password;
this.power_id = power_id;
this.r_address = r_address;
}
public int getR_id() {
return r_id;
}
public void setR_id(int r_id) {
this.r_id = r_id;
}
public String getR_name() {
return r_name;
}
public void setR_name(String r_name) {
this.r_name = r_name;
}
public String getR_username() {
return r_username;
}
public void setR_username(String r_username) {
this.r_username = r_username;
}
public String getR_password() {
return r_password;
}
public void setR_password(String r_password) {
this.r_password = r_password;
}
public int getPower_id() {
return power_id;
}
public void setPower_id(int power_id) {
this.power_id = power_id;
}
@Override
public String toString() {
return "Role{" +
"r_id=" + r_id +
", r_name='" + r_name + '\'' +
", r_username='" + r_username + '\'' +
", r_password='" + r_password + '\'' +
", power_id=" + power_id +
", r_address='" + r_address + '\'' +
'}';
}
}
bean.Product.java
package com.xsj.bean;
public class Product {
// 实体--商品
private int pro_id;
private String pro_name;
private double pro_price;
private int pro_num;
private String pro_info;
private int pro_good;
private int role_id;//提供商品的商家编号
public Product() {
}
public Product(int pro_id, String pro_name, double pro_price, int pro_num, String pro_info, int pro_good, int role_id) {
this.pro_id = pro_id;
this.pro_name = pro_name;
this.pro_price = pro_price;
this.pro_num = pro_num;
this.pro_info = pro_info;
this.pro_good = pro_good;
this.role_id = role_id;
}
public int getPro_id() {
return pro_id;
}
public void setPro_id(int pro_id) {
this.pro_id = pro_id;
}
public String getPro_name() {
return pro_name;
}
public void setPro_name(String pro_name) {
this.pro_name = pro_name;
}
public double getPro_price() {
return pro_price;
}
public void setPro_price(double pro_price) {
this.pro_price = pro_price;
}
public int getPro_num() {
return pro_num;
}
public void setPro_num(int pro_num) {
this.pro_num = pro_num;
}
public String getPro_info() {
return pro_info;
}
public void setPro_info(String pro_info) {
this.pro_info = pro_info;
}
public int getPro_good() {
return pro_good;
}
public void setPro_good(int pro_good) {
this.pro_good = pro_good;
}
public int getRole_id() {
return role_id;
}
public void setRole_id(int role_id) {
this.role_id = role_id;
}
@Override
public String toString() {
return pro_id +
"\t\t" + pro_name +
"\t\t" + pro_price +
"\t\t" + pro_num +
"\t\t" + pro_info +
"\t\t" + pro_good +
"\t\t" + role_id;
}
}
bean.Cart.java
package com.xsj.bean;
import java.util.Date;
public class Cart {
// 实体--购物车
private int cart_id;
private int user_id;
private int pro_id;
private double cart_money;
private Date cart_time;
public Cart() {
}
public Cart(int cart_id, int user_id, int pro_id, double cart_money, Date cart_time) {
this.cart_id = cart_id;
this.user_id = user_id;
this.pro_id = pro_id;
this.cart_money = cart_money;
this.cart_time = cart_time;
}
public int getCart_id() {
return cart_id;
}
public void setCart_id(int cart_id) {
this.cart_id = cart_id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public int getPro_id() {
return pro_id;
}
public void setPro_id(int pro_id) {
this.pro_id = pro_id;
}
public double getCart_money() {
return cart_money;
}
public void setCart_money(double cart_money) {
this.cart_money = cart_money;
}
public Date getCart_time() {
return cart_time;
}
public void setCart_time(Date cart_time) {
this.cart_time = cart_time;
}
@Override
public String toString() {
return cart_id +
"\t\t\t" + user_id +
"\t\t\t" + pro_id +
"\t\t\t" + cart_money +
"\t\t\t" + cart_time;
}
}
bean.Chat.java
package com.xsj.bean;
import java.util.Date;
public class Cart {
// 实体--购物车
private int cart_id;
private int user_id;
private int pro_id;
private double cart_money;
private Date cart_time;
public Cart() {
}
public Cart(int cart_id, int user_id, int pro_id, double cart_money, Date cart_time) {
this.cart_id = cart_id;
this.user_id = user_id;
this.pro_id = pro_id;
this.cart_money = cart_money;
this.cart_time = cart_time;
}
public int getCart_id() {
return cart_id;
}
public void setCart_id(int cart_id) {
this.cart_id = cart_id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public int getPro_id() {
return pro_id;
}
public void setPro_id(int pro_id) {
this.pro_id = pro_id;
}
public double getCart_money() {
return cart_money;
}
public void setCart_money(double cart_money) {
this.cart_money = cart_money;
}
public Date getCart_time() {
return cart_time;
}
public void setCart_time(Date cart_time) {
this.cart_time = cart_time;
}
@Override
public String toString() {
return cart_id +
"\t\t\t" + user_id +
"\t\t\t" + pro_id +
"\t\t\t" + cart_money +
"\t\t\t" + cart_time;
}
}
bean.Indent.java
package com.xsj.bean;
public class Indent {
// --实体 订单
private String ind_id;
private int user_id;
private int pro_id;
private String ind_adds;
private String ind_sads;
private double ind_money;
private int ind_state;
public Indent(){}
public Indent(String ind_id, int user_id, int pro_id, String ind_adds, String ind_sads, double ind_money, int ind_state) {
this.ind_id = ind_id;
this.user_id = user_id;
this.pro_id = pro_id;
this.ind_adds = ind_adds;
this.ind_sads = ind_sads;
this.ind_money = ind_money;
this.ind_state = ind_state;
}
public int getPro_id() {
return pro_id;
}
public void setPro_id(int pro_id) {
this.pro_id = pro_id;
}
public String getInd_id() {
return ind_id;
}
public void setInd_id(String ind_id) {
this.ind_id = ind_id;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getInd_adds() {
return ind_adds;
}
public void setInd_adds(String ind_adds) {
this.ind_adds = ind_adds;
}
public String getInd_sads() {
return ind_sads;
}
public void setInd_sads(String ind_sads) {
this.ind_sads = ind_sads;
}
public double getInd_money() {
return ind_money;
}
public void setInd_money(double ind_money) {
this.ind_money = ind_money;
}
public int getInd_state() {
return ind_state;
}
public void setInd_state(int ind_state) {
this.ind_state = ind_state;
}
@Override
public String toString() {
return ind_id +
"\t\t"+ user_id +
"\t\t" + pro_id +
"\t\t" + ind_adds +
"\t\t" + ind_sads +
"\t\t" + ind_money +
"\t\t" + ind_state;
}
}
service.RoleService.java
package com.xsj.service;
import com.xsj.bean.Cart;
import com.xsj.bean.Indent;
import com.xsj.bean.Product;
import com.xsj.bean.Role;
import com.xsj.bean.Chat;
import java.util.List;
public interface RoleService {
// 封装方法
List<Role> selectAllRole();
Role loginRole(String username, String password);
List selectAllProduct();
int insertCart(int productId,int user_id);
int clearCart(int r_id, List<Integer> productId);
double findPrice(int productId);
List<Cart> showCart(int r_id);
List<Indent> showIndent(int r_id);
List<String> findIndentId(int r_id);
int alterInformation(Role role);
List<Product> showMyProdeuct(int r_id);
int addProduct(int r_id);
int outOfShelfPro(int r_id);
List<Indent> showAllIndent();
int alterIndentInformation(String indId);
int examIllegalChat(int illegalChatId);
int chatPro(int r_id, int chatproId);
int isMyPro(int r_id,int chatId);
int insertReplyRes(int chatId, String reply);
int isBuyPro(int r_id,int chatproId);
List<Chat> showAllChat();
List<Chat> showAllChatByAdmin();
List<Chat> showMyChat(int r_id);
List<Chat> showAllChatBySelf(int r_id);
}
service.RoleServiceImpl.java
package com.xsj.service;
import com.xsj.bean.Cart;
import com.xsj.bean.Indent;
import com.xsj.bean.Product;
import com.xsj.bean.Role;
import com.xsj.bean.Chat;
import com.xsj.dao.RoleDao;
import com.xsj.dao.RoleDaoImpl;
import java.util.List;
public class RoleServiceImpl implements RoleService{
public RoleDao roleDao;
// 通过roleDao调用其实现类的返回值
public RoleServiceImpl() {
roleDao = new RoleDaoImpl();
}
@Override
public List<Role> selectAllRole() {
return roleDao.selectRoleByAll();
}
@Override
public Role loginRole(String username, String password) {
return roleDao.selectRoleByUsernameAndPassword(username,password);
}
@Override
public List selectAllProduct() {
return roleDao.showAllProduct();
}
@Override
public int insertCart(int productId, int user_id) {
return roleDao.insertCartById(productId,user_id);
}
@Override
public int clearCart(int r_id, List<Integer> productId) {
return roleDao.dealCartAndIndent(r_id,productId);
}
@Override
public double findPrice(int productId) {
return roleDao.findPriceByProID(productId);
}
@Override
public List<Cart> showCart(int r_id) {
return roleDao.showCartByUserId(r_id);
}
@Override
public List<Indent> showIndent(int r_id) {
return roleDao.showIndentByUserId(r_id);
}
@Override
public List<String> findIndentId(int r_id) {
return roleDao.findIndentIdByUserId(r_id);
}
@Override
public int alterInformation(Role role) {
return roleDao.alterInform(role);
}
@Override
public List<Product> showMyProdeuct(int r_id) {
return roleDao.showMyProductByBusId(r_id);
}
@Override
public int addProduct(int r_id) {
return roleDao.addProductByBusId(r_id);
}
@Override
public int outOfShelfPro(int r_id) {
return roleDao.outOfShelfProByBusId(r_id);
}
@Override
public List<Indent> showAllIndent() {
return roleDao.showAllIndentByAdmin();
}
@Override
public int alterIndentInformation(String indId) {
return roleDao.alterIndentInformationByAdmin(indId);
}
@Override
public int examIllegalChat(int illegalChatId) {
return roleDao.shieldChat(illegalChatId);
}
@Override
public int chatPro(int r_id, int chatproId) {
return roleDao.insertChatPro(r_id,chatproId);
}
@Override
public int isMyPro(int r_id,int chatId) {
return roleDao.isMyProByChatId(r_id,chatId);
}
@Override
public int insertReplyRes(int chatId, String reply) {
return roleDao.insertReplyByChatId(chatId,reply);
}
@Override
public int isBuyPro(int r_id,int chatproId) {
return roleDao.isUserBuyPro(r_id,chatproId);
}
@Override
public List<Chat> showAllChat() {
return roleDao.showAllChatByUser();
}
@Override
public List<Chat> showAllChatByAdmin() {
return roleDao.showChatByAdmin();
}
@Override
public List<Chat> showMyChat(int r_id) {
return roleDao.showMyChatByBusId(r_id);
}
@Override
public List<Chat> showAllChatBySelf(int r_id) {
return roleDao.showAllChatByUserSelf(r_id);
}
}
dao.RoleDao.java
package com.xsj.dao;
import com.xsj.bean.Cart;
import com.xsj.bean.Indent;
import com.xsj.bean.Product;
import com.xsj.bean.Role;
import com.xsj.bean.Chat;
import java.util.List;
public interface RoleDao {
List<Role> selectRoleByAll();
Role selectRoleByUsernameAndPassword(String username, String password);
List showAllProduct();
int insertCartById(int productId, int user_id);
int dealCartAndIndent(int user_id, List<Integer> productId);
double findPriceByProID(int productId);
List<Cart> showCartByUserId(int r_id);
List<Indent> showIndentByUserId(int r_id);
List<String> findIndentIdByUserId(int r_id);
int alterInform(Role role);
List<Product> showMyProductByBusId(int r_id);
int addProductByBusId(int r_id);
int outOfShelfProByBusId(int r_id);
List<Indent> showAllIndentByAdmin();
int alterIndentInformationByAdmin(String ind_id);
int shieldChat(int illegalChatId);
int insertChatPro(int r_id, int chatproId);
int isMyProByChatId(int r_id,int chatId);
int insertReplyByChatId(int chatId, String reply);
int isUserBuyPro(int r_id,int chatproId);
List<Chat> showAllChatByUser();
List<Chat> showChatByAdmin();
List<Chat> showMyChatByBusId(int r_id);
List<Chat> showAllChatByUserSelf(int r_id);
}
dao.RoleDaoImpl.java
package com.xsj.dao;
import com.xsj.bean.Cart;
import com.xsj.bean.Indent;
import com.xsj.bean.Product;
import com.xsj.bean.Role;
import com.xsj.bean.Chat;
import com.xsj.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
public class RoleDaoImpl implements RoleDao{
private static Connection conn;
private PreparedStatement ps;
private ResultSet rs;
//根据商品编号找到商家编号 当返回结果为0的时候没找到
public int findBusIdByProId(int pro_id){
conn = JdbcUtils.getConnection();
int bus_id = 0;
String sql = "SELECT bus_id FROM product where pro_id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,pro_id);
rs = ps.executeQuery();
if(rs.next()){
bus_id = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return bus_id;
}
//根据用户编号找到用户地址
public String findRoleAddByRoleId(int r_id){
conn = JdbcUtils.getConnection();
String sql = "SELECT r_address from role where r_id = ?";
String r_add = "";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
rs = ps.executeQuery();
if(!rs.next()){
System.out.println("未找到地址,请检查用户id");
}else {
r_add = rs.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return r_add;
}
@Override
public List<Role> selectRoleByAll() {
// 创建集合存放数据
List roles = new ArrayList<>();
// 编写sql语句
String sql = "select * from role";
// 防止sql语句携带参数
try {
ps = conn.prepareStatement(sql);
// 执行sql语句
rs = ps.executeQuery(sql);
// 遍历结果集
while (rs.next()){
Role role = new Role();
role.setR_id(rs.getInt(1));
role.setR_name(rs.getString(2));
role.setR_username(rs.getString(3));
role.setR_password(rs.getString(4));
role.setR_address(rs.getString(5));
role.setPower_id(rs.getInt(6));
roles.add(role);
}
} catch (SQLException e) {
e.printStackTrace();
}
return roles;
}
@Override
public Role selectRoleByUsernameAndPassword(String username, String password) {
conn = JdbcUtils.getConnection();
Role role = new Role();
String sql = "select * from role where r_username = ? and r_password = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,username);//填写的第一个问号
ps.setString(2,password);//填写的第二个问号
rs = ps.executeQuery();
if (!rs.next()){
System.out.println("用户名密码错误");
}else {
role.setR_id(rs.getInt(1));
role.setR_name(rs.getString(2));
role.setR_address(rs.getString(5));
role.setPower_id(rs.getInt(6));
}
} catch (SQLException e) {
e.printStackTrace();
}
return role;
}
// alt + enter
@Override
public List<Product> showAllProduct() {
conn = JdbcUtils.getConnection();
// 创建集合存放数据
List products = new ArrayList<>();
// 编写sql语句
String sql = "select * from product";
// 防止sql语句携带参数
try {
ps = conn.prepareStatement(sql);
// 执行sql语句
rs = ps.executeQuery(sql);
// 遍历结果集
while (rs.next()){
Product product = new Product();
product.setPro_id(rs.getInt(1));
product.setPro_name(rs.getString(2));
product.setPro_price(rs.getDouble(3));
product.setPro_num(rs.getInt(4));
product.setPro_info(rs.getString(5));
product.setPro_good(rs.getInt(6));
products.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
}
return products;
}
@Override
public int insertCartById(int productId, int user_id) {
conn = JdbcUtils.getConnection();
int res = 0;
double proPri = findPriceByProID(productId);//查询商品价格
if(proPri==0){
System.out.println("查找不到商品的价格");
return res;
}else{
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sql = "insert into cart(user_id,pro_id,cart_money,cart_time) VALUES(?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,user_id);
ps.setInt(2,productId);
ps.setDouble(3,proPri);
ps.setString(4,df.format(date));
res = ps.executeUpdate();
System.out.println("--------------------------------------------------------");
} catch (SQLException e) {
e.printStackTrace();
}
}
return res;//如果是0表示没有插入成功
}
// 1、生成账单
// 2、清空购物车
@Override
public int dealCartAndIndent(int user_id, List<Integer> productId) {
conn = JdbcUtils.getConnection();
for(Integer productid:productId){
String ind_id = "";//订单编号
// 1、统计购买的数量和金额
int cartNum = 0;
double cartMoney = 0;
String sql = "select sum(cart_money),count(cart_id) from cart WHERE user_id = ? and pro_id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,user_id);
ps.setInt(2,productid);
rs = ps.executeQuery();
if (!rs.next()){
System.out.println("你尚未添加任何商品,请先添加至购物车或者直接购买");
}else {
cartMoney = rs.getDouble(1);
cartNum = rs.getInt(2);
// 根据不同的商品编号找到不同的商家,进行统计分类(忽略)
// 通过user_id获取一下地址
int bus_id = findBusIdByProId(productid);
String user_add = findRoleAddByRoleId(user_id);
String bus_add = findRoleAddByRoleId(bus_id);
// 生成订单,然后清除购物车
sql = "insert into indent(ind_id,user_id,pro_id,ind_adds,ind_sads,ind_money,ind_state) values(?,?,?,?,?,?,?)";
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
ind_id = UUID.randomUUID()+ df.format(date);
ps = conn.prepareStatement(sql);
ps.setString(1,ind_id);
ps.setInt(2,user_id);
ps.setInt(3,productid);
ps.setString(4,user_add);
ps.setString(5,bus_add);
ps.setDouble(6,cartMoney);
ps.setInt(7,1);
int intIndent = ps.executeUpdate();
if (intIndent>0){
// 清除购物车
sql = "DELETE FROM cart WHERE user_id = ? and pro_id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1,user_id);
ps.setInt(2,productid);
int cartRes = ps.executeUpdate();
if (cartRes != cartNum){
System.out.println("-----清除购物车出错-----");
}else {
System.out.println("-----成功清除购物车中的已购商品!!!-----");
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return 1;
}
@Override
// 如果结果为0,则表示没有找到商品的价格,需要重传,否则就是查找成功
public double findPriceByProID(int productId) {
conn = JdbcUtils.getConnection();
String sql = "select * from product where pro_id = ?";
double proPri = 0;
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,productId);
rs = ps.executeQuery();
if(!rs.next()){
System.out.println("不存在该商品");
}else{
proPri = rs.getDouble(3);
}
}catch (SQLException e){
e.printStackTrace();
}
return proPri;
}
@Override
public List<Cart> showCartByUserId(int r_id) {
// 根据用户编号查看用户购物车
conn = JdbcUtils.getConnection();
List carts = new ArrayList<>();
String sql = "select * from cart where user_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
rs = ps.executeQuery();
while(rs.next()){
Cart cart = new Cart();
cart.setCart_id(rs.getInt(1));
cart.setUser_id(rs.getInt(2));
cart.setPro_id(rs.getInt(3));
cart.setCart_money(rs.getDouble(4));
cart.setCart_time(rs.getDate(5));
carts.add(cart);
}
}catch (SQLException e){
e.printStackTrace();
}
return carts;
}
@Override
public List<Indent> showIndentByUserId(int r_id) {
conn = JdbcUtils.getConnection();
List indents = new ArrayList<>();
String sql = "select * from indent where user_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
rs = ps.executeQuery();
while(rs.next()){
Indent indent = new Indent();
indent.setInd_id(rs.getString(1));
indent.setUser_id(rs.getInt(2));
indent.setPro_id(rs.getInt(3));
indent.setInd_adds(rs.getString(4));
indent.setInd_sads(rs.getString(5));
indent.setInd_money(rs.getDouble(6));
indent.setInd_state(rs.getInt(7));
indents.add(indent);
}
}catch (SQLException e){
e.printStackTrace();
}
return indents;
}
@Override
public List<String> findIndentIdByUserId(int r_id) {
conn = JdbcUtils.getConnection();
String sql = "SELECT ind_id FROM indent where user_id = ?";
String indIdRes = "";
List<String> indIDs = new ArrayList<>();
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
rs = ps.executeQuery();
while(rs.next()){
indIdRes = rs.getString(1);
indIDs.add(indIdRes);
}
}catch (SQLException e){
e.printStackTrace();
}
return indIDs;
}
@Override
public int alterInform(Role role) {
conn = JdbcUtils.getConnection();
String sql = "";
Scanner sc = new Scanner(System.in);
System.out.println("----------------------请输入要修改的内容----------------------");
System.out.println("1、昵称 2、用户名 3、密码 4、地址 5、退出");
int alter_choice = sc.nextInt();
switch (alter_choice){
case 1:
System.out.println("请输入新的昵称");
String new_rName = sc.next();
sql = "UPDATE role set r_name = ? where r_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setString(1,new_rName);
ps.setInt(2,role.getR_id());
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改昵称为:"+new_rName);
}
else{
System.out.println("-----未成功修改昵称-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 2:
System.out.println("请输入新的用户名");
String new_rUsername = sc.next();
sql = "UPDATE role set r_username = ? where r_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setString(1,new_rUsername);
ps.setInt(2,role.getR_id());
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改用户名为:"+new_rUsername);
}
else{
System.out.println("-----未成功修改用户名-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 3:
System.out.println("请输入新的密码");
String new_password = sc.next();
sql = "UPDATE role set r_password = ? where r_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setString(1,new_password);
ps.setInt(2,role.getR_id());
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改密码为"+new_password);
}
else{
System.out.println("-----未成功修改密码-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 4:
System.out.println("请输入新的地址");
String new_rAddress = sc.next();
sql = "UPDATE role set r_address = ? where r_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setString(1,new_rAddress);
ps.setInt(2,role.getR_id());
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改地址为"+new_rAddress);
}
else{
System.out.println("-----未成功修改地址-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 5:
break;
}
return alter_choice;
}
@Override
public List<Product> showMyProductByBusId(int r_id) {
conn = JdbcUtils.getConnection();
// 创建集合存放数据
List<Product> products = new ArrayList<>();
String sql = "select * from product where bus_id = ?";
// 防止sql语句携带参数
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
rs = ps.executeQuery();
// 遍历结果集
while (rs.next()){
Product product = new Product();
product.setPro_id(rs.getInt(1));
product.setPro_name(rs.getString(2));
product.setPro_price(rs.getDouble(3));
product.setPro_num(rs.getInt(4));
product.setPro_info(rs.getString(5));
product.setPro_good(rs.getInt(6));
products.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
}
return products;
}
@Override
public int addProductByBusId(int r_id) {
conn = JdbcUtils.getConnection();
Scanner sc = new Scanner(System.in);
System.out.println("请输入新上架商品名称");
String newProName = sc.next();
System.out.println("请输入新上架商品的价格");
double newProPrice = sc.nextDouble();
System.out.println("请输入新上架商品的库存");
int newProNum = sc.nextInt();
System.out.println("请输入新上架商品的描述");
String newProInfo = sc.next();
String sql = "insert into product(pro_name,pro_price,pro_num,pro_info,pro_good,bus_id) values(?,?,?,?,?,?)";
try{
ps = conn.prepareStatement(sql);
ps.setString(1,newProName);
ps.setDouble(2,newProPrice);
ps.setInt(3,newProNum);
ps.setString(4,newProInfo);
ps.setInt(5,0);//新上架商品默认好评为0
ps.setInt(6,r_id);
int addRes = ps.executeUpdate();
if(addRes==1){
System.out.println("上架新商品成功");
return addRes;
}else{
System.out.println("上架新商品失败");
}
}catch (SQLException e){
e.printStackTrace();
}
return 0;
}
@Override
public int outOfShelfProByBusId(int r_id) {
conn = JdbcUtils.getConnection();
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的商品编号");
int delProId = sc.nextInt();
String sql = "DELETE FROM product WHERE bus_id = ? AND pro_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
ps.setInt(2,delProId);
int delRes = ps.executeUpdate();
if(delRes==1){
System.out.println("已成功下架商品编号为:"+delProId+"的商品!");
return delRes;
}else{
System.out.println("下架商品编号为:"+delProId+"的商品失败!");
}
}catch (SQLException e){
e.printStackTrace();
}
return 0;
}
@Override
public List<Indent> showAllIndentByAdmin() {
conn = JdbcUtils.getConnection();
List indents = new ArrayList<>();
String sql = "select * from indent";
try {
ps = conn.prepareStatement(sql);
// 执行sql语句
rs = ps.executeQuery(sql);
// 遍历结果集
while (rs.next()){
Indent indent = new Indent();
indent.setInd_id(rs.getString(1));
indent.setUser_id(rs.getInt(2));
indent.setPro_id(rs.getInt(3));
indent.setInd_adds(rs.getString(4));
indent.setInd_sads(rs.getString(5));
indent.setInd_money(rs.getDouble(6));
indent.setInd_state(rs.getInt(7));
indents.add(indent);
}
} catch (SQLException e) {
e.printStackTrace();
}
return indents;
}
@Override
public int alterIndentInformationByAdmin(String ind_id) {
conn = JdbcUtils.getConnection();
String sql = "";
Scanner sc = new Scanner(System.in);
System.out.println("----------------------请输入要修改的内容----------------------");
System.out.println("1、收货人编号 2、商品编号 3、收货地址 4、发货地址 5、订单金额 6、订单状态 7、退出");
int alter_choice = sc.nextInt();
switch (alter_choice){
case 1:
System.out.println("请输入新的收货人编号");
int new_userId = sc.nextInt();
sql = "UPDATE indent set user_id = ? where ind_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,new_userId);
ps.setString(2,ind_id);
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改收货人编号为:"+new_userId);
}
else{
System.out.println("-----未成功修改收货人编号-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 2:
System.out.println("请输入新的商品编号");
int new_proId = sc.nextInt();
sql = "UPDATE indent set pro_id = ? where ind_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,new_proId);
ps.setString(2,ind_id);
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改商品编号为:"+new_proId);
}
else{
System.out.println("-----未成功修改商品编号-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 3:
System.out.println("请输入新的收货地址");
String new_indAdd= sc.next();
sql = "UPDATE indent set ind_adds = ? where ind_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setString(1,new_indAdd);
ps.setString(2,ind_id);
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改收货地址为:"+new_indAdd);
}
else{
System.out.println("-----未成功修改收货地址-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 4:
System.out.println("请输入新的发货地址");
String new_indSads= sc.next();
sql = "UPDATE indent set ind_sads = ? where ind_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setString(1,new_indSads);
ps.setString(2,ind_id);
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改发货地址为:"+new_indSads);
}
else{
System.out.println("-----未成功修改发货地址-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 5:
System.out.println("请输入新的订单金额");
double new_indMoney = sc.nextDouble();
sql = "UPDATE indent set ind_money = ? where ind_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setDouble(1,new_indMoney);
ps.setString(2,ind_id);
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改订单金额为:"+new_indMoney);
}
else{
System.out.println("-----未成功修改订单金额-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 6:
System.out.println("请输入新的订单状态");
int new_indstate= sc.nextInt();
sql = "UPDATE indent set ind_state = ? where ind_id = ?";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,new_indstate);
ps.setString(2,ind_id);
int updateRes = ps.executeUpdate();
if(updateRes==1){
System.out.println("-----已修改订单状态为:"+new_indstate);
}
else{
System.out.println("-----未成功修改订单状态-----");
}
}catch (SQLException e){
e.printStackTrace();
}
System.out.println("");
break;
case 7:
break;
}
return alter_choice;
}
@Override
public int shieldChat(int illegalChatId) {
conn = JdbcUtils.getConnection();
String sql = "UPDATE chat set c_states = 4 where chat_id = ?";
int shieldRes = 0;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1,illegalChatId);
shieldRes = ps.executeUpdate();
if(shieldRes==1){
System.out.println("-----该评论已被屏蔽");
}else{
System.out.println("-----评论未被屏蔽");
}
}catch (SQLException e){
e.printStackTrace();
}
return shieldRes;
}
// 用户发表评论
@Override
public int insertChatPro(int r_id, int chatproId) {
conn = JdbcUtils.getConnection();
Scanner sc = new Scanner(System.in);
System.out.println("请输入评论内容");
String content = sc.next();
int insertChatRes = 0;
String sql = "insert into chat(user_id,pro_id,c_content,c_states) VALUES(?,?,?,?)";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
ps.setInt(2,chatproId);
ps.setString(3,content);
ps.setInt(4,2);
insertChatRes = ps.executeUpdate();
if(insertChatRes==1){
System.out.println("-----感谢您的宝贵意见!");
}else{
System.out.println("-----您的评论发表失败哦!");
}
}catch (SQLException e){
e.printStackTrace();
}
return insertChatRes;
}
@Override
public int isMyProByChatId(int r_id,int chatId) {
conn = JdbcUtils.getConnection();
int proId = 0;
String sql = "SELECT bus_id FROM product WHERE pro_id in (SELECT pro_id FROM chat WHERE chat_id = ?)";
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,chatId);
rs = ps.executeQuery();
if(rs.next()){
proId = rs.getInt(1);
}
if(proId==r_id){
return 1;
}
}catch (SQLException e){
e.printStackTrace();
}
return 0;
}
@Override
public int insertReplyByChatId(int chatId, String reply) {
conn = JdbcUtils.getConnection();
String sql = "UPDATE chat set bus_reply = ?,c_states=3 WHERE chat_id = ?";
int replyRes = 0;
try{
ps = conn.prepareStatement(sql);
ps.setString(1,reply);
ps.setInt(2,chatId);
replyRes = ps.executeUpdate();
}catch (SQLException e){
e.printStackTrace();
}
return replyRes;
}
@Override
public int isUserBuyPro(int r_id,int chatproId) {
conn = JdbcUtils.getConnection();
int isRes = 0;
String sql = "select * from indent where pro_id = ? and user_id = ?";
try {
ps=conn.prepareStatement(sql);
ps.setInt(1,chatproId);
ps.setInt(2,r_id);
rs = ps.executeQuery();
if(rs.next()){
isRes = 1;
}
}catch (SQLException e){
e.printStackTrace();
}
return isRes;
}
// 查看所有合法评论
@Override
public List<Chat> showAllChatByUser() {
conn = JdbcUtils.getConnection();
String sql = "select * from chat where c_states!=4";
List<Chat> chats = new ArrayList<>();
try{
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Chat chat = new Chat();
chat.setChat_id(rs.getInt(1));
chat.setUser_id(rs.getInt(2));
chat.setPro_id(rs.getInt(3));
chat.setC_content(rs.getString(4));
chat.setBus_reply(rs.getString(5));
chat.setC_states(rs.getInt(6));
chats.add(chat);
}
}catch (SQLException e){
e.printStackTrace();
}
return chats;
}
// 查看所有评论
@Override
public List<Chat> showChatByAdmin() {
conn = JdbcUtils.getConnection();
String sql = "select * from chat";
List<Chat> chats = new ArrayList<>();
try{
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
Chat chat = new Chat();
chat.setChat_id(rs.getInt(1));
chat.setUser_id(rs.getInt(2));
chat.setPro_id(rs.getInt(3));
chat.setC_content(rs.getString(4));
chat.setBus_reply(rs.getString(5));
chat.setC_states(rs.getInt(6));
chats.add(chat);
}
}catch (SQLException e){
e.printStackTrace();
}
return chats;
}
// 查看有关店铺编号为r_id的所有不违规评论
@Override
public List<Chat> showMyChatByBusId(int r_id) {
conn = JdbcUtils.getConnection();
String sql = "select * from chat where c_states!=4 and pro_id in(select pro_id from product where bus_id=?)";
List<Chat> chats = new ArrayList<>();
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
rs = ps.executeQuery();
while(rs.next()){
Chat chat = new Chat();
chat.setChat_id(rs.getInt(1));
chat.setUser_id(rs.getInt(2));
chat.setPro_id(rs.getInt(3));
chat.setC_content(rs.getString(4));
chat.setBus_reply(rs.getString(5));
chat.setC_states(rs.getInt(6));
chats.add(chat);
}
}catch (SQLException e){
e.printStackTrace();
}
return chats;
}
@Override
public List<Chat> showAllChatByUserSelf(int r_id) {
conn = JdbcUtils.getConnection();
String sql = "select * from chat where c_states!=4 and user_id = ?";
List<Chat> chats = new ArrayList<>();
try{
ps = conn.prepareStatement(sql);
ps.setInt(1,r_id);
rs = ps.executeQuery();
while(rs.next()){
Chat chat = new Chat();
chat.setChat_id(rs.getInt(1));
chat.setUser_id(rs.getInt(2));
chat.setPro_id(rs.getInt(3));
chat.setC_content(rs.getString(4));
chat.setBus_reply(rs.getString(5));
chat.setC_states(rs.getInt(6));
chats.add(chat);
}
}catch (SQLException e){
e.printStackTrace();
}
return chats;
}
}
main
package com.xsj;
import com.xsj.bean.Product;
import com.xsj.bean.Role;
import com.xsj.bean.Cart;
import com.xsj.bean.Indent;
import com.xsj.bean.Chat;
import com.xsj.service.RoleService;
import com.xsj.service.RoleServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class main {
// 主程序调用实际操作
public static void main(String[] args) {
RoleService roleService = new RoleServiceImpl();
Scanner sc = new Scanner(System.in);
// 1、先进行登录
System.out.println("请输入用户名,密码");
System.out.print("用户名:");
String username = sc.next();
System.out.print("密码:");
String password = sc.next();
// 分用户、管理员和商家 判断登录信息是否正确,如果正确,再判断power_id值
Role role = roleService.loginRole(username, password);
switch (role.getPower_id()) {
case 1:
// 管理员
jumpToAdmin(role);
break;
case 2:
// 商家
jumpToBussiness(role);
break;
case 3:
//用户
jumpToUser(role);
break;
}
}
private static void jumpToUser(Role role) {
System.out.println("欢迎回来,尊贵的用户:" + role.getR_name());
int user_behavior = jumpToUserBehavior(role);
while(user_behavior!=8){
user_behavior = jumpToUserBehavior(role);
}
System.out.println("尊贵的用户:"+role.getR_name()+" 已退出");
System.out.println("欢迎下次光临哦~亲");
}
private static int jumpToUserBehavior(Role role) {
RoleService roleService = new RoleServiceImpl();
Scanner sc = new Scanner(System.in);
System.out.println("------请选择执行的操作------");
System.out.println("1、修改个人信息");
System.out.println("2、查看订单");
System.out.println("3、查看购物车");
System.out.println("4、进入商城");
System.out.println("5、评价商品");
System.out.println("6、查看已发表评论");
System.out.println("7、查看所有评论");
System.out.println("8、退出登录");
int user_choice = sc.nextInt();
switch (user_choice){
case 1:
//修改个人信息
int alterRes = roleService.alterInformation(role);
while(alterRes != 5){
System.out.println("------继续修改------");
alterRes = roleService.alterInformation(role);
}
System.out.println("------您已退出修改个人信息界面------");
System.out.println("");
break;
case 2:
//查看订单
System.out.println("------订单如下------");
List<Indent> indents = roleService.showIndent(role.getR_id());
System.out.println("订单编号\t\t" + "用户编号\t\t" + "商品编号\t\t"
+"收货地址\t\t" + "发货地址\t\t" + "订单总额\t\t" + "订单状态");
for(Indent indent:indents){
System.out.println(indent.toString());
}
break;
case 3:
//查看购物车
System.out.println("------购物车如下------");
List<Cart> carts = roleService.showCart(role.getR_id());
System.out.println("购物车编号\t\t" + "用户编号\t\t" + "商品编号\t\t" + "价格\t\t"
+ "添加购物车时间");
for (Cart cart : carts) {
System.out.println(cart.toString());
}
break;
case 4:
System.out.println("");
System.out.println("------在线商城------");
List<Product> products = roleService.selectAllProduct();
System.out.println("商品ID\t\t" + "商品名称\t\t" + "商品价格\t\t" + "剩余数量\t\t"
+ "商品描述\t\t" + "最新热度\t\t"+"所属商家");
for (Product product : products) {
System.out.println(product.toString());
}
System.out.println("1、添加购物车 2、前往下单 3、退出在线商城");
int i = sc.nextInt();
if (i == 1){
// 加入购物车
addCart(role);
}
else if(i == 2) {
// 下单 输入想要买单的商品号
List<Integer> buyProductId = new ArrayList<>();
int buyproId = 0;
while(true){
System.out.println("-----请输入您想下单的商品号-----");
buyproId = sc.nextInt();
buyProductId.add(buyproId);
System.out.println("1、继续购买 2、直接付款");
int temp = sc.nextInt();
if(temp!=1){
break;
}
}
// 根据输入的商品号
goToCheckOut(role,buyProductId);
}else{
System.out.println("------您已退出在线商城------");
}
break;
case 5:
// 根据订单上的商品编号 对商品进行评论
System.out.println("请输入想要评价的商品的编号");
int chatproId = sc.nextInt();
// 判断商品是否在订单中
int isBuyPro = roleService.isBuyPro(role.getR_id(),chatproId);
if (isBuyPro == 1){
roleService.chatPro(role.getR_id(),chatproId);
}else{
System.out.println("只有购买体验后才有发言权哦~");
}
break;
case 6:
// 查看自己发表的所有评论(未被屏蔽的)
List<Chat> chats1 = roleService.showAllChatBySelf(role.getR_id());
System.out.println("评论ID\t\t" + "发表用户编号\t\t" + "评价商品编号\t\t" + "评论内容\t\t"
+ "商家回复\t\t" + "评论状态");
for (Chat chat : chats1) {
System.out.println(chat.toString());
}
break;
case 7:
// 查看所有评论(未被屏蔽的)
List<Chat> chats2 = roleService.showAllChat();
System.out.println("评论ID\t\t" + "发表用户编号\t\t" + "评价商品编号\t\t" + "评论内容\t\t"
+ "商家回复\t\t" + "评论状态");
for (Chat chat : chats2) {
System.out.println(chat.toString());
}
break;
case 8:
break;
}
return user_choice;
}
public static void addCart(Role role){
RoleService roleService = new RoleServiceImpl();
Scanner sc = new Scanner(System.in);
System.out.println("------请选择心仪的商品加入购物车------");
System.out.print("输入商品ID:");
int productId = sc.nextInt();
int res = roleService.insertCart(productId,role.getR_id());
if (res >= 1 ){
System.out.println("添加成功");
System.out.println("1、继续添加商品 2、前往下单 3、退出在线商城");
int i = sc.nextInt();
switch (i){
case 1:
// 已经加入购物车内容,可以显示出来
addCart(role);
// 每次向购物车加入物品,显示一遍购物车
List<Cart> carts = roleService.showCart(role.getR_id());
System.out.println("购物车编号\t\t" + "用户编号\t\t" + "商品编号\t\t" + "价格\t\t"
+ "添加购物车时间");
for (Cart cart : carts) {
System.out.println(cart.toString());
}
break;
case 2:
// 下单(用户编号,地址,付款金额,商家发货地址)
List<Integer> buyProIds = new ArrayList<>();
boolean temp = true;
while(temp){
System.out.println("请输入购物车内要下单的商品编号:");
int buyProId = sc.nextInt();
buyProIds.add(buyProId);
System.out.println("是否继续下单:1、继续下单 2、去结账");
int buy_choice = sc.nextInt();
switch (buy_choice){
case 1:
break;
case 2:
temp = false;
break;
}
}
goToCheckOut(role,buyProIds);
case 3:
System.out.println("------您已退出在线商城------");
break;
}
}else{
System.out.println("添加失败,请重新选择");
addCart(role);
}
}
private static void goToCheckOut(Role role, List productId) {
RoleService roleService = new RoleServiceImpl();
Scanner sc = new Scanner(System.in);
int clearRes = roleService.clearCart(role.getR_id(),productId);
if (clearRes>0){
// 根据user_id查询订单表,获取订单信息
List<String> indId = roleService.findIndentId(role.getR_id());
for(String ind_id : indId){
System.out.println("下单成功,订单号为:" + ind_id);
}
}else {
System.out.println("下单失败,请重新处理");
}
}
private static void jumpToBussiness(Role role) {
System.out.println("欢迎回来,黑心的商家:" + role.getR_name());
int bus_behavior = jumpToBusinessBehavior(role);
while(bus_behavior!=8){
bus_behavior = jumpToBusinessBehavior(role);
}
System.out.println("尊贵的商家:"+role.getR_name()+" 已退出");
System.out.println("祝您早日发财哦~亲");
}
private static int jumpToBusinessBehavior(Role role){
RoleService roleService = new RoleServiceImpl();
Scanner sc = new Scanner(System.in);
System.out.println("------请选择执行的操作------");
System.out.println("1、修改个人信息");
System.out.println("2、浏览店铺商品");
System.out.println("3、新增商品");
System.out.println("4、下架商品");
System.out.println("5、更新商品");
System.out.println("6、查看店铺评论");
System.out.println("7、回复评论");
System.out.println("8、退出登录");
int business_choice = sc.nextInt();
switch (business_choice){
case 1:
int alterRes = roleService.alterInformation(role);
while(alterRes != 5){
System.out.println("------继续修改------");
alterRes = roleService.alterInformation(role);
}
System.out.println("------您已退出修改个人信息界面------");
System.out.println("");
break;
case 2:
// 浏览本店铺商品
List<Product> products = roleService.showMyProdeuct(role.getR_id());
System.out.println("商品ID\t\t" + "商品名称\t\t" + "商品价格\t\t" + "剩余数量\t\t"
+ "商品描述\t\t" + "最新热度\t\t"+"所属商家");
for(Product product:products){
System.out.println(product.toString());
}
break;
case 3:
// 新增商品
int addRes = roleService.addProduct(role.getR_id());
while(addRes==0){
System.out.println("-----是否重新上架新商品-----");
System.out.println("-----1、重新上架 2、不上架-----");
int bus_choice = sc.nextInt();
if(bus_choice == 1){
addRes = roleService.addProduct(role.getR_id());
}
}
break;
case 4:
// 下架商品
int outRes = roleService.outOfShelfPro(role.getR_id());
while(outRes==0){
System.out.println("-----是否重新下架该商品-----");
System.out.println("-----1、重新下架 2、不下架-----");
int bus_choice = sc.nextInt();
if(bus_choice == 1){
outRes = roleService.addProduct(role.getR_id());
}
}
break;
case 5:
// 更新商品
// 先下架,修改好后,重新上架
int UpdateRes1 = roleService.outOfShelfPro(role.getR_id());
while(UpdateRes1==0){
System.out.println("-----是否重新下架该商品-----");
System.out.println("-----1、重新下架 2、不下架-----");
int bus_choice = sc.nextInt();
if(bus_choice == 1){
UpdateRes1 = roleService.addProduct(role.getR_id());
}
}
int UpdateRes2 = roleService.addProduct(role.getR_id());
while(UpdateRes2==0){
System.out.println("-----是否重新上架新商品-----");
System.out.println("-----1、重新上架 2、不上架-----");
int bus_choice = sc.nextInt();
if(bus_choice == 1){
UpdateRes2 = roleService.addProduct(role.getR_id());
}
}
break;
case 6:
// 查看本店铺商品评论
List<Chat> chats = roleService.showMyChat(role.getR_id());
System.out.println("评论ID\t\t" + "发表用户编号\t\t" + "评价商品编号\t\t" + "评论内容\t\t"
+ "商家回复\t\t" + "评论状态");
for (Chat chat : chats) {
System.out.println(chat.toString());
}
break;
case 7:
// 回复评论
System.out.println("请输入想要回复的评论编号");
int chatId = sc.nextInt();
// 判断是不是该店铺的商品
int isMyPro = roleService.isMyPro(role.getR_id(),chatId);
if (isMyPro==1){
System.out.println("请输入想要回复的评论,例:谢谢宝贝的支持哦");
String reply = sc.next();
int replyRes = roleService.insertReplyRes(chatId,reply);
if(replyRes==1){
System.out.println("-----回复评论成功!");
}else{
System.out.println("-----回复评论失败!");
}
}else {
System.out.println("该商品不是本店的哦~,无需回复");
}
break;
case 8:
break;
}
return business_choice;
}
private static void jumpToAdmin(Role role) {
System.out.println("欢迎回来,高贵的管理员:" + role.getR_name());
int admin_behavior = jumpToAdminBehavior(role);
while(admin_behavior!=8){
admin_behavior = jumpToAdminBehavior(role);
}
System.out.println("尊贵的管理员:"+role.getR_name()+" 已退出");
System.out.println("工作顺利,早点休息哦~亲");
}
private static int jumpToAdminBehavior(Role role){
RoleService roleService = new RoleServiceImpl();
Scanner sc = new Scanner(System.in);
System.out.println("------请选择执行的操作------");
System.out.println("1、修改个人信息");
System.out.println("2、浏览商城");
System.out.println("3、下架商品");
System.out.println("4、查询订单");
System.out.println("5、修改订单");
System.out.println("6、查看评论");
System.out.println("7、审核评论");
System.out.println("8、退出登录");
int admin_choice = sc.nextInt();
switch (admin_choice){
case 1:
// 修改个人信息
int alterRes = roleService.alterInformation(role);
while(alterRes != 5){
System.out.println("------继续修改------");
alterRes = roleService.alterInformation(role);
}
System.out.println("------您已退出修改个人信息界面------");
System.out.println("");
break;
case 2:
// 浏览商城
System.out.println("");
System.out.println("------在线商城------");
List<Product> products = roleService.selectAllProduct();
System.out.println("商品ID\t\t" + "商品名称\t\t" + "商品价格\t\t" + "剩余数量\t\t"
+ "商品描述\t\t" + "最新热度\t\t"+"所属商家");
for (Product product : products) {
System.out.println(product.toString());
}
System.out.println("");
break;
case 3:
// 下架商品
System.out.println("请输入下架商品所属商家编号");
int bus_id = sc.nextInt();
int outRes = roleService.outOfShelfPro(bus_id);
while(outRes==0){
System.out.println("-----是否重新下架该商品-----");
System.out.println("-----1、重新下架 2、不下架-----");
int bus_choice = sc.nextInt();
if(bus_choice == 1){
outRes = roleService.outOfShelfPro(bus_id);
}
}
break;
case 4:
// 查询订单
List<Indent> indents = roleService.showAllIndent();
System.out.println("订单编号\t\t" + "用户编号\t\t" + "商品编号\t\t"
+"收货地址\t\t" + "发货地址\t\t" + "订单总额\t\t" + "订单状态");
for(Indent indent:indents){
System.out.println(indent.toString());
}
break;
case 5:
// 修改订单
System.out.println("请输入要修改订单的订单号");
String indId = sc.next();
int alterIndRes = roleService.alterIndentInformation(indId);
while(alterIndRes != 7){
System.out.println("------继续修改------");
alterIndRes = roleService.alterIndentInformation(indId);
}
System.out.println("------您已退出修改订单信息界面------");
System.out.println("");
break;
case 6:
// 查看全部评论
List<Chat> chats = roleService.showAllChatByAdmin();
System.out.println("评论ID\t\t" + "发表用户编号\t\t" + "评价商品编号\t\t" + "评论内容\t\t"
+ "商家回复\t\t" + "评论状态");
for (Chat chat : chats) {
System.out.println(chat.toString());
}
break;
case 7:
// 审核评论
System.out.println("输入违规的评论编号");
int illegalChatId = sc.nextInt();
int examineRes = roleService.examIllegalChat(illegalChatId);
System.out.println("感谢您为净化网络贡献一份力量哦~");
break;
case 8:
// 退出登录
break;
}
return admin_choice;
}
}