文章目录
- 地址簿功能模块
- 需求分析
- 代码开发
- 用户下单
- 需求分析
- 代码开发
- 订单支付
- 微信支付介绍
- 微信支付准备工作
- 代码导入
地址簿功能模块
需求分析
代码开发
Controller层
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "C端-地址簿接口")
public class AddressBookController {
@Autowired
private AddressBookService addressBookService;
/**
* 查询当前登录用户的所有地址信息
* @return
*/
@GetMapping("/list")
@ApiOperation("查询当前登录用户的所有地址信息")
public Result<List<AddressBook>> list(){
AddressBook addressBook = new AddressBook();
addressBook.setUserId(BaseContext.getCurrentId());
List<AddressBook> addressBooks = addressBookService.list(addressBook);
return Result.success(addressBooks);
}
/**
* 新增地址
* @param addressBook
* @return
*/
@PostMapping
@ApiOperation("新增地址")
public Result save(@RequestBody AddressBook addressBook){
addressBook.setUserId(BaseContext.getCurrentId());
addressBook.setIsDefault(0);
addressBookService.save(addressBook);
return Result.success();
}
/**
* 根据id查询地址
* @param id
* @return
*/
@GetMapping("/{id}")
@ApiOperation("根据id查询地址")
public Result<AddressBook> getById(@PathVariable Long id){
AddressBook addressBook = addressBookService.getById(id);
return Result.success(addressBook);
}
/**
* 根据id删除地址
* @param id
* @return
*/
@DeleteMapping
@ApiOperation("根据id删除地址")
public Result delete(Long id){
addressBookService.deleteById(id);
return Result.success();
}
/**
* 根据id修改地址
* @param addressBook
* @return
*/
@PutMapping
@ApiOperation("根据id修改地址")
public Result update(@RequestBody AddressBook addressBook){
addressBookService.update(addressBook);
return Result.success();
}
/**
* 设置默认地址
* @return
*/
@PutMapping("/default")
@ApiOperation("设置默认地址")
public Result setDefault(@RequestBody AddressBook addressBook){
addressBookService.setIsDefault(addressBook);
return Result.success();
}
/**
* 查询默认地址
* @return
*/
@GetMapping("/default")
@ApiOperation("查询默认地址")
public Result<AddressBook> getDefault(){
AddressBook addressBook = new AddressBook();
addressBook.setIsDefault(1);
addressBook.setUserId(BaseContext.getCurrentId());
List<AddressBook> list = addressBookService.list(addressBook);
if (list != null && list.size() == 1){
return Result.success(list.get(0));
}
return Result.error("没有查询到默认地址");
}
}
Service实现类
@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
@Autowired
private AddressBookMapper addressBookMapper;
/**
* 查询当前登录用户的所有地址信息
* @param addressBook
* @return
*/
@Override
public List<AddressBook> list(AddressBook addressBook) {
return addressBookMapper.list(addressBook);
}
/**
* 新增地址
* @param addressBook
*/
@Override
public void save(AddressBook addressBook) {
addressBookMapper.insert(addressBook);
}
/**
* 根据id查询地址
* @param id
* @return
*/
@Override
public AddressBook getById(Long id) {
AddressBook addressBook = new AddressBook();
addressBook.setUserId(BaseContext.getCurrentId());
addressBook.setId(id);
return addressBookMapper.getById(addressBook);
}
/**
* 根据id删除地址
* @param id
*/
@Override
public void deleteById(Long id) {
addressBookMapper.deleteById(id);
}
/**
* 根据id修改地址
* @param addressBook
* @return
*/
@Override
public void update(AddressBook addressBook) {
addressBookMapper.update(addressBook);
}
/**
* 设置默认地址
* @param addressBook
*/
@Override
@Transactional
public void setIsDefault(AddressBook addressBook) {
//1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
addressBook.setIsDefault(0);
addressBook.setUserId(BaseContext.getCurrentId());
addressBookMapper.updateIsDefaultByUserId(addressBook);
//2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
addressBook.setIsDefault(1);
addressBookMapper.update(addressBook);
}
}
Mapper接口层
@Mapper
public interface AddressBookMapper {
/**
* 查询当前登录用户的所有地址信息
* @param userId
* @return
*/
List<AddressBook> list(AddressBook addressBook);
/**
* 新增地址
* @param addressBook
*/
void insert(AddressBook addressBook);
/**
* 根据id查询地址
* @param addressBook
* @return
*/
@Select("select * from address_book where user_id = #{userId} and id = #{id}")
AddressBook getById(AddressBook addressBook);
/**
* 根据id删除地址
* @param id
*/
@Delete("delete from address_book where id = #{id}")
void deleteById(Long id);
/**
* 根据id修改地址
* @return
*/
void update(AddressBook addressBook);
/**
* 设置默认地址
* @param addressBook
*/
@Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
void updateIsDefaultByUserId(AddressBook addressBook);
}
XML文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.mapper.AddressBookMapper">
<select id="list" resultType="com.sky.entity.AddressBook">
select * from address_book
<where>
<if test="userId != null">
and user_id = #{userId}
</if>
<if test="phone != null">
and phone = #{phone}
</if>
<if test="isDefault != null">
and is_default = #{isDefault}
</if>
</where>
</select>
<insert id="insert">
insert into address_book (user_id, consignee, sex, phone, province_code, province_name, city_code, city_name, district_code, district_name, detail, label, is_default) VALUES
(#{userId},#{consignee},#{sex},#{phone},#{provinceCode},#{provinceName},#{cityCode},#{cityName},#{districtCode},#{districtName},#{detail},#{label},#{isDefault})
</insert>
<update id="update" parameterType="addressBook">
update address_book
<set>
<if test="consignee != null">
consignee = #{consignee},
</if>
<if test="sex != null">
sex = #{sex},
</if>
<if test="phone != null">
phone = #{phone},
</if>
<if test="provinceCode != null">
province_code = #{provinceCode},
</if>
<if test="provinceName != null">
province_name = #{provinceName},
</if>
<if test="cityCode != null">
city_code = #{cityCode},
</if>
<if test="cityName != null">
city_name = #{cityName},
</if>
<if test="districtCode != null">
district_code = #{districtCode},
</if>
<if test="districtName != null">
district_name = #{districtName},
</if>
<if test="detail != null">
detail = #{detail},
</if>
<if test="label != null">
label = #{label},
</if>
<if test="isDefault != null">
is_default = #{isDefault},
</if>
</set>
where id = #{id}
</update>
</mapper>
用户下单
需求分析
代码开发
Controller层
@RestController("UserOrderController")
@RequestMapping("/user/order")
@Api(tags = "C端-订单相关接口")
public class OrderController {
@Autowired
private OrderService orderService;
/**
* 用户下单
* @param orderSubmitDTO
* @return
*/
@PostMapping("/submit")
@ApiOperation("用户下单")
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO orderSubmitDTO){
OrderSubmitVO orderSubmitVO = orderService.submitOrder(orderSubmitDTO);
return Result.success(orderSubmitVO);
}
}
Service实现类
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderDetailMapper orderDetailMapper;
@Autowired
private ShopppingCartMapper shopppingCartMapper;
@Autowired
private AddressBookMapper addressBookMapper;
/**
* 用户下单
* @param orderSubmitDTO
* @return
*/
@Override
@Transactional
public OrderSubmitVO submitOrder(OrdersSubmitDTO orderSubmitDTO) {
//处理各种业务异常(地址簿为空,数据车数据为空)
Long userId = BaseContext.getCurrentId();
AddressBook addressBook = new AddressBook();
addressBook.setUserId(userId);
addressBook.setId(orderSubmitDTO.getAddressBookId());
AddressBook addressBook1 = addressBookMapper.getById(addressBook);
if(addressBook1 == null){
//抛出业务异常
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
}
//查询当前用户的购物车数据
ShoppingCart shoppingCart = new ShoppingCart();
shoppingCart.setUserId(userId);
List<ShoppingCart> shoppingCartList = shopppingCartMapper.list(shoppingCart);
if (shoppingCartList == null || shoppingCartList.size() == 0){
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
}
//向订单表插入一条数据
Orders orders = new Orders();
BeanUtils.copyProperties(orderSubmitDTO, orders);
orders.setOrderTime(LocalDateTime.now());
orders.setPayStatus(Orders.UN_PAID);
orders.setStatus(Orders.PENDING_PAYMENT);
orders.setNumber(String.valueOf(System.currentTimeMillis()));
orders.setPhone(addressBook1.getPhone());
orders.setConsignee(addressBook1.getConsignee());
orders.setUserId(userId);
orderMapper.insert(orders);
//向订单明细表插入n条数据
List<OrderDetail> orderDetailList = new ArrayList<>();
for (ShoppingCart cart: shoppingCartList
) {
OrderDetail orderDetail = new OrderDetail();//订单明细
BeanUtils.copyProperties(cart, orderDetail);
orderDetail.setOrderId(orders.getId());//设置当前订单明细关联的id
//批量插入
orderDetailList.add(orderDetail);
}
//批量插入订单明细数据
orderDetailMapper.insertBatch(orderDetailList);
//清空用户购物车
shopppingCartMapper.deleteByUserId(userId);
//封装VO返回结果
OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
.id(orders.getId())
.orderTime(orders.getOrderTime())
.orderNumber(orders.getNumber())
.orderAmount(orders.getAmount())
.build();
return orderSubmitVO;
}
}
两个Mapper接口
@Mapper
public interface OrderMapper {
/**
* 插入订单数据
* @param orders
*/
void insert(Orders orders);
}
@Mapper
public interface OrderDetailMapper {
/**
* 批量插入明细数据
* @param orderDetailList
*/
void insertBatch(List<OrderDetail> orderDetailList);
}
两个XML文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.mapper.OrderMapper">
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into orders (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone,
address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status,
delivery_time, pack_amount, tableware_number, tableware_status) VALUES
(#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},
#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},
#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})
</insert>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sky.mapper.OrderDetailMapper">
<insert id="insertBatch">
insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor, number, amount) VALUES
<foreach collection="orderDetailList" item="od" separator=",">
(#{od.name},#{od.image},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},#{od.number},#{od.amount})
</foreach>
</insert>
</mapper>
订单支付
微信支付介绍
微信支付准备工作
下载cpolar,配置内网穿透文件
代码导入
其中遇到视频中的微信支付需要商家认证才能开通,个人做项目只能模拟支付。可通过下面文章中的方法跳过支付流程。苍穹外卖项目模拟微信支付