|
@@ -0,0 +1,186 @@
|
|
|
|
|
+package com.zxyj.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
+import com.zxyj.common.Result;
|
|
|
|
|
+import com.zxyj.dto.ProductDTO;
|
|
|
|
|
+import com.zxyj.dto.ProductListQueryDTO;
|
|
|
|
|
+import com.zxyj.entity.Product;
|
|
|
|
|
+import com.zxyj.entity.User;
|
|
|
|
|
+import com.zxyj.mapper.ProductMapper;
|
|
|
|
|
+import com.zxyj.mapper.UserMapper;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+public class ProductService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ProductMapper productMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UserMapper userMapper;
|
|
|
|
|
+
|
|
|
|
|
+ public Result<Map<String, Object>> list(ProductListQueryDTO query) {
|
|
|
|
|
+ Page<Product> page = new Page<>(query.getPage(), query.getSize());
|
|
|
|
|
+ LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
|
|
|
|
|
+ .eq(Product::getStatus, "on_shelf")
|
|
|
|
|
+ .orderByDesc(Product::getCreatedAt);
|
|
|
|
|
+ if (query.getProductType() != null && !query.getProductType().isEmpty()) {
|
|
|
|
|
+ wrapper.eq(Product::getProductType, query.getProductType());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (query.getDomain() != null && !query.getDomain().isEmpty()) {
|
|
|
|
|
+ wrapper.eq(Product::getDomain, query.getDomain());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (query.getKeyword() != null && !query.getKeyword().isEmpty()) {
|
|
|
|
|
+ wrapper.like(Product::getName, query.getKeyword());
|
|
|
|
|
+ }
|
|
|
|
|
+ IPage<Product> result = productMapper.selectPage(page, wrapper);
|
|
|
|
|
+ List<ProductDTO> records = result.getRecords().stream()
|
|
|
|
|
+ .map(ProductDTO::from)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
|
|
+ data.put("records", records);
|
|
|
|
|
+ data.put("total", result.getTotal());
|
|
|
|
|
+ data.put("page", result.getCurrent());
|
|
|
|
|
+ data.put("size", result.getSize());
|
|
|
|
|
+ return Result.success(data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<ProductDTO> detail(Long id) {
|
|
|
|
|
+ Product product = productMapper.selectById(id);
|
|
|
|
|
+ if (product == null) {
|
|
|
|
|
+ return Result.error("商品不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!"on_shelf".equals(product.getStatus()) && !"approved".equals(product.getStatus())) {
|
|
|
|
|
+ return Result.error("商品未上架");
|
|
|
|
|
+ }
|
|
|
|
|
+ return Result.success(ProductDTO.from(product));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<ProductDTO> create(Product product, Long vendorId) {
|
|
|
|
|
+ if (vendorId == null) {
|
|
|
|
|
+ return Result.error("请先登录");
|
|
|
|
|
+ }
|
|
|
|
|
+ User vendor = userMapper.selectById(vendorId);
|
|
|
|
|
+ if (vendor == null || !"approved".equals(vendor.getVendorStatus())) {
|
|
|
|
|
+ return Result.error("仅审核通过的服务商可发布商品");
|
|
|
|
|
+ }
|
|
|
|
|
+ product.setVendorId(vendorId);
|
|
|
|
|
+ product.setVendorName(vendor.getNickname() != null ? vendor.getNickname() : vendor.getRealName());
|
|
|
|
|
+ product.setStatus("pending");
|
|
|
|
|
+ product.setCreatedAt(new Date());
|
|
|
|
|
+ product.setUpdatedAt(new Date());
|
|
|
|
|
+ productMapper.insert(product);
|
|
|
|
|
+ return Result.success(ProductDTO.from(product));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<ProductDTO> update(Product product, Long vendorId) {
|
|
|
|
|
+ Product existing = productMapper.selectById(product.getId());
|
|
|
|
|
+ if (existing == null) {
|
|
|
|
|
+ return Result.error("商品不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!existing.getVendorId().equals(vendorId)) {
|
|
|
|
|
+ return Result.error("无权修改此商品");
|
|
|
|
|
+ }
|
|
|
|
|
+ product.setStatus("pending");
|
|
|
|
|
+ product.setUpdatedAt(new Date());
|
|
|
|
|
+ productMapper.updateById(product);
|
|
|
|
|
+ return Result.success(ProductDTO.from(productMapper.selectById(product.getId())));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<List<ProductDTO>> myProducts(Long vendorId) {
|
|
|
|
|
+ List<Product> list = productMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<Product>()
|
|
|
|
|
+ .eq(Product::getVendorId, vendorId)
|
|
|
|
|
+ .orderByDesc(Product::getCreatedAt)
|
|
|
|
|
+ );
|
|
|
|
|
+ List<ProductDTO> dtos = list.stream().map(ProductDTO::from).collect(Collectors.toList());
|
|
|
|
|
+ return Result.success(dtos);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<String> shelve(Long productId, Long vendorId, String action) {
|
|
|
|
|
+ Product product = productMapper.selectById(productId);
|
|
|
|
|
+ if (product == null) {
|
|
|
|
|
+ return Result.error("商品不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!product.getVendorId().equals(vendorId)) {
|
|
|
|
|
+ return Result.error("无权操作");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!"approved".equals(product.getStatus())) {
|
|
|
|
|
+ return Result.error("商品未通过审核,无法上下架");
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("shelve".equals(action)) {
|
|
|
|
|
+ product.setStatus("on_shelf");
|
|
|
|
|
+ } else if ("unshelve".equals(action)) {
|
|
|
|
|
+ product.setStatus("off_shelf");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return Result.error("操作类型错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ product.setUpdatedAt(new Date());
|
|
|
|
|
+ productMapper.updateById(product);
|
|
|
|
|
+ return Result.success("操作成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<String> review(Long productId, String action, String rejectReason) {
|
|
|
|
|
+ Product product = productMapper.selectById(productId);
|
|
|
|
|
+ if (product == null) {
|
|
|
|
|
+ return Result.error("商品不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!"pending".equals(product.getStatus())) {
|
|
|
|
|
+ return Result.error("仅待审核商品可审核");
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("approve".equals(action)) {
|
|
|
|
|
+ product.setStatus("approved");
|
|
|
|
|
+ product.setRejectReason(null);
|
|
|
|
|
+ } else if ("reject".equals(action)) {
|
|
|
|
|
+ product.setStatus("rejected");
|
|
|
|
|
+ product.setRejectReason(rejectReason != null ? rejectReason : "不符合规范");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return Result.error("操作类型错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ product.setUpdatedAt(new Date());
|
|
|
|
|
+ productMapper.updateById(product);
|
|
|
|
|
+ return Result.success("操作成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<List<ProductDTO>> pendingReview() {
|
|
|
|
|
+ List<Product> list = productMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<Product>()
|
|
|
|
|
+ .eq(Product::getStatus, "pending")
|
|
|
|
|
+ .orderByAsc(Product::getCreatedAt)
|
|
|
|
|
+ );
|
|
|
|
|
+ List<ProductDTO> dtos = list.stream().map(ProductDTO::from).collect(Collectors.toList());
|
|
|
|
|
+ return Result.success(dtos);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Result<List<ProductDTO>> adminList(String status) {
|
|
|
|
|
+ LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
|
|
|
|
|
+ .orderByDesc(Product::getCreatedAt);
|
|
|
|
|
+ if (status != null && !status.isEmpty()) {
|
|
|
|
|
+ wrapper.eq(Product::getStatus, status);
|
|
|
|
|
+ }
|
|
|
|
|
+ List<Product> list = productMapper.selectList(wrapper);
|
|
|
|
|
+ List<ProductDTO> dtos = list.stream().map(ProductDTO::from).collect(Collectors.toList());
|
|
|
|
|
+ return Result.success(dtos);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean decreaseStock(Long productId, Integer quantity) {
|
|
|
|
|
+ Product product = productMapper.selectById(productId);
|
|
|
|
|
+ if (product == null || product.getStock() < quantity) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ product.setStock(product.getStock() - quantity);
|
|
|
|
|
+ product.setSalesCount((product.getSalesCount() != null ? product.getSalesCount() : 0) + quantity);
|
|
|
|
|
+ product.setUpdatedAt(new Date());
|
|
|
|
|
+ productMapper.updateById(product);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|