Search in sources :

Example 1 with SaveSpecialGoodsActivityModel

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

the class ActivityService method saveSpecialGoodsActivity.

/**
 * 保存特价商品活动
 *
 * @param saveSpecialGoodsActivityModel
 * @return
 * @throws ParseException
 */
public ApiRest saveSpecialGoodsActivity(SaveSpecialGoodsActivityModel saveSpecialGoodsActivityModel) throws ParseException {
    BigInteger tenantId = saveSpecialGoodsActivityModel.getTenantId();
    String tenantCode = saveSpecialGoodsActivityModel.getTenantCode();
    BigInteger branchId = saveSpecialGoodsActivityModel.getBranchId();
    BigInteger userId = saveSpecialGoodsActivityModel.getUserId();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_PATTERN);
    Date startTime = simpleDateFormat.parse(saveSpecialGoodsActivityModel.getStartTime() + " 00:00:00");
    Date endTime = simpleDateFormat.parse(saveSpecialGoodsActivityModel.getEndTime() + " 23:59:59");
    Validate.isTrue(endTime.after(startTime), "活动结束时间必须大于开始时间!");
    String sql = "SELECT * " + "FROM activity " + "WHERE tenant_id = #{tenantId} " + "AND branch_id = #{branchId} " + "AND deleted = 0 " + "AND type = 3 " + "AND status IN (1, 2) " + "AND ((start_time <= #{startTime} AND end_time >= #{endTime}) OR (start_time >= #{startTime} AND start_time <= #{endTime}) OR (end_time >= #{startTime} AND end_time <= #{endTime})) " + "LIMIT 0, 1";
    Map<String, Object> findActivityParameters = new HashMap<String, Object>();
    findActivityParameters.put("sql", sql);
    findActivityParameters.put("tenantId", tenantId);
    findActivityParameters.put("branchId", branchId);
    findActivityParameters.put("startTime", startTime);
    findActivityParameters.put("endTime", endTime);
    Map<String, Object> activityMap = universalMapper.executeUniqueResultQuery(findActivityParameters);
    if (MapUtils.isNotEmpty(activityMap)) {
        throw new RuntimeException("活动日期与促销活动【" + activityMap.get("name") + "】在时间上冲突!");
    }
    List<SaveSpecialGoodsActivityModel.SpecialGoodsActivityInfo> specialGoodsActivityInfos = saveSpecialGoodsActivityModel.getSpecialGoodsActivityInfos();
    List<BigInteger> goodsIds = new ArrayList<BigInteger>();
    List<BigInteger> goodsSpecificationIds = new ArrayList<BigInteger>();
    for (SaveSpecialGoodsActivityModel.SpecialGoodsActivityInfo specialGoodsActivityInfo : specialGoodsActivityInfos) {
        goodsIds.add(specialGoodsActivityInfo.getGoodsId());
        goodsSpecificationIds.add(specialGoodsActivityInfo.getGoodsSpecificationId());
    }
    // 查询出涉及的所有商品
    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, saveSpecialGoodsActivityModel.getName(), 3, startTime, endTime, userId, "保存活动信息!");
    activityMapper.insert(activity);
    List<SpecialGoodsActivity> specialGoodsActivities = new ArrayList<SpecialGoodsActivity>();
    List<CanNotOperateReason> canNotOperateReasons = new ArrayList<CanNotOperateReason>();
    for (SaveSpecialGoodsActivityModel.SpecialGoodsActivityInfo specialGoodsActivityInfo : specialGoodsActivityInfos) {
        Goods goods = goodsMap.get(specialGoodsActivityInfo.getGoodsId());
        Validate.notNull(goods, "商品不存在!");
        GoodsSpecification goodsSpecification = goodsSpecificationMap.get(specialGoodsActivityInfo.getGoodsSpecificationId());
        Validate.notNull(goodsSpecification, "商品规格不存在!");
        SpecialGoodsActivity specialGoodsActivity = new SpecialGoodsActivity();
        specialGoodsActivity.setTenantId(tenantId);
        specialGoodsActivity.setTenantCode(tenantCode);
        specialGoodsActivity.setBranchId(branchId);
        specialGoodsActivity.setActivityId(activity.getId());
        specialGoodsActivity.setGoodsId(goods.getId());
        specialGoodsActivity.setGoodsSpecificationId(goodsSpecification.getId());
        int discountType = specialGoodsActivityInfo.getDiscountType();
        specialGoodsActivity.setDiscountType(discountType);
        if (discountType == 1) {
            specialGoodsActivity.setSpecialPrice(specialGoodsActivityInfo.getSpecialPrice());
        } else if (discountType == 2) {
            specialGoodsActivity.setDiscountRate(specialGoodsActivityInfo.getDiscountRate());
        }
        specialGoodsActivity.setCreateUserId(userId);
        specialGoodsActivity.setLastUpdateUserId(userId);
        specialGoodsActivity.setLastUpdateRemark("保存特价商品活动!");
        specialGoodsActivities.add(specialGoodsActivity);
        String reason = "该商品已参与促销活动【" + activity.getName() + "】,活动期间不可%s!如需更改,请先取消活动!";
        CanNotOperateReason canNotOperateReason = CanNotOperateReasonUtils.constructCanNotOperateReason(tenantId, tenantCode, branchId, goods.getId(), "goods", activity.getId(), "activity", 3, reason);
        canNotOperateReasons.add(canNotOperateReason);
        String usedOtherActivityReason = "商品【%s】已参与促销活动【" + activity.getName() + "】,不可参与其他促销活动!";
        CanNotOperateReason canNotUsedOtherActivityReason = CanNotOperateReasonUtils.constructCanNotOperateReason(tenantId, tenantCode, branchId, goods.getId(), "goods", activity.getId(), "activity", 4, usedOtherActivityReason);
        canNotOperateReasons.add(canNotUsedOtherActivityReason);
    }
    specialGoodsActivityMapper.insertAll(specialGoodsActivities);
    canNotOperateReasonMapper.insertAll(canNotOperateReasons);
    ApiRest apiRest = new ApiRest();
    apiRest.setMessage("保存特价商品活动成功!");
    apiRest.setSuccessful(true);
    return apiRest;
}
Also used : SearchModel(build.dream.common.utils.SearchModel) ApiRest(build.dream.common.api.ApiRest) SaveSpecialGoodsActivityModel(build.dream.catering.models.activity.SaveSpecialGoodsActivityModel) BigInteger(java.math.BigInteger) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with SaveSpecialGoodsActivityModel

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

