目录
2.6 TOrderDetailController.java
一、com.test.annotation 注解层
1.1 RequirePermission.java
package com.test.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD}) // 定义了该注解可以用在哪些地方,这里指定它只能用在方法上
@Retention(RetentionPolicy.RUNTIME) // 指定了注解的生命周期,这里设置为运行时,使其在程序运行期仍然可用
public @interface RequirePermission {
String value(); // 注解中定义了一个名为"value"的成员,用于传递一些额外的信息
}
二、com.test.controller 控制器层
2.1 LoginController.java
package com.test.controller;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.digest.DigestUtils; // 导入密码加密相关的类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.domain.Permission;
import com.test.domain.User;
import com.test.service.PermissionService;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
public class LoginController {
@Autowired
private UserService userService; // 用户服务类,用于处理用户相关业务
@Autowired
private PermissionService permissionService; // 权限服务类,用于处理权限相关业务
@RequestMapping("login")
@ResponseBody
public Result login(String userName, String password, HttpSession session) {
// 处理用户登录请求
User user = this.userService.findUserByUserName(userName); // 通过用户名查找用户
if (user == null) {
// 用户不存在
return Result.fail(404, "用户不存在");
}
if (!user.getLoginName().equals("admin") && user.getStatus().equals("1")) {
// 用户已被禁用
return Result.fail(404, "用户已经被禁用");
}
if (!DigestUtils.md5Hex(password).equals(user.getLoginPwd())) {
// 用户密码不正确
return Result.fail(403, "用户密码不正确");
}
// 将登录用户对象存储在会话中
session.setAttribute("loginUser", user);
return Result.succeed("manageUI"); // 返回成功信息和视图名称
}
@RequestMapping("logout")
public String logout(HttpSession session) {
// 处理用户注销请求
session.invalidate(); // 使会话失效,注销用户
return "redirect:/index.jsp"; // 重定向到登录页面
}
@RequestMapping("manageUI")
public String manageUI(HttpSession session) {
// 展示管理用户界面
Object obj = session.getAttribute("loginUser"); // 获取会话中的登录用户对象
if (obj == null) {
return "redirect:/index.jsp"; // 如果用户未登录,重定向到登录页面
}
User user = (User) obj; // 转换为User对象
// 获取用户的权限信息
Map<String, List<Permission>> permissionMap = this.permissionService.getPermissionMapByUserId(user.getId);
// 设置用户的菜单和权限列表
user.setMenuList(permissionMap.get("menuList"));
user.setPermissionList(permissionMap.get("permissionList"));
return "manageUI"; // 返回管理界面的视图名称
}
}
这段代码是一个简单的Spring MVC控制器类,主要用于处理用户登录和注销等功能。以下是对代码的简要解释:
1. **包导入**:在代码的开头,导入了一些需要使用的Java类和包,包括了Servlet和Spring框架的相关类,以及Apache Commons Codec库。
2. **类定义**:定义了名为 `LoginController` 的Spring MVC控制器类,使用 `@Controller` 注解标记,表明这是一个控制器类,它可以处理HTTP请求。
3. **成员变量**:该类包含了两成员变量,分别是 `userService` 和 `permissionService`,它们通过 `@Autowired` 注解注入了对应的服务类。这允许控制器类使用这些服务来处理业务逻辑。
4. **请求映射方法**:这个类包含了多个请求映射方法,每个方法都使用 `@RequestMapping` 注解标记,指定了可以处理的HTTP请求路径。以下是其中几个方法的解释:
- `login` 方法:处理用户登录请求。接收用户名(userName)和密码(password)作为参数,通过调用 `userService` 的方法验证用户。如果验证通过,将用户对象存储在会话(HttpSession)中,并返回一个包含成功信息的 `Result` 对象,通常用于前端异步请求。
- `logout` 方法:处理用户注销请求。通过使会话失效(invalidate),用户被注销,然后重定向到登录页面。
- `manageUI` 方法:用于管理用户界面。它首先检查会话中是否存在登录用户对象,如果不存在则重定向到登录页面。如果用户已经登录,它会获取用户的权限信息并将其保存在用户对象中,然后返回管理界面的视图名称。
5. **返回值类型**:大部分方法的返回值类型是字符串,这些字符串通常表示视图的逻辑名称,Spring MVC将根据这些名称找到对应的视图模板进行渲染。
这个控制器类主要负责用户的登录、注销以及管理界面的展示。登录时,它会验证用户的用户名和密码,如果通过验证,将用户对象存储在会话中,以便在用户访问其他受限资源时进行权限检查。管理界面则用于展示用户相关信息和权限菜单。
2.2 PermissionController.java
package com.test.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.Permission;
import com.test.service.PermissionService;
import com.test.vo.Result;
@Controller
@RequestMapping("permission")
public class PermissionController {
@Autowired
private PermissionService permissionService; // 用于处理权限相关业务的服务类
@RequirePermission("permission:listUI")
@RequestMapping("listUI")
public String listUI() {
return "permission/listUI"; // 返回用于显示权限列表的视图名称
}
@RequirePermission("permission:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search) {
// 处理权限列表请求,根据分页和搜索条件获取权限列表数据
PageInfo<Permission> pageInfo = this.permissionService.getListByPage(offset / limit + 1, limit, search);
return Result.succeed(pageInfo); // 返回包含权限列表数据的成功结果
}
@RequirePermission("role:setPermission")
@RequestMapping("getPermissionListWithChecked/{roleId}")
@ResponseBody
public Result getPermissionListWithChecked(@PathVariable("roleId") int roleId) {
// 处理获取拥有的权限列表请求,传入角色ID,获取该角色拥有的权限列表数据
List<Permission> permissionList = this.permissionService.getPermissionListByRoleId(roleId);
return Result.succeed(permissionList); // 返回包含拥有的权限列表数据的成功结果
}
// 保存、修改、删除方法
@RequestMapping("saveUI")
public String saveUI(Integer id, HttpServletRequest request) {
// 处理进入权限保存/修改页面请求,根据权限ID(id)获取权限对象并传递给前端视图
if (id != null) {
Permission permission = this.permissionService.getById(id);
if (permission != null) {
request.setAttribute("permission", permission);
}
}
// 获取不包含按钮的权限列表,以及对权限进行递归格式化
List<Permission> parentList = this.permissionService.getPermissionWithoutButton();
List<Permission> rt = new ArrayList<>();
recurData(parentList, rt, "━━");
request.setAttribute("parentList", rt);
return "permission/saveUI"; // 返回权限保存/修改页面的视图名称
}
@RequirePermission("permission:add;permission:update")
@RequestMapping("save")
public String save(Permission permission) {
// 处理保存权限信息的请求,包括添加新权限和更新已存在权限
if (permission.getId() != null) {
this.permissionService.update(permission); // 更新已存在的权限
} else {
this.permissionService.save(permission); // 添加新权限
}
return "redirect:/permission/listUI"; // 保存成功后重定向到权限列表页面
}
@RequirePermission("permission:delete")
@RequestMapping("delete/{id}")
@ResponseBody
public Result delete(@PathVariable("id") int id) {
// 处理删除权限的请求,根据权限ID进行删除
try {
this.permissionService.deleteById(id);
return Result.succeed(); // 返回成功结果
} catch (Exception e) {
e.printStackTrace();
return Result.fail(403, e.getMessage()); // 返回失败结果,附带异常信息
}
}
private void recurData(List<Permission> source, List<Permission> dest, String prefix) {
// 辅助方法,用于递归格式化权限列表,加入前缀以表示层级关系
for (Permission permission : source) {
permission.setName("┣" + prefix + permission.getName());
dest.add(permission);
if (permission.getChildren() != null && !permission.getChildren().isEmpty()) {
recurData(permission.getChildren(), dest, prefix + prefix);
}
}
}
}
这段代码是一个Spring MVC控制器,主要用于处理与权限管理相关的操作。以下是代码的主要作用:
1. **权限管理**:控制器负责管理系统的权限,包括权限的创建、修改、删除以及展示权限列表等操作。
2. **前端页面渲染**:提供了一些与前端页面相关的请求处理方法,如 `listUI` 和 `saveUI`,用于显示权限列表和权限编辑页面。
3. **权限检查**:通过注解 `@RequirePermission` 标记一些请求处理方法,以实现对用户权限的检查。这可以确保只有具有相应权限的用户可以访问这些方法。
4. **数据交互**:通过请求和响应,与前端页面交互,展示权限列表、处理权限编辑、保存和删除操作。
5. **递归处理**:控制器中的 `recurData` 方法用于递归格式化权限列表,以显示层级关系。这在前端界面中用于显示权限菜单等。
总之,这段代码是一个权限管理模块的控制器,用于实现权限的创建、修改、删除,以及对权限列表的展示和前端交互,同时也实现了权限的动态检查,确保只有授权用户可以执行相应的操作。
2.3 RoleController.java
package com.test.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.Role;
import com.test.service.RoleService;
import com.test.vo.Result;
@Controller
@RequestMapping("role")
public class RoleController {
@Autowired
private RoleService roleService; // 用于处理角色相关业务的服务类
@RequirePermission("role:listUI")
@RequestMapping("listUI")
public String listUI() {
// 显示角色列表的页面,该方法由前端请求触发
return "role/listUI"; // 返回角色列表视图的逻辑名称
}
@RequirePermission("role:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search) {
// 处理获取角色列表的请求,包括分页和搜索条件
PageInfo<Role> pageInfo = this.roleService.getListByPage(offset / limit + 1, limit, search);
return Result.succeed(pageInfo); // 返回包含角色列表数据的成功结果
}
@RequirePermission("user:setRole")
@RequestMapping("getRoleListWithSelected/{userId}")
@ResponseBody
public Result getRoleListWithSelected(@PathVariable("userId") int userId) {
// 处理获取与特定用户相关的角色列表的请求
List<Role> roleList = this.roleService.getRoleListByUserId(userId);
return Result.succeed(roleList); // 返回包含选定的角色列表数据的成功结果
}
// 保存、修改、删除方法
@RequestMapping("saveUI")
public String saveUI(Integer id, HttpServletRequest request) {
// 处理进入角色保存/修改页面的请求,根据角色ID(id)获取角色对象并传递给前端视图
if (id != null) {
Role role = this.roleService.getById(id);
if (role != null) {
request.setAttribute("role", role);
}
}
return "role/saveUI"; // 返回角色保存/修改页面的视图名称
}
@RequirePermission("role:add;role:update")
@RequestMapping("save")
public String save(Role role) {
// 处理保存角色信息的请求,包括添加新角色和更新已存在角色
if (role.getId() == null) {
this.roleService.save(role); // 添加新角色
} else {
this.roleService.update(role); // 更新已存在的角色
}
return "redirect:/role/listUI"; // 保存成功后重定向到角色列表页面
}
@RequirePermission("role:delete")
@RequestMapping("delete/{ids}")
@ResponseBody
public Result delete(@PathVariable("ids") String ids) {
// 处理删除角色的请求,根据角色ID进行删除
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
this.roleService.deleteById(Integer.parseInt(idsStr[0])); // 单个角色删除
} else {
this.roleService.deleteBatchByIds(idsStr); // 批量角色删除
}
return Result.succeed(); // 返回成功结果
}
@RequirePermission("role:setPermission")
@RequestMapping("setPermission")
@ResponseBody
public Result setPermission(int roleId, String permissionIds) {
// 处理设置角色权限的请求,通过传递角色ID和权限ID列表进行关联
this.roleService.saveRolePermission(roleId, permissionIds);
return Result.succeed(); // 返回成功结果
}
}
这段代码是一个Spring MVC控制器,主要用于处理与角色管理相关的操作。以下是代码的主要作用:
1. **角色管理**:控制器负责管理系统的角色,包括角色的创建、修改、删除以及展示角色列表等操作。
2. **前端页面渲染**:提供了一些与前端页面相关的请求处理方法,如 `listUI` 和 `saveUI`,用于显示角色列表和角色编辑页面。
3. **权限检查**:通过注解 `@RequirePermission` 标记一些请求处理方法,以实现对用户权限的检查。这可以确保只有具有相应权限的用户可以访问这些方法。
4. **数据交互**:通过请求和响应,与前端页面交互,展示角色列表、处理角色编辑、保存和删除操作。
5. **分页处理**:通过 `offset` 和 `limit` 参数进行分页操作,实现分页查询角色列表。
6. **与用户关联**:提供方法 `getRoleListWithSelected` 用于获取与特定用户相关的角色列表,通常用于用户角色分配。
7. **设置角色权限**:提供方法 `setPermission` 用于设置角色的权限,通过传递角色ID和权限ID列表进行关联。
总之,这段代码是一个角色管理模块的控制器,用于实现角色的创建、修改、删除,以及对角色列表的展示和前端交互,同时也实现了权限的动态检查,确保只有授权用户可以执行相应的操作。
2.4 TBusinessController.java
package com.test.controller;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger; // 导入日志处理相关类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind(@RequestMapping);
import org.springframework.web.multipart.MultipartFile; // 导入文件上传相关类
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TBusiness;
import com.test.domain.User;
import com.test.service.TBusinessService;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("business")
public class TBusinessController {
private static Logger logger = Logger.getLogger(TBusinessController.class); // 创建日志记录器对象
@Autowired
private TBusinessService userService; // 用户服务类,用于处理用户相关业务
@Autowired
private UserService uService; // 用户服务类,用于处理用户相关业务
@RequirePermission("business:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
// 显示商家列表的页面,该方法由前端请求触发
return "business/listUI"; // 返回商家列表视图的逻辑名称
}
@RequirePermission("business:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
logger.info("查询数据集开始");
User u = (User) session.getAttribute("loginUser");
PageInfo<TBusiness> pageInfo = new PageInfo<TBusiness>();
if (u != null && u.getuType().equals("1")) {
pageInfo = this.userService.getListByCustomerPage(offset / limit + 1, limit, u.getId(), search);
} else {
pageInfo = this.userService.getListByPage(offset / limit + 1, limit, search);
}
return Result.succeed(pageInfo); // 返回包含商家列表数据的成功结果
}
// 保存、修改、删除方法
@RequestMapping("saveUI")
public String saveUI(Integer id, HttpServletRequest request) {
// 处理进入商家保存/修改页面的请求,根据商家ID(id)获取商家对象并传递给前端视图
if (id != null) {
TBusiness user = this.userService.getById(id);
if (user != null) {
request.setAttribute("business", user);
}
}
// 获取用户列表,过滤条件为用户状态为0和用户类型为5的用户
List<User> uList = uService.getList().stream().filter(ls -> ls.getStatus() == 0 && ls.getuType().equals("5")).collect(Collectors.toList());
request.setAttribute("uList", uList);
// 获取用户列表,过滤条件为用户状态为0和用户类型为2的用户
List<User> tList = uService.getList().stream().filter(ls -> ls.getStatus() == 0 && ls.getuType().equals("2")).collect(Collectors.toList());
request.setAttribute("tList", tList);
return "business/saveUI"; // 返回商家保存/修改页面的视图名称
}
@RequirePermission("business:add;business:update")
@RequestMapping("save")
public String add(TBusiness user, HttpServletRequest request, MultipartFile file, HttpSession session) throws IllegalStateException, IOException {
// 处理保存商家信息的请求,包括添加新商家和更新已存在商家
User u = (User) session.getAttribute("loginUser");
if (user.getBusinessId() != null) {
this.userService.update(user); // 更新已存在的商家
} else {
user.setCreteBy(u.getId().toString());
user.setCreateTime(new Date());
this.userService.save(user); // 添加新商家
}
return "redirect:/business/listUI"; // 保存成功后重定向到商家列表页面
}
@RequirePermission("business:delete")
@RequestMapping("delete/{ids}")
@ResponseBody
public Result delete(@PathVariable("ids") String ids) {
// 处理删除商家的请求,根据商家ID进行删除
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
this.userService.deleteById(Integer.parseInt(idsStr[0]));
} else {
for (int i = 0; i < idsStr.length; i++) {
this.userService.deleteById(Integer.parseInt(idsStr[i]));
}
}
return Result.succeed(); // 返回成功结果
}
}
这段代码是一个Spring MVC控制器,主要用于处理与商家(TBusiness)管理相关的操作。以下是代码的具体作用:
1. **商家管理**:控制器负责管理系统中的商家信息,包括商家的创建、修改、删除以及展示商家列表等操作。
2. **前端页面渲染**:提供了一些与前端页面相关的请求处理方法,如 `listUI` 和 `saveUI`,用于显示商家列表和商家编辑页面。
3. **权限检查**:通过注解 `@RequirePermission` 标记一些请求处理方法,以实现对用户权限的检查。这确保只有具有相应权限的用户可以访问这些方法。
4. **数据交互**:通过请求和响应,与前端页面交互,展示商家列表、处理商家编辑、保存和删除操作。
5. **分页处理**:通过 `offset` 和 `limit` 参数进行分页操作,实现分页查询商家列表。
6. **文件上传**:支持文件上传,通过 `MultipartFile` 处理上传的文件数据。
7. **日志记录**:使用日志记录器(Logger)记录系统日志,以便跟踪和分析应用程序的运行。
8. **与用户关联**:通过获取登录用户的信息,根据用户类型,决定是显示所有商家还是仅显示与特定客户相关的商家。
9. **商家关联用户**:提供方法 `getRoleListWithSelected` 用于获取商家所属的用户列表,通常用于商家分配。
总之,这段代码是一个商家管理模块的控制器,用于实现商家的创建、修改、删除,以及对商家列表的展示和前端交互,同时也实现了权限的动态检查,确保只有授权用户可以执行相应的操作。此外,它还支持文件上传和日志记录,以提高系统的功能和可维护性。
2.5 TOrderController.java
package com.test.controller;
import java.io.IOException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger; // 导入日志处理相关类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils; // 导入Spring工具类
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; // 导入文件上传相关类
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TOrder;
import com.test.domain.TOrderDetail;
import com.test.domain.TProduct;
import com.test.domain.User;
import com.test.service.TOrderDetailService;
import com.test.service.TOrderService;
import com.test.service.TProductService;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("order")
public class TOrderController {
private static Logger logger = Logger.getLogger(TOrderController.class); // 创建日志记录器对象
@Autowired
private TOrderService userService; // 订单服务类,用于处理订单相关业务
@Autowired
private UserService uService; // 用户服务类,用于处理用户相关业务
@Autowired
private TProductService productService; // 产品服务类,用于处理产品相关业务
@Autowired
private TOrderDetailService orderDetailService; // 订单详情服务类,用于处理订单详情相关业务
@RequirePermission("order:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
// 显示订单列表的页面,该方法由前端请求触发
return "order/listUI"; // 返回订单列表视图的逻辑名称
}
@RequirePermission("order:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
logger.info("查询数据集开始");
User u = (User) session.getAttribute("loginUser");
PageInfo<TOrder> pageInfo = new PageInfo<TOrder>();
if (u != null && u.getuType().equals("1")) {
pageInfo = this.userService.getListByCustomerPage(offset / limit + 1, limit, u.getId(), search);
} else {
pageInfo = this.userService.getListByPage(offset / limit + 1, limit, search);
}
return Result.succeed(pageInfo); // 返回包含订单列表数据的成功结果
}
// 保存、修改、删除方法
@RequestMapping("saveUI")
public String saveUI(Integer id, HttpServletRequest request) {
// 处理进入订单保存/修改页面的请求,根据订单ID(id)获取订单对象并传递给前端视图
if (id != null) {
TOrder user = this.userService.getById(id);
if (user != null) {
request.setAttribute("order", user);
}
}
// 获取产品列表
List<TProduct> productList = this.productService.getList();
request.setAttribute("productList", productList);
// 获取用户列表,过滤条件为用户状态为0和用户类型为5的用户
List<User> uList = uService.getList().stream().filter(ls -> ls.getStatus() == 0 && ls.getuType().equals("5")).collect(Collectors.toList());
request.setAttribute("uList", uList);
// 获取用户列表,过滤条件为用户状态为0和用户类型为2的用户
List<User> tList = uService.getList().stream().filter(ls -> ls.getStatus() == 0 && ls.getuType().equals("2")).collect(Collectors.toList());
request.setAttribute("tList", tList);
return "order/saveUI"; // 返回订单保存/修改页面的视图名称
}
@RequirePermission("order:add;order:update")
@RequestMapping("save")
public String add(TOrder user, HttpServletRequest request, MultipartFile file, HttpSession session) throws IllegalStateException, IOException {
// 处理保存订单信息的请求,包括添加新订单和更新已存在订单
TProduct product = this.productService.getById(user.getProductId());
User u = (User) session.getAttribute("loginUser");
if (user.getOrderId() != null) {
this.userService.update(user); // 更新已存在的订单
} else {
user.setCreteBy(u.getId().toString());
user.setDhTime(new Date().toLocaleString());
Date currdate = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(currdate);
ca.add(Calendar.DATE, 7);
currdate = ca.getTime();
user.setJhTime(currdate.toLocaleString());
user.setpProdname(product.getProductName());
if (product.getpZkamount() != null) {
user.setFkAmount(user.getOrderNum() * product.getpZkamount());
} else {
user.setFkAmount(user.getOrderNum() * product.getpAmount());
}
this.userService.save(user); // 添加新订单
}
return "redirect:/order/listUI"; // 保存成功后重定向到订单列表页面
}
@RequirePermission("order:delete")
@RequestMapping("delete/{ids}")
@ResponseBody
public Result delete(@PathVariable("ids") String ids) {
// 处理删除订单的请求,根据订单ID进行删除
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
List<TOrderDetail> uList = orderDetailService.getList().stream().filter(ls -> ls.getOrderId() == Integer.parseInt(idsStr[0])).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(uList)) {
this.orderDetailService.deleteById(uList.get(0).getdId());
}
this.userService.deleteById(Integer.parseInt(idsStr[0]));
} else {
for (int i = 0; i < idsStr.length; i++) {
Integer orderid = Integer.parseInt(idsStr[i]);
List<TOrderDetail> uList = orderDetailService.getList().stream().filter(ls -> ls.getOrderId() == orderid).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(uList)) {
for (TOrderDetail detail : uList) {
this.orderDetailService.deleteById(detail.getdId());
}
}
this.userService.deleteById(Integer.parseInt(idsStr[i]));
}
}
return Result.succeed(); // 返回成功结果
}
@RequestMapping("detailOrderUI")
public String detailOrderUI(Integer id, HttpServletRequest request) {
if (id != null) {
TOrder user = this.userService.getById(id);
if (user != null) {
request.setAttribute("order", user);
}
}
List<TOrderDetail> productList = this.orderDetailService.getList();
List<TOrderDetail> uList = orderDetailService.getList().stream().filter(ls -> ls.getOrderId() == id).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(uList)) {
request.setAttribute("detailList", productList);
}
return "order/detailOrderUI"; // 返回订单详情视图的逻辑名称
}
}
这段代码是一个Spring MVC控制器,主要用于处理与订单(TOrder)和订单详情(TOrderDetail)管理相关的操作。以下是代码的具体作用:
1. **订单管理**:控制器负责管理系统中的订单信息,包括订单的创建、修改、删除以及展示订单列表等操作。订单可以包括有关产品、用户、发货时间、计划到货时间等信息。
2. **前端页面渲染**:提供了一些与前端页面相关的请求处理方法,如 `listUI`、`saveUI` 和 `detailOrderUI`,用于显示订单列表、订单编辑页面和订单详情页面。
3. **权限检查**:通过注解 `@RequirePermission` 标记一些请求处理方法,以实现对用户权限的检查。这确保只有具有相应权限的用户可以访问这些方法。
4. **数据交互**:通过请求和响应,与前端页面交互,展示订单列表、处理订单编辑、保存和删除操作。
5. **分页处理**:通过 `offset` 和 `limit` 参数进行分页操作,实现分页查询订单列表。
6. **文件上传**:支持文件上传,通过 `MultipartFile` 处理上传的文件数据,可能用于订单相关文件的附件上传。
7. **日志记录**:使用日志记录器(Logger)记录系统日志,以便跟踪和分析应用程序的运行。
8. **与用户关联**:通过获取登录用户的信息,根据用户类型,决定是显示所有订单还是仅显示与特定客户相关的订单。
9. **订单详情**:提供方法 `detailOrderUI` 用于查看订单详情,包括与订单相关的订单详情信息。
总之,这段代码是一个订单管理模块的控制器,用于实现订单的创建、修改、删除,以及对订单列表和订单详情的展示和前端交互,同时也实现了权限的动态检查,确保只有授权用户可以执行相应的操作。此外,它还支持文件上传和日志记录,以提高系统的功能和可维护性。
2.6 TOrderDetailController.java
package com.test.controller;
import java.io.IOException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TOrder;
import com.test.domain.TOrderDetail;
import com.test.domain.TProduct;
import com.test.domain.User;
import com.test.service.TOrderDetailService;
import com.test.service.TOrderService;
import com.test.service.TProductService;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("orderdetail")
public class TOrderDetailController {
private static Logger logger = Logger.getLogger(TOrderDetailController.class); // 创建日志记录器对象
@Autowired
private TOrderDetailService userService; // 订单详情服务类,用于处理订单详情相关业务
@Autowired
private UserService uService; // 用户服务类,用于处理用户相关业务
@Autowired
private TProductService productService; // 产品服务类,用于处理产品相关业务
@RequirePermission("orderdetail:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
// 显示订单详情列表的页面,该方法由前端请求触发
return "orderdetail/listUI"; // 返回订单详情列表视图的逻辑名称
}
@RequirePermission("orderdetail:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
// 处理分页查询订单详情列表的请求
logger.info("查询数据集开始"); // 记录日志
User u = (User) session.getAttribute("loginUser");
PageInfo<TOrderDetail> pageInfo = new PageInfo<TOrderDetail>();
if (u != null && u.getuType().equals("1")) {
pageInfo = this.userService.getListByCustomerPage(offset / limit + 1, limit, u.getId(), search);
} else {
pageInfo = this.userService.getListByPage(offset / limit + 1, limit, search);
}
return Result.succeed(pageInfo); // 返回包含订单详情列表数据的成功结果
}
}
这段代码是一个Spring MVC控制器,主要用于处理订单详情(TOrderDetail)的管理操作。以下是代码的具体作用:
1. **订单详情管理**:此控制器负责管理系统中的订单详情信息,包括订单详情的创建、修改、删除以及展示订单详情列表等操作。订单详情通常包括有关产品、数量、价格、订单关联等信息。
2. **前端页面渲染**:提供了一些与前端页面相关的请求处理方法,如 `listUI`,用于显示订单详情列表的页面。
3. **权限检查**:通过注解 `@RequirePermission` 标记一些请求处理方法,以实现对用户权限的检查。这确保只有具有相应权限的用户可以访问这些方法。
4. **数据交互**:通过请求和响应,与前端页面交互,展示订单详情列表,处理订单详情的创建、修改和删除操作。
5. **分页处理**:通过 `offset` 和 `limit` 参数进行分页操作,实现分页查询订单详情列表。
6. **与用户关联**:通过获取登录用户的信息,根据用户类型,决定是显示所有订单详情还是仅显示与特定客户相关的订单详情。
总之,这段代码是一个订单详情管理模块的控制器,用于实现订单详情的创建、修改、删除,以及对订单详情列表的展示和前端交互,同时也实现了权限的动态检查,确保只有授权用户可以执行相应的操作。它还支持分页查询和与用户关联,以根据用户类型动态展示相关的订单详情信息。
2.7 TProductController.java
package com.test.controller;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory @Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TBusiness;
import com.test.domain.TProduct;
import com.test.domain.TThreshold;
import com.test.domain.User;
import com.test.service.TBusinessService;
import com.test.service.TProductService;
import com.test.service.TThresholdService;
import com.test.service.UserService;
import com.test.vo.Result;
// 控制器类,用于处理产品相关的请求
@Controller
@RequestMapping("product")
public class TProductController {
// 创建一个日志记录器
private static Logger logger = Logger.getLogger(TProductController.class);
@Autowired
private TProductService productService; // 自动注入产品服务
@Autowired
private TThresholdService shoppingCartService; // 自动注入购物车服务
@Autowired
private UserService userService; // 自动注入用户服务
@Autowired
private TBusinessService businessService; // 自动注入商家服务
// 显示产品列表页面
@RequirePermission("product:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
return "product/listUI";
}
// 处理产品列表请求,并返回JSON响应
@RequirePermission("product:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
logger.info("查询数据集开始");
User user = (User) session.getAttribute("loginUser");
PageInfo<TProduct> pageInfo = new PageInfo<TProduct>();
if (user != null && user.getuType().equals("1")) {
pageInfo = productService.getListByCustomerPage(offset / limit + 1, limit, user.getId(), search);
} else {
pageInfo = productService.getListByPage(offset / limit + 1, limit, search);
}
return Result.succeed(pageInfo);
}
// 保存/修改/删除方法
// 显示保存或修改产品页面
@RequestMapping("saveUI")
public String saveUI(Integer id, HttpServletRequest request) {
if (id != null) {
TProduct product = productService.getById(id);
if (product != null) {
request.setAttribute("product", product);
}
}
List<TBusiness> businessList = businessService.getList();
request.setAttribute("businessList", businessList);
List<User> userList = userService.getList().stream().filter(user -> user.getStatus() == 0 && user.getuType().equals("5")).collect(Collectors.toList());
request.setAttribute("userList", userList);
List<User> technicianList = userService.getList().stream().filter(user -> user.getStatus() == 0 && user.getuType().equals("2")).collect(Collectors.toList());
request.setAttribute("technicianList", technicianList);
return "product/saveUI";
}
// 处理保存或修改产品的请求
@RequirePermission("product:add;product:update")
@RequestMapping("save")
public String add(TProduct product, HttpServletRequest request, MultipartFile file, HttpSession session) throws IllegalStateException, IOException {
TBusiness business = businessService.getById(product.getBusinessId());
User user = (User) session.getAttribute("loginUser");
if (product.getProductId() != null) {
product.setBusinessName(business.getBusinessName());
productService.update(product);
} else {
product.setBusinessName(business.getBusinessName());
product.setCreteBy(user.getId().toString());
product.setCreateTime(new Date());
productService.save(product);
}
return "redirect:/product/listUI";
}
// 处理删除产品的请求
@RequirePermission("product:delete")
@RequestMapping("delete/{ids}")
@ResponseBody
public Result delete(@PathVariable("ids") String ids) {
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
productService.deleteById(Integer.parseInt(idsStr[0]));
} else {
for (int i = 0; i < idsStr.length; i++) {
productService.deleteById(Integer.parseInt(idsStr[i]));
}
}
return Result.succeed();
}
// 处理产品设为热销的请求
@RequestMapping("sirexiao/{ids}")
@ResponseBody
public Result sirexiao(@PathVariable("ids") String ids) {
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
TProduct product = productService.getById(Integer.parseInt(idsStr[0]));
product.setIsrexiao("是");
productService.update(product);
} else {
for (int i = 0; i < idsStr.length; i++) {
TProduct product = productService.getById(Integer.parseInt(idsStr[i]));
product.setIsrexiao("是");
productService.update(product);
}
}
return Result.succeed();
}
//添加购物车
@RequestMapping("addshopping/{ids}")
@ResponseBody
public Result addshopping(@PathVariable("ids") String ids, HttpSession session) {
User user = (User) session.getAttribute("loginUser");
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
TProduct product = productService.getById(Integer.parseInt(idsStr[0]));
Integer productId = Integer.parseInt(idsStr[0]);
List<TThreshold> cartList = shoppingCartService.getList().stream().filter(cart -> cart.getProductId() == productId && cart.getCreteBy().equals(user.getId().toString())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(cartList)) {
TThreshold cart = cartList.get(0);
cart.setOrderNum(cartList.get(0).getOrderNum() + 1);
shoppingCartService.update(cart);
} else {
TThreshold cart = new TThreshold();
cart.setCreteBy(user.getId().toString());
cart.setProductId(product.getProductId());
cart.setProductName(product.getProductName());
cart.setFkAmount(product.getpAmount());
cart.setOrderNum(Double.valueOf("1"));
cart.setDhTime(new Date().toLocaleString());
shoppingCartService.save(cart);
}
} else {
for (int i = 0; i < idsStr.length; i++) {
TProduct product = productService.getById(Integer.parseInt(idsStr[i]));
Integer productId = Integer.parseInt(idsStr[i]);
List<TThreshold> cartList = shoppingCartService.getList().stream().filter(cart -> cart.getProductId() == productId && cart.getCreteBy().equals(user.getId().toString())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(cartList)) {
TThreshold cart = cartList.get(0);
cart.setOrderNum(cartList.get(0).getOrderNum() + 1);
shoppingCartService.update(cart);
} else {
TThreshold cart = new TThreshold();
cart.setCreteBy(user.getId().toString());
cart.setProductId(product.getProductId());
cart.setProductName(product.getProductName());
cart.setFkAmount(product.getpAmount());
cart.setOrderNum(Double.valueOf("1"));
cart.setDhTime(new Date().toLocaleString());
shoppingCartService.save(cart);
}
}
}
return Result.succeed();
}
}
这段代码是一个Java Spring MVC控制器,通常用于处理与产品(`TProduct`)相关的请求。它包含以下主要功能和作用:
1. **请求映射**:使用`@RequestMapping`注解将不同的HTTP请求映射到相应的处理方法。例如,`/list`请求将由`list`方法处理。
2. **权限验证**:使用`@RequirePermission`注解来限制访问这些方法的权限。只有具有特定权限的用户才能执行这些操作。
3. **列表显示**:`listUI`方法用于显示产品列表的用户界面。通常返回一个HTML视图,让用户查看产品列表。
4. **数据检索**:`list`方法用于在产品列表上执行数据检索,包括分页和搜索功能。它获取产品数据,根据用户的身份(用户或管理员)来决定要返回的数据。
5. **编辑和保存**:`saveUI`方法用于显示编辑产品的用户界面,允许用户输入或编辑产品信息。`save`方法用于接收表单数据并保存或更新产品信息。
6. **删除产品**:`delete`方法用于删除一个或多个产品。它接受一个包含产品ID的字符串,并将其解析并执行相应的删除操作。
7. **标记为热销产品**:`sirexiao`方法用于将产品标记为热销产品。这将影响产品在页面上的展示。
8. **添加购物车**:`addshopping`方法用于将产品添加到用户的购物车。根据用户的身份和已有购物车内容,它可能会更新购物车中的商品数量或添加新的商品。
总之,这段代码主要负责产品信息的管理,包括显示产品列表、编辑和保存产品、删除产品、标记产品为热销,以及添加产品到购物车等功能。这是一个典型的电子商务或产品管理系统的控制器代码。
2.8 TStockController.java
package com.test.controller;
import java.io.IOException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TStock;
import com.test.domain.TProduct;
import com.test.domain.User;
import com.test.service.TStockService;
import com.test.service.TProductService;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("stock")
public class TStockController {
// 创建一个用于记录日志的Logger
private static Logger logger = Logger.getLogger(TStockController.class);
// 自动装配库存服务
@Autowired
private TStockService userService;
// 自动装配用户服务
@Autowired
private UserService uService;
// 自动装配产品服务
@Autowired
private TProductService productService;
// 映射到"/stock/listUI"的请求,用于显示库存管理页面
@RequirePermission("stock:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
return "stock/listUI"; // 返回库存管理页面的视图
}
// 映射到"/stock/list"的请求,用于获取库存信息并返回JSON响应
@RequirePermission("stock:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
logger.info("查询数据集开始"); // 记录日志信息
User u = (User) session.getAttribute("loginUser"); // 获取当前登录用户
PageInfo<TStock> pageInfo = new PageInfo<TStock>();
// 根据用户类型不同,调用不同的服务方法获取库存信息
if (u != null && u.getuType().equals("1")) {
pageInfo = this.userService.getListByCustomerPage(offset / limit + 1, limit, u.getId(), search);
} else {
pageInfo = this.userService.getListByPage(offset / limit + 1, limit, search);
}
return Result.succeed(pageInfo); // 返回包含库存信息的JSON响应
}
// 映射到"/stock/saveUI"的请求,用于显示添加或编辑库存信息的页面
@RequestMapping("saveUI")
public String saveUI(Integer id, HttpServletRequest request) {
if (id != null) {
TStock user = this.userService.getById(id);
if (user != null) {
request.setAttribute("stock", user);
}
}
List<TProduct> productList = this.productService.getList();
request.setAttribute("productList", productList);
List<User> uList = uService.getList().stream().filter(ls -> ls.getStatus() == 0 && ls.getuType().equals("5"))
.collect(Collectors.toList());
request.setAttribute("uList", uList);
List<User> tList = uService.getList().stream().filter(ls -> ls.getStatus() == 0 && ls.getuType().equals("2"))
.collect(Collectors.toList());
request.setAttribute("tList", tList);
return "stock/saveUI";
}
// 映射到"/stock/save"的请求,用于保存库存信息
@RequirePermission("stock:add;stock:update")
@RequestMapping("save")
public String add(TStock user, HttpServletRequest request, MultipartFile file, HttpSession session)
throws IllegalStateException, IOException {
TProduct product = this.productService.getById(user.getProductId());
User u = (User) session.getAttribute("loginUser");
if (user.getStockId() != null) {
this.userService.update(user); // 如果库存信息已存在,执行更新操作
} else {
user.setCreteBy(u.getId().toString());
user.setDhTime(new Date().toLocaleString());
user.setpProdname(product.getProductName());
this.userService.save(user); // 如果库存信息不存在,执行添加操作
}
return "redirect:/stock/listUI"; // 保存后重定向到库存管理页面
}
// 映射到"/stock/delete"的请求,用于删除库存信息
@RequirePermission("stock:delete")
@RequestMapping("delete/{ids}")
@ResponseBody
public Result delete(@PathVariable("ids") String ids) {
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
this.userService.deleteById(Integer.parseInt(idsStr[0])); // 单个库存信息的删除
} else {
for (int i = 0; i < idsStr.length; i++) {
this.userService.deleteById(Integer.parseInt(idsStr[i]); // 批量删除库存信息
}
}
return Result.succeed(); // 返回删除成功的JSON响应
}
}
这段代码是一个Java Spring MVC控制器,通常用于处理与库存(`TStock`)相关的请求。它包含以下主要功能和作用:
1. **请求映射**:使用`@RequestMapping`注解将不同的HTTP请求映射到相应的处理方法。例如,`/list`请求将由`list`方法处理。
2. **权限验证**:使用`@RequirePermission`注解来限制访问这些方法的权限。只有具有特定权限的用户才能执行这些操作。
3. **列表显示**:`listUI`方法用于显示库存列表的用户界面。通常返回一个HTML视图,让用户查看库存列表。
4. **数据检索**:`list`方法用于在库存列表上执行数据检索,包括分页和搜索功能。它获取库存数据,根据用户的身份(用户或管理员)来决定要返回的数据。
5. **编辑和保存**:`saveUI`方法用于显示编辑库存的用户界面,允许用户输入或编辑库存信息。`save`方法用于接收表单数据并保存或更新库存信息。
6. **删除库存**:`delete`方法用于删除一个或多个库存记录。它接受一个包含库存ID的字符串,并将其解析并执行相应的删除操作。
总之,这段代码主要负责库存信息的管理,包括显示库存列表、编辑和保存库存、删除库存等功能。库存管理通常用于跟踪商品的库存数量和状态。这是一个典型的库存管理系统的控制器代码。
2.9 TThresholdController.java
package com.test.controller;
import java.io.IOException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TBusiness;
import com.test.domain.TOrder;
import com.test.domain.TOrderDetail;
import com.test.domain.TProduct;
import com.test.domain.TThreshold;
import com.test.domain.User;
import com.test.service.TBusinessService;
import com.test.service.TOrderDetailService;
import com.test.service.TOrderService;
import com.test.service.TProductService;
import com.test.service.TThresholdService;
import com.test.service.UserService;
import com.test.vo.Result;
// 控制器类,用于处理门槛(Threshold)相关的请求
@Controller
@RequestMapping("threshold")
public class TThresholdController {
// 创建一个日志记录器
private static Logger logger = Logger.getLogger(TThresholdController.class);
@Autowired
private TThresholdService thresholdService; // 自动注入门槛服务
@Autowired
private TProductService productService; // 自动注入产品服务
@Autowired
private UserService userService; // 自动注入用户服务
@Autowired
private TBusinessService businessService; // 自动注入商家服务
@Autowired
private TOrderService orderService; // 自动注入订单服务
@Autowired
private TOrderDetailService orderDetailService; // 自动注入订单详情服务
// 显示门槛列表页面
@RequirePermission("threshold:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
return "threshold/listUI";
}
// 处理门槛列表请求,并返回JSON响应
@RequirePermission("threshold:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
logger.info("查询数据集开始");
User user = (User) session.getAttribute("loginUser");
PageInfo<TThreshold> pageInfo = new PageInfo<TThreshold>();
if (user != null && user.getuType().equals("1")) {
pageInfo = thresholdService.getListByCustomerPage(offset / limit + 1, limit, user.getId(), search);
} else {
pageInfo = thresholdService.getListByPage(offset / limit + 1, limit, search);
}
return Result.succeed(pageInfo);
}
// 显示保存或修改门槛页面
@RequestMapping("saveUI")
public String saveUI(Integer tId, HttpServletRequest request) {
if (tId != null) {
TThreshold threshold = thresholdService.getById(tId);
if (threshold != null) {
request.setAttribute("threshold", threshold);
}
}
List<TBusiness> businessList = businessService.getList();
request.setAttribute("businessList", businessList);
List<User> userList = userService.getList().stream().filter(user -> user.getStatus() == 0 && user.getuType().equals("5")).collect(Collectors.toList());
request.setAttribute("userList", userList);
List<User> technicianList = userService.getList().stream().filter(user -> user.getStatus() == 0 && user.getuType().equals("2")).collect(Collectors.toList());
request.setAttribute("technicianList", technicianList);
return "threshold/saveUI";
}
// 处理保存或修改门槛的请求
@RequirePermission("threshold:add;threshold:update")
@RequestMapping("save")
public String add(TThreshold threshold, HttpServletRequest request, MultipartFile file, HttpSession session) throws IllegalStateException, IOException {
User user = (User) session.getAttribute("loginUser");
if (threshold.gettId() != null) {
thresholdService.update(threshold);
} else {
threshold.setCreteBy(user.getId().toString());
threshold.setDhTime(new Date().toLocaleString());
thresholdService.save(threshold);
}
return "redirect:/threshold/listUI";
}
// 处理删除门槛的请求
@RequirePermission("threshold:delete")
@RequestMapping("delete/{ids}")
@ResponseBody
public Result delete(@PathVariable("ids") String ids) {
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
thresholdService.deleteById(Integer.parseInt(idsStr[0]));
} else {
for (int i = 0; i < idsStr.length; i++) {
thresholdService.deleteById(Integer.parseInt(idsStr[i]));
}
}
return Result.succeed();
}
// 下单功能的代码(被注释掉了)
@RequestMapping("addorder/{ids}")
@ResponseBody
public Result addorder(@PathVariable("ids") String ids, HttpSession session) {
// 从会话中获取当前登录用户
User user = (User) session.getAttribute("loginUser");
// 将请求中的商品ID字符串拆分为数组
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
// 处理单个商品下单
TThreshold prod = this.thresholdService.getById(Integer.parseInt(idsStr[0]));
Integer productid = Integer.parseInt(idsStr[0]);
// 创建订单对象
TOrder order = new TOrder();
order.setCreteBy(user.getId().toString());
order.setDhTime(new Date().toLocaleString());
Date currdate = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(currdate);
ca.add(Calendar.DATE, 7);
currdate = ca.getTime();
order.setJhTime(currdate.toLocaleString());
order.setOrderNum(prod.getOrderNum());
// 计算订单总金额
if (prod.getFkAmount() != null) {
order.setFkAmount(prod.getOrderNum() * prod.getFkAmount());
}
// 保存订单
this.orderService.save(order);
// 获取刚创建的订单
TOrder orderd = this.orderService.getList().stream().sorted(Comparator.comparing(TOrder::getOrderId).reversed()).collect(Collectors.toList()).get(0);
// 创建订单详情
TOrderDetail detail = new TOrderDetail();
detail.setDetailNum(prod.getOrderNum());
detail.setDetailAmount(prod.getFkAmount());
detail.setProductId(prod.getProductId());
detail.setProductName(prod.getProductName());
detail.setOrderId(orderd.getOrderId());
detail.setCreteBy(user.getId().toString());
// 保存订单详情
this.orderDetailService.save(detail);
} else {
// 处理多个商品下单
Double num = 0.0;
Double amount = 0.0;
for (int i = 0; i < idsStr.length; i++) {
TThreshold prod = this.thresholdService.getById(Integer.parseInt(idsStr[i]);
num += prod.getOrderNum();
amount += prod.getFkAmount() * prod.getOrderNum();
}
// 创建订单对象
TOrder order = new TOrder();
order.setCreteBy(user.getId().toString());
order.setDhTime(new Date().toLocaleString());
Date currdate = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(currdate);
ca.add(Calendar.DATE, 7);
currdate = ca.getTime();
order.setJhTime(currdate.toLocaleString());
order.setFkAmount(amount);
order.setOrderNum(num);
// 保存订单
this.orderService.save(order);
// 获取刚创建的订单
TOrder orderd = this.orderService.getList().stream().sorted(Comparator.comparing(TOrder::getOrderId).reversed()).collect(Collectors.toList()).get(0);
for (int i = 0; i < idsStr.length; i++) {
TThreshold prod = this.thresholdService.getById(Integer.parseInt(idsStr[i]));
// 创建订单详情
TOrderDetail detail = new TOrderDetail();
detail.setDetailNum(prod.getOrderNum());
detail.setDetailAmount(prod.getFkAmount());
detail.setProductId(prod.getProductId());
detail.setProductName(prod.getProductName());
detail.setOrderId(orderd.getOrderId());
detail.setCreteBy(user.getId().toString());
// 保存订单详情
this.orderDetailService.save(detail);
}
}
// 返回成功的响应
return Result.succeed();
}
}
这段代码是一个控制器(Controller),主要负责阈值(Threshold)信息的管理。它实现了以下功能:
1. **显示阈值管理页面(listUI):** 包括阈值信息的列表,可以查看和操作阈值信息。
2. **查询阈值信息(list):** 根据用户权限不同,调用不同的服务方法来获取阈值信息列表,以便在前端页面显示。
3. **编辑和保存阈值信息(saveUI和save):** 用户可以通过编辑页面输入阈值信息,然后保存到数据库中。如果阈值信息已存在,则执行更新操作;如果不存在,则执行添加操作。
4. **删除阈值信息(delete):** 用户可以删除单个或多个阈值信息。
这段代码涉及到阈值信息的增、删、查、改功能,用于管理业务规则和控制条件的设置。通过不同的请求映射和相应的方法,实现了对阈值信息的管理和维护。这对于管理业务流程和规则非常重要,以确保系统按照设定的条件运行。
2.10 TtongjiController.java
package com.test.controller;
import java.io.IOException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TBusiness;
import com.test.domain.TOrder;
import com.test.domain.TOrderDetail;
import com.test.domain.TProduct;
import com.test.domain.TThreshold;
import com.test.domain.TXinDai;
import com.test.domain.User;
import com.test.service.TBusinessService;
import com.test.service.TOrderDetailService;
import com.test.service.TOrderService;
import com.test.service.TProductService;
import com.test.service.TThresholdService;
import com.test.service.TXinDaiService;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("tongji")
public class TtongjiController {
// 创建日志记录器
private static Logger logger = Logger.getLogger(TtongjiController.class);
@Autowired
private TXinDaiService userService; // 注入TXinDaiService,用于处理统计相关的业务逻辑
// 标识访问权限的注解,指定访问统计页面需要的权限
@RequirePermission("tongji:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
return "tongji/listUI"; // 返回统计页面的视图名称
}
}
这段代码是一个控制器(Controller),主要用于统计(tongji)相关的功能。具体来说,代码包含以下内容:
1. **统计信息展示页面(listUI):** 该方法用于显示统计信息的展示页面。用户可以在该页面查看各种统计数据。
2. **RequirePermission注解:** 控制器的方法上使用`@RequirePermission`注解来进行权限控制,以确保只有具备相应权限的用户能够访问统计信息的展示页面。
这段代码的主要目的是展示统计信息的页面,其中可能包括各种统计图表、数据汇总等内容。通过RequirePermission注解来保护这些统计信息,只有授权用户可以查看。
2.11 TXinDaiController.java
package com.test.controller;
import java.io.IOException;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.TBusiness;
import com.test.domain.TOrder;
import com.test.domain.TOrderDetail;
import com.test.domain.TProduct;
import com.test.domain.TThreshold;
import com.test.domain.TXinDai;
import com.test.domain.User;
import com.test.service.TBusinessService;
import com.test.service.TOrderDetailService;
import com.test.service.TOrderService;
import com.test.service.TProductService;
import com.test.service.TThresholdService;
import com.test.service.TXinDaiService;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("xindai")
public class TXinDaiController {
// 创建日志记录器
private static Logger logger = Logger.getLogger(TXinDaiController.class);
@Autowired
private TXinDaiService userService; // 注入TXinDaiService,用于处理信贷相关的业务逻辑
// 标识访问权限的注解,指定访问信贷页面需要的权限
@RequirePermission("xindai:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
return "xindai/listUI"; // 返回信贷页面的视图名称
}
// 标识访问权限的注解,指定访问信贷列表数据的权限
@RequirePermission("xindai:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
logger.info("查询数据集开始");
User u = (User) session.getAttribute("loginUser");
PageInfo<TXinDai> pageInfo = new PageInfo<TXinDai>();
pageInfo = this.userService.getListByPage(offset / limit + 1, limit, search);
return Result.succeed(pageInfo); // 返回信贷信息的查询结果
}
}
这段代码是一个Java类,它充当了一个Web应用程序中的控制器(Controller)的角色。它的主要作用是处理与信贷(信用贷款)信息相关的HTTP请求,包括展示信贷信息列表和信贷信息的详细内容。具体来说,它完成了以下几个作用:
1. **处理HTTP请求映射**:使用`@RequestMapping`注解标记方法,将HTTP请求映射到不同的URL路径。例如,`@RequestMapping("listUI")`表示将HTTP请求映射到"/xindai/listUI"路径。
2. **限制访问权限**:使用`@RequirePermission`注解标记方法,以指定需要的用户权限才能访问相应的页面或执行相应的操作。例如,`@RequirePermission("xindai:listUI")`表示只有具有"xindai:listUI"权限的用户才能访问信贷信息列表页面。
3. **处理HTTP GET请求**:在方法中通常处理HTTP GET请求,用于显示信贷信息列表页面(`listUI`方法)。
4. **处理HTTP AJAX请求**:方法返回`@ResponseBody`注解,以支持通过AJAX方式获取数据。例如,`list`方法用于获取信贷信息列表数据,以`Result`对象的形式返回数据。
5. **与Service层交互**:通过`@Autowired`注解注入`TXinDaiService`,以便在Controller中调用Service层的方法来处理业务逻辑。
6. **日志记录**:使用Apache Log4j库来记录日志信息,用于调试和跟踪应用程序的行为。
总的来说,这段代码是一个Web应用程序中的控制器,用于管理信贷信息的展示和相关操作,包括页面渲染和数据的获取。
2.12 UserController.java
package com.test.controller;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.User;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
// 显示用户信息列表页面
@RequirePermission("user:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
return "user/listUI";
}
// 获取用户信息列表数据(用于AJAX)
@RequirePermission("user:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
// 从Session中获取登录用户信息
User u = (User) session.getAttribute("loginUser");
PageInfo<User> pageInfo = new PageInfo<User>();
if (!u.getuType().equals("0")) {
// 非管理员用户只能查看自己的数据
pageInfo = this.userService.getListByPageType(offset / limit + 1, limit, u.getId().toString(), search);
} else {
// 管理员用户可以查看所有用户的数据
pageInfo = this.userService.getListByPage(offset / limit + 1, limit, search);
}
return Result.succeed(pageInfo);
}
// 显示用户信息编辑页面
@RequestMapping("saveUI")
public String saveUI(Integer id, HttpServletRequest request) {
if (id != null) {
// 如果传入用户ID,根据ID获取用户信息以便编辑
User user = this.userService.getById(id);
if (user != null) {
request.setAttribute("user", user);
}
}
return "user/saveUI";
}
// 添加或修改用户信息
@RequirePermission("user:add;user:update")
@RequestMapping("save")
public String add(User user, HttpSession session) {
User u = (User) session.getAttribute("loginUser");
if (user.getId() != null) {
if (StringUtils.isNoneBlank(user.getNewpassword())) {
// 如果有新密码,对新密码进行MD5加密
user.setLoginPwd(DigestUtils.md5Hex(user.getNewpassword()));
}
if (!u.getLoginPwd().equals(user.getLoginPwd())) {
// 如果修改了原密码,对原密码进行MD5加密
user.setLoginPwd(DigestUtils.md5Hex(user.getLoginPwd()));
}
// 更新用户信息
this.userService.update(user);
} else {
// 如果没有用户ID,表示新建用户
user.setCreateTime(new Date());
// 对用户密码进行MD5加密
user.setLoginPwd(DigestUtils.md5Hex(user.getLoginPwd()));
this.userService.save(user);
}
return "redirect:/user/listUI";
}
// 删除用户或批量删除用户
@RequirePermission("user:delete")
@RequestMapping("delete/{ids}")
@ResponseBody
public Result delete(@PathVariable("ids") String ids) {
String[] idsStr = ids.split(",");
if (idsStr.length == 1) {
// 如果只有一个ID,删除单个用户
this.userService.deleteById(Integer.parseInt(idsStr[0]));
} else {
// 如果有多个ID,批量删除用户
this.userService.deleteBatchByIds(idsStr);
}
return Result.succeed();
}
// 设置用户角色
@RequirePermission("user:setRole")
@RequestMapping("setRole")
@ResponseBody
public Result setRole(int userId, String roleIds) {
// 设置用户角色,用于权限管理
this.userService.saveUserRole(userId, roleIds);
return Result.succeed();
}
// 显示修改密码页面
@RequestMapping("saveUIPwd")
public String saveUIPwd(Integer id, HttpServletRequest request) {
if (id != null) {
// 如果传入用户ID,根据ID获取用户信息以便修改密码
User user = this.userService.getById(id);
if (user != null) {
request.setAttribute("user", user);
}
}
return "user/saveUIPwd";
}
}
这段代码是一个Java类,充当了一个Web应用程序中的控制器(Controller)的角色,主要处理用户管理相关的HTTP请求,包括用户信息的展示、添加、修改、删除和设置角色等操作。下面是该代码的主要作用:
1. **处理HTTP请求映射**:使用`@RequestMapping`注解标记方法,将HTTP请求映射到不同的URL路径。例如,`@RequestMapping("listUI")`表示将HTTP请求映射到"/user/listUI"路径。
2. **限制访问权限**:使用`@RequirePermission`注解标记方法,以指定需要的用户权限才能访问相应的页面或执行相应的操作。例如,`@RequirePermission("user:listUI")`表示只有具有"user:listUI"权限的用户才能访问用户信息列表页面。
3. **处理HTTP GET请求**:在方法中通常处理HTTP GET请求,用于显示用户信息列表页面(`listUI`方法)。
4. **处理HTTP AJAX请求**:方法返回`@ResponseBody`注解,以支持通过AJAX方式获取数据。例如,`list`方法用于获取用户信息列表数据,以`Result`对象的形式返回数据。
5. **与Service层交互**:通过`@Autowired`注解注入`UserService`,以便在Controller中调用Service层的方法来处理用户管理的业务逻辑。
6. **密码加密**:使用Apache Commons Codec库中的`DigestUtils`类来对用户密码进行MD5加密,保障密码的安全性。
7. **用户信息管理**:实现了用户信息的增删改查功能,包括添加新用户、修改用户信息、删除用户等操作。
8. **角色设置**:实现了为用户设置角色的功能,用于权限管理。
9. **修改密码**:提供了修改密码的功能,用于用户密码的管理。
总的来说,这段代码是一个Web应用程序中的用户管理控制器,用于管理用户的展示和相关操作,包括页面渲染和数据的获取,以及用户密码的管理。
2.13 UserSetUpController.java
package com.test.controller;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.test.annotation.RequirePermission;
import com.test.domain.User;
import com.test.service.UserService;
import com.test.vo.Result;
@Controller
@RequestMapping("grsz")
public class UserSetUpController {
@Autowired
private UserService userService;
// 显示个人设置页面
@RequirePermission("grsz:listUI")
@RequestMapping("listUI")
public String listUI(HttpServletRequest request) {
return "grsz/listUI";
}
// 获取个人设置信息(用于AJAX)
@RequirePermission("grsz:listUI")
@RequestMapping("list")
@ResponseBody
public Result list(int offset, int limit, String search, HttpSession session) {
// 从Session中获取登录用户信息
User u = (User) session.getAttribute("loginUser");
PageInfo<User> pageInfo = new PageInfo<User>();
// 只查看当前登录用户的信息
pageInfo = this.userService.getListByPageType(offset / limit + 1, limit, u.getId().toString(), search);
return Result.succeed(pageInfo);
}
// 显示个人设置编辑页面
@RequestMapping("saveUI")
public String saveUI(String id, HttpServletRequest request) {
if (id != null) {
// 如果传入用户ID,根据ID获取用户信息以便编辑
User user = this.userService.getById(Integer.parseInt(id));
if (user != null) {
request.setAttribute("user", user);
}
}
return "grsz/saveUI";
}
// 添加或修改个人设置信息
@RequirePermission("grsz:add;grsz:update")
@RequestMapping("save")
public String add(User user, HttpSession session) {
User u = (User) session.getAttribute("loginUser");
if (user.getId() != null) {
if (StringUtils.isNoneBlank(user.getNewpassword())) {
// 如果有新密码,对新密码进行MD5加密
user.setLoginPwd(DigestUtils.md5Hex(user.getNewpassword()));
}
if (!u.getLoginPwd().equals(user.getLoginPwd())) {
// 如果修改了原密码,对原密码进行MD5加密
user.setLoginPwd(DigestUtils.md5Hex(user.getLoginPwd()));
}
this.userService.update(user);
} else {
// 如果没有用户ID,表示新建用户
user.setCreateTime(new Date());
// 对用户密码进行MD5加密
user.setLoginPwd(DigestUtils.md5Hex(user.getLoginPwd()));
this.userService.save(user);
}
return "redirect:/grsz/listUI";
}
// 显示修改密码页面
@RequestMapping("saveUIPwd")
public String saveUIPwd(String id, HttpServletRequest request) {
if (id != null) {
// 如果传入用户ID,根据ID获取用户信息以便修改密码
User user = this.userService.getById(Integer.parseInt(id));
if (user != null) {
request.setAttribute("user", user);
}
}
return "grsz/saveUIPwd";
}
}
这段代码是一个控制器类,用于管理用户的个人设置,包括查看个人信息、编辑个人信息和修改密码。以下是该代码的主要作用:
1. 提供用户访问个人设置页面的入口,显示个人设置信息:
- `listUI` 方法用于显示个人设置页面。
- `list` 方法用于获取个人设置信息并返回给前端,支持分页浏览。2. 允许用户编辑个人信息:
- `saveUI` 方法用于显示编辑个人信息的页面,可以根据传入的用户 ID 获取用户信息以便编辑。
- `add` 方法用于添加或更新用户的个人设置信息。如果用户提供了新密码,将对新密码进行 MD5 加密,然后保存到数据库中。同时,如果用户修改了原密码,也会对原密码进行 MD5 加密。3. 支持修改密码功能:
- `saveUIPwd` 方法用于显示修改密码的页面,允许用户修改密码。
总之,这段代码允许用户管理其个人设置,包括查看和编辑个人信息以及修改密码。它通过 Spring MVC 控制器处理用户请求并与 UserService 进行交互以实现上述功能。