1.代客下单相关接口

This commit is contained in:
SongZhang 2024-08-14 15:04:53 +08:00
parent 9bfa67d71b
commit 441ecf38f1
16 changed files with 537 additions and 110 deletions

View File

@ -105,6 +105,7 @@ public class TokenProvider implements InitializingBean {
.getBody();
}
/**
* @param token 需要检查的token
*/
@ -137,4 +138,17 @@ public class TokenProvider implements InitializingBean {
}
return null;
}
public String getSubject() {
HttpServletRequest request = SpringContextHolder.getRequest();
final String requestHeader = request.getHeader(properties.getHeader());
if (requestHeader != null && requestHeader.startsWith(properties.getTokenStartWith())) {
Claims claims = jwtParser
.parseClaimsJws(requestHeader.substring(7))
.getBody();
return claims.getSubject();
}
return null;
}
}

View File

@ -0,0 +1,15 @@
package cn.ysk.cashier.cons;
public interface RedisConstant {
//在线用户
String ONLINE_USER = "ONLINE:USER";
String CART = "CZG:CART:";
public final Object PRODUCT = "PRODUCT:";
public final String OUT_NUMBER="ORDER:NUMBER:";
public static final String ORDER_MESSAGE="ORDER:MESSAGE:";
public static final String ORDER_PRODUCT_NUM = "ORDER_NUM:";
public static final String ORDER_CART_EXISTS = "ORDER_CART_EXISTS:";
}

View File

@ -5,4 +5,6 @@ public interface RabbitConstants {
String CONS_COLLECT_ROUTINGKEY_PUT = "cons_collect_routingkey_put";
String CONS_MSG_COLLECT_PUT = "cons_msg_collect_put";
String CONS_MSG_COLLECT_ROUTINGKEY_PUT = "cons_msg_collect_routingkey_put";
}

View File

