Search in sources :

Example 1 with SaveBuyGiveActivityModel

use of build.dream.catering.models.activity.SaveBuyGiveActivityModel in project erp-catering by liuyandong33.

the class ActivityController method saveBuyGiveActivity.

/**
 * 保存买A赠B活动
 *
 * @return
 */
@RequestMapping(value = "/saveBuyGiveActivity")
@ResponseBody
public String saveBuyGiveActivity() {
    ApiRest apiRest = null;
    Map<String, String> requestParameters = ApplicationHandler.getRequestParameters();
    try {
        SaveBuyGiveActivityModel saveBuyGiveActivityModel = ApplicationHandler.instantiateObject(SaveBuyGiveActivityModel.class, requestParameters);
        String buyGiveActivityInfos = requestParameters.get("buyGiveActivityInfos");
        saveBuyGiveActivityModel.setBuyGiveActivityInfos(buyGiveActivityInfos);
        saveBuyGiveActivityModel.validateAndThrow();
        apiRest = activityService.saveBuyGiveActivity(saveBuyGiveActivityModel);
    } catch (Exception e) {
        LogUtils.error("保存买A赠B活动失败", controllerSimpleName, "saveBuyGiveActivity", e, requestParameters);
        apiRest = new ApiRest(e);
    }
    return GsonUtils.toJson(apiRest);
}
Also used : SaveBuyGiveActivityModel(build.dream.catering.models.activity.SaveBuyGiveActivityModel) ApiRest(build.dream.common.api.ApiRest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with SaveBuyGiveActivityModel

use of build.dream.catering.models.activity.SaveBuyGiveActivityModel in project erp-catering by liuyandong33.

the class ActivityService method saveBuyGiveActivity.

@Transactional(rollbackFor = Exception.class)
public ApiRest saveBuyGiveActivity(SaveBuyGiveActivityModel saveBuyGiveActivityModel) throws ParseException {
    BigInteger tenantId = saveBuyGiveActivityModel.getTenantId();
    String tenantCode = saveBuyGiveActivityModel.getTenantCode();
    BigInteger branchId = saveBuyGiveActivityModel.getBranchId();
    BigInteger userId = saveBuyGiveActivityModel.getUserId();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_PATTERN);
    Date startTime = simpleDateFormat.parse(saveBuyGiveActivityModel.getStartTime() + " 00:00:00");
    Date endTime = simpleDateFormat.parse(saveBuyGiveActivityModel.getEndTime() + " 23:59:59");
    Validate.isTrue(endTime.after(startTime), "活动结束时间必须大于开始时间!");
    List<SaveBuyGiveActivityModel.BuyGiveActivityInfo> buyGiveActivityInfos = saveBuyGiveActivityModel.getBuyGiveActivityInfos();
    List<BigInteger> goodsIds = new ArrayList<BigInteger>();
    List<BigInteger> goodsSpecificationIds = new ArrayList<BigInteger>();
    for (SaveBuyGiveActivityModel.BuyGiveActivityInfo buyGiveActivityInfo : buyGiveActivityInfos) {
        goodsIds.add(buyGiveActivityInfo.getBuyGoodsId());
        goodsSpecificationIds.add(buyGiveActivityInfo.getBuyGoodsSpecificationId());
        goodsIds.add(buyGiveActivityInfo.getGiveGoodsId());
        goodsSpecificationIds.add(buyGiveActivityInfo.getGiveGoodsSpecificationId());
    }
    // 查询出涉及的所有商品
    SearchModel goodsSearchModel = new SearchModel(true);
    goodsSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsIds);
    goodsSearchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    goodsSearchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    List<Goods> goodses = goodsMapper.findAll(goodsSearchModel);
    // 封装商品id与商品之间的map
    Map<BigInteger, Goods> goodsMap = new HashMap<BigInteger, Goods>();
    for (Goods goods : goodses) {
        goodsMap.put(goods.getId(), goods);
    }
    SearchModel canNotOperateReasonSearchModel = new SearchModel();
    canNotOperateReasonSearchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    canNotOperateReasonSearchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    canNotOperateReasonSearchModel.addSearchCondition("table_id", Constants.SQL_OPERATION_SYMBOL_IN, goodsIds);
    canNotOperateReasonSearchModel.addSearchCondition("operate_type", Constants.SQL_OPERATION_SYMBOL_EQUALS, 4);
    CanNotOperateReason persistenceCanNotOperateReason = canNotOperateReasonMapper.find(canNotOperateReasonSearchModel);
    if (persistenceCanNotOperateReason != null) {
        Goods goods = goodsMap.get(persistenceCanNotOperateReason.getTableId());
        Validate.notNull(goods, "商品不存在!");
        throw new RuntimeException(String.format(persistenceCanNotOperateReason.getReason(), goods.getName()));
    }
    // 查询出涉及的所有商品规格
    SearchModel goodsSpecificationSearchModel = new SearchModel(true);
    goodsSpecificationSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsSpecificationIds);
    goodsSpecificationSearchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    goodsSpecificationSearchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.findAll(goodsSpecificationSearchModel);
    // 封装商品规格id与商品规格之间的map
    Map<BigInteger, GoodsSpecification> goodsSpecificationMap = new HashMap<BigInteger, GoodsSpecification>();
    for (GoodsSpecification goodsSpecification : goodsSpecifications) {
        goodsSpecificationMap.put(goodsSpecification.getId(), goodsSpecification);
    }
    Activity activity = ActivityUtils.constructActivity(tenantId, tenantCode, branchId, saveBuyGiveActivityModel.getName(), 1, startTime, endTime, userId, "保存活动信息!");
    activityMapper.insert(activity);
    List<BuyGiveActivity> buyGiveActivities = new ArrayList<BuyGiveActivity>();
    List<CanNotOperateReason> canNotOperateReasons = new ArrayList<CanNotOperateReason>();
    for (SaveBuyGiveActivityModel.BuyGiveActivityInfo buyGiveActivityInfo : buyGiveActivityInfos) {
        Goods buyGoods = goodsMap.get(buyGiveActivityInfo.getBuyGoodsId());
        Validate.notNull(buyGoods, "商品不存在!");
        GoodsSpecification buyGoodsSpecification = goodsSpecificationMap.get(buyGiveActivityInfo.getBuyGoodsSpecificationId());
        Validate.notNull(buyGoods, "商品规格不存在!");
        Goods giveGoods = goodsMap.get(buyGiveActivityInfo.getGiveGoodsId());
        Validate.notNull(buyGoods, "商品不存在!");
        GoodsSpecification giveGoodsSpecification = goodsSpecificationMap.get(buyGiveActivityInfo.getGiveGoodsSpecificationId());
        Validate.notNull(buyGoods, "商品规格不存在!");
        BuyGiveActivity buyGiveActivity = new BuyGiveActivity();
        buyGiveActivity.setTenantId(tenantId);
        buyGiveActivity.setTenantCode(tenantCode);
        buyGiveActivity.setBranchId(branchId);
        buyGiveActivity.setActivityId(activity.getId());
        buyGiveActivity.setBuyGoodsId(buyGoods.getId());
        buyGiveActivity.setBuyGoodsSpecificationId(buyGoodsSpecification.getId());
        buyGiveActivity.setBuyQuantity(buyGiveActivityInfo.getBuyQuantity());
        buyGiveActivity.setGiveGoodsId(giveGoods.getId());
        buyGiveActivity.setGiveGoodsSpecificationId(giveGoodsSpecification.getId());
        buyGiveActivity.setGiveQuantity(buyGiveActivityInfo.getGiveQuantity());
        buyGiveActivity.setCreateUserId(userId);
        buyGiveActivity.setLastUpdateUserId(userId);
        buyGiveActivity.setLastUpdateRemark("保存买A赠B活动!");
        buyGiveActivities.add(buyGiveActivity);
        String reason = "该商品已参与促销活动【" + activity.getName() + "】,活动期间不可%s!如需更改,请先取消活动!";
        canNotOperateReasons.add(CanNotOperateReasonUtils.constructCanNotOperateReason(tenantId, tenantCode, tenantId, buyGoods.getId(), "goods", activity.getId(), "activity", 3, reason));
        canNotOperateReasons.add(CanNotOperateReasonUtils.constructCanNotOperateReason(tenantId, tenantCode, tenantId, giveGoods.getId(), "goods", activity.getId(), "activity", 3, reason));
    }
    buyGiveActivityMapper.insertAll(buyGiveActivities);
    canNotOperateReasonMapper.insertAll(canNotOperateReasons);
    ApiRest apiRest = new ApiRest();
    apiRest.setMessage("保存买A赠B活动成功!");
    apiRest.setSuccessful(true);
    return apiRest;
}
Also used : SearchModel(build.dream.common.utils.SearchModel) SaveBuyGiveActivityModel(build.dream.catering.models.activity.SaveBuyGiveActivityModel) ApiRest(build.dream.common.api.ApiRest) BigInteger(java.math.BigInteger) SimpleDateFormat(java.text.SimpleDateFormat) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SaveBuyGiveActivityModel (build.dream.catering.models.activity.SaveBuyGiveActivityModel)2 ApiRest (build.dream.common.api.ApiRest)2 SearchModel (build.dream.common.utils.SearchModel)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