Search in sources :

Example 1 with SaveDietOrderModel

use of build.dream.catering.models.dietorder.SaveDietOrderModel in project erp-catering by liuyandong33.

the class DietOrderService method saveDietOrder.

/**
 * 保存订单信息
 *
 * @param saveDietOrderModel
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public ApiRest saveDietOrder(SaveDietOrderModel saveDietOrderModel) {
    BigInteger tenantId = saveDietOrderModel.getTenantId();
    String tenantCode = saveDietOrderModel.getTenantCode();
    BigInteger branchId = saveDietOrderModel.getBranchId();
    BigInteger userId = saveDietOrderModel.getUserId();
    List<BigInteger> goodsIds = new ArrayList<BigInteger>();
    List<BigInteger> goodsSpecificationIds = new ArrayList<BigInteger>();
    List<BigInteger> goodsFlavorGroupIds = new ArrayList<BigInteger>();
    List<BigInteger> goodsFlavorIds = new ArrayList<BigInteger>();
    List<SaveDietOrderModel.GroupInfo> groupInfos = saveDietOrderModel.getGroupInfos();
    for (SaveDietOrderModel.GroupInfo groupInfo : groupInfos) {
        List<SaveDietOrderModel.DetailInfo> detailInfos = groupInfo.getDetailInfos();
        for (SaveDietOrderModel.DetailInfo detailInfo : detailInfos) {
            goodsIds.add(detailInfo.getGoodsId());
            goodsSpecificationIds.add(detailInfo.getGoodsSpecificationId());
            List<SaveDietOrderModel.FlavorInfo> flavorInfos = detailInfo.getFlavorInfos();
            if (CollectionUtils.isNotEmpty(flavorInfos)) {
                for (SaveDietOrderModel.FlavorInfo flavorInfo : flavorInfos) {
                    goodsFlavorGroupIds.add(flavorInfo.getFlavorGroupId());
                    goodsFlavorIds.add(flavorInfo.getFlavorId());
                }
            }
        }
    }
    // 查询出订单中包含的所有商品
    SearchModel goodsSearchModel = new SearchModel(true);
    goodsSearchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    goodsSearchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    goodsSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsIds);
    List<Goods> goodses = goodsMapper.findAll(goodsSearchModel);
    // 查询出订单中包含的所有商品规格
    SearchModel goodsSpecificationSearchModel = new SearchModel(true);
    goodsSpecificationSearchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    goodsSpecificationSearchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    goodsSpecificationSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsSpecificationIds);
    List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.findAll(goodsSpecificationSearchModel);
    // 查询出订单中包含的所有口味组
    List<GoodsFlavorGroup> goodsFlavorGroups = new ArrayList<GoodsFlavorGroup>();
    if (CollectionUtils.isNotEmpty(goodsFlavorGroupIds)) {
        SearchModel goodsFlavorGroupSearchModel = new SearchModel(true);
        goodsFlavorGroupSearchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
        goodsFlavorGroupSearchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
        goodsFlavorGroupSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsFlavorGroupIds);
        goodsFlavorGroups = goodsFlavorGroupMapper.findAll(goodsFlavorGroupSearchModel);
    }
    // 查询出订单中包含的所有口味
    List<GoodsFlavor> goodsFlavors = new ArrayList<GoodsFlavor>();
    if (CollectionUtils.isNotEmpty(goodsFlavorIds)) {
        SearchModel goodsFlavorSearchModel = new SearchModel(true);
        goodsFlavorSearchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
        goodsFlavorSearchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
        goodsFlavorSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsFlavorIds);
        goodsFlavors = goodsFlavorMapper.findAll(goodsFlavorSearchModel);
    }
    // 封装商品id与商品之间的map
    Map<BigInteger, Goods> goodsMap = new HashMap<BigInteger, Goods>();
    for (Goods goods : goodses) {
        goodsMap.put(goods.getId(), goods);
    }
    // 封装商品规格id与商品规格之间的map
    Map<BigInteger, GoodsSpecification> goodsSpecificationMap = new HashMap<BigInteger, GoodsSpecification>();
    for (GoodsSpecification goodsSpecification : goodsSpecifications) {
        goodsSpecificationMap.put(goodsSpecification.getId(), goodsSpecification);
    }
    // 封装商品口味组id与商品口味组之间的map
    Map<BigInteger, GoodsFlavorGroup> goodsFlavorGroupMap = new HashMap<BigInteger, GoodsFlavorGroup>();
    for (GoodsFlavorGroup goodsFlavorGroup : goodsFlavorGroups) {
        goodsFlavorGroupMap.put(goodsFlavorGroup.getId(), goodsFlavorGroup);
    }
    // 封装商品口味id与商品口味之间的map
    Map<BigInteger, GoodsFlavor> goodsFlavorMap = new HashMap<BigInteger, GoodsFlavor>();
    for (GoodsFlavor goodsFlavor : goodsFlavors) {
        goodsFlavorMap.put(goodsFlavor.getId(), goodsFlavor);
    }
    DietOrder dietOrder = new DietOrder();
    dietOrder.setTenantId(tenantId);
    dietOrder.setTenantCode(tenantCode);
    dietOrder.setBranchId(branchId);
    String orderNumberPrefix = null;
    Integer orderType = saveDietOrderModel.getOrderType();
    if (orderType == DietOrderConstants.ORDER_TYPE_SCAN_CODE_ORDER) {
        orderNumberPrefix = "SC";
    }
    Integer daySerialNumber = sequenceMapper.nextValue(SerialNumberGenerator.generatorTodaySequenceName(tenantId, branchId, "diet_order_number"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    String orderNumber = orderNumberPrefix + simpleDateFormat.format(new Date()) + SerialNumberGenerator.nextSerialNumber(8, daySerialNumber);
    dietOrder.setOrderNumber(orderNumber);
    dietOrder.setOrderType(orderType);
    dietOrder.setOrderStatus(DietOrderConstants.ORDER_STATUS_PENDING);
    dietOrder.setPayStatus(DietOrderConstants.PAY_STATUS_UNPAID);
    dietOrder.setRefundStatus(DietOrderConstants.REFUND_STATUS_NO_REFUND);
    dietOrder.setDaySerialNumber(daySerialNumber.toString());
    boolean invoiced = saveDietOrderModel.getInvoiced();
    dietOrder.setInvoiced(invoiced);
    if (invoiced) {
        dietOrder.setInvoiceType(saveDietOrderModel.getInvoiceType());
        dietOrder.setInvoice(saveDietOrderModel.getInvoice());
    }
    dietOrder.setCreateUserId(userId);
    dietOrder.setLastUpdateUserId(userId);
    dietOrder.setLastUpdateRemark("保存订单信息!");
    dietOrderMapper.insert(dietOrder);
    BigDecimal dietOrderTotalAmount = BigDecimal.ZERO;
    BigInteger dietOrderId = dietOrder.getId();
    // 存放订单优惠金额
    BigDecimal dietOrderDiscountAmount = BigDecimal.ZERO;
    // 存放赠品订单详情
    List<DietOrderDetail> giveDietOrderDetails = new ArrayList<DietOrderDetail>();
    // 用来保存订单活动
    Map<BigInteger, DietOrderActivity> dietOrderActivityMap = new HashMap<BigInteger, DietOrderActivity>();
    for (SaveDietOrderModel.GroupInfo groupInfo : groupInfos) {
        DietOrderGroup dietOrderGroup = DietOrderUtils.constructDietOrderGroup(tenantId, tenantCode, branchId, dietOrderId, groupInfo.getName(), groupInfo.getType(), userId, "保存订单分组信息!");
        dietOrderGroupMapper.insert(dietOrderGroup);
        List<SaveDietOrderModel.DetailInfo> detailInfos = groupInfo.getDetailInfos();
        for (SaveDietOrderModel.DetailInfo detailInfo : detailInfos) {
            Goods goods = goodsMap.get(detailInfo.getGoodsId());
            Validate.notNull(goods, "商品不存在!");
            GoodsSpecification goodsSpecification = goodsSpecificationMap.get(detailInfo.getGoodsSpecificationId());
            Validate.notNull(goodsSpecification, "商品规格不存在!");
            BigDecimal flavorIncrease = BigDecimal.ZERO;
            List<SaveDietOrderModel.FlavorInfo> flavorInfos = detailInfo.getFlavorInfos();
            List<DietOrderDetailGoodsFlavor> dietOrderDetailGoodsFlavors = new ArrayList<DietOrderDetailGoodsFlavor>();
            if (CollectionUtils.isNotEmpty(flavorInfos)) {
                for (SaveDietOrderModel.FlavorInfo flavorInfo : flavorInfos) {
                    GoodsFlavorGroup goodsFlavorGroup = goodsFlavorGroupMap.get(flavorInfo.getFlavorGroupId());
                    Validate.notNull(goodsFlavorGroup, "口味组不存在!");
                    GoodsFlavor goodsFlavor = goodsFlavorMap.get(flavorInfo.getFlavorId());
                    Validate.notNull(goodsFlavor, "口味不存在!");
                    flavorIncrease = flavorIncrease.add(goodsFlavor.getPrice());
                    DietOrderDetailGoodsFlavor dietOrderDetailGoodsFlavor = DietOrderUtils.constructDietOrderDetailGoodsFlavor(tenantId, tenantCode, branchId, dietOrderId, dietOrderGroup.getId(), null, goodsFlavorGroup.getId(), goodsFlavorGroup.getName(), goodsFlavor.getId(), goodsFlavor.getName(), goodsFlavor.getPrice(), userId, "保存订单口味信息!");
                    dietOrderDetailGoodsFlavors.add(dietOrderDetailGoodsFlavor);
                }
            }
            BigDecimal quantity = BigDecimal.valueOf(detailInfo.getQuantity());
            BigDecimal price = goodsSpecification.getPrice();
            BigDecimal totalAmount = price.add(flavorIncrease).multiply(quantity);
            BigDecimal discountAmount = BigDecimal.ZERO;
            // 处理特价商品
            SpecialGoodsActivityBean specialGoodsActivityBean = DietOrderUtils.findSpecialGoodsActivityBean(tenantId, branchId, goods.getId(), goodsSpecification.getId());
            if (specialGoodsActivityBean != null) {
                Integer discountType = specialGoodsActivityBean.getDiscountType();
                if (discountType == 1) {
                    discountAmount = price.subtract(specialGoodsActivityBean.getSpecialPrice()).multiply(quantity);
                } else {
                    discountAmount = price.subtract(price.multiply(specialGoodsActivityBean.getDiscountRate()).divide(HUNDRED));
                }
                BigInteger activityId = specialGoodsActivityBean.getActivityId();
                DietOrderActivity dietOrderActivity = dietOrderActivityMap.get(activityId);
                if (dietOrderActivity == null) {
                    dietOrderActivity = DietOrderUtils.constructDietOrderActivity(tenantId, tenantCode, branchId, dietOrderId, activityId, specialGoodsActivityBean.getActivityName(), specialGoodsActivityBean.getActivityType(), discountAmount.multiply(MINUS_ONE), userId, "保存订单活动信息!");
                    dietOrderActivityMap.put(activityId, dietOrderActivity);
                } else {
                    dietOrderActivity.setAmount(dietOrderActivity.getAmount().multiply(discountAmount.multiply(MINUS_ONE)));
                }
            }
            dietOrderDiscountAmount = dietOrderDiscountAmount.add(discountAmount);
            BigDecimal payableAmount = totalAmount.subtract(discountAmount);
            DietOrderDetail dietOrderDetail = DietOrderUtils.constructDietOrderDetail(tenantId, tenantCode, branchId, dietOrderId, dietOrderGroup.getId(), goods.getId(), goods.getName(), goodsSpecification.getId(), goodsSpecification.getName(), goods.getCategoryId(), goodsSpecification.getPrice(), flavorIncrease, detailInfo.getQuantity(), totalAmount, discountAmount, payableAmount, userId, "保存订单详情信息!");
            dietOrderDetailMapper.insert(dietOrderDetail);
            if (CollectionUtils.isNotEmpty(dietOrderDetailGoodsFlavors)) {
                for (DietOrderDetailGoodsFlavor dietOrderDetailGoodsFlavor : dietOrderDetailGoodsFlavors) {
                    dietOrderDetailGoodsFlavor.setDietOrderDetailId(dietOrderDetail.getId());
                }
                dietOrderDetailGoodsFlavorMapper.insertAll(dietOrderDetailGoodsFlavors);
            }
            dietOrderTotalAmount = dietOrderTotalAmount.add(totalAmount);
            // 处理买A赠B活动
            BuyGiveActivityBean buyGiveActivityBean = DietOrderUtils.findBuyGiveActivityBean(tenantId, branchId, goods.getId(), goodsSpecification.getId(), detailInfo.getQuantity());
            if (buyGiveActivityBean != null) {
                DietOrderDetail giveDietOrderDetail = DietOrderUtils.constructDietOrderDetail(tenantId, tenantCode, branchId, dietOrderId, null, buyGiveActivityBean.getGiveGoodsId(), buyGiveActivityBean.getGiveGoodsName(), buyGiveActivityBean.getGiveGoodsSpecificationId(), buyGiveActivityBean.getGiveGoodsSpecificationName(), BigInteger.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, buyGiveActivityBean.getGiveQuantity(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, userId, "保存订单详情信息!");
                giveDietOrderDetails.add(giveDietOrderDetail);
                if (!dietOrderActivityMap.containsKey(buyGiveActivityBean.getActivityId())) {
                    DietOrderActivity dietOrderActivity = DietOrderUtils.constructDietOrderActivity(tenantId, tenantCode, branchId, dietOrderId, buyGiveActivityBean.getActivityId(), buyGiveActivityBean.getActivityName(), buyGiveActivityBean.getActivityType(), BigDecimal.ZERO, userId, "保存订单活动信息!");
                    dietOrderActivityMap.put(buyGiveActivityBean.getActivityId(), dietOrderActivity);
                }
            }
        }
    }
    if (CollectionUtils.isNotEmpty(giveDietOrderDetails)) {
        DietOrderGroup giveDietOrderGroup = DietOrderUtils.constructDietOrderGroup(tenantId, tenantCode, branchId, dietOrderId, "赠品", "discount", userId, "保存订单分组信息!");
        dietOrderGroupMapper.insert(giveDietOrderGroup);
        for (DietOrderDetail giveDietOrderDetail : giveDietOrderDetails) {
            giveDietOrderDetail.setDietOrderGroupId(giveDietOrderGroup.getId());
        }
        dietOrderDetailMapper.insertAll(giveDietOrderDetails);
    }
    // 处理整单优惠活动
    FullReductionActivityBean fullReductionActivityBean = DietOrderUtils.findFullReductionActivityBean(dietOrderTotalAmount, tenantId.toString(), branchId.toString());
    if (fullReductionActivityBean != null) {
        Integer discountType = fullReductionActivityBean.getDiscountType();
        BigDecimal fullReductionActivityDiscountAmount = null;
        if (discountType == 1) {
            fullReductionActivityDiscountAmount = fullReductionActivityBean.getDiscountAmount();
        } else if (discountType == 2) {
            fullReductionActivityDiscountAmount = dietOrderTotalAmount.multiply(fullReductionActivityBean.getDiscountRate()).divide(HUNDRED);
        }
        dietOrderDiscountAmount = dietOrderDiscountAmount.add(fullReductionActivityDiscountAmount);
        DietOrderActivity dietOrderActivity = DietOrderUtils.constructDietOrderActivity(tenantId, tenantCode, branchId, dietOrderId, fullReductionActivityBean.getActivityId(), fullReductionActivityBean.getActivityName(), fullReductionActivityBean.getActivityType(), fullReductionActivityDiscountAmount.multiply(MINUS_ONE), userId, "保存订单活动信息!");
        dietOrderActivityMap.put(fullReductionActivityBean.getActivityId(), dietOrderActivity);
    }
    if (MapUtils.isNotEmpty(dietOrderActivityMap)) {
        dietOrderActivityMapper.insertAll(new ArrayList<DietOrderActivity>(dietOrderActivityMap.values()));
    }
    dietOrder.setTotalAmount(dietOrderTotalAmount);
    dietOrder.setDiscountAmount(dietOrderDiscountAmount);
    dietOrder.setPayableAmount(dietOrderTotalAmount.subtract(dietOrderDiscountAmount));
    dietOrder.setPaidAmount(BigDecimal.ZERO);
    dietOrderMapper.update(dietOrder);
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("id", dietOrder.getId());
    data.put("orderNumber", dietOrder.getOrderNumber());
    return new ApiRest(data, "保存订单成功!");
}
Also used : SearchModel(build.dream.common.utils.SearchModel) BuyGiveActivityBean(build.dream.catering.beans.BuyGiveActivityBean) SpecialGoodsActivityBean(build.dream.catering.beans.SpecialGoodsActivityBean) ApiRest(build.dream.common.api.ApiRest) BigDecimal(java.math.BigDecimal) FullReductionActivityBean(build.dream.catering.beans.FullReductionActivityBean) SaveDietOrderModel(build.dream.catering.models.dietorder.SaveDietOrderModel) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) SimpleDateFormat(java.text.SimpleDateFormat) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SaveDietOrderModel

use of build.dream.catering.models.dietorder.SaveDietOrderModel in project erp-catering by liuyandong33.

the class DietOrderController method saveDietOrder.

/**
 * 保存订单
 *
 * @return
 */
