Search in sources :

Example 1 with UpdateModel

use of build.dream.common.utils.UpdateModel in project erp-catering by liuyandong33.

the class GoodsService method saveGoods.

/**
 * 保存菜品信息
 *
 * @param saveGoodsModel
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public ApiRest saveGoods(SaveGoodsModel saveGoodsModel) {
    BigInteger tenantId = saveGoodsModel.getTenantId();
    String tenantCode = saveGoodsModel.getTenantCode();
    BigInteger branchId = saveGoodsModel.getBranchId();
    BigInteger userId = saveGoodsModel.getUserId();
    if (saveGoodsModel.getId() != null) {
        BigInteger goodsId = saveGoodsModel.getId();
        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_EQUALS, goodsId);
        Goods goods = goodsMapper.find(goodsSearchModel);
        Validate.notNull(goods, "商品不存在!");
        // 验证商品是否可以编辑
        validateCanNotOperate(tenantId, branchId, "goods", goodsId, 1);
        goods.setName(saveGoodsModel.getName());
        goods.setCategoryId(saveGoodsModel.getCategoryId());
        goods.setImageUrl(saveGoodsModel.getImageUrl());
        goodsMapper.update(goods);
        // 删除需要删除的规格
        if (CollectionUtils.isNotEmpty(saveGoodsModel.getDeleteGoodsSpecificationIds())) {
            UpdateModel updateModel = new UpdateModel(true);
            updateModel.setTableName("goods_specification");
            updateModel.addContentValue("last_update_user_id", userId);
            updateModel.addContentValue("last_update_remark", "删除商品规格信息!");
            updateModel.addContentValue("deleted", 1);
            updateModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
            updateModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
            updateModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, saveGoodsModel.getDeleteGoodsSpecificationIds());
            universalMapper.universalUpdate(updateModel);
        }
        // 删除需要删除的口味组及其下的口味
        if (CollectionUtils.isNotEmpty(saveGoodsModel.getDeleteGoodsFlavorGroupIds())) {
            UpdateModel deleteGoodsFlavorGroupUpdateModel = new UpdateModel(true);
            deleteGoodsFlavorGroupUpdateModel.setTableName("goods_flavor_group");
            deleteGoodsFlavorGroupUpdateModel.addContentValue("last_update_user_id", userId);
            deleteGoodsFlavorGroupUpdateModel.addContentValue("last_update_remark", "删除商品口味组信息!");
            deleteGoodsFlavorGroupUpdateModel.addContentValue("delete", 1);
            deleteGoodsFlavorGroupUpdateModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
            deleteGoodsFlavorGroupUpdateModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
            deleteGoodsFlavorGroupUpdateModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, saveGoodsModel.getDeleteGoodsFlavorGroupIds());
            universalMapper.universalUpdate(deleteGoodsFlavorGroupUpdateModel);
            UpdateModel deleteGoodsFlavorUpdateModel = new UpdateModel(true);
            deleteGoodsFlavorUpdateModel.setTableName("goods_flavor");
            deleteGoodsFlavorUpdateModel.addContentValue("last_update_user_id", userId);
            deleteGoodsFlavorUpdateModel.addContentValue("last_update_remark", "删除商品口味信息!");
            deleteGoodsFlavorUpdateModel.addContentValue("delete", 1);
            deleteGoodsFlavorUpdateModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
            deleteGoodsFlavorUpdateModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
            deleteGoodsFlavorUpdateModel.addSearchCondition("goods_flavor_group_id", Constants.SQL_OPERATION_SYMBOL_IN, saveGoodsModel.getDeleteGoodsFlavorGroupIds());
            universalMapper.universalUpdate(deleteGoodsFlavorUpdateModel);
        }
        // 查询出需要修改的商品规格
        List<SaveGoodsModel.GoodsSpecificationInfo> goodsSpecificationInfos = saveGoodsModel.getGoodsSpecificationInfos();
        List<BigInteger> goodsSpecificationIds = new ArrayList<BigInteger>();
        for (SaveGoodsModel.GoodsSpecificationInfo goodsSpecificationInfo : goodsSpecificationInfos) {
            if (goodsSpecificationInfo.getId() != null) {
                goodsSpecificationIds.add(goodsSpecificationInfo.getId());
            }
        }
        Map<BigInteger, GoodsSpecification> goodsSpecificationMap = new HashMap<BigInteger, GoodsSpecification>();
        if (CollectionUtils.isNotEmpty(goodsSpecificationIds)) {
            SearchModel searchModel = new SearchModel(true);
            searchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
            searchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
            searchModel.addSearchCondition("goods_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, goodsId);
            searchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsSpecificationIds);
            List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.findAll(searchModel);
            for (GoodsSpecification goodsSpecification : goodsSpecifications) {
                goodsSpecificationMap.put(goodsSpecification.getId(), goodsSpecification);
            }
        }
        // 处理所有规格,修改与更新
        for (SaveGoodsModel.GoodsSpecificationInfo goodsSpecificationInfo : goodsSpecificationInfos) {
            List<GoodsSpecification> insertGoodsSpecifications = new ArrayList<GoodsSpecification>();
            if (goodsSpecificationInfo.getId() != null) {
                GoodsSpecification goodsSpecification = goodsSpecificationMap.get(goodsSpecificationInfo.getId());
                Validate.notNull(goodsSpecification, "商品规格不存在!");
                goodsSpecification.setName(goodsSpecificationInfo.getName());
                goodsSpecification.setPrice(goodsSpecificationInfo.getPrice());
                goodsSpecificationMapper.update(goodsSpecification);
            } else {
                GoodsSpecification goodsSpecification = buildGoodsSpecification(tenantId, tenantCode, branchId, goodsId, goodsSpecificationInfo, userId);
                insertGoodsSpecifications.add(goodsSpecification);
            }
            if (CollectionUtils.isNotEmpty(insertGoodsSpecifications)) {
                goodsSpecificationMapper.insertAll(insertGoodsSpecifications);
            }
        }
        List<SaveGoodsModel.FlavorGroupInfo> flavorGroupInfos = saveGoodsModel.getFlavorGroupInfos();
        if (CollectionUtils.isNotEmpty(flavorGroupInfos)) {
            // 用来保存需要修改的口味组id
            List<BigInteger> goodsFlavorGroupIds = new ArrayList<BigInteger>();
            // 用来保存需要删除的口味id
            List<BigInteger> deleteGoodsFlavorIds = new ArrayList<BigInteger>();
            // 用来保存需要修改的口味id
            List<BigInteger> goodsFlavorIds = new ArrayList<BigInteger>();
            for (SaveGoodsModel.FlavorGroupInfo goodsFlavorGroupInfo : flavorGroupInfos) {
                if (goodsFlavorGroupInfo.getId() != null) {
                    goodsFlavorGroupIds.add(goodsFlavorGroupInfo.getId());
                    if (CollectionUtils.isNotEmpty(goodsFlavorGroupInfo.getDeleteGoodsFlavorIds())) {
                        deleteGoodsFlavorIds.addAll(goodsFlavorGroupInfo.getDeleteGoodsFlavorIds());
                    }
                    for (SaveGoodsModel.FlavorInfo flavorGroupInfo : goodsFlavorGroupInfo.getFlavorInfos()) {
                        if (flavorGroupInfo.getId() != null) {
                            goodsFlavorIds.add(flavorGroupInfo.getId());
                        }
                    }
                }
            }
            // 删除需要删除的口味
            if (CollectionUtils.isNotEmpty(deleteGoodsFlavorIds)) {
                UpdateModel deleteGoodsFlavorUpdateModel = new UpdateModel(true);
                deleteGoodsFlavorUpdateModel.setTableName("goods_flavor");
                deleteGoodsFlavorUpdateModel.addContentValue("last_update_user_id", userId);
                deleteGoodsFlavorUpdateModel.addContentValue("last_update_remark", "删除商品口味信息!");
                deleteGoodsFlavorUpdateModel.addContentValue("delete", 1);
                deleteGoodsFlavorUpdateModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
                deleteGoodsFlavorUpdateModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
                deleteGoodsFlavorUpdateModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, deleteGoodsFlavorIds);
                universalMapper.universalUpdate(deleteGoodsFlavorUpdateModel);
            }
            // 查询出需要修改的口味组
            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("goods_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, goodsId);
            goodsFlavorGroupSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsFlavorGroupIds);
            List<GoodsFlavorGroup> goodsFlavorGroups = goodsFlavorGroupMapper.findAll(goodsFlavorGroupSearchModel);
            Map<BigInteger, GoodsFlavorGroup> goodsFlavorGroupMap = new HashMap<BigInteger, GoodsFlavorGroup>();
            for (GoodsFlavorGroup goodsFlavorGroup : goodsFlavorGroups) {
                goodsFlavorGroupMap.put(goodsFlavorGroup.getId(), goodsFlavorGroup);
            }
            // 查询出需要修改的口味
            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("goods_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, goodsId);
            goodsFlavorSearchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_IN, goodsFlavorIds);
            List<GoodsFlavor> goodsFlavors = goodsFlavorMapper.findAll(goodsFlavorSearchModel);
            Map<BigInteger, GoodsFlavor> goodsFlavorMap = new HashMap<BigInteger, GoodsFlavor>();
            for (GoodsFlavor goodsFlavor : goodsFlavors) {
                goodsFlavorMap.put(goodsFlavor.getId(), goodsFlavor);
            }
            for (SaveGoodsModel.FlavorGroupInfo flavorGroupInfo : flavorGroupInfos) {
                // 用来保存需要新增的口味,便于批量插入
                List<GoodsFlavor> insertGoodsFlavors = new ArrayList<GoodsFlavor>();
                if (flavorGroupInfo.getId() != null) {
                    GoodsFlavorGroup goodsFlavorGroup = goodsFlavorGroupMap.get(flavorGroupInfo.getId());
                    Validate.notNull(goodsFlavorGroup, "口味组不存在!");
                    goodsFlavorGroup.setName(flavorGroupInfo.getName());
                    goodsFlavorGroup.setLastUpdateUserId(userId);
                    goodsFlavorGroup.setLastUpdateRemark("修改口味组信息!");
                    goodsFlavorGroupMapper.update(goodsFlavorGroup);
                    for (SaveGoodsModel.FlavorInfo flavorInfo : flavorGroupInfo.getFlavorInfos()) {
                        if (flavorInfo.getId() != null) {
                            GoodsFlavor goodsFlavor = goodsFlavorMap.get(flavorInfo.getId());
                            Validate.notNull(goodsFlavor, "商品口味不存在!");
                            goodsFlavor.setName(flavorInfo.getName());
                            goodsFlavor.setPrice(flavorInfo.getPrice() == null ? BigDecimal.ZERO : flavorInfo.getPrice());
                            goodsFlavor.setLastUpdateUserId(userId);
                            goodsFlavor.setLastUpdateRemark("修改口味信息!");
                            goodsFlavorMapper.update(goodsFlavor);
                        } else {
                            GoodsFlavor goodsFlavor = buildGoodsFlavor(flavorInfo, tenantId, tenantCode, branchId, goodsId, goodsFlavorGroup.getId(), userId);
                            insertGoodsFlavors.add(goodsFlavor);
                        // goodsFlavorMapper.insert(goodsFlavor);
                        }
                    }
                } else {
                    GoodsFlavorGroup goodsFlavorGroup = buildGoodsFlavorGroup(tenantId, tenantCode, branchId, goodsId, flavorGroupInfo, userId);
                    goodsFlavorGroupMapper.insert(goodsFlavorGroup);
                    for (SaveGoodsModel.FlavorInfo flavorInfo : flavorGroupInfo.getFlavorInfos()) {
                        GoodsFlavor goodsFlavor = buildGoodsFlavor(flavorInfo, tenantId, tenantCode, branchId, goodsId, goodsFlavorGroup.getId(), userId);
                        insertGoodsFlavors.add(goodsFlavor);
                    // goodsFlavorMapper.insert(goodsFlavor);
                    }
                }
                if (CollectionUtils.isNotEmpty(insertGoodsFlavors)) {
                    goodsFlavorMapper.insertAll(insertGoodsFlavors);
                }
            }
        }
    } else {
        // 新增商品
        Goods goods = new Goods();
        goods.setTenantId(tenantId);
        goods.setTenantCode(tenantCode);
        goods.setBranchId(branchId);
        goods.setName(saveGoodsModel.getName());
        goods.setType(saveGoodsModel.getType());
        goods.setCategoryId(saveGoodsModel.getCategoryId());
        goods.setImageUrl(saveGoodsModel.getImageUrl());
        goods.setCreateUserId(userId);
        goods.setLastUpdateUserId(userId);
        goods.setLastUpdateRemark("新增商品信息!");
        goodsMapper.insert(goods);
        BigInteger goodsId = goods.getId();
        // 新增所有规格
        List<GoodsSpecification> insertGoodsSpecifications = new ArrayList<GoodsSpecification>();
        List<SaveGoodsModel.GoodsSpecificationInfo> goodsSpecificationInfos = saveGoodsModel.getGoodsSpecificationInfos();
        for (SaveGoodsModel.GoodsSpecificationInfo goodsSpecificationInfo : goodsSpecificationInfos) {
            GoodsSpecification goodsSpecification = buildGoodsSpecification(tenantId, tenantCode, branchId, goodsId, goodsSpecificationInfo, userId);
            insertGoodsSpecifications.add(goodsSpecification);
        }
        goodsSpecificationMapper.insertAll(insertGoodsSpecifications);
        List<SaveGoodsModel.FlavorGroupInfo> flavorGroupInfos = saveGoodsModel.getFlavorGroupInfos();
        if (CollectionUtils.isNotEmpty(flavorGroupInfos)) {
            List<GoodsFlavor> insertGoodsFlavors = new ArrayList<GoodsFlavor>();
            for (SaveGoodsModel.FlavorGroupInfo flavorGroupInfo : flavorGroupInfos) {
                GoodsFlavorGroup goodsFlavorGroup = buildGoodsFlavorGroup(tenantId, tenantCode, branchId, goodsId, flavorGroupInfo, userId);
                goodsFlavorGroupMapper.insert(goodsFlavorGroup);
                for (SaveGoodsModel.FlavorInfo flavorInfo : flavorGroupInfo.getFlavorInfos()) {
                    GoodsFlavor goodsFlavor = buildGoodsFlavor(flavorInfo, tenantId, tenantCode, branchId, goodsId, goodsFlavorGroup.getId(), userId);
                    insertGoodsFlavors.add(goodsFlavor);
                }
            }
            goodsFlavorMapper.insertAll(insertGoodsFlavors);
        }
    }
    ApiRest apiRest = new ApiRest();
    apiRest.setMessage("保存商品信息成功!");
    apiRest.setSuccessful(true);
    return apiRest;
}
Also used : SearchModel(build.dream.common.utils.SearchModel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApiRest(build.dream.common.api.ApiRest) BigInteger(java.math.BigInteger) UpdateModel(build.dream.common.utils.UpdateModel) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with UpdateModel

use of build.dream.common.utils.UpdateModel in project erp-catering by liuyandong33.

the class GoodsService method deleteGoods.

/**
 * 删除商品、商品规格、商品口味组、商品口味
 * @param deleteGoodsModel
 * @return
 */
