package cn.lili.controller.goods;
|
|
import cn.lili.common.aop.annotation.DemoSite;
|
import cn.lili.common.context.ThreadContextHolder;
|
import cn.lili.common.enums.ResultCode;
|
import cn.lili.common.enums.ResultUtil;
|
import cn.lili.common.exception.RetryException;
|
import cn.lili.common.exception.ServiceException;
|
import cn.lili.common.security.OperationalJudgment;
|
import cn.lili.common.security.context.UserContext;
|
import cn.lili.common.utils.StringUtils;
|
import cn.lili.common.vo.ResultMessage;
|
import cn.lili.modules.goods.entity.dos.Goods;
|
import cn.lili.modules.goods.entity.dos.GoodsSku;
|
import cn.lili.modules.goods.entity.dto.GoodsOperationDTO;
|
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
|
import cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO;
|
import cn.lili.modules.goods.entity.enums.GoodsStatusEnum;
|
import cn.lili.modules.goods.entity.vos.GoodsSkuVO;
|
import cn.lili.modules.goods.entity.vos.GoodsVO;
|
import cn.lili.modules.goods.entity.vos.StockWarningVO;
|
import cn.lili.modules.goods.service.GoodsService;
|
import cn.lili.modules.goods.service.GoodsSkuService;
|
import cn.lili.modules.search.service.EsGoodsIndexService;
|
import cn.lili.modules.statistics.aop.PageViewPoint;
|
import cn.lili.modules.statistics.aop.enums.PageViewEnum;
|
import cn.lili.modules.store.entity.dos.StoreDetail;
|
import cn.lili.modules.store.service.StoreDetailService;
|
import cn.lili.utils.COSUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import io.swagger.annotations.*;
|
import lombok.extern.slf4j.Slf4j;
|
import org.elasticsearch.action.ActionListener;
|
import org.elasticsearch.client.RequestOptions;
|
import org.elasticsearch.index.reindex.BulkByScrollResponse;
|
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.http.MediaType;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.http.HttpServletResponse;
|
import javax.validation.Valid;
|
import javax.validation.constraints.NotNull;
|
import java.io.IOException;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* 店铺端,商品接口
|
*
|
* @author pikachu
|
* @since 2020-02-23 15:18:56
|
*/
|
@RestController
|
@Slf4j
|
@Api(tags = "店铺端,商品接口")
|
@RequestMapping("/store/goods/goods")
|
public class GoodsStoreController {
|
|
/**
|
* 商品
|
*/
|
@Autowired
|
private GoodsService goodsService;
|
/**
|
* 商品sku
|
*/
|
@Autowired
|
private GoodsSkuService goodsSkuService;
|
@Autowired
|
private COSUtil cosUtil;
|
|
|
@ApiOperation(value = "分页获取商品列表")
|
@GetMapping(value = "/list")
|
public ResultMessage<IPage<Goods>> getByPage(GoodsSearchParams goodsSearchParams) {
|
//获取当前登录商家账号
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
goodsSearchParams.setStoreId(storeId);
|
IPage<Goods> page = goodsService.queryByParams(goodsSearchParams);
|
page.getRecords().forEach(goodsVO -> {
|
if (StringUtils.isNotBlank(goodsVO.getOriginal())&& !goodsVO.getOriginal().contains("http")) {
|
goodsVO.setOriginal(cosUtil.getPreviewUrl(goodsVO.getOriginal()));
|
}
|
if (StringUtils.isNotBlank(goodsVO.getGoodsVideo())&& !goodsVO.getGoodsVideo().contains("http")) {
|
goodsVO.setGoodsVideo(cosUtil.getPreviewUrl(goodsVO.getGoodsVideo()));
|
}
|
});
|
return ResultUtil.data(page);
|
}
|
|
@ApiOperation(value = "分页获取商品Sku列表")
|
@GetMapping(value = "/sku/list")
|
public ResultMessage<IPage<GoodsSku>> getSkuByPage(GoodsSearchParams goodsSearchParams) {
|
//获取当前登录商家账号
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
goodsSearchParams.setStoreId(storeId);
|
return ResultUtil.data(goodsSkuService.getGoodsSkuByPage(goodsSearchParams));
|
}
|
|
@ApiOperation(value = "分页获取库存告警商品列表")
|
@GetMapping(value = "/list/stock")
|
public ResultMessage<IPage<GoodsSku>> getWarningStockByPage(GoodsSearchParams goodsSearchParams) {
|
//获取当前登录商家账号
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
goodsSearchParams.setStoreId(storeId);
|
goodsSearchParams.setAlertQuantity(true);
|
goodsSearchParams.setMarketEnable(GoodsStatusEnum.UPPER.name());
|
IPage<GoodsSku> goodsSkuPage = goodsSkuService.getGoodsSkuByPage(goodsSearchParams);
|
return ResultUtil.data(goodsSkuPage);
|
}
|
|
@ApiOperation(value = "批量修改商品预警库存")
|
@PutMapping(value = "/batch/update/alert/stocks", consumes = "application/json")
|
public ResultMessage<Object> batchUpdateAlertQuantity(@RequestBody List<GoodsSkuStockDTO> updateStockList) {
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
// 获取商品skuId集合
|
List<String> goodsSkuIds = updateStockList.stream().map(GoodsSkuStockDTO::getSkuId).collect(Collectors.toList());
|
// 根据skuId集合查询商品信息
|
List<GoodsSku> goodsSkuList = goodsSkuService.list(new LambdaQueryWrapper<GoodsSku>().in(GoodsSku::getId, goodsSkuIds).eq(GoodsSku::getStoreId, storeId));
|
// 过滤不符合当前店铺的商品
|
List<String> filterGoodsSkuIds = goodsSkuList.stream().map(GoodsSku::getId).collect(Collectors.toList());
|
List<GoodsSkuStockDTO> collect = updateStockList.stream().filter(i -> filterGoodsSkuIds.contains(i.getSkuId())).collect(Collectors.toList());
|
goodsSkuService.batchUpdateAlertQuantity(collect);
|
return ResultUtil.success();
|
}
|
|
@ApiOperation(value = "修改商品预警库存")
|
@PutMapping(value = "/update/alert/stocks", consumes = "application/json")
|
public ResultMessage<Object> updateAlertQuantity(@RequestBody GoodsSkuStockDTO goodsSkuStockDTO) {
|
goodsSkuService.updateAlertQuantity(goodsSkuStockDTO);
|
return ResultUtil.success();
|
}
|
|
|
@ApiOperation(value = "通过id获取")
|
@GetMapping(value = "/get/{id}")
|
public ResultMessage<GoodsVO> get(@PathVariable String id) {
|
GoodsVO goods = OperationalJudgment.judgment(goodsService.getGoodsVO(id));
|
return ResultUtil.data(goods);
|
}
|
|
@ApiOperation(value = "新增商品")
|
@PostMapping(value = "/create", consumes = "application/json", produces = "application/json")
|
public ResultMessage<GoodsOperationDTO> save(@Valid @RequestBody GoodsOperationDTO goodsOperationDTO) {
|
goodsService.addGoods(goodsOperationDTO);
|
return ResultUtil.success();
|
}
|
|
@ApiOperation(value = "修改商品")
|
@PutMapping(value = "/update/{goodsId}", consumes = "application/json", produces = "application/json")
|
public ResultMessage<GoodsOperationDTO> update(@Valid @RequestBody GoodsOperationDTO goodsOperationDTO, @PathVariable String goodsId) {
|
goodsService.editGoods(goodsOperationDTO, goodsId);
|
return ResultUtil.success();
|
}
|
|
@DemoSite
|
@ApiOperation(value = "下架商品", notes = "下架商品时使用")
|
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true)
|
@PutMapping(value = "/under")
|
public ResultMessage<Object> underGoods(@RequestParam List<String> goodsId) {
|
|
goodsService.updateGoodsMarketAble(goodsId, GoodsStatusEnum.DOWN, "商家下架");
|
return ResultUtil.success();
|
}
|
|
@ApiOperation(value = "上架商品", notes = "上架商品时使用")
|
@PutMapping(value = "/up")
|
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true)
|
public ResultMessage<Object> unpGoods(@RequestParam List<String> goodsId) {
|
goodsService.updateGoodsMarketAble(goodsId, GoodsStatusEnum.UPPER, "");
|
return ResultUtil.success();
|
}
|
|
@DemoSite
|
@ApiOperation(value = "删除商品")
|
@PutMapping(value = "/delete")
|
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true)
|
public ResultMessage<Object> deleteGoods(@RequestParam List<String> goodsId) {
|
goodsService.deleteGoods(goodsId);
|
return ResultUtil.success();
|
}
|
|
@ApiOperation(value = "设置商品运费模板")
|
@PutMapping(value = "/freight")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "query", allowMultiple = true),
|
@ApiImplicitParam(name = "templateId", value = "运费模板ID", required = true, paramType = "query")
|
})
|
public ResultMessage<Object> freight(@RequestParam List<String> goodsId, @RequestParam String templateId) {
|
goodsService.freight(goodsId, templateId);
|
return ResultUtil.success();
|
}
|
|
@ApiOperation(value = "根据goodsId分页获取商品规格列表")
|
@GetMapping(value = "/sku/{goodsId}/list")
|
public ResultMessage<List<GoodsSkuVO>> getSkuByList(@PathVariable String goodsId) {
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
return ResultUtil.data(goodsSkuService.getGoodsSkuVOList(goodsSkuService.list(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goodsId).eq(GoodsSku::getStoreId, storeId))));
|
}
|
|
@ApiOperation(value = "修改商品库存")
|
@PutMapping(value = "/update/stocks", consumes = "application/json")
|
public ResultMessage<Object> updateStocks(@RequestBody List<GoodsSkuStockDTO> updateStockList) {
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
// 获取商品skuId集合
|
List<String> goodsSkuIds = updateStockList.stream().map(GoodsSkuStockDTO::getSkuId).collect(Collectors.toList());
|
// 根据skuId集合查询商品信息
|
List<GoodsSku> goodsSkuList = goodsSkuService.list(new LambdaQueryWrapper<GoodsSku>().in(GoodsSku::getId, goodsSkuIds).eq(GoodsSku::getStoreId, storeId));
|
// 过滤不符合当前店铺的商品
|
List<String> filterGoodsSkuIds = goodsSkuList.stream().map(GoodsSku::getId).collect(Collectors.toList());
|
List<GoodsSkuStockDTO> collect = updateStockList.stream().filter(i -> filterGoodsSkuIds.contains(i.getSkuId())).collect(Collectors.toList());
|
goodsSkuService.updateStocks(collect);
|
return ResultUtil.success();
|
}
|
@ApiOperation(value = "通过id获取商品信息")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, paramType = "path"),
|
@ApiImplicitParam(name = "skuId", value = "skuId", required = true, paramType = "path")
|
})
|
@GetMapping(value = "/sku/{goodsId}/{skuId}")
|
@PageViewPoint(type = PageViewEnum.SKU, id = "#id")
|
public ResultMessage<Map<String, Object>> getSku(@NotNull(message = "商品ID不能为空") @PathVariable("goodsId") String goodsId,
|
@NotNull(message = "SKU ID不能为空") @PathVariable("skuId") String skuId) {
|
try {
|
// 读取选中的列表
|
Map<String, Object> map = goodsSkuService.getGoodsSkuDetail(goodsId, skuId);
|
return ResultUtil.data(map);
|
} catch (ServiceException se) {
|
log.info(se.getMsg(), se);
|
throw se;
|
} catch (Exception e) {
|
log.error(ResultCode.GOODS_ERROR.message(), e);
|
return ResultUtil.error(ResultCode.GOODS_ERROR);
|
}
|
|
}
|
|
|
@ApiOperation(value = "分页获取商品Sku列表")
|
@GetMapping(value = "/queryExportStock")
|
public void queryExportStock(GoodsSearchParams goodsSearchParams) {
|
//获取当前登录商家账号
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
goodsSearchParams.setStoreId(storeId);
|
|
HttpServletResponse response = ThreadContextHolder.getHttpResponse();
|
goodsSkuService.queryExportStock(response,goodsSearchParams);
|
}
|
|
@ApiOperation(value = "上传商品库存列表")
|
@PostMapping(value = "/importStockExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
public ResultMessage<Object> importStockExcel(@RequestPart("files") MultipartFile files) {
|
//获取当前登录商家账号
|
String storeId = Objects.requireNonNull(UserContext.getCurrentUser()).getStoreId();
|
goodsSkuService.importStock(storeId,files);
|
return ResultUtil.success(ResultCode.SUCCESS);
|
}
|
|
|
|
}
|