@RequestMapping(value = "/saveDietOrder")
@ResponseBody
public String saveDietOrder() {
    ApiRest apiRest = null;
    Map<String, String> requestParameters = ApplicationHandler.getRequestParameters();
    try {
        SaveDietOrderModel saveDietOrderModel = ApplicationHandler.instantiateObject(SaveDietOrderModel.class, requestParameters);
        String groups = requestParameters.get("groups");
        saveDietOrderModel.setGroupInfos(groups);
        saveDietOrderModel.validateAndThrow();
        apiRest = dietOrderService.saveDietOrder(saveDietOrderModel);
    } catch (Exception e) {
        LogUtils.error("保存订单失败", controllerSimpleName, "saveDietOrder", e, requestParameters);
        apiRest = new ApiRest(e);
    }
    return GsonUtils.toJson(apiRest);
}
Also used : SaveDietOrderModel(build.dream.catering.models.dietorder.SaveDietOrderModel) ApiRest(build.dream.common.api.ApiRest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

SaveDietOrderModel (build.dream.catering.models.dietorder.SaveDietOrderModel)2 ApiRest (build.dream.common.api.ApiRest)2 BuyGiveActivityBean (build.dream.catering.beans.BuyGiveActivityBean)1 FullReductionActivityBean (build.dream.catering.beans.FullReductionActivityBean)1 SpecialGoodsActivityBean (build.dream.catering.beans.SpecialGoodsActivityBean)1 SearchModel (build.dream.common.utils.SearchModel)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Transactional (org.springframework.transaction.annotation.Transactional)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1