@ -81,7 +81,7 @@ public class TbPlaceController {
@DeleteMapping("/clearCart")
@Log("代客下单 清空购物车")
@ApiOperation("代客下单 清空购物车 /shop/table")
@ApiOperation("代客下单 清空购物车 /shop/table"/**/)
public ResponseEntity<Object> clearCart(@Validated @RequestBody ClearCartDTO clearCartDTO) {
tbShopTableService.clearCart(clearCartDTO);
return new ResponseEntity<>(HttpStatus.OK);
@ -95,9 +95,10 @@ public class TbPlaceController {
@RequestParam Long tableId,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer size,
@RequestParam Integer shopId
@RequestParam Integer shopId,
@RequestParam(required = false) Integer vipUserId
) {
return ResponseEntity.ok(tbShopTableService.getCart(tableId, page, size, shopId));
return ResponseEntity.ok(tbShopTableService.getCart(tableId, page, size, shopId, vipUserId));
}
@AnonymousAccess
@ -106,10 +107,11 @@ public class TbPlaceController {
@Log("代客下单 查询购物车")
@ApiOperation("代客下单 ")
public ResponseEntity<Object> getMasterId(
@RequestParam Integer shopId,
@RequestParam Long tableId,
@RequestParam Integer shopId
@RequestParam(required = false) Integer vipUserId
) {
return ResponseEntity.ok(tbShopTableService.getMasterId(tableId, shopId));
return ResponseEntity.ok(tbShopTableService.getMasterId(shopId, tableId, vipUserId));
}
@AnonymousAccess

View File

@ -8,8 +8,12 @@ import javax.validation.constraints.NotNull;
@Data
public class AddCartDTO {
@NotEmpty
private String masterId;
private Integer vipUserId;
@NotNull
private Integer productId;
@NotNull
private Integer skuId;
@NotNull
private Integer shopId;

View File

@ -2,10 +2,14 @@ package cn.ysk.cashier.dto.shoptable;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Data
public class ClearCartDTO {
@NotEmpty
private String masterId;
private Integer vipUserId;
@NotNull
private Long tableId;
@NotNull

View File

@ -7,6 +7,9 @@ import javax.validation.constraints.NotNull;
@Data
public class CreateOrderDTO {
@NotEmpty
private String masterId;
private Integer vipUserId;
@NotNull
private Integer shopId;
@NotEmpty

View File

@ -8,6 +8,9 @@ import javax.validation.constraints.NotNull;
@Data
public class PackCartDTO {
@NotEmpty
private String masterId;
private Integer vipUserId;
@NotNull
private Integer shopId;
@NotEmpty

View File

@ -3,6 +3,7 @@ package cn.ysk.cashier.dto.shoptable;
import lombok.Data;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Data

View File

@ -0,0 +1,7 @@
package cn.ysk.cashier.mybatis.mapper;
import cn.ysk.cashier.pojo.order.TbOrderDetail;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface TbOrderDetailMapper extends BaseMapper<TbOrderDetail> {
}

View File

@ -19,4 +19,12 @@ public interface TbProducSkutMapper extends BaseMapper<TbProductSku> {
@Update("update tb_product_sku set stock_number=#{stocktakinNum} where id=#{id} and stock_number=#{stockNumber}")
Integer updateStock(@Param("id") Integer id,@Param("stockNumber") Double stockNumber,@Param("stocktakinNum") Integer stocktakinNum);
@Update("update tb_product_sku set stock_number=stock_number+#{addNum} WHERE id=#{skuId}")
int incrStock(@Param("skuId") Integer skuId, @Param("addNum") Integer addNum);
@Update("update tb_product_sku set stock_number=stock_number-#{num} WHERE id=#{id} and stock_number-#{num} >= 0")
int decrStock(String id, int num);
@Update("update tb_product_sku set stock_number=stock_number-#{num} WHERE id=#{id} ")
int decrStockUnCheck(String id, int num);
}

View File

@ -5,6 +5,7 @@ import cn.ysk.cashier.pojo.product.TbProductSku;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.jpa.repository.Query;
public interface TbProductMapper extends BaseMapper<TbProduct> {
@ -14,4 +15,13 @@ public interface TbProductMapper extends BaseMapper<TbProduct> {
@Select("select * from tb_product product where product.id=#{id} and product.shop_id=#{shopId} and product.is_del=0")
TbProduct selectByIdAndShopId(@Param("shopId") Integer shopId, @Param("id") Integer id);
@Update("update tb_product set stock_number=stock_number+#{addNum} WHERE id=#{id}")
int incrStock(@Param("id") Integer id, @Param("addNum") Integer addNum);
@Update("update tb_product set stock_number=stock_number-#{decrNum} WHERE id=#{productId} and stock_number-#{decrNum} >= 0")
int decrStock(@Param("productId") Integer productId, @Param("decrNum") int decrNum);
@Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id}")
int decrStockUnCheck(Integer id, int num);
}

View File

@ -220,6 +220,9 @@ public class TbOrderInfo implements Serializable {
@ApiModelProperty(value = "是否购买优惠券")
private String isBuyCoupon;
@Column(name = "`out_number`")
private String outNumber;
@Column(name = "`is_use_coupon`")
@ApiModelProperty(value = "是否使用优惠券")
private String isUseCoupon;

View File

@ -2,7 +2,6 @@ package cn.ysk.cashier.repository.order;
import cn.ysk.cashier.pojo.order.TbCashierCart;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
@ -23,8 +22,6 @@ public interface TbCashierCartRepository extends JpaRepository<TbCashierCart, In
@Modifying
void updateToFinal(@Param("id") Integer id);
@Query("select cart from TbCashierCart cart where cart.tableId=:tableId and cart.status='create' and cart.shopId=:shopId and cart.skuId=:skuId")
TbCashierCart selectActivateBySkuId(String skuId, String shopId, Long tableId);
@Query("delete from TbCashierCart cart where cart.shopId=:shopId and cart.id=:cartId and cart.tableId=:tableId and cart.status='create'")
@Modifying

View File

@ -1,36 +1,44 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.ysk.cashier.service.impl.shopimpl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.ysk.cashier.config.security.security.TokenProvider;
import cn.ysk.cashier.cons.RedisConstant;
import cn.ysk.cashier.cons.rabbit.RabbitConstants;
import cn.ysk.cashier.dto.shoptable.*;
import cn.ysk.cashier.enums.ShopWxMsgTypeEnum;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.mapper.TbCashierCartMapper;
import cn.ysk.cashier.mybatis.mapper.TbProductMapper;
import cn.ysk.cashier.mybatis.entity.TbShopOpenId;
import cn.ysk.cashier.mybatis.mapper.*;
import cn.ysk.cashier.pojo.order.TbCashierCart;
import cn.ysk.cashier.pojo.order.TbOrderDetail;
import cn.ysk.cashier.pojo.order.TbOrderInfo;
import cn.ysk.cashier.pojo.product.TbProduct;
import cn.ysk.cashier.pojo.product.TbProductSku;
import cn.ysk.cashier.pojo.shop.TbMerchantAccount;
import cn.ysk.cashier.pojo.shop.TbShopInfo;
import cn.ysk.cashier.pojo.shop.TbShopTable;
import cn.ysk.cashier.repository.order.TbCashierCartRepository;
import cn.ysk.cashier.repository.product.TbProductRepository;
import cn.ysk.cashier.repository.product.TbProductSkuRepository;
import cn.ysk.cashier.utils.RedisUtils;
import cn.ysk.cashier.utils.ValidationUtil;
import cn.ysk.cashier.utils.FileUtil;
import cn.ysk.cashier.repository.shop.TbShopInfoRepository;
import cn.ysk.cashier.utils.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dianguang.cloud.ossservice.model.DateUtils;
@ -42,25 +50,28 @@ import cn.ysk.cashier.dto.shop.TbShopTableQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbShopTableMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import cn.ysk.cashier.utils.QueryHelp;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.*;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
/**
* @website https://eladmin.vip
* @description 服务实现
* @author lyf
* @date 2024-01-18
**/
* @author lyf
* @website https://eladmin.vip
* @description 服务实现
* @date 2024-01-18
**/
@Slf4j
@Service
@RequiredArgsConstructor
@ -74,31 +85,41 @@ public class TbShopTableServiceImpl implements TbShopTableService {
private final TbProductMapper productMapper;
private final TbCashierCartMapper cashierCartMapper;
private final RedisUtils redisUtils;
private final TokenProvider tokenProvider;
private final TbMerchantAccountMapper merchantAccountMapper;
private final TbOrderInfoMapper orderInfoMapper;
private final TbOrderDetailMapper orderDetailMapper;
private final TbProducSkutMapper producSkutMapper;
private final RabbitTemplate rabbitTemplate;
private final TbShopInfoRepository shopInfoRepository;
private final TbShopOpenIdMapper shopOpenIdMapper;
private final WxAccountUtil wxAccountUtil;
private final WxMsgUtils wxMsgUtils;
/**
*桌码前缀
* 桌码前缀
*/
private final String QRCODE = "https://kysh.sxczgkj.cn/codeplate?code=";
@Override
public Map<String,Object> queryAll(TbShopTableQueryCriteria criteria, Pageable pageable){
if (criteria.getAreaId() == 0){
public Map<String, Object> queryAll(TbShopTableQueryCriteria criteria, Pageable pageable) {
if (criteria.getAreaId() == 0) {
criteria.setAreaId(null);
}
Page<TbShopTable> page = tbShopTableRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
Page<TbShopTable> page = tbShopTableRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
for (TbShopTable date : page.getContent()) {
if (!"".equals(date.getQrcode())){
date.setQrcode(QRCODE+date.getQrcode().trim());
if (!"".equals(date.getQrcode())) {
date.setQrcode(QRCODE + date.getQrcode().trim());
}
}
HashMap<String, Object> map = new HashMap<>();
map.put("content",page.getContent());
map.put("totalElements",page.getTotalElements());
map.put("content", page.getContent());
map.put("totalElements", page.getTotalElements());
return map;
}
@Override
public Map<String,Object> queryAllNoPage(TbShopTableQueryCriteria criteria){
public Map<String, Object> queryAllNoPage(TbShopTableQueryCriteria criteria) {
if (null == criteria.getAreaId() || criteria.getAreaId() == 0) {
criteria.setAreaId(null);
}
@ -106,22 +127,22 @@ public class TbShopTableServiceImpl implements TbShopTableService {
ArrayList<Map<String, Object>> infoList = new ArrayList<>();
for (TbShopTable date : tbShopTableList) {
Map<String, Object> itemMap = BeanUtil.beanToMap(date, false, false);
if (!"".equals(date.getQrcode())){
itemMap.put("qrcode",QRCODE+date.getQrcode().trim());
itemMap.put("tableId",date.getQrcode());
if (!"".equals(date.getQrcode())) {
itemMap.put("qrcode", QRCODE + date.getQrcode().trim());
itemMap.put("tableId", date.getQrcode());
}
infoList.add(itemMap);
}
int i = tbShopTableRepository.countAllByShopId(criteria.getShopId());
HashMap<String, Object> map = new HashMap<>();
map.put("content",infoList);
map.put("total",i);
map.put("content", infoList);
map.put("total", i);
return map;
}
@Override
public List<TbShopTableDto> queryAll(TbShopTableQueryCriteria criteria){
return tbShopTableMapper.toDto(tbShopTableRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
public List<TbShopTableDto> queryAll(TbShopTableQueryCriteria criteria) {
return tbShopTableMapper.toDto(tbShopTableRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
}
@Override
@ -133,7 +154,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
@Transactional
public TbShopTableDto findById(Integer id) {
TbShopTable tbShopTable = tbShopTableRepository.findById(id).orElseGet(TbShopTable::new);
ValidationUtil.isNull(tbShopTable.getId(),"TbShopTable","id",id);
ValidationUtil.isNull(tbShopTable.getId(), "TbShopTable", "id", id);
return tbShopTableMapper.toDto(tbShopTable);
}
@ -141,14 +162,14 @@ public class TbShopTableServiceImpl implements TbShopTableService {
public void binding(TbShopTable resources) {
//判是否绑定过
TbShopTable byQrcode = tbShopTableRepository.findByQrcode(resources.getQrcode());
if (byQrcode != null){
if (byQrcode != null) {
throw new BadRequestException("已绑定");
}
TbShopTable tbShopTable = tbShopTableRepository.findById(resources.getId()).orElseGet(TbShopTable::new);
if (tbShopTable.getId() == null){
if (tbShopTable.getId() == null) {
throw new BadRequestException("找不到台桌");
}
ValidationUtil.isNull( tbShopTable.getId(),"TbShopTable","id",resources.getId());
ValidationUtil.isNull(tbShopTable.getId(), "TbShopTable", "id", resources.getId());
tbShopTable.copy(resources);
tbShopTableRepository.save(tbShopTable);
}
@ -167,7 +188,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
@Transactional(rollbackFor = Exception.class)
public void update(TbShopTable resources) {
TbShopTable tbShopTable = tbShopTableRepository.findById(resources.getId()).orElseGet(TbShopTable::new);
ValidationUtil.isNull( tbShopTable.getId(),"TbShopTable","id",resources.getId());
ValidationUtil.isNull(tbShopTable.getId(), "TbShopTable", "id", resources.getId());
tbShopTable.copy(resources);
tbShopTableRepository.save(tbShopTable);
}
@ -183,9 +204,9 @@ public class TbShopTableServiceImpl implements TbShopTableService {
public void download(List<TbShopTableDto> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (TbShopTableDto tbShopTable : all) {
Map<String,Object> map = new LinkedHashMap<>();
map.put(" name", tbShopTable.getName());
map.put(" shopId", tbShopTable.getShopId());
Map<String, Object> map = new LinkedHashMap<>();
map.put(" name", tbShopTable.getName());
map.put(" shopId", tbShopTable.getShopId());
map.put("客座数,允许的客座数量", tbShopTable.getMaxCapacity());
map.put("台桌排序", tbShopTable.getSort());
map.put("区域Id", tbShopTable.getAreaId());
@ -196,8 +217,8 @@ public class TbShopTableServiceImpl implements TbShopTableService {
map.put("当type=0时amount生效,为台桌的低消金额", tbShopTable.getAmount());
map.put("当type=2时perhour生效为计时类型,每小时收款金额", tbShopTable.getPerhour());
map.put("台桌展示图---预留", tbShopTable.getView());
map.put(" createdAt", tbShopTable.getCreatedAt());
map.put(" updatedAt", tbShopTable.getUpdatedAt());
map.put(" createdAt", tbShopTable.getCreatedAt());
map.put(" updatedAt", tbShopTable.getUpdatedAt());
list.add(map);
}
FileUtil.downloadExcel(list, response);
@ -231,9 +252,9 @@ public class TbShopTableServiceImpl implements TbShopTableService {
tbCashierCart.setStatus("create");
tbCashierCart.setSalePrice(productSku.getSalePrice());
tbCashierCart.setTotalAmount(new BigDecimal(updateCartDTO.getNum()).multiply(productSku.getSalePrice()));
if (tbCashierCart.getIsPack().equals("false")){
if (tbCashierCart.getIsPack().equals("false")) {
tbCashierCart.setPackFee(BigDecimal.ZERO);
}else {
} else {
tbCashierCart.setPackFee(new BigDecimal(updateCartDTO.getNum()).multiply(product.getPackFee()));
tbCashierCart.setTotalAmount(tbCashierCart.getTotalAmount().add(tbCashierCart.getPackFee()));
}
@ -258,7 +279,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
throw new BadRequestException("商品不存在或已下架, id: " + addCartDTO.getSkuId());
}
if ((product.getIsDistribute().equals(1) && product.getStockNumber() < 1 )
if ((product.getIsDistribute().equals(1) && product.getStockNumber() < 1)
|| (!product.getIsDistribute().equals(1) && productSku.getStockNumber() < 1)
) {
throw new BadRequestException("商品库存不足");
@ -269,10 +290,19 @@ public class TbShopTableServiceImpl implements TbShopTableService {
throw new BadRequestException("桌码不存在,桌码" + addCartDTO.getTableId());
}
TbCashierCart tbCashierCart = cashierCartRepository.selectActivateBySkuId(String.valueOf(addCartDTO.getSkuId()), String.valueOf(addCartDTO.getShopId()), Long.valueOf(addCartDTO.getTableId()));
LambdaQueryWrapper<TbCashierCart> query = new LambdaQueryWrapper<TbCashierCart>().eq(TbCashierCart::getShopId, addCartDTO.getShopId())
.eq(TbCashierCart::getTableId, addCartDTO.getTableId());
if (addCartDTO.getVipUserId() != null) {
query.eq(TbCashierCart::getUserId, addCartDTO.getVipUserId());
} else {
query.isNull(TbCashierCart::getUserId);
}
TbCashierCart tbCashierCart = cashierCartMapper.selectOne(query);
// 首次加入
if (tbCashierCart == null) {
tbCashierCart = new TbCashierCart();
tbCashierCart.setUserId(addCartDTO.getVipUserId());
tbCashierCart.setCoverImg(product.getCoverImg());
tbCashierCart.setCreatedAt(System.currentTimeMillis());
tbCashierCart.setIsSku(product.getTypeEnum());
@ -281,6 +311,7 @@ public class TbShopTableServiceImpl implements TbShopTableService {
tbCashierCart.setProductId(String.valueOf(product.getId()));
tbCashierCart.setSalePrice(productSku.getSalePrice());
tbCashierCart.setSkuId(productSku.getId().toString());
tbCashierCart.setMasterId(addCartDTO.getMasterId());
tbCashierCart.setShopId(String.valueOf(addCartDTO.getShopId()));
tbCashierCart.setTradeDay(DateUtils.getDay());
tbCashierCart.setStatus("create");
@ -288,9 +319,9 @@ public class TbShopTableServiceImpl implements TbShopTableService {
tbCashierCart.setIsGift(String.valueOf(addCartDTO.isGift()));
tbCashierCart.setSalePrice(productSku.getSalePrice());
tbCashierCart.setTotalAmount(new BigDecimal(addCartDTO.getNum()).multiply(productSku.getSalePrice()));
if (!addCartDTO.isPack()){
if (!addCartDTO.isPack()) {
tbCashierCart.setPackFee(BigDecimal.ZERO);
}else {
} else {
tbCashierCart.setPackFee(new BigDecimal(addCartDTO.getNum()).multiply(product.getPackFee()));
tbCashierCart.setTotalAmount(tbCashierCart.getTotalAmount().add(tbCashierCart.getPackFee()));
@ -302,12 +333,12 @@ public class TbShopTableServiceImpl implements TbShopTableService {
tbCashierCart.setNumber(addCartDTO.getNum());
tbCashierCart.setCategoryId(product.getCategoryId());
cashierCartRepository.save(tbCashierCart);
}else {
} else {
tbCashierCart.setTotalAmount(new BigDecimal(addCartDTO.getNum()).multiply(productSku.getSalePrice()));
if (!addCartDTO.isPack()){
if (!addCartDTO.isPack()) {
tbCashierCart.setPackFee(BigDecimal.ZERO);
}else {
} else {
tbCashierCart.setPackFee(new BigDecimal(addCartDTO.getNum()).multiply(product.getPackFee()));
tbCashierCart.setTotalAmount(tbCashierCart.getTotalAmount().add(tbCashierCart.getPackFee()));
@ -335,20 +366,41 @@ public class TbShopTableServiceImpl implements TbShopTableService {
@Override
public void removeCart(RemoveCartDTO removeCartDTO) {
cashierCartRepository.deleteByIdAndShopId(String.valueOf(removeCartDTO.getShopId()), removeCartDTO.getCartId(), removeCartDTO.getTableId());
// 会员点单
cashierCartMapper.delete(new LambdaQueryWrapper<TbCashierCart>().eq(TbCashierCart::getShopId, removeCartDTO.getShopId())
.eq(TbCashierCart::getId, removeCartDTO.getCartId()));
}
@Override
public void clearCart(ClearCartDTO clearCartDTO) {
if (clearCartDTO.getVipUserId() != null) {
cashierCartMapper.delete(new LambdaQueryWrapper<TbCashierCart>().eq(TbCashierCart::getShopId, clearCartDTO.getShopId())
.eq(TbCashierCart::getTableId, clearCartDTO.getTableId())
.eq(TbCashierCart::getUserId, clearCartDTO.getVipUserId()));
} else {
cashierCartMapper.delete(new LambdaQueryWrapper<TbCashierCart>().eq(TbCashierCart::getShopId, clearCartDTO.getShopId())
.eq(TbCashierCart::getTableId, clearCartDTO.getTableId())
.eq(TbCashierCart::getMasterId, clearCartDTO.getMasterId())
.isNull(TbCashierCart::getUserId));
}
tbShopTableRepository.deleteByTableIdAndShopId(clearCartDTO.getTableId(), clearCartDTO.getShopId());
}
@Override
public com.baomidou.mybatisplus.extension.plugins.pagination.Page<TbCashierCart> getCart(Long tableId, Integer page, Integer size, Integer shopId) {
com.baomidou.mybatisplus.extension.plugins.pagination.Page<TbCashierCart> cartPage = cashierCartMapper.selectPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page, size), new LambdaQueryWrapper<TbCashierCart>()
public com.baomidou.mybatisplus.extension.plugins.pagination.Page<TbCashierCart> getCart(Long tableId, Integer page, Integer size, Integer shopId, Integer vipUserId) {
LambdaQueryWrapper<TbCashierCart> queryWrapper = new LambdaQueryWrapper<TbCashierCart>()
.eq(TbCashierCart::getTableId, tableId)
.eq(TbCashierCart::getStatus, "create")
.eq(TbCashierCart::getShopId, shopId));
.eq(TbCashierCart::getShopId, shopId);
if (vipUserId != null) {
queryWrapper.eq(TbCashierCart::getUserId, vipUserId);
}else {
queryWrapper.isNull(TbCashierCart::getUserId);
}
com.baomidou.mybatisplus.extension.plugins.pagination.Page<TbCashierCart> cartPage = cashierCartMapper
.selectPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page, size), queryWrapper);
List<TbCashierCart> records = cartPage.getRecords();
ArrayList<Integer> skuIds = new ArrayList<>();
@ -378,14 +430,23 @@ public class TbShopTableServiceImpl implements TbShopTableService {
@Override
public void pack(PackCartDTO packCartDTO) {
List<TbCashierCart> tbCashierCarts = cashierCartMapper.selectList(new LambdaQueryWrapper<TbCashierCart>().eq(TbCashierCart::getTableId, packCartDTO.getTableId())
LambdaQueryWrapper<TbCashierCart> queryWrapper = new LambdaQueryWrapper<TbCashierCart>()
.eq(TbCashierCart::getTableId, packCartDTO.getTableId())
.eq(TbCashierCart::getShopId, packCartDTO.getShopId())
.eq(TbCashierCart::getStatus, "create"));
.eq(TbCashierCart::getStatus, "create");
if (packCartDTO.getVipUserId() != null) {
queryWrapper.eq(TbCashierCart::getUserId, packCartDTO.getVipUserId());
} else {
queryWrapper.isNull(TbCashierCart::getUserId);
}
List<TbCashierCart> tbCashierCarts = cashierCartMapper.selectList(queryWrapper);
tbCashierCarts.forEach(item -> {
if (packCartDTO.getState().equals(0) && item.getIsPack().equals("true")) {
item.setIsPack("false");
item.setTotalAmount(item.getTotalAmount().subtract(item.getPackFee()));
}else {
} else {
item.setIsPack("true");
item.setTotalAmount(item.getTotalAmount().add(item.getPackFee()));
}
@ -396,56 +457,349 @@ public class TbShopTableServiceImpl implements TbShopTableService {
@Override
public Object createOrder(CreateOrderDTO createOrderDTO) {
// 生成取餐码
String day = DateUtils.getDay();
LambdaQueryWrapper<TbCashierCart> queryWrapper = new LambdaQueryWrapper<TbCashierCart>()
.eq(TbCashierCart::getShopId, createOrderDTO.getShopId())
.eq(TbCashierCart::getTableId, createOrderDTO.getTableId())
.eq(TbCashierCart::getStatus, "create");
if (createOrderDTO.getVipUserId() != null) {
queryWrapper.eq(TbCashierCart::getUserId, createOrderDTO.getVipUserId());
}else {
queryWrapper.eq(TbCashierCart::getMasterId, createOrderDTO.getMasterId())
.isNull(TbCashierCart::getUserId);
}
List<TbCashierCart> cashierCarts = cashierCartMapper
.selectList(queryWrapper);
if (cashierCarts.isEmpty()) {
throw new BadRequestException("购物车为空,请先添加商品");
}
return null;
BigDecimal totalAmount = BigDecimal.ZERO;
BigDecimal packAMount = BigDecimal.ZERO;
BigDecimal feeAmount = BigDecimal.ZERO;
BigDecimal saleAmount = BigDecimal.ZERO;
List<TbOrderDetail> orderDetails = new ArrayList<>();
Integer orderId = null;
for (TbCashierCart cashierCart : cashierCarts) {
totalAmount = totalAmount.add(cashierCart.getTotalAmount());
packAMount = packAMount.add(cashierCart.getPackFee());
feeAmount = cashierCart.getPackFee();
TbProductSku productSku = productSkuRepository.findById(Integer.valueOf(cashierCart.getSkuId())).orElse(null);
TbOrderDetail orderDetail = new TbOrderDetail();
if (Objects.nonNull(productSku)) {
saleAmount = saleAmount.add(productSku.getSalePrice());
orderDetail.setProductSkuName(productSku.getSpecSnap());
}
orderDetail.setCreateTime(DateUtil.date().toTimestamp());
orderDetail.setNum(cashierCart.getNumber());
orderDetail.setPrice(cashierCart.getSalePrice());
orderDetail.setPriceAmount(cashierCart.getTotalAmount());
orderDetail.setProductId(Integer.valueOf(cashierCart.getProductId()));
orderDetail.setProductSkuId(Integer.valueOf(cashierCart.getSkuId()));
orderDetail.setProductName(cashierCart.getName());
orderDetail.setShopId(Integer.valueOf(cashierCart.getShopId()));
orderDetail.setPackAmount(cashierCart.getPackFee());
orderDetail.setStatus("unpaid");
orderDetail.setProductImg(cashierCart.getCoverImg());
orderDetails.add(orderDetail);
if (cashierCart.getOrderId() != null) {
orderId = cashierCart.getOrderId();
}
}
TbOrderInfo orderInfo = null;
if (orderId != null) {
orderInfo = orderInfoMapper.selectById(orderId);
}
// 修改订单信息
if (orderInfo != null) {
// 删除历史订单
orderDetailMapper.delete(new LambdaQueryWrapper<TbOrderDetail>().eq(TbOrderDetail::getOrderId, orderId));
orderInfo.setUpdatedAt(System.currentTimeMillis());
orderInfo.setSettlementAmount(totalAmount);
orderInfo.setAmount(totalAmount);
orderInfo.setOriginAmount(totalAmount);
orderInfo.setStatus("unpaid");
orderInfo.setOrderAmount(totalAmount);
orderInfo.setRemark(createOrderDTO.getNote());
orderInfo.setFreightAmount(feeAmount);
orderInfo.setProductAmount(saleAmount);
orderInfo.setTradeDay(DateUtils.getDay());
orderInfoMapper.updateById(orderInfo);
}else {
String orderNo = generateOrderNumber();
orderInfo = new TbOrderInfo();
orderInfo.setOrderNo(orderNo);
orderInfo.setAmount(totalAmount);
orderInfo.setPackFee(packAMount);
orderInfo.setSettlementAmount(totalAmount);
orderInfo.setOriginAmount(totalAmount);
orderInfo.setProductAmount(saleAmount);
orderInfo.setOrderAmount(totalAmount);
orderInfo.setFreightAmount(feeAmount);
orderInfo.setTableId(createOrderDTO.getTableId());
orderInfo.setSendType("table");
orderInfo.setOrderType("cash");
orderInfo.setShopId(createOrderDTO.getShopId().toString());
orderInfo.setRefundAble(1);
orderInfo.setTradeDay(day);
orderInfo.setMasterId(createOrderDTO.getMasterId());
orderInfo.setRemark(createOrderDTO.getNote());
}
// 更新取餐号
orderInfo.setOutNumber(updateOutNumber(String.valueOf(createOrderDTO.getShopId())).toString());
orderInfoMapper.insert(orderInfo);
// 添加订单详细数据
orderId = orderInfo.getId();
for (TbOrderDetail orderDetail : orderDetails) {
orderDetail.setOrderId(orderId);
orderDetailMapper.insert(orderDetail);
}
// 更新购物车记录的orderId
// 是否是第一次添加的商品
boolean isFirst = true;
for (TbCashierCart cashierCart : cashierCarts) {
TbProduct product = productMapper.selectById(cashierCart.getProductId());
TbProductSku productSku = productSkuRepository.findById(Integer.valueOf(cashierCart.getSkuId())).orElse(null);
log.info("下单,开始校验库存预警,购物车id:{}", cashierCart.getId());
CompletableFuture.runAsync(() -> checkWarnLineAndSendMsg(productSku, product, Integer.valueOf(cashierCart.getShopId()), cashierCart.getNumber()));
// 已经添加的商品修改数量
isFirst = updateStock(cashierCart);
cashierCart.setOrderId(orderId);
cashierCart.setUpdatedAt(System.currentTimeMillis());
cashierCartMapper.updateById(cashierCart);
}
if (isFirst) {
redisTemplate.delete("SHOP:CODE:USER:pc:" + createOrderDTO.getShopId() + ":"
+ day + ":" + createOrderDTO.getTableId() + ":" + (createOrderDTO.getVipUserId() == null ? "" : createOrderDTO.getVipUserId()));
}
// 推送耗材信息
pushConsMsg(orderInfo, cashierCarts);
return orderInfo;
}
/**
* 增加库存
* @param productId 商品id
* @param skuId sku
* @param addNum 增加的库存数量
*/
public void incrStock(Integer productId, Integer skuId, Integer addNum) {
TbProduct product = productMapper.selectById(productId);
if (product.getIsDistribute() == 1) {
productMapper.incrStock(product.getId(), addNum);
}else {
producSkutMapper.incrStock(skuId, addNum);
}
}
/**
* 减少库存
* @param productId 商品数据
* @param skuId sku
* @param decrNum 减少的数量
*/
public void decrStock(Integer productId, String skuId, int decrNum) {
TbProduct product = productMapper.selectById(productId);
if (product.getIsDistribute() == 1) {
if (product.getIsStock() == 1) {
if (productMapper.decrStock(productId, decrNum) < 1) {
throw new BadRequestException("库存不足,下单失败");
}
}else {
productMapper.decrStockUnCheck(productId, decrNum);
}
}else {
if (product.getIsStock() == 1) {
if (producSkutMapper.decrStock(String.valueOf(skuId), decrNum) < 1) {
throw new BadRequestException("库存不足,下单失败");
}
}else {
producSkutMapper.decrStockUnCheck(String.valueOf(skuId), decrNum);
}
}
}
/**
* 推送耗材消耗信息
*/
private void pushConsMsg(TbOrderInfo orderInfo, List<TbCashierCart> cashierCarts) {
log.info("创建订单发送更新耗材消息订单id{}", orderInfo.getId());
//修改耗材数据
JSONObject jsonObject = new JSONObject();
jsonObject.put("orderId", orderInfo.getId());
jsonObject.put("type", "create");
rabbitTemplate.convertAndSend(RabbitConstants.CONS_COLLECT_PUT, RabbitConstants.CONS_COLLECT_ROUTINGKEY_PUT,
jsonObject.toJSONString(), new CorrelationData(UUID.randomUUID().toString()));
ThreadUtil.execAsync(() -> {
ThreadUtil.sleep(5, TimeUnit.SECONDS);
for (TbCashierCart cashierCart : cashierCarts) {
JSONObject objectMsg = new JSONObject();
objectMsg.put("skuId", Integer.valueOf(cashierCart.getSkuId()));
objectMsg.put("shopId", Integer.valueOf(cashierCart.getShopId()));
rabbitTemplate.convertAndSend(RabbitConstants.CONS_MSG_COLLECT_PUT, RabbitConstants.CONS_MSG_COLLECT_ROUTINGKEY_PUT,
objectMsg.toJSONString(), new CorrelationData(UUID.randomUUID().toString()));
}
});
}
/**
* 更新库存
* @param cashierCart 购物车
* @return 是否是第一次添加的商品
*/
private boolean updateStock(TbCashierCart cashierCart) {
if (cashierCart.getOrderId() != null) {
String message = redisTemplate.opsForValue().get(RedisConstant.ORDER_PRODUCT_NUM + cashierCart.getId());
if (message != null) {
int lastNum = Integer.parseInt(message);
// 数量减少, 返还库存
if (lastNum > cashierCart.getNumber()) {
incrStock(Integer.parseInt(cashierCart.getProductId()), Integer.parseInt(cashierCart.getSkuId()), lastNum - cashierCart.getNumber());
} else {
decrStock(Integer.parseInt(cashierCart.getProductId()), cashierCart.getSkuId(), cashierCart.getNumber() - lastNum);
}
redisTemplate.opsForValue().set(RedisConstant.ORDER_PRODUCT_NUM + cashierCart.getId(), cashierCart.getNumber().toString(), 24 * 60 * 60, TimeUnit.SECONDS);
}
return false;
// 首次添加的商品
} else {
redisTemplate.opsForValue().set(RedisConstant.ORDER_PRODUCT_NUM + cashierCart.getId(), cashierCart.getNumber().toString(), 24 * 60 * 60, TimeUnit.SECONDS);
// 修改库存
decrStock(Integer.parseInt(cashierCart.getProductId()), cashierCart.getSkuId(), cashierCart.getNumber());
return true;
}
}
/**
* 更新取餐号
* @param shopId 店铺id
* @return 当前取餐号
*/
public Integer updateOutNumber(String shopId) {
JSONObject object = new JSONObject();
String outNumber = redisTemplate.opsForValue().get(RedisConstant.OUT_NUMBER.concat(shopId));
Integer number = 1;
if (Objects.isNull(outNumber)) {
object.put("outNumber", number);
object.put("times", DateUtils.getDay());
} else {
object = JSONObject.parseObject(outNumber);
if (object.getString("times").equals(DateUtils.getDay())) {
number = object.getInteger("outNumber") + 1;
object.put("outNumber", number);
} else {
object.put("outNumber", number);
object.put("times", DateUtils.getDay());
}
}
redisTemplate.opsForValue().set(RedisConstant.OUT_NUMBER.concat(shopId), object.toString());
return number;
}
/**
* 校验商品库存警戒线并通知商户
*
* @param productSku sku
*/
private void checkWarnLineAndSendMsg(TbProductSku productSku, TbProduct product, Integer shopId, Integer num) {
TbShopInfo shopInfo = shopInfoRepository.getById(shopId);
if (productSku.getWarnLine() == null) {
return;
}
if (product.getIsStock() == null || product.getIsStock() != 1) {
return;
}
if (productSku.getStockNumber() == null) {
productSku.setStockNumber((double) 0);
}
if (product.getStockNumber() == null) {
product.setStockNumber(0);
}
if (
(product.getIsDistribute() == 1 && product.getStockNumber() - num <= productSku.getWarnLine())
|| (product.getIsDistribute() != 1) && productSku.getStockNumber() - num <= productSku.getWarnLine()
) {
List<TbShopOpenId> openIdList = shopOpenIdMapper.selectList(new LambdaQueryWrapper<TbShopOpenId>()
.eq(TbShopOpenId::getShopId, shopId)
.eq(TbShopOpenId::getStatus, 1)
.and((queryWrapper) -> queryWrapper.eq(TbShopOpenId::getType, ShopWxMsgTypeEnum.ALL_MSG.getType())
.or().eq(TbShopOpenId::getType, ShopWxMsgTypeEnum.STOCK_MSG.getType()))
.groupBy(TbShopOpenId::getOpenId)
);
wxMsgUtils.aboardStockMsg(shopInfo.getShopName(), shopId, product.getName(),
product.getIsDistribute() == 1 ? product.getStockNumber()-num : (int) (productSku.getStockNumber() - num));
}
}
public String generateOrderNumber() {
String date = DateUtils.getSdfTimes();
Random random = new Random();
int randomNum = random.nextInt(900) + 100;
return "DD" + date + randomNum;
}
public synchronized String generateOrderCode(String day, String clientType, String shopId) {
String code = redisUtils.get("SHOP:CODE:" + clientType + ":" + shopId + ":" + day)+"";
String code = redisUtils.get("SHOP:CODE:" + clientType + ":" + shopId + ":" + day) + "";
// 使用顺序递增的计数器生成取餐码
String orderCode = "";
if (StringUtils.isEmpty(code) || "null".equals(code)) {
orderCode = "1";
redisUtils.set("SHOP:CODE:" + clientType + ":" + shopId + ":" + day,"1");
redisTemplate.opsForValue().set("SHOP:CODE:" + clientType + ":" + shopId + ":" + day, "1");
} else {
orderCode =String.valueOf(Integer.parseInt(code)+1);
orderCode = String.valueOf(Integer.parseInt(code) + 1);
}
redisUtils.set("SHOP:CODE:" + clientType + ":" + shopId + ":" + day, "#" + Integer.parseInt(code.replace("#", "")) + 2);
boolean flag = redisUtils.setNx("SHOP:CODE:SET" + clientType + ":" + shopId + ":" + day, orderCode);
if (flag){
return generateOrderCode(day,clientType,shopId);
redisTemplate.opsForValue().set("SHOP:CODE:" + clientType + ":" + shopId + ":" + day, "#" + Integer.parseInt(code.replace("#", "")) + 2);
boolean flag = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent("SHOP:CODE:SET" + clientType + ":" + shopId + ":" + day, orderCode));
if (flag) {
return generateOrderCode(day, clientType, shopId);
}
// 增加计数器
return orderCode;
}
// @Override
// public Object getMasterId(Long tableId, Integer shopId) {
// String day = DateUtils.getDay();
// JSONObject jsonObject = new JSONObject();
// String key="SHOP:CODE:USER:pc" + ":" + shopId + ":" + day + userId;
// String userCode = redisUtils.get(key)+"";
// if ("1".equals(type)) {
// String code = "#" + generateOrderCode(day, clientType, shopId);
// redisUtil.saveMessage(key, code);
// jsonObject.put("code", code);
// } else {
// if (StringUtils.isEmpty(userCode)||"null".equals(userCode)||"#null".equals(userCode)) {
// String code = "#" + generateOrderCode(day, clientType, shopId);
// redisUtil.saveMessage(key, code);
// jsonObject.put("code", code);
// } else {
// jsonObject.put("code", userCode);
// }
// }
// }
private final StringRedisTemplate redisTemplate;
@Override
public Object getMasterId(Long tableId, Integer shopId) {
return null;
public Object getMasterId(Integer shopId, Long tableId, Integer vipUserId) {
String account = tokenProvider.getSubject();
if (account == null) {
throw new BadRequestException("token解析失败");
}
TbMerchantAccount merchantAccount = merchantAccountMapper.selectOne(new LambdaQueryWrapper<TbMerchantAccount>().eq(TbMerchantAccount::getAccount, account));
String day = DateUtils.getDay();
JSONObject jsonObject = new JSONObject();
String key = "SHOP:CODE:USER:pc" + ":" + shopId + ":" + day + ":" + tableId + ":" + (vipUserId == null ? "" : vipUserId);
String userCode = redisTemplate.opsForValue().get(key);
if (StringUtils.isEmpty(userCode) || "null".equals(userCode) || "#null".equals(userCode)) {
String code = "#" + generateOrderCode(day, "pc", String.valueOf(shopId));
redisTemplate.opsForValue().set(key, code);
jsonObject.put("masterId", code);
} else {
jsonObject.put("masterId", userCode);
}
return jsonObject;
}
}

View File

@ -102,7 +102,7 @@ public interface TbShopTableService {
void clearCart(ClearCartDTO clearCartDTO);
Page<TbCashierCart> getCart(Long tableId, Integer page, Integer size, Integer shopId);
Page<TbCashierCart> getCart(Long tableId, Integer page, Integer size, Integer shopId, Integer vipUserId);
TbCashierCart updateCart(UpdateCartDTO updateCartDTO);
@ -110,5 +110,5 @@ public interface TbShopTableService {
Object createOrder(CreateOrderDTO createOrderDTO);
Object getMasterId(Long tableId, Integer shopId);
Object getMasterId(Integer shopId, Long tableId, Integer vipUserId);
}