赞
踩
源码获取:博客首页 "资源" 里下载!
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
JSP +SpringBoot + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。
- @Controller
- @RequestMapping("/customer")
- public class CustomerController {
- @Autowired
- private CustomerViewService customerViewService;
- @Autowired
- private CustomerService customerService;
- @Autowired
- private BasicDataService basicDataService;
- @Autowired
- private UserService userService;
-
- @RequestMapping("/list")
- @RequiresPermissions("customer:list")
- public String list() {
- return "customer/list";
- }
-
- @ResponseBody
- @RequestMapping("/getCustomers")
- @RequiresPermissions("customer:list")
- public PageInfo<CustomerView> getCustomers(String keyWord,
- @RequestParam(defaultValue = "1") Integer pageNum,
- @RequestParam(defaultValue = "10") Integer pageSize) {
- //开始分页,这里启动并设置页码,和每页结果数量后,后续结果会自动为分页后结果
- PageHelper.startPage(pageNum, pageSize);
-
- CustomerViewExample customerViewExample = new CustomerViewExample();
- CustomerViewExample.Criteria criteria = customerViewExample.createCriteria();
- /**
- * StringUtils.isNotBlank 可以判断 ""/" "/null 为 false
- */
- if (StringUtils.isNotBlank(keyWord)) {
- //权限名条件
- criteria.andCustomerNameLike("%" + keyWord + "%");
- }
-
- //判断当前登录用户是否为业务员,业务员只能查看自己的客户
- Subject subject = SecurityUtils.getSubject();
- User user = (User) subject.getPrincipal();
- if (user.getRoleId() == ConstantDataField.SALESMAN_ROLE_ID) {
- //客户的业务员id等于登录用户的id
- criteria.andUserIdEqualTo(user.getUserId());
- }
-
- List<CustomerView> customers = customerViewService.selectByExample(customerViewExample);
- //获得分页对象
- PageInfo<CustomerView> pageInfo = new PageInfo<>(customers);
-
- return pageInfo;
- }
-
- @ResponseBody
- @RequestMapping("/delete")
- @RequiresPermissions("customer:delete")
- public Map<String, String> delete(Long[] customerIds) {
- HashMap<String, String> map = new HashMap<>();
- if (customerIds.length == 1) {
- int res = customerService.deleteByPrimaryKey(customerIds[0]);
- if (res == 1) {
- map.put("status", "true");
- map.put("info", "删除成功!");
- return map;
- }
- map.put("status", "false");
- map.put("info", "删除失败!客户已经不存在");
- return map;
- }
- //批量删除
- int success = 0;
- int total = customerIds.length;
- for(Long customerId : customerIds) {
- success += customerService.deleteByPrimaryKey(customerId);
- }
- map.put("status", "true");
- map.put("info", "成功删除选中 " + total + " 个客户中的 " + success + " 个客户");
- return map;
- }
-
-
- @RequestMapping("/add")
- @RequiresPermissions("customer:insert")
- public String add(Model m) {
- //查出地区列表
- BasicDataExample basicDataExample = new BasicDataExample();
- basicDataExample.createCriteria().andParentIdEqualTo(ConstantDataField.AREA_BASICDATA_ID);
- List<BasicData> basicDatas= basicDataService.selectByExample(basicDataExample);
- m.addAttribute("areas",basicDatas);
-
- //获取当前登录用户
- Subject subject = SecurityUtils.getSubject();
- User user = (User) subject.getPrincipal();
-
- //如果是业务员进行操作,只能选择自己作为业务员
- List<User> users = new ArrayList<>();
- if (user.getRoleId() == ConstantDataField.SALESMAN_ROLE_ID) {
- users.add(user);
- } else {
- UserExample userExample = new UserExample();
- userExample.createCriteria().andRoleIdEqualTo(ConstantDataField.SALESMAN_ROLE_ID);
- users = userService.selectByExample(userExample);
- }
- m.addAttribute("users", users);
-
- return "customer/add";
- }
-
- @ResponseBody
- @RequestMapping("/insert")
- @RequiresPermissions("customer:insert")
- public Boolean insert(Customer customer){
- int res = customerService.insert(customer);
- if (res == 1) {
- return true;
- }
- return false;
- }
-
- @RequestMapping("/edit")
- @RequiresPermissions("customer:update")
- public String edit(Model m, Long customerId) {
- //查出地区列表
- BasicDataExample basicDataExample = new BasicDataExample();
- basicDataExample.createCriteria().andParentIdEqualTo(ConstantDataField.AREA_BASICDATA_ID);
- List<BasicData> basicDatas= basicDataService.selectByExample(basicDataExample);
- m.addAttribute("areas",basicDatas);
-
- //查出业务员
- UserExample userExample = new UserExample();
- userExample.createCriteria().andRoleIdEqualTo(ConstantDataField.SALESMAN_ROLE_ID);
- List<User> users = userService.selectByExample(userExample);
- m.addAttribute("users", users);
-
- //查出当前客户
- Customer customer = customerService.selectByPrimaryKey(customerId);
- m.addAttribute("customer", customer);
- return "customer/edit";
- }
-
- @ResponseBody
- @RequestMapping("/update")
- @RequiresPermissions("customer:update")
- public Boolean update(Customer customer) {
- int res = customerService.updateByPrimaryKeySelective(customer);
- if (res == 1) {
- return true;
- }
- return false;
- }
- }
- @Controller
- @RequestMapping("/admin")
- public class AdminController {
- @Autowired
- private UserService userService;
- @Autowired
- private RoleService roleService;
-
- @RequestMapping("/list")
- @RequiresPermissions("admin:list")
- public String adminList() {
- return "admin/list";
- }
-
- /**
- * 该方法返回 admin 集合的 JSON 字符串,使用 PageHelper 工具进行分页
- * @param keyWord 搜索关键字
- * @param pageNum 页数,默认为 1
- * @param pageSize 每个页面用户容量,默认为 10/页
- * @return
- */
- @ResponseBody
- @RequestMapping("/getAdmins")
- @RequiresPermissions("admin:list")
- public PageInfo<User> getAdmins(String keyWord,
- @RequestParam(defaultValue = "1") Integer pageNum,
- @RequestParam(defaultValue = "10") Integer pageSize) {
- //开始分页,这里启动并设置页码,和每页结果数量后,后续结果会自动为分页后结果
- PageHelper.startPage(pageNum, pageSize);
-
- UserExample userExample = new UserExample();
-
- /**
- * StringUtils.isNotBlank 可以判断 ""/" "/null 为 false
- */
- if (StringUtils.isNotBlank(keyWord)) {
- //用户名条件
- UserExample.Criteria criteriaUserName = userExample.createCriteria();
- criteriaUserName.andUsernameLike("%" + keyWord + "%");
-
- //真实名字条件
- UserExample.Criteria criteriaRealName = userExample.createCriteria();
- criteriaRealName.andRealnameLike("%" + keyWord + "%");
-
- //将两个条件用 or 组合
- userExample.or(criteriaRealName);
- }
-
- List<User> users = userService.selectByExample(userExample);
- //获得分页对象
- PageInfo<User> pageInfo = new PageInfo<>(users);
-
- return pageInfo;
- }
-
- @ResponseBody
- @RequestMapping("/delete")
- @RequiresPermissions("admin:delete")
- public Map<String, String> delete(Long[] userIds) {
- HashMap<String, String> map = new HashMap<>();
- if (userIds.length == 1) {
- int res = userService.deleteByPrimaryKey(userIds[0]);
- if (res == 1) {
- map.put("status", "true");
- map.put("info", "删除成功!");
- return map;
- }
- map.put("status", "false");
- map.put("info", "删除失败!用户已经不存在");
- return map;
- }
- //批量删除
- int success = 0;
- int total = userIds.length;
- for(Long userId : userIds) {
- success += userService.deleteByPrimaryKey(userId);
- }
- map.put("status", "true");
- map.put("info", "成功删除选中 " + total + " 个用户中的 " + success + " 个用户");
- return map;
- }
-
- @RequiresPermissions("admin:insert")
- @RequestMapping("/add")
- public String addAdmin(Model m) {
- RoleExample roleExample = new RoleExample();
- List<Role> roles = roleService.selectByExample(roleExample);
- m.addAttribute("roles", roles);
- return "admin/add";
- }
-
- @ResponseBody
- @RequestMapping("/checkUsername")
- @RequiresPermissions("admin:list")
- public Boolean checkUsername(String username) {
- UserExample userExample = new UserExample();
- UserExample.Criteria criteria = userExample.createCriteria();
- criteria.andUsernameEqualTo(username);
- System.out.println(userService.selectByExample(userExample));
- if (userService.selectByExample(userExample).size() != 0) {
- return false;
- }
- return true;
- }
-
- @ResponseBody
- @RequestMapping("/insert")
- @RequiresPermissions("admin:insert")
- public Boolean insert(User user){
- String salt = UUID.randomUUID().toString().substring(0, 4);
- String hashedPassword = new SimpleHash("md5", user.getPassword(), salt,3).toString();
- user.setSalt(salt);
- user.setPassword(hashedPassword);
- user.setCreateDate(new Date());
- int res = userService.insert(user);
- if (res == 1) {
- return true;
- }
- return false;
- }
-
- @RequestMapping("/edit")
- @RequiresPermissions("admin:update")
- public String edit(Model m, Long userId) {
- m.addAttribute("user", userService.selectByPrimaryKey(userId));
- m.addAttribute("roles", roleService.selectByExample(new RoleExample()));
- return "admin/edit";
- }
-
- @ResponseBody
- @RequestMapping("/update")
- @RequiresPermissions("admin:update")
- public Boolean update(User user) {
- if (StringUtils.isBlank(user.getPassword())) {
- user.setPassword(null);
- } else {
- String salt = UUID.randomUUID().toString().substring(0, 4);
- String hashedPassword = new SimpleHash("md5", user.getPassword(), salt, 3).toString();
- user.setPassword(hashedPassword);
- user.setSalt(salt);
- }
- int res = userService.updateByPrimaryKeySelective(user);
- if (res == 1) {
- return true;
- }
- return false;
- }
- }
-
- @Controller
- @RequestMapping("/order")
- public class OrderController {
-
- @Autowired
- private OrderViewService orderViewService;
-
- @Autowired
- private OrderService orderService;
-
- @Autowired
- private UserService userService;
-
- @Autowired
- private CustomerService customerService;
-
- @Autowired
- private BasicDataService basicDataService;
-
- @Autowired
- private OrderDetailService orderDetailService;
-
- //工具方法
- public PageInfo<OrderView> getOrdersUtil(String keyWord, Integer pageNum, Integer pageSize) {
- //开始分页,这里启动并设置页码,和每页结果数量后,后续结果会自动为分页后结果
- PageHelper.startPage(pageNum, pageSize);
-
- OrderViewExample orderViewExample = new OrderViewExample();
- OrderViewExample.Criteria criteria = orderViewExample.createCriteria();
- /**
- * StringUtils.isNotBlank 可以判断 ""/" "/null 为 false
- */
- if (StringUtils.isNotBlank(keyWord)) {
- //客户名条件
- criteria.andCustomerNameLike("%" + keyWord + "%");
- }
-
- //判断当前登录用户是否为业务员,业务员只能查看自己的订单
- Subject subject = SecurityUtils.getSubject();
- User user = (User) subject.getPrincipal();
- if (user.getRoleId() == ConstantDataField.SALESMAN_ROLE_ID) {
- //客户的业务员用户名等于登录用户的用户名
- criteria.andUsernameEqualTo(user.getUsername());
- }
-
- List<OrderView> orders = orderViewService.selectByExample(orderViewExample);
- //获得分页对象
- PageInfo<OrderView> pageInfo = new PageInfo<>(orders);
-
- return pageInfo;
- }
-
- @RequestMapping("/list")
- @RequiresPermissions("order:list")
- public String list() {
- return "order/list";
- }
-
- @ResponseBody
- @RequestMapping("/getOrders")
- @RequiresPermissions("order:list")
- public PageInfo<OrderView> getOrders(String keyWord,
- @RequestParam(defaultValue = "1") Integer pageNum,
- @RequestParam(defaultValue = "10") Integer pageSize) {
- return getOrdersUtil(keyWord, pageNum, pageSize);
- }
-
- @ResponseBody
- @RequestMapping("/delete")
- @RequiresPermissions("order:delete")
- public Map<String, String> delete(Long[] orderIds) {
- HashMap<String, String> map = new HashMap<>();
- //单个删除
- if (orderIds.length == 1) {
- int res = orderService.deleteByPrimaryKey(orderIds[0]);
- if (res == 1) {
- map.put("status", "true");
- map.put("info", "删除成功!");
- return map;
- }
- map.put("status", "false");
- map.put("info", "删除失败!订单已经不存在");
- return map;
- }
- //批量删除
- int success = 0;
- int total = orderIds.length;
- for(Long orderId : orderIds) {
- success += orderService.deleteByPrimaryKey(orderId);
- }
- map.put("status", "true");
- map.put("info", "成功删除选中 " + total + " 个订单中的 " + success + " 个订单");
- return map;
- }
-
- @RequestMapping("/add")
- @RequiresPermissions("order:insert")
- public String add(Model m) {
- this.getOrderGeneralData(m);
- return "order/add";
- }
-
-
- /*
- 用 @RequestBody 注解,将前台传入的 JSON 字符串解析成对象
- */
- @RequestMapping("/insert")
- @RequiresPermissions("order:insert")
- @ResponseBody
- @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, timeout = 5)
- public Boolean insert(@RequestBody Order order) {
- //插入 order,在此之后,order 获得了 orderId
- int res = orderService.insert(order);
- //获取订单详情集合
- List<OrderDetail> orderDetails = order.getorderDetails();
- orderDetails.forEach(orderDetail -> {
- orderDetail.setOrderId(order.getOrderId());
- orderDetailService.insert(orderDetail);
- });
- if (res == 1) {
- return true;
- }
- return false;
- }
-
- @RequestMapping("/edit")
- @RequiresPermissions("order:update")
- public String edit(Long orderId, Model m) {
- //获取常规数据
- getOrderGeneralData(m);
-
- //添加选中的订单
- Order selectedOrder = orderService.selectByPrimaryKey(orderId);
- m.addAttribute("selectedOrder", selectedOrder);
-
- //添加订单详情
- OrderDetailExample orderDetailExample = new OrderDetailExample();
- orderDetailExample.createCriteria().andOrderIdEqualTo(orderId);
- List<OrderDetail> orderDetails = orderDetailService.selectByExample(orderDetailExample);
- m.addAttribute("orderDetails", orderDetails);
-
- return "order/edit";
- }
-
- @RequiresPermissions("order:update")
- @RequestMapping("/update")
- @ResponseBody
- public Boolean update(@RequestBody Order order) {
- OrderDetailExample orderDetailExample = new OrderDetailExample();
- orderDetailExample.createCriteria().andOrderIdEqualTo(order.getOrderId());
- List<OrderDetail> dbOrderDetails = orderDetailService.selectByExample(orderDetailExample); //数据库中的orderDetails
- List<OrderDetail> formOrderDetails = order.getorderDetails(); //表单数据中的orderDetails
-
- //循环比较数据库数据和表单新数据,删除数据库中在修改中删除的数据
- dbOrderDetails.forEach(dbOrderDetail -> {
- if (!formOrderDetails.contains(dbOrderDetail)) {
- orderDetailService.deleteByPrimaryKey(dbOrderDetail.getOrderDetailId());
- }
- });
-
- //循环比较表单新数据和数据库数据,若存在则修改,不存在则新增
- formOrderDetails.forEach(formOrderDetail -> {
- if (dbOrderDetails.contains(formOrderDetail)) {
- orderDetailService.updateByPrimaryKeySelective(formOrderDetail);
- } else {
- formOrderDetail.setOrderId(order.getOrderId());
- orderDetailService.insert(formOrderDetail);
- }
- });
-
- //修改Order
- int res = orderService.updateByPrimaryKey(order);
- if (res == 1){
- return true;
- }
- return false;
- }
-
- //抽取的工具方法
- private void getOrderGeneralData(Model m){
- User user = (User)SecurityUtils.getSubject().getPrincipal(); //获取当前用户
- Long userRoleId = user.getRoleId(); //当前用户角色ID
- Long userId = user.getUserId(); //当前用户ID
-
- //查找业务员
- UserExample userExample = new UserExample();
- List<User> users = new ArrayList<>();
- //如果当前用户是业务员只能用自己用户添加
- if (userRoleId == ConstantDataField.SALESMAN_ROLE_ID) {
- users.add(userService.selectByPrimaryKey(userId));
- } else {
- userExample.createCriteria().andRoleIdEqualTo(ConstantDataField.SALESMAN_ROLE_ID);
- users = userService.selectByExample(userExample);
- }
- m.addAttribute("users", users);
-
- //查找客户
- CustomerExample customerExample = new CustomerExample();
- List<Customer> customers;
- //如果当前用户是业务员只能获得自己的客户
- if (userRoleId == ConstantDataField.SALESMAN_ROLE_ID) {
- customerExample.createCriteria().andUserIdEqualTo(userId);
- customers = customerService.selectByExample(customerExample);
- } else {
- customers = customerService.selectByExample(customerExample);
- }
- m.addAttribute("customers", customers);
-
- //查找地区
- BasicDataExample areaExample = new BasicDataExample();
- areaExample.createCriteria().andParentIdEqualTo(ConstantDataField.AREA_BASICDATA_ID);
- List<BasicData> areas = basicDataService.selectByExample(areaExample);
- m.addAttribute("areas", areas);
-
- //查找付款方式
- BasicDataExample paymentExample = new BasicDataExample();
- paymentExample.createCriteria().andParentIdEqualTo(ConstantDataField.PAYMENT_BASICDATA_ID);
- List<BasicData> payments = basicDataService.selectByExample(paymentExample);
- m.addAttribute("payments", payments);
-
- //查找运送方式
- BasicDataExample transportExample = new BasicDataExample();
- transportExample.createCriteria().andParentIdEqualTo(ConstantDataField.TRANSPORT_BASICDATA_ID);
- List<BasicData> transports = basicDataService.selectByExample(transportExample);
- m.addAttribute("transports", transports);
-
- //查找取件方式
- BasicDataExample pickupExample = new BasicDataExample();
- pickupExample.createCriteria().andParentIdEqualTo(ConstantDataField.PICKUP_BASICDATA_ID);
- List<BasicData> pickups = basicDataService.selectByExample(pickupExample);
- m.addAttribute("pickups", pickups);
-
- //查找单位
- BasicDataExample unitExample = new BasicDataExample();
- unitExample.createCriteria().andParentIdEqualTo(ConstantDataField.UNIT_BASICDATA_ID);
- List<BasicData> units = basicDataService.selectByExample(unitExample);
- m.addAttribute("units", units);
- }
- }
源码获取:博客首页 "资源" 里下载!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。