commit 0f65a3cc3bdcd35f04b9e2eaaf7d3f530808e073 Author: ming <1579189568@qq.com> Date: Wed Dec 4 11:21:36 2024 +0800 lmx diff --git a/Account.java b/Account.java new file mode 100644 index 00000000..386649bc --- /dev/null +++ b/Account.java @@ -0,0 +1,86 @@ +package com.example.entity; + +/** + * 角色用户父类 + */ +public class Account { + private Integer id; + /** 用户名 */ + private String username; + /** 名称 */ + private String name; + /** 密码 */ + private String password; + /** 角色标识 */ + private String role; + /** 新密码 */ + private String newPassword; + /** 头像 */ + private String avatar; + + private String token; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public String getNewPassword() { + return newPassword; + } + + public void setNewPassword(String newPassword) { + this.newPassword = newPassword; + } + + public String getAvatar() { + return avatar; + } + + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } +} diff --git a/Address.java b/Address.java new file mode 100644 index 00000000..e2df6f0c --- /dev/null +++ b/Address.java @@ -0,0 +1,59 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 地址信息表 +*/ +public class Address implements Serializable { + private static final long serialVersionUID = 1L; + + private Integer id; + private Integer userId; + private String username; + private String useraddress; + private String phone; + + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getUseraddress() { + return useraddress; + } + + public void setUseraddress(String useraddress) { + this.useraddress = useraddress; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } +} + + diff --git a/AddressController.java b/AddressController.java new file mode 100644 index 00000000..d77d5428 --- /dev/null +++ b/AddressController.java @@ -0,0 +1,87 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Address; +import com.example.service.AddressService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 地址前端操作接口 + **/ +@RestController +@RequestMapping("/address") +public class AddressController { + + @Resource + private AddressService addressService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Address address) { + addressService.add(address); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + addressService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + addressService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Address address) { + addressService.updateById(address); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Address address = addressService.selectById(id); + return Result.success(address); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Address address ) { + List
list = addressService.selectAll(address); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Address address, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo
page = addressService.selectPage(address, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/AddressMapper.java b/AddressMapper.java new file mode 100644 index 00000000..9ddedb2e --- /dev/null +++ b/AddressMapper.java @@ -0,0 +1,39 @@ +package com.example.mapper; + +import com.example.entity.Address; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作address相关数据接口 +*/ +public interface AddressMapper { + + /** + * 新增 + */ + int insert(Address address); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Address address); + + /** + * 根据ID查询 + */ + Address selectById(Integer id); + + /** + * 查询所有 + */ + List
selectAll(Address address); + +} \ No newline at end of file diff --git a/AddressService.java b/AddressService.java new file mode 100644 index 00000000..bfa6f928 --- /dev/null +++ b/AddressService.java @@ -0,0 +1,84 @@ +package com.example.service; + +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.entity.Address; +import com.example.mapper.AddressMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 地址业务处理 + **/ +@Service +public class AddressService { + + @Resource + private AddressMapper addressMapper; + + /** + * 新增 + */ + public void add(Address address) { + addressMapper.insert(address); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + addressMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + addressMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Address address) { + addressMapper.updateById(address); + } + + /** + * 根据ID查询 + */ + public Address selectById(Integer id) { + return addressMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List
selectAll(Address address) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.USER.name().equals(currentUser.getRole())) { + address.setUserId(currentUser.getId()); + } + return addressMapper.selectAll(address); + } + + /** + * 分页查询 + */ + public PageInfo
selectPage(Address address, Integer pageNum, Integer pageSize) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.USER.name().equals(currentUser.getRole())) { + address.setUserId(currentUser.getId()); + } + PageHelper.startPage(pageNum, pageSize); + List
list = addressMapper.selectAll(address); + return PageInfo.of(list); + } +} \ No newline at end of file diff --git a/Collect.java b/Collect.java new file mode 100644 index 00000000..181b472e --- /dev/null +++ b/Collect.java @@ -0,0 +1,96 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 收藏信息表 +*/ +public class Collect implements Serializable { + private static final long serialVersionUID = 1L; + + private Integer id; + private Integer userId; + private Integer businessId; + private Integer goodsId; + + private String businessName; + private String goodsName; + private String goodsImg; + private String goodUnit; + private Double goodsPrice; + + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public Integer getBusinessId() { + return businessId; + } + + public void setBusinessId(Integer businessId) { + this.businessId = businessId; + } + + public Integer getGoodsId() { + return goodsId; + } + + public void setGoodsId(Integer goodsId) { + this.goodsId = goodsId; + } + + public String getBusinessName() { + return businessName; + } + + public void setBusinessName(String businessName) { + this.businessName = businessName; + } + + public String getGoodsName() { + return goodsName; + } + + public void setGoodsName(String goodsName) { + this.goodsName = goodsName; + } + + public String getGoodsImg() { + return goodsImg; + } + + public void setGoodsImg(String goodsImg) { + this.goodsImg = goodsImg; + } + + public String getGoodUnit() { + return goodUnit; + } + + public void setGoodUnit(String goodUnit) { + this.goodUnit = goodUnit; + } + + public Double getGoodsPrice() { + return goodsPrice; + } + + public void setGoodsPrice(Double goodsPrice) { + this.goodsPrice = goodsPrice; + } +} + + diff --git a/CollectController.java b/CollectController.java new file mode 100644 index 00000000..e8cb4053 --- /dev/null +++ b/CollectController.java @@ -0,0 +1,87 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Collect; +import com.example.service.CollectService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 收藏前端操作接口 + **/ +@RestController +@RequestMapping("/collect") +public class CollectController { + + @Resource + private CollectService collectService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Collect collect) { + collectService.add(collect); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + collectService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + collectService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Collect collect) { + collectService.updateById(collect); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Collect collect = collectService.selectById(id); + return Result.success(collect); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Collect collect ) { + List list = collectService.selectAll(collect); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Collect collect, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = collectService.selectPage(collect, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/CollectMapper.java b/CollectMapper.java new file mode 100644 index 00000000..34764d96 --- /dev/null +++ b/CollectMapper.java @@ -0,0 +1,41 @@ +package com.example.mapper; + +import com.example.entity.Collect; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作collect相关数据接口 +*/ +public interface CollectMapper { + + /** + * 新增 + */ + int insert(Collect collect); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Collect collect); + + /** + * 根据ID查询 + */ + Collect selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Collect collect); + + @Select("select * from collect where user_id = #{userId} and goods_id = #{goodsId}") + Collect selectByUserIdAndGoodsId(@Param("userId") Integer userId, @Param("goodsId") Integer goodsId); +} \ No newline at end of file diff --git a/CollectService.java b/CollectService.java new file mode 100644 index 00000000..687e116e --- /dev/null +++ b/CollectService.java @@ -0,0 +1,88 @@ +package com.example.service; + +import cn.hutool.core.util.ObjectUtil; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.entity.Collect; +import com.example.exception.CustomException; +import com.example.mapper.CollectMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 收藏业务处理 + **/ +@Service +public class CollectService { + + @Resource + private CollectMapper collectMapper; + + /** + * 新增 + */ + public void add(Collect collect) { + // 判断一下该用户有没有收藏过该商品,如果有,就要提示用户不能重复收藏 + Collect dbCollect = collectMapper.selectByUserIdAndGoodsId(collect.getUserId(), collect.getGoodsId()); + if (ObjectUtil.isNotEmpty(dbCollect)) { + throw new CustomException(ResultCodeEnum.COLLECT_ALREADY_ERROR); + } + collectMapper.insert(collect); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + collectMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + collectMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Collect collect) { + collectMapper.updateById(collect); + } + + /** + * 根据ID查询 + */ + public Collect selectById(Integer id) { + return collectMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Collect collect) { + return collectMapper.selectAll(collect); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Collect collect, Integer pageNum, Integer pageSize) { + Account currentUser = TokenUtils.getCurrentUser(); + if (RoleEnum.USER.name().equals(currentUser.getRole())) { + collect.setUserId(currentUser.getId()); + } + PageHelper.startPage(pageNum, pageSize); + List list = collectMapper.selectAll(collect); + return PageInfo.of(list); + } +} \ No newline at end of file diff --git a/Notice.java b/Notice.java new file mode 100644 index 00000000..c77f7ec6 --- /dev/null +++ b/Notice.java @@ -0,0 +1,62 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 公告信息表 +*/ +public class Notice implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + /** 标题 */ + private String title; + /** 内容 */ + private String content; + /** 创建时间 */ + private String time; + /** 创建人 */ + private String user; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + +} \ No newline at end of file diff --git a/NoticeController.java b/NoticeController.java new file mode 100644 index 00000000..e2757d3a --- /dev/null +++ b/NoticeController.java @@ -0,0 +1,86 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Notice; +import com.example.service.NoticeService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; +import java.util.List; + +/** + * 公告信息表前端操作接口 + **/ +@RestController +@RequestMapping("/notice") +public class NoticeController { + + @Resource + private NoticeService noticeService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody Notice notice) { + noticeService.add(notice); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + noticeService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + noticeService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody Notice notice) { + noticeService.updateById(notice); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + Notice notice = noticeService.selectById(id); + return Result.success(notice); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(Notice notice ) { + List list = noticeService.selectAll(notice); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(Notice notice, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = noticeService.selectPage(notice, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/NoticeMapper.java b/NoticeMapper.java new file mode 100644 index 00000000..3d2b4a0c --- /dev/null +++ b/NoticeMapper.java @@ -0,0 +1,36 @@ +package com.example.mapper; + +import com.example.entity.Notice; +import java.util.List; + +/** + * 操作notice相关数据接口 +*/ +public interface NoticeMapper { + + /** + * 新增 + */ + int insert(Notice notice); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Notice notice); + + /** + * 根据ID查询 + */ + Notice selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Notice notice); + +} \ No newline at end of file diff --git a/NoticeService.java b/NoticeService.java new file mode 100644 index 00000000..8787113d --- /dev/null +++ b/NoticeService.java @@ -0,0 +1,79 @@ +package com.example.service; + +import cn.hutool.core.date.DateUtil; +import com.example.entity.Account; +import com.example.entity.Notice; +import com.example.mapper.NoticeMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import javax.annotation.Resource; +import org.springframework.stereotype.Service; +import java.util.List; + +/** + * 公告信息表业务处理 + **/ +@Service +public class NoticeService { + + @Resource + private NoticeMapper noticeMapper; + + /** + * 新增 + */ + public void add(Notice notice) { + notice.setTime(DateUtil.today()); + Account currentUser = TokenUtils.getCurrentUser(); + notice.setUser(currentUser.getUsername()); + noticeMapper.insert(notice); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + noticeMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + noticeMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Notice notice) { + noticeMapper.updateById(notice); + } + + /** + * 根据ID查询 + */ + public Notice selectById(Integer id) { + return noticeMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Notice notice) { + return noticeMapper.selectAll(notice); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Notice notice, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = noticeMapper.selectAll(notice); + return PageInfo.of(list); + } + +} \ No newline at end of file diff --git a/TypeMapper.java b/TypeMapper.java new file mode 100644 index 00000000..9a6b8bd0 --- /dev/null +++ b/TypeMapper.java @@ -0,0 +1,37 @@ +package com.example.mapper; + +import com.example.entity.Type; + +import java.util.List; + +/** + * 操作type相关数据接口 +*/ +public interface TypeMapper { + + /** + * 新增 + */ + int insert(Type type); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(Type type); + + /** + * 根据ID查询 + */ + Type selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(Type type); + +} \ No newline at end of file diff --git a/TypeService.java b/TypeService.java new file mode 100644 index 00000000..81c930c4 --- /dev/null +++ b/TypeService.java @@ -0,0 +1,75 @@ +package com.example.service; + + +import com.example.entity.Type; +import com.example.mapper.TypeMapper; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 分类信息表业务处理 + **/ +@Service +public class TypeService { + + @Resource + private TypeMapper typeMapper; + + /** + * 新增 + */ + public void add(Type type) { + typeMapper.insert(type); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + typeMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + typeMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(Type type) { + typeMapper.updateById(type); + } + + /** + * 根据ID查询 + */ + public Type selectById(Integer id) { + return typeMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(Type type) { + return typeMapper.selectAll(type); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(Type type, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = typeMapper.selectAll(type); + return PageInfo.of(list); + } + +} \ No newline at end of file diff --git a/User.java b/User.java new file mode 100644 index 00000000..73670bfa --- /dev/null +++ b/User.java @@ -0,0 +1,104 @@ +package com.example.entity; + +import java.io.Serializable; + +/** + * 用户 +*/ +public class User extends Account implements Serializable { + private static final long serialVersionUID = 1L; + + /** ID */ + private Integer id; + /** 用户名 */ + private String username; + /** 密码 */ + private String password; + /** 昵称 */ + private String name; + /** 电话 */ + private String phone; + /** 邮箱 */ + private String email; + /** 头像 */ + private String avatar; + /** 角色标识 */ + private String role; + + @Override + public Integer getId() { + return id; + } + + @Override + public void setId(Integer id) { + this.id = id; + } + + @Override + public String getUsername() { + return username; + } + + @Override + public void setUsername(String username) { + this.username = username; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public void setPassword(String password) { + this.password = password; + } + + @Override + public String getName() { + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public String getAvatar() { + return avatar; + } + + @Override + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + @Override + public String getRole() { + return role; + } + + @Override + public void setRole(String role) { + this.role = role; + } + +} \ No newline at end of file diff --git a/UserController.java b/UserController.java new file mode 100644 index 00000000..62c4b0df --- /dev/null +++ b/UserController.java @@ -0,0 +1,89 @@ +package com.example.controller; + +import com.example.common.Result; +import com.example.entity.Business; +import com.example.entity.User; +import com.example.service.BusinessService; +import com.example.service.UserService; +import com.github.pagehelper.PageInfo; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 用户前端操作接口 + **/ +@RestController +@RequestMapping("/user") +public class UserController { + + @Resource + private UserService userService; + + /** + * 新增 + */ + @PostMapping("/add") + public Result add(@RequestBody User user) { + userService.add(user); + return Result.success(); + } + + /** + * 删除 + */ + @DeleteMapping("/delete/{id}") + public Result deleteById(@PathVariable Integer id) { + userService.deleteById(id); + return Result.success(); + } + + /** + * 批量删除 + */ + @DeleteMapping("/delete/batch") + public Result deleteBatch(@RequestBody List ids) { + userService.deleteBatch(ids); + return Result.success(); + } + + /** + * 修改 + */ + @PutMapping("/update") + public Result updateById(@RequestBody User user) { + userService.updateById(user); + return Result.success(); + } + + /** + * 根据ID查询 + */ + @GetMapping("/selectById/{id}") + public Result selectById(@PathVariable Integer id) { + User user = userService.selectById(id); + return Result.success(user); + } + + /** + * 查询所有 + */ + @GetMapping("/selectAll") + public Result selectAll(User user ) { + List list = userService.selectAll(user); + return Result.success(list); + } + + /** + * 分页查询 + */ + @GetMapping("/selectPage") + public Result selectPage(User user, + @RequestParam(defaultValue = "1") Integer pageNum, + @RequestParam(defaultValue = "10") Integer pageSize) { + PageInfo page = userService.selectPage(user, pageNum, pageSize); + return Result.success(page); + } + +} \ No newline at end of file diff --git a/UserMapper.java b/UserMapper.java new file mode 100644 index 00000000..0e4fa3d0 --- /dev/null +++ b/UserMapper.java @@ -0,0 +1,40 @@ +package com.example.mapper; + +import com.example.entity.User; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * 操作user相关数据接口 +*/ +public interface UserMapper { + + /** + * 新增 + */ + int insert(User user); + + /** + * 删除 + */ + int deleteById(Integer id); + + /** + * 修改 + */ + int updateById(User user); + + /** + * 根据ID查询 + */ + User selectById(Integer id); + + /** + * 查询所有 + */ + List selectAll(User user); + + @Select("select * from user where username = #{username}") + User selectByUsername(String username); +} \ No newline at end of file diff --git a/UserService.java b/UserService.java new file mode 100644 index 00000000..9327fe9d --- /dev/null +++ b/UserService.java @@ -0,0 +1,136 @@ +package com.example.service; + +import cn.hutool.core.util.ObjectUtil; +import com.example.common.Constants; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.common.enums.StatusEnum; +import com.example.entity.Account; +import com.example.entity.User; +import com.example.exception.CustomException; +import com.example.mapper.UserMapper; +import com.example.utils.TokenUtils; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * 用户业务处理 + **/ +@Service +public class UserService { + + @Resource + private UserMapper userMapper; + + /** + * 新增 + */ + public void add(User user) { + User dbUser = userMapper.selectByUsername(user.getUsername()); + if (ObjectUtil.isNotNull(dbUser)) { + throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR); + } + if (ObjectUtil.isEmpty(user.getPassword())) { + user.setPassword(Constants.USER_DEFAULT_PASSWORD); + } + if (ObjectUtil.isEmpty(user.getName())) { + user.setName(user.getUsername()); + } + user.setRole(RoleEnum.USER.name()); + userMapper.insert(user); + } + + /** + * 删除 + */ + public void deleteById(Integer id) { + userMapper.deleteById(id); + } + + /** + * 批量删除 + */ + public void deleteBatch(List ids) { + for (Integer id : ids) { + userMapper.deleteById(id); + } + } + + /** + * 修改 + */ + public void updateById(User user) { + userMapper.updateById(user); + } + + /** + * 根据ID查询 + */ + public User selectById(Integer id) { + return userMapper.selectById(id); + } + + /** + * 查询所有 + */ + public List selectAll(User user) { + return userMapper.selectAll(user); + } + + /** + * 分页查询 + */ + public PageInfo selectPage(User user, Integer pageNum, Integer pageSize) { + PageHelper.startPage(pageNum, pageSize); + List list = userMapper.selectAll(user); + return PageInfo.of(list); + } + + /** + * 登录 + */ + public Account login(Account account) { + Account dbUser = userMapper.selectByUsername(account.getUsername()); + if (ObjectUtil.isNull(dbUser)) { + throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR); + } + if (!account.getPassword().equals(dbUser.getPassword())) { + throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR); + } + // 生成token + String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name(); + String token = TokenUtils.createToken(tokenData, dbUser.getPassword()); + dbUser.setToken(token); + return dbUser; + } + + /** + * 注册 + */ + public void register(Account account) { + User user = new User(); + BeanUtils.copyProperties(account, user); + add(user); + } + + /** + * 修改密码 + */ + public void updatePassword(Account account) { + User dbUser = userMapper.selectByUsername(account.getUsername()); + if (ObjectUtil.isNull(dbUser)) { + throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR); + } + if (!account.getPassword().equals(dbUser.getPassword())) { + throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR); + } + dbUser.setPassword(account.getNewPassword()); + userMapper.updateById(dbUser); + } + +} \ No newline at end of file diff --git a/WebController.java b/WebController.java new file mode 100644 index 00000000..80d96d57 --- /dev/null +++ b/WebController.java @@ -0,0 +1,97 @@ +package com.example.controller; + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.example.common.Result; +import com.example.common.enums.ResultCodeEnum; +import com.example.common.enums.RoleEnum; +import com.example.entity.Account; +import com.example.service.AdminService; +import com.example.service.BusinessService; +import com.example.service.UserService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +/** + * 基础前端接口 + */ +@RestController +public class WebController { + + @Resource + private AdminService adminService; + @Resource + private BusinessService businessService; + @Resource + private UserService userService; + + @GetMapping("/") + public Result hello() { + return Result.success("访问成功"); + } + + /** + * 登录 + */ + @PostMapping("/login") + public Result login(@RequestBody Account account) { + if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword()) + || ObjectUtil.isEmpty(account.getRole())) { + return Result.error(ResultCodeEnum.PARAM_LOST_ERROR); + } + if (RoleEnum.ADMIN.name().equals(account.getRole())) { + account = adminService.login(account); + } + if (RoleEnum.BUSINESS.name().equals(account.getRole())){ + account = businessService.login(account); + } + if(RoleEnum.USER.name().equals(account.getRole())){ + account = userService.login(account); + } + return Result.success(account); + } + + /** + * 注册 + */ + @PostMapping("/register") + public Result register(@RequestBody Account account) { + if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword()) + || ObjectUtil.isEmpty(account.getRole())) { + return Result.error(ResultCodeEnum.PARAM_LOST_ERROR); + } + if (RoleEnum.ADMIN.name().equals(account.getRole())) { + adminService.register(account); + } + if (RoleEnum.BUSINESS.name().equals(account.getRole())) { + businessService.register(account); + } + if (RoleEnum.USER.name().equals(account.getRole())) { + userService.register(account); + } + return Result.success(); + } + + /** + * 修改密码 + */ + @PutMapping("/updatePassword") + public Result updatePassword(@RequestBody Account account) { + if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword()) + || ObjectUtil.isEmpty(account.getNewPassword())) { + return Result.error(ResultCodeEnum.PARAM_LOST_ERROR); + } + if (RoleEnum.ADMIN.name().equals(account.getRole())) { + adminService.updatePassword(account); + } + if (RoleEnum.BUSINESS.name().equals(account.getRole())) { + businessService.updatePassword(account); + } + if (RoleEnum.USER.name().equals(account.getRole())) { + userService.updatePassword(account); + } + return Result.success(); + } + +}