the class ActivityController method saveSpecialGoodsActivity.

/**
 * 保存特价商品活动
 *
 * @return
 */
@RequestMapping(value = "/saveSpecialGoodsActivity")
@ResponseBody
public String saveSpecialGoodsActivity() {
    ApiRest apiRest = null;
    Map<String, String> requestParameters = ApplicationHandler.getRequestParameters();
    try {
        SaveSpecialGoodsActivityModel saveSpecialGoodsActivityModel = ApplicationHandler.instantiateObject(SaveSpecialGoodsActivityModel.class, requestParameters);
        String specialGoodsActivityInfos = requestParameters.get("specialGoodsActivityInfos");
        saveSpecialGoodsActivityModel.setSpecialGoodsActivityInfos(specialGoodsActivityInfos);
        saveSpecialGoodsActivityModel.validateAndThrow();
        apiRest = activityService.saveSpecialGoodsActivity(saveSpecialGoodsActivityModel);
    } catch (Exception e) {
        LogUtils.error("保存特价商品活动失败", controllerSimpleName, "saveSpecialGoodsActivity", e, requestParameters);
        apiRest = new ApiRest(e);
    }
    return GsonUtils.toJson(apiRest);
}
Also used : SaveSpecialGoodsActivityModel(build.dream.catering.models.activity.SaveSpecialGoodsActivityModel) ApiRest(build.dream.common.api.ApiRest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

SaveSpecialGoodsActivityModel (build.dream.catering.models.activity.SaveSpecialGoodsActivityModel)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 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1