From 963e311b60b5f8809582cab59f0f170179bf5f0a Mon Sep 17 00:00:00 2001 From: wangguocheng Date: Mon, 29 Apr 2024 09:42:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=98=E9=85=92=E4=B8=9A=E5=8A=A1=E9=80=BB?= =?UTF-8?q?=E8=BE=91+mybatisPlus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/me/zhengjie/utils/PageUtil.java | 9 +- eladmin-system/pom.xml | 14 +- .../me/zhengjie/config/MybatisPlusConfig.java | 36 +++++ .../productInfo/product/domain/TbProduct.java | 5 + .../productSku/domain/TbProductSku.java | 5 + .../domain/TbMerchantAccount.java | 5 + .../impl/TbMerchantRegisterServiceImpl.java | 1 - .../me/zhengjie/mybatis/entity/StorageVo.java | 13 ++ .../mybatis/entity/TbUserStorage.java | 33 +++++ .../mapper/TbMerchantAccountMapper.java | 30 +++++ .../mybatis/mapper/TbProducSkutMapper.java | 10 ++ .../mybatis/mapper/TbProductMapper.java | 10 ++ .../mybatis/mapper/TbUserStorageMapper.java | 9 ++ .../mybatis/rest/StorageController.java | 40 ++++++ .../zhengjie/mybatis/service/ShopService.java | 34 +++++ .../mybatis/service/impl/ShopServiceImpl.java | 123 ++++++++++++++++++ 16 files changed, 372 insertions(+), 5 deletions(-) create mode 100644 eladmin-system/src/main/java/me/zhengjie/config/MybatisPlusConfig.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/entity/StorageVo.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/entity/TbUserStorage.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbMerchantAccountMapper.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProducSkutMapper.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProductMapper.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbUserStorageMapper.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/rest/StorageController.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/service/ShopService.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/mybatis/service/impl/ShopServiceImpl.java diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/PageUtil.java b/eladmin-common/src/main/java/me/zhengjie/utils/PageUtil.java index 1382584a..c646484c 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/PageUtil.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/PageUtil.java @@ -15,6 +15,7 @@ */ package me.zhengjie.utils; +import org.apache.poi.ss.formula.functions.T; import org.springframework.data.domain.Page; import java.util.*; @@ -49,8 +50,12 @@ public class PageUtil extends cn.hutool.core.util.PageUtil { map.put("totalElements",page.getTotalElements()); return map; } - - + public static Map toPlusPage(List list, Integer total) { + Map map = new LinkedHashMap<>(2); + map.put("content",list); + map.put("totalElements",total); + return map; + } /** * 自定义分页 */ diff --git a/eladmin-system/pom.xml b/eladmin-system/pom.xml index 71bc491b..bb182b32 100644 --- a/eladmin-system/pom.xml +++ b/eladmin-system/pom.xml @@ -93,10 +93,20 @@ 2.1.4 + + + + + com.baomidou - mybatis-plus-extension - 3.3.2 + mybatis-plus-generator + 3.5.3.1 + + + com.baomidou + mybatis-plus-boot-starter + 3.5.3.1 diff --git a/eladmin-system/src/main/java/me/zhengjie/config/MybatisPlusConfig.java b/eladmin-system/src/main/java/me/zhengjie/config/MybatisPlusConfig.java new file mode 100644 index 00000000..f9e6df14 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/config/MybatisPlusConfig.java @@ -0,0 +1,36 @@ +package me.zhengjie.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.*; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import java.util.Collections; + + +@Configuration +@MapperScan("me.zhengjie.mybatis.mapper") +@EnableTransactionManagement +public class MybatisPlusConfig { + + + @Bean + public PaginationInnerInterceptor paginationInnerInterceptor() { + PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor(); + // 设置最大单页限制数量,默认 500 条,-1 不受限制 + paginationInterceptor.setMaxLimit(-1L); + paginationInterceptor.setDbType(DbType.MYSQL); + // 开启 count 的 join 优化,只针对部分 left join + paginationInterceptor.setOptimizeJoin(true); + return paginationInterceptor; + } + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor(){ + MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); + mybatisPlusInterceptor.setInterceptors(Collections.singletonList(paginationInnerInterceptor())); + return mybatisPlusInterceptor; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/product/domain/TbProduct.java b/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/product/domain/TbProduct.java index 59794157..33b7bfe0 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/product/domain/TbProduct.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/product/domain/TbProduct.java @@ -15,6 +15,9 @@ */ package me.zhengjie.modules.productInfo.product.domain; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import cn.hutool.core.bean.BeanUtil; import io.swagger.annotations.ApiModelProperty; @@ -36,12 +39,14 @@ import java.io.Serializable; @Entity @Data @Table(name="tb_product") +@TableName("tb_product") public class TbProduct implements Serializable { @Id @Column(name = "`id`") @GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value = "id") + @TableId(type = IdType.AUTO) private Integer id; @Column(name = "`source_path`") diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/productSku/domain/TbProductSku.java b/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/productSku/domain/TbProductSku.java index c41b82a9..9e2a7ce6 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/productSku/domain/TbProductSku.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/productInfo/productSku/domain/TbProductSku.java @@ -16,7 +16,10 @@ package me.zhengjie.modules.productInfo.productSku.domain; import cn.hutool.json.JSON; +import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import cn.hutool.core.bean.BeanUtil; import io.swagger.annotations.ApiModelProperty; @@ -35,12 +38,14 @@ import java.io.Serializable; @Entity @Data @Table(name="tb_product_sku") +@TableName("tb_product_sku") public class TbProductSku implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "`id`") @ApiModelProperty(value = "自增id") + @TableId(type = IdType.AUTO) private Integer id; @Column(name = "`shop_id`") diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/merchantAccount/domain/TbMerchantAccount.java b/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/merchantAccount/domain/TbMerchantAccount.java index 450ff3fd..71c6f6cc 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/merchantAccount/domain/TbMerchantAccount.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/merchantAccount/domain/TbMerchantAccount.java @@ -15,6 +15,9 @@ */ package me.zhengjie.modules.shopInfo.merchantAccount.domain; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import cn.hutool.core.bean.BeanUtil; import io.swagger.annotations.ApiModelProperty; @@ -31,6 +34,7 @@ import java.io.Serializable; **/ @Entity @Data +@TableName("tb_merchant_account") @Table(name="tb_merchant_account") public class TbMerchantAccount implements Serializable { @@ -38,6 +42,7 @@ public class TbMerchantAccount implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "`id`") @ApiModelProperty(value = "自增id") + @TableId(type = IdType.AUTO) private Integer id; @Column(name = "`account`",nullable = false) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/shopRegister/service/impl/TbMerchantRegisterServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/shopRegister/service/impl/TbMerchantRegisterServiceImpl.java index 4564d273..a3d860b0 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/shopRegister/service/impl/TbMerchantRegisterServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/shopInfo/shopRegister/service/impl/TbMerchantRegisterServiceImpl.java @@ -15,7 +15,6 @@ */ package me.zhengjie.modules.shopInfo.shopRegister.service.impl; -import com.baomidou.mybatisplus.extension.api.R; import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.shopInfo.shopRegister.domain.TbMerchantRegister; import me.zhengjie.utils.ValidationUtil; diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/entity/StorageVo.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/entity/StorageVo.java new file mode 100644 index 00000000..98a9d4ef --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/entity/StorageVo.java @@ -0,0 +1,13 @@ +package me.zhengjie.mybatis.entity; + +import lombok.Data; + +@Data +public class StorageVo { + private Integer id; + private String productName; + private String img; + private String account; + private String unit; + private Integer num; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/entity/TbUserStorage.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/entity/TbUserStorage.java new file mode 100644 index 00000000..a4821d70 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/entity/TbUserStorage.java @@ -0,0 +1,33 @@ +package me.zhengjie.mybatis.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.Date; + +@Data +@EqualsAndHashCode(callSuper = true) +@TableName("tb_user_storage") +public class TbUserStorage extends Model { + private static final long serialVersionUID = 1L; + @TableId(type = IdType.ASSIGN_UUID) + private String id; // 记录ID + private Integer userId; // 用户ID + private String shopId; // 用户ID + private Integer productId; + private Integer num; + private Integer outNum; + private Integer inNum; + private Date createTime; + private String account; + private String unit; + private String productName; + private Integer skuId; + private String skuName; + private String imgUrl; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbMerchantAccountMapper.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbMerchantAccountMapper.java new file mode 100644 index 00000000..97fa7574 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbMerchantAccountMapper.java @@ -0,0 +1,30 @@ +/* +* 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 me.zhengjie.mybatis.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import me.zhengjie.modules.shopInfo.merchantAccount.domain.TbMerchantAccount; +import me.zhengjie.mybatis.entity.TbUserStorage; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +/** +* @website https://eladmin.vip +* @author lyf +* @date 2024-02-08 +**/ +public interface TbMerchantAccountMapper extends BaseMapper { +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProducSkutMapper.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProducSkutMapper.java new file mode 100644 index 00000000..ba8e0856 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProducSkutMapper.java @@ -0,0 +1,10 @@ +package me.zhengjie.mybatis.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import me.zhengjie.modules.productInfo.product.domain.TbProduct; +import me.zhengjie.modules.productInfo.productSku.domain.TbProductSku; + +public interface TbProducSkutMapper extends BaseMapper { + + +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProductMapper.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProductMapper.java new file mode 100644 index 00000000..0e9c1915 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbProductMapper.java @@ -0,0 +1,10 @@ +package me.zhengjie.mybatis.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import me.zhengjie.modules.productInfo.product.domain.TbProduct; +import me.zhengjie.mybatis.entity.TbUserStorage; + +public interface TbProductMapper extends BaseMapper { + + +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbUserStorageMapper.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbUserStorageMapper.java new file mode 100644 index 00000000..efa2e1b2 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/mapper/TbUserStorageMapper.java @@ -0,0 +1,9 @@ +package me.zhengjie.mybatis.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import me.zhengjie.mybatis.entity.TbUserStorage; + +public interface TbUserStorageMapper extends BaseMapper { + + +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/rest/StorageController.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/rest/StorageController.java new file mode 100644 index 00000000..f0f7d3e1 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/rest/StorageController.java @@ -0,0 +1,40 @@ +package me.zhengjie.mybatis.rest; + +import io.swagger.annotations.Api; +import lombok.RequiredArgsConstructor; +import me.zhengjie.annotation.Log; +import me.zhengjie.mybatis.entity.StorageVo; +import me.zhengjie.mybatis.service.ShopService; +import me.zhengjie.utils.SecurityUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +@Api(tags = "/shop/storage") +@RequestMapping("/api/storage") +public class StorageController { + @Autowired + private ShopService shopService; + @PostMapping("/findStorage") + public ResponseEntity findStorage(@RequestParam Integer shopId,String account,Pageable pageable){ + return new ResponseEntity<>(shopService.findStorage(shopId,account,pageable), HttpStatus.OK); + } + @PostMapping("/outStorage") + @Log("商品出库") + public ResponseEntity outStorage(@RequestParam Integer id,@RequestParam Integer num){ + String userName = SecurityUtils.getCurrentUsername(); + shopService.outStorage(id,userName,num); + return new ResponseEntity<>( HttpStatus.OK); + } + @PostMapping("/inStorage") + @Log("商品入库") + public ResponseEntity inStorage(@RequestBody StorageVo storageVo){ + String userName = SecurityUtils.getCurrentUsername(); + shopService.inStorage(storageVo,userName); + return new ResponseEntity<>(HttpStatus.OK); + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/service/ShopService.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/service/ShopService.java new file mode 100644 index 00000000..9607cf52 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/service/ShopService.java @@ -0,0 +1,34 @@ +/* +* 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 me.zhengjie.mybatis.service; + +import me.zhengjie.mybatis.entity.StorageVo; +import org.springframework.data.domain.Pageable; + +/** +* @website https://eladmin.vip +* @description 服务接口 +* @author admin +* @date 2023-10-30 +**/ +public interface ShopService { + Object findStorage(Integer shopId, String account, Pageable pageable); + + void outStorage(Integer id, String userName, Integer num); + + + void inStorage(StorageVo storageVo, String userName); +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/mybatis/service/impl/ShopServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/mybatis/service/impl/ShopServiceImpl.java new file mode 100644 index 00000000..4f0dd66d --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/mybatis/service/impl/ShopServiceImpl.java @@ -0,0 +1,123 @@ +/* + * 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 me.zhengjie.mybatis.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import me.zhengjie.exception.NewBadRequestException; +import me.zhengjie.modules.productInfo.productSku.domain.TbProductSku; +import me.zhengjie.modules.shopInfo.merchantAccount.domain.TbMerchantAccount; +import me.zhengjie.mybatis.entity.StorageVo; +import me.zhengjie.mybatis.entity.TbUserStorage; +import me.zhengjie.mybatis.mapper.TbMerchantAccountMapper; +import me.zhengjie.mybatis.mapper.TbProducSkutMapper; +import me.zhengjie.mybatis.mapper.TbProductMapper; +import me.zhengjie.mybatis.mapper.TbUserStorageMapper; +import me.zhengjie.mybatis.service.ShopService; +import me.zhengjie.utils.*; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.*; + +/** + * @author admin + * @website https://eladmin.vip + * @description 服务实现 + * @date 2023-10-30 + **/ +@Service +public class ShopServiceImpl implements ShopService { + + @Autowired + private TbUserStorageMapper userStorageMapper; + @Autowired + private TbMerchantAccountMapper tbMerchantAccountMapper; + @Autowired + private TbProducSkutMapper producSkutMapper; + @Autowired + private TbProductMapper productMapper; + @Override + public Object findStorage(Integer shopId, String account, Pageable pageable) { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("shop_id", shopId); + if (StringUtils.isNotEmpty(account)) { + queryWrapper.eq("account", account); + } + IPage iPage = userStorageMapper.selectPage(new Page<>(pageable.getPageNumber(), pageable.getPageSize()), queryWrapper); + return PageUtil.toPage(iPage.getRecords(), iPage.getTotal()); + } + + @Override + public void outStorage(Integer id, String userName, Integer num) { + if (Objects.isNull(num) || num < 0) { + throw new NewBadRequestException("请输入出库数量"); + } + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("account", userName); + TbMerchantAccount merchantAccount = tbMerchantAccountMapper.selectOne(wrapper); + + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("id", id); + queryWrapper.eq("shop_id", merchantAccount.getShopId()); + TbUserStorage userStorage = userStorageMapper.selectOne(queryWrapper); + if (Objects.isNull(userStorage)) { + throw new NewBadRequestException("存储商品在该店铺不存在"); + } + if (userStorage.getNum() < num) { + throw new NewBadRequestException("库存不足"); + } + userStorage.setNum(userStorage.getNum() - num); + userStorage.setOutNum(userStorage.getOutNum() + num); + userStorageMapper.updateById(userStorage); + } + + @Override + public void inStorage(StorageVo storageVo, String userName) { + if (Objects.isNull(storageVo.getNum()) || storageVo.getNum() < 0) { + throw new NewBadRequestException("请输入出库数量"); + } + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq("account", userName); + TbMerchantAccount merchantAccount = tbMerchantAccountMapper.selectOne(wrapper); + + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("product_name", storageVo.getProductName()); + queryWrapper.eq("account", storageVo.getAccount()); + queryWrapper.eq("shop_id", merchantAccount.getShopId()); + TbUserStorage userStorage = userStorageMapper.selectOne(queryWrapper); + if (Objects.nonNull(userStorage)) { + userStorage.setNum(userStorage.getNum() + storageVo.getNum()); + userStorage.setInNum(userStorage.getInNum() + storageVo.getNum()); + userStorageMapper.updateById(userStorage); + }else { + userStorage = new TbUserStorage(); + userStorage.setAccount(storageVo.getAccount()); + userStorage.setCreateTime(new Date()); + userStorage.setShopId(merchantAccount.getShopId()); + userStorage.setNum(storageVo.getNum()); + userStorage.setOutNum(0); + userStorage.setInNum(storageVo.getNum()); + userStorage.setImgUrl(storageVo.getImg()); + userStorage.setProductName(storageVo.getImg()); + userStorage.setUnit(storageVo.getUnit()); + userStorageMapper.insert(userStorage); + } + } +} \ No newline at end of file