@Transactional(rollbackFor = Exception.class)
public ApiRest deleteGoods(DeleteGoodsModel deleteGoodsModel) {
    BigInteger tenantId = deleteGoodsModel.getTenantId();
    BigInteger branchId = deleteGoodsModel.getBranchId();
    BigInteger userId = deleteGoodsModel.getUserId();
    BigInteger goodsId = deleteGoodsModel.getGoodsId();
    SearchModel searchModel = new SearchModel(true);
    searchModel.addSearchCondition("id", Constants.SQL_OPERATION_SYMBOL_EQUALS, goodsId);
    searchModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    searchModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    Goods goods = goodsMapper.find(searchModel);
    Validate.notNull(goods, "商品不存在!");
    validateCanNotOperate(tenantId, branchId, "goods", goodsId, 2);
    goods.setLastUpdateUserId(userId);
    goods.setLastUpdateRemark("删除商品信息!");
    goods.setDeleted(true);
    goodsMapper.update(goods);
    // 删除该商品的所有规格
    UpdateModel goodsSpecificationUpdateModel = new UpdateModel(true);
    goodsSpecificationUpdateModel.setTableName("goods_specification");
    goodsSpecificationUpdateModel.addContentValue("deleted", 1);
    goodsSpecificationUpdateModel.addContentValue("last_update_user_id", userId);
    goodsSpecificationUpdateModel.addContentValue("last_update_remark", "删除商品规格信息!");
    goodsSpecificationUpdateModel.addSearchCondition("goods_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, goodsId);
    goodsSpecificationUpdateModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    goodsSpecificationUpdateModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    universalMapper.universalUpdate(goodsSpecificationUpdateModel);
    // 删除该商品的所有口味组
    UpdateModel goodsFlavorGroupUpdateModel = new UpdateModel(true);
    goodsFlavorGroupUpdateModel.setTableName("goods_flavor_group");
    goodsFlavorGroupUpdateModel.addContentValue("deleted", 1);
    goodsFlavorGroupUpdateModel.addContentValue("last_update_user_id", userId);
    goodsFlavorGroupUpdateModel.addContentValue("last_update_remark", "删除商品口味组信息!");
    goodsFlavorGroupUpdateModel.addSearchCondition("goods_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, goodsId);
    goodsFlavorGroupUpdateModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    goodsFlavorGroupUpdateModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    universalMapper.universalUpdate(goodsFlavorGroupUpdateModel);
    // 删除该商品的所有口味
    UpdateModel goodsFlavorUpdateModel = new UpdateModel(true);
    goodsFlavorUpdateModel.setTableName("goods_flavor");
    goodsFlavorUpdateModel.addContentValue("deleted", 1);
    goodsFlavorUpdateModel.addContentValue("last_update_user_id", userId);
    goodsFlavorUpdateModel.addContentValue("last_update_remark", "删除商品口味组信息!");
    goodsFlavorUpdateModel.addSearchCondition("goods_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, goodsId);
    goodsFlavorUpdateModel.addSearchCondition("tenant_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, tenantId);
    goodsFlavorUpdateModel.addSearchCondition("branch_id", Constants.SQL_OPERATION_SYMBOL_EQUALS, branchId);
    universalMapper.universalUpdate(goodsFlavorUpdateModel);
    ApiRest apiRest = new ApiRest();
    apiRest.setMessage("删除商品信息成功!");
    apiRest.setSuccessful(true);
    return apiRest;
}
Also used : SearchModel(build.dream.common.utils.SearchModel) BigInteger(java.math.BigInteger) UpdateModel(build.dream.common.utils.UpdateModel) ApiRest(build.dream.common.api.ApiRest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ApiRest (build.dream.common.api.ApiRest)2 SearchModel (build.dream.common.utils.SearchModel)2 UpdateModel (build.dream.common.utils.UpdateModel)2 BigInteger (java.math.BigInteger)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1