g.自定义jsr303校验注解
1.自定义校验注解
package com.like.mall.common.valid;
@Documented
@Constraint(validatedBy = { ListValueConstraint.class}) // 通过什么校验器进行校验
@Target({METHOD,FIELD,ANNOTATION_TYPE,CONSTRUCTOR,PARAMETER,TYPE_USE}) // 可以标注在哪些属性上
@Retention(RUNTIME)
public @interface ListValue {
// 错误信息
String message() default "{com.like.mall.valid.common.ListValue.message";
// 校验分组
Class<?>[] groups() default {};
//
Class <? extends Payload> [] payload() default{};
int[] ints() default { };
}
2.创建注解的校验器
package com.like.mall.common.valid;
/**
* @author like
* @since 2020-11-01 9:43
* ListValue的校验器
* 思路:
* 判断传入的值是否是ints里面的
*/
public class ListValueConstraint implements ConstraintValidator<ListValue, Integer> {
private Set<Integer> set = new HashSet<Integer>();
@Override
public void initialize(ListValue constraintAnnotation) {
// 添加到集合中
int[] ints = constraintAnnotation.ints();
for (int i : ints) {
set.add(i);
}
}
/**
* 执行校验
* @param value 需要校验的值
*/
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
return set.contains(value);
}
}
3.添加校验失败的默认信息
4.使用
/**
* 显示状态[0-不显示;1-显示]
*/
@ListValue(ints ={0,1}) // 自定义校验注解
private Integer showStatus;
5.测试
h.SPU和SKU
SPU:(标准化单元产品) -==小米8==
是商品信息聚合的最小单位,是一组可复用、易检索的标准化信息的集合,该==集合描述了一个产品的特性==
SKU:-==小米8 6+128G 黑色==
具体的产品的属性
i.product库各表之间的联系
attr:商品属性表
arrt_group:属性分组表
attr_attrgroup_relation:关联表
product_attr_value:商品对应的商品属性
j.平台属性
导入sql文件
添加前端文件
1.获取指定分类属性分组
需求:
1.默认显示所有属性分组
2.点击分类,显示对应的属性分组
实现:
/**
* 商品系统-平台属性-属性分组
* - 根据分类信息显示对应的分组信息
* - 默认显示所有
* -
*/
@RequestMapping("/list/{catelogId}")
public R list(@RequestParam Map<String, Object> params,
@PathVariable Long catelogId) {
PageUtils page= /* attrGroupService.queryPage(params);*/
attrGroupService.queryPage(params,catelogId);
return R
.ok()
.put("page", page);
}
@Override
public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
IPage<AttrGroupEntity> page = null;
// 默认显示所有
if (catelogId == 0) {
page = page(new Query<AttrGroupEntity>().getPage(params),
new QueryWrapper<AttrGroupEntity>());
} else {
// select * from pms_attr_group where catelog_id = ? and ( attr_group_id = key or attr_group_name like "%key%"
String key = (String) params.get("key"); // 获取参数中的key
QueryWrapper<AttrGroupEntity> query = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
if (StringUtils.isNotBlank(key)) {
query.and(o -> {
o
.eq("attr_group_id", key)
.or()
.like("attr_group_name", key);
});
}
page = page(new Query<AttrGroupEntity>().getPage(params), query);
}
return new PageUtils(page);
}
2.解决添加属性分组选择分类时子节点为空还显示选择的问题
CategoryEntity添加注解@JsonInclude(NON_EMPTY)
/**
* 在表中不存在,专门用来存放子节点
*/
@JsonInclude(NON_EMPTY) // 不为空的时候才返回
@TableField(exist = false)
private List<CategoryEntity> children;
3.修复点击修改的时候所属分类不能回显的问题
1.根据属性分组中的分类信息找到当前分类
2.递归查找它的父分类并保存
3.保存分类路径
4.AttrGroupEntity中添加属性
k.添加mp分页插件
package com.like.mall.product.config;
/**
* @author like
* @since 2020-11-01 13:13
*/
@Configuration
@EnableTransactionManagement
@MapperScan("com.like.mall.product.dao")
public class MybatisConfig {
// 分页插件
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置请求的页面大于最后页面的操作,true 返回首页
paginationInnerInterceptor.setOverflow(true);
// 设置最大分页
paginationInnerInterceptor.setMaxLimit(500L);
return paginationInnerInterceptor;
}
}
l.品牌管理:品牌和分类关联
pms_category_brand_relation
pms_brand
pms_category
1.获取当前品牌的分类
2.保存品牌和分类的关系
前端传入品牌和分类的id,后端获取对应的名字,关联起来
3.级联修改
brand
category
m.平台属性:规格参数
AttrController
1.添加规格参数的时候一起添加属性id和属性分组id
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody AttrVo attr){
// attrService.save(attr);
// 保存attr 和 attr group 连接表
attrService.saveAttr(attr);
return R.ok();
}
@Override
@Transactional // 事务
public void saveAttr(AttrVo attr) {
// 1.保存基本数据
AttrEntity ae = new AttrEntity();
BeanUtils.copyProperties(attr, ae);
save(ae);
// 2.保存关联关系
AttrAttrgroupRelationEntity aarEntity = AttrAttrgroupRelationEntity
.builder()
.attrGroupId(attr.getAttrGroupId())
// .attrId(getByNameAndCatelogId(ae).getAttrId())
.attrId(ae.getAttrId())
.build();
aarDao.insert(aarEntity);
}
2.查询当前分类的属性
1.默认显示所有
2.点击分类显示对应的属性
3.参数名模糊查询
/**
* 查询当前分类的id
*/
@GetMapping("/base/list/{catelogId}")
public R baseAttrList(@RequestParam Map<String, Object> params,
@PathVariable("catelogId") Long catelogId) {
PageUtils page = attrService.queryBaseAttrPage(params, catelogId);
return R.ok()
.put("page", page);
}
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> query = new QueryWrapper<>();
// 1.组装查询条件
if(catelogId != 0) {
query.eq("catelog_id", catelogId);
}
String key = (String) params.get("key");
if(StringUtils.isNotBlank(key)) {
// attr_id attr_name
query.and(q -> {
q.eq("attr_id", key)
.or()
.like("attr_name", key);
});
}
// 2.执行查询
IPage<AttrEntity> page = page(new Query<AttrEntity>().getPage(params), query);
// 3.封装返回
PageUtils pageUtils = new PageUtils(page);
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> attrRespVoList =
records.stream()
.map(attrEntity -> {
AttrRespVo vo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity, vo);
// 3.1.获取当前attr对应的分组信息
AttrAttrgroupRelationEntity aare = aarDao.selectOne(
// 查询属性分组组名和分类
new QueryWrapper<AttrAttrgroupRelationEntity>().eq(
"attr_id",
vo.getAttrId()));
if(aare != null) {
AttrGroupEntity attrGroup = agDao.selectById(
aare.getAttrGroupId());
vo.setGroupName(attrGroup.getAttrGroupName());
}
// 3.2.获取当前attr对应的分类信息
CategoryEntity category = cDao.selectById(vo.getCatelogId());
if(category != null) {
vo.setCatelogName(category.getName());
}
return vo;
})
.collect(Collectors.toList());
pageUtils.setList(attrRespVoList);
return pageUtils;
}
3.点击修改:添加回显信息(完整的分类路径)
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo vo = new AttrRespVo();
// 1.获取attr信息
AttrEntity attr = getById(attrId);
BeanUtils.copyProperties(attr, vo);
// 2.获取分组信息
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = aarDao.selectOne(
new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
if(attrAttrgroupRelationEntity != null) {
vo.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
}
assert attrAttrgroupRelationEntity != null;
AttrGroupEntity attrGroupEntity = agDao.selectById(attrAttrgroupRelationEntity.getAttrGroupId());
if(attrGroupEntity != null) {
vo.setGroupName(attrGroupEntity.getAttrGroupName());
}
// 3.获取分类完整路径
Long[] catelogPath = categoryService.findCatelogPath(attr.getCatelogId());
vo.setCatelogPath(catelogPath);
return vo;
}
4.属性和属性分组联系的级联更新
@Override
public void updateDetail(AttrVo attr) {
AttrEntity attrE = new AttrEntity();
BeanUtils.copyProperties(attr, attrE);
// 1.先更新attr
updateById(attrE);
// 2.修改分组关联
AttrAttrgroupRelationEntity relation =
AttrAttrgroupRelationEntity.builder()
.attrId(attr.getAttrId())
.attrGroupId(attr.getAttrGroupId())
.build();
aarDao.update(relation, new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
}
平台属性-销售属性
获取销售信息方法:
由于和规格参数公用的是一张表,根据attrType来区别。所以公用一个查询当前分类属性的方法
修改url请求地址为动态的
/**
* 查询当前分类的属性([0-销售属性,1-基本属性,2-既是销售属性又是基本属性])
*/
@GetMapping("/{attrType}/list/{catelogId}")
public R baseAttrList(@RequestParam Map<String, Object> params,
@PathVariable("catelogId") Long catelogId,
@PathVariable String attrType) {
PageUtils page = attrService.queryBaseAttrPage(params, catelogId,attrType);
return R.ok()
.put("page", page);
}
修改queryBaseAttrPage()方法,添加第三个值 attrType,添加查询条件
添加判断,如果是基本属性就需要查询属性分组信息
保存销售属性
如果是销售属性就不需要保存属性分组信息。
销售属性数据回显
修改销售属性
商品系统-平台属性-属性分组
查询当前属性分组所关联的基本属性(规格参数)
/**
* 获取attrGroup所关联的分类信息
*
* @param attrGroupId attr组id
*
* @return {@link R}
*/
@GetMapping("/{attrGroupId}/attr/relation")
public R attrRelation(@PathVariable Long attrGroupId) {
List<AttrEntity> attrs = attrService.getAttrGroupById(attrGroupId);
return R.ok()
.put("data", attrs);
}
@Override
public List<AttrEntity> getAttrGroupById(Long attrGroupId) {
List<AttrAttrgroupRelationEntity> ens = aarDao.selectList(
new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
// 收集所有id
List<Long> ids =
ens.stream()
.map(AttrAttrgroupRelationEntity::getAttrId)
.collect(Collectors.toList());
return listByIds(ids);
}
删除属性分组和属性的关联关系
/**
* 删除属性分组和属性的关联关系
*
* @param vos 参数个数
*
* @return {@link R}
*/
@PostMapping("/attr/relation/delete")
public R delAttrAndAttrGroupRelation(@RequestBody List<AttrGroupRelationVo> vos) {
Long count = attrGroupService.delAttrAndAttrGroupRelation(vos);
return R.ok().put("count",count);
}
@Override
public Long delAttrAndAttrGroupRelation(List<AttrGroupRelationVo> vos) {
List<AttrAttrgroupRelationEntity> entities =
vos.stream()
.map((vo) -> {
AttrAttrgroupRelationEntity attrAttrgroupRelationEntity =
AttrAttrgroupRelationEntity.builder()
.build();
BeanUtils.copyProperties(vo, attrAttrgroupRelationEntity);
return attrAttrgroupRelationEntity;
})
.collect(Collectors.toList());
return relationDao.delAttrAndAttrGroupRelation(entities);
}
<delete id="delAttrAndAttrGroupRelation">
delete from `mall-product`.`pms_attr_attrgroup_relation` where
<foreach collection="entities" item="item" separator=" or " open="(" close=")">
(attr_id =#{item.attrId} and attr_group_id = #{item.attrGroupId})
</foreach>
</delete>
查询出:当前分类,没有关联过分组的属性
@GetMapping("/{attrGroupId}/noattr/relation")
public R attrNoRelation(@RequestParam Map<String, Object> params,
@PathVariable Long attrGroupId) {
PageUtils page = attrService.getNoRelationAttr(params, attrGroupId);
return R.ok().put("page", page);
}
@Override
public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrGroupId) {
// 1.当前分组只能关联自己所属的分类里面的所有属性
AttrGroupEntity attrGroupEntity = agDao.selectById(attrGroupId);
Long catelogId = attrGroupEntity.getCatelogId();
// 2.当前分类中所有属性分组
List<AttrGroupEntity> otherAttrGroup = agDao.selectList(new QueryWrapper<AttrGroupEntity>()
.eq(catelogId != null, "catelog_id", catelogId));
List<Long> otherAttrGroupIds = otherAttrGroup.stream()
.map(AttrGroupEntity::getAttrGroupId)
.collect(Collectors.toList()); // 收集属性分组id
// 3.这些分组所关联的属性
List<AttrAttrgroupRelationEntity> relationEntities = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>()
.in(!otherAttrGroupIds.isEmpty(), "attr_group_id", otherAttrGroupIds));
List<Long> attrIds = relationEntities.stream()
.map(AttrAttrgroupRelationEntity::getAttrId)
.collect(Collectors.toList()); // 收集关联过属性分组的属性
// 4.查询出:当前分类,没有关联过分组的属性
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>()
.eq(catelogId != null, "catelog_id", catelogId)
.notIn(!attrIds.isEmpty(), "attr_id", attrIds) // 查询没有关联过属性分组的属性
.eq("attr_type", ATTR_TYPE_BASE.getCode()); // 查询基本属性
// 模糊查询
String key = (String) params.get("key");
if (StringUtils.isNotBlank(key)) {
queryWrapper.and(query -> {
query.eq("attr_id", key)
.or()
.like("attr_name", key);
});
}
IPage<AttrEntity> page = page(new Query<AttrEntity>().getPage(params), queryWrapper);
return new PageUtils(page);
}
保存属性分组和属性的关联关系
/**
* 添加属性和属性分组关系
*
* @param vos vos
* @return {@link R}
*/
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
int count = attrAttrgroupRelationService.saveAttrAndAttrGroupRrtion(vos);
return R.ok();
}
@Override
public int saveAttrAndAttrGroupRrtion(List<AttrGroupRelationVo> vos) {
List<AttrAttrgroupRelationEntity> entities = vos.stream().map(item -> {
AttrAttrgroupRelationEntity aagr = AttrAttrgroupRelationEntity.builder().build();
BeanUtils.copyProperties(item, aagr);
return aagr;
}).collect(Collectors.toList());
saveBatch(entities);
return entities.size();
}
商品系统-商品维护-发布商品
会员系统-获取所有会员等级
使用自动生成的接口
获取当前分类所关联的品牌
CategoryBrandRelationController
/**
* 获取分类关系品牌列表
*
* @param catId 分类id
* @return {@link R}
*/
@GetMapping("/brands/list")
public R catRelationBrandList(@RequestParam(required = true) Long catId) {
List<BrandEntity> entities = categoryBrandRelationService.getBrandsByCatId(catId);
List<BrandVo> brandVos = entities.stream()
.map(i -> BrandVo.builder()
.brandName(i.getName())
.brandId(i.getBrandId())
.build())
.collect(Collectors.toList());
return R.ok().put("data",brandVos);
}
@Override
public List<BrandEntity> getBrandsByCatId(Long catId) {
// 1.查询出和当前分类项关联的品牌id
List<CategoryBrandRelationEntity> categoryBrandRelationEntities = categoryBrandRelationService
.list(new QueryWrapper<CategoryBrandRelationEntity>()
.eq("catelog_id", catId));
// 2.根据上面的list选出品牌id
List<Long> brandIdList = categoryBrandRelationEntities.stream()
.map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());
// 3.根据brandId查找brand
return brandService.listByIds(brandIdList);
}
获取分类下所有属性分组,关联属性
AttrGroupController
@GetMapping("/{catelogId}/withattr")
public R getAttrGroupAndAttrByCatId(@PathVariable Long catelogId) {
// 1.查询当前分类下所有属性分组
List<AttrGroupAndAttrVo> vos = attrGroupService.getAttrGroupAndAttrByCatId(catelogId);
return R.ok().put("data", vos);
}
@Override
public List<AttrGroupAndAttrVo> getAttrGroupAndAttrByCatId(Long catelogId) {
// 1、查询所有分组
List<AttrGroupEntity> attrGroup = list(new QueryWrapper<AttrGroupEntity>()
.eq("catelog_id", catelogId));
return attrGroup.stream()
.map(i -> {
AttrGroupAndAttrVo vo = new AttrGroupAndAttrVo();
BeanUtils.copyProperties(i, vo);
// 2、根据分组id查询所有属性
List<AttrEntity> attrs = attrService.getAttrGroupById(vo.getAttrGroupId());
vo.setAttrs(attrs);
return vo;
})
.collect(Collectors.toList());
}
保存spu信息 -复杂(远程调用)
SpuInfoController.save
/**
* 保存
*/
@PostMapping("/save")
public R save(@RequestBody SpuSaveVo vo){
spuInfoService.saveSpuInfo(vo);
return R.ok();
}
spuInfoService.saveSpuInfo
@Override
@Transactional
public void saveSpuInfo(SpuSaveVo vo) {
// 1.保存spu基本信息 spu_info
SpuInfoEntity spuInfo = new SpuInfoEntity();
BeanUtils.copyProperties(vo, spuInfo);
spuInfo.setCreateTime(new Date());
spuInfo.setUpdateTime(new Date());
this.saveBaseSpuInfo(spuInfo);
// 2.保存spu描述图片 spu_info_desc
List<String> decript = vo.getDecript();
SpuInfoDescEntity spuInfoDesc = new SpuInfoDescEntity();
spuInfoDesc.setSpuId(spuInfo.getId());
spuInfoDesc.setDecript(String.join(",", decript));
spuInfoDescService.saveSpuInfoDesc(spuInfoDesc);
// 3.保存spu图片集 spu_images
List<String> images = vo.getImages();
spuImagesService.saveImages(spuInfo.getId(), images);
// 4.保存spu规格参数 product_attr_value
List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
List<ProductAttrValueEntity> productAttrValueEntities = baseAttrs.stream()
.map(i -> {
AttrEntity attr = attrService.getById(i.getAttrId());
return ProductAttrValueEntity.builder()
.attrId(i.getAttrId())
.attrName(attr.getAttrName())
.attrValue(i.getAttrValues())
.quickShow(i.getShowDesc())
.spuId(spuInfo.getId())
.build();
}).collect(Collectors.toList());
productAttrValueService.saveProductAttrValueEntities(productAttrValueEntities);
// 5.保存spu的积分信息 跨库: mall-sale spu_bounds
Bounds bounds = vo.getBounds();
// product 要给 coupon 发送数据 -> To:SpuBoundTo,创建于公共包common中
SpuBoundTo spuBoundTo = new SpuBoundTo();
BeanUtils.copyProperties(bounds, spuBoundTo);
spuBoundTo.setSpuId(spuInfo.getId());
R saveSpuBounds = couponFeignService.saveSpuBounds(spuBoundTo);
if (saveSpuBounds.getCode() != 0) {
log.error("远程调用coupon服务失败{}(saveSpuBounds):" + spuBoundTo);
}
// 6.保存spu对应的sku信息
List<Skus> skus = vo.getSkus();
if (!skus.isEmpty()) {
skus.forEach(sku -> {
// - sku基本信息 sku_info
SkuInfoEntity skuInfo = new SkuInfoEntity();
BeanUtils.copyProperties(sku, skuInfo);
skuInfo.setBrandId(spuInfo.getBrandId());
skuInfo.setCatalogId(spuInfo.getCatalogId());
skuInfo.setSaleCount(0L);
skuInfo.setSpuId(spuInfo.getId());
String defaultImg = "";
for (Images image : sku.getImages()) {
if (image.getDefaultImg() == 1) {
defaultImg = image.getImgUrl();
}
}
skuInfo.setSkuDefaultImg(defaultImg);
skuInfoService.saveSkuInfo(skuInfo);
// - sku图片信息 sku_images
Long skuId = skuInfo.getSkuId();
List<SkuImagesEntity> skuImages = sku.getImages().stream()
.map(i -> {
SkuImagesEntity skuImage = new SkuImagesEntity();
skuImage.setSkuId(skuId);
skuImage.setImgUrl(i.getImgUrl());
skuImage.setDefaultImg(i.getDefaultImg());
return skuImage;
}).filter(e -> !e.getImgUrl().isEmpty())
.collect(Collectors.toList());
skuImagesService.saveBatch(skuImages);
// - sku销售属性信息 sku_sal_attr_value
List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = sku.getAttr().stream().map(a -> {
SkuSaleAttrValueEntity skuSaleAttrValue = new SkuSaleAttrValueEntity();
BeanUtils.copyProperties(a, skuSaleAttrValue);
skuSaleAttrValue.setSkuId(skuId);
return skuSaleAttrValue;
}).collect(Collectors.toList());
skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
// - sku优惠满减信息 跨库:mall-sale sms_sku_ladder sms_sku_full_reduction sms_spu_bounds
SkuReductionTo skuReductionTo = new SkuReductionTo();
BeanUtils.copyProperties(sku, skuReductionTo);
skuReductionTo.setSkuId(skuId);
if (skuReductionTo.getFullCount() <= 0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
R saveSkuReduction = couponFeignService.saveSkuReduction(skuReductionTo);
if (saveSkuReduction.getCode() != 0) {
log.error("远程调用coupon服务失败{}(saveSkuReduction):" + skuReductionTo);
}
}
});
}
}
this.saveBaseSpuInfo
@Override
public void saveBaseSpuInfo(SpuInfoEntity spuInfo) {
baseMapper.insert(spuInfo);
}
spuInfoDescService.saveSpuInfoDesc
@Override
public void saveSpuInfoDesc(SpuInfoDescEntity spuInfoDesc) {
baseMapper.insert(spuInfoDesc);
}
spuImagesService.saveImages
@Override
public void saveImages(Long id, List<String> images) {
if (!images.isEmpty()) {
List<SpuImagesEntity> spuImages = images.stream()
.map(i -> {
SpuImagesEntity spuImage = new SpuImagesEntity();
spuImage.setId(id);
spuImage.setImgUrl(i);
return spuImage;
}).collect(Collectors.toList());
saveBatch(spuImages);
}
}
productAttrValueService.saveProductAttrValueEntities
@Override
public void saveProductAttrValueEntities(List<ProductAttrValueEntity> productAttrValueEntities) {
saveBatch(productAttrValueEntities);
}
远程调用-保存spu积分信息
/**
* 调用远程coupon服务保存spu积分信息
*
* @param spuBoundTo spu积分TO
* @return {@link R}
*/
@PostMapping("/coupon/spubounds/save")
R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
/**
* 保存
*/
@PostMapping("/save")
public R save(@RequestBody SpuBoundsEntity spuBounds){
spuBoundsService.save(spuBounds);
return R.ok();
}
保存sku-skuInfoService.saveSkuInfo
@Override
public void saveSkuInfo(SkuInfoEntity skuInfo) {
save(skuInfo);
}
保存sku-skuImagesService.saveBatch
skuImagesService.saveBatch(skuImages);
保存sku-skuSaleAttrValueService.saveBatch
skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
远程调用-保存sku详细信息
@PostMapping("/coupon/skufullreduction/saveinfo")
R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
/**
* 列表
*/
@PostMapping("/saveinfo")
public R saveInfo(@RequestBody SkuReductionTo skuReductionTo) {
skuFullReductionService.saveSkuReduction(skuReductionTo);
return R.ok();
}
@Override
public void saveSkuReduction(SkuReductionTo skuReductionTo) {
// - sku优惠满减信息 跨库:mall-sale sms_sku_ladder sms_sku_full_reduction sms_member_price
SkuLadderEntity skuLadder = new SkuLadderEntity();
skuLadder.setSkuId(skuReductionTo.getSkuId());
skuLadder.setFullCount(skuReductionTo.getFullCount());
skuLadder.setDiscount(skuReductionTo.getDiscount());
skuLadder.setAddOther(skuReductionTo.getCountStatus());
// skuLadder.setPrice();
if (skuLadder.getFullCount() > 0) skuLadderService.save(skuLadder);
SkuFullReductionEntity skuFullReduction = new SkuFullReductionEntity();
BeanUtils.copyProperties(skuReductionTo, skuFullReduction);
if (skuFullReduction.getFullPrice().compareTo(new BigDecimal(0)) > 0)
this.save(skuFullReduction);
List<MemberPriceEntity> memberPriceEntityList = skuReductionTo.getMemberPrice().stream()
.map(item -> {
MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
memberPriceEntity.setMemberLevelId(item.getId());
memberPriceEntity.setMemberLevelName(item.getName());
memberPriceEntity.setMemberPrice(item.getPrice());
memberPriceEntity.setAddOther(1);
return memberPriceEntity;
})
.filter(i-> i.getMemberPrice().compareTo(new BigDecimal(0)) > 0)
.collect(Collectors.toList());
memberPriceService.saveBatch(memberPriceEntityList);
}
spu管理:spu搜索
SpuInfoController
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = spuInfoService.queryPageByCondition(params);
return R.ok().put("page", page);
}
@Override
public PageUtils queryPageByCondition(Map<String, Object> params) {
QueryWrapper<SpuInfoEntity> query = new QueryWrapper<>();
String key = (String) params.get("key");
if (StringUtils.isNotBlank(key)) {
query.and(q -> {
q.eq("id", key).or().like("spu_name", key);
});
}
queryCondition(query, params, "status", "publish_status");
queryCondition(query, params, "branId", "brand_id");
queryCondition(query, params, "catelogId", "catelog_id");
IPage<SpuInfoEntity> page = this.page(
new Query<SpuInfoEntity>().getPage(params),
query
);
return new PageUtils(page);
}
public void queryCondition(QueryWrapper<SpuInfoEntity> query, Map<String, Object> params, String key, String colName) {
String status = (String) params.get(key);
if (StringUtils.isNotBlank(status)) {
query.and(q -> {
q.eq(colName, status);
});
}
}
商品系统:商品管理-sku搜索
@Override
public PageUtils queryPageByCondition(Map<String, Object> params) {
QueryWrapper<SkuInfoEntity> query = new QueryWrapper<>();
// 1、name
String key = (String) params.get("key");
if (StringUtils.isNotBlank(key)) {
query.and(q -> {
q.eq("sku_id", key).or().like("sku_name", key);
});
}
String catalogId = (String) params.get("catelogId");
if (StringUtils.isNotBlank(catalogId) && !"0".equalsIgnoreCase(catalogId)) {
query.eq("catalog_id", catalogId);
}
queryCondition(query, params, "branId", "brand_id");
// 2、范围
rangeMin(params, query);
rangeMax(params, query);
IPage<SkuInfoEntity> page = this.page(
new Query<SkuInfoEntity>().getPage(params),
query
);
return new PageUtils(page);
}
public void queryCondition(QueryWrapper<SkuInfoEntity> query, Map<String, Object> params, String key, String colName) {
String status = (String) params.get(key);
if (StringUtils.isNotBlank(status)) {
query.and(q -> {
q.eq(colName, status);
});
}
}
public void rangeMax(Map<String, Object> params, QueryWrapper<SkuInfoEntity> query) {
String max = (String) params.get("max");
if (StringUtils.isNotBlank(max)) {
BigDecimal bigDecimal = new BigDecimal(max);
if (bigDecimal.compareTo(new BigDecimal(0)) > 0)
query.le("price", max);
}
}
public void rangeMin(Map<String, Object> params, QueryWrapper<SkuInfoEntity> query) {
String min = (String) params.get("min");
if (StringUtils.isNotBlank(min)) {
BigDecimal bigDecimal = new BigDecimal(min);
if (bigDecimal.compareTo(new BigDecimal(0)) > 0)
query.ge("price", min);
}
}
库存系统
仓库维护:搜索功能
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<WareInfoEntity> query = new QueryWrapper<>();
String key = (String) params.get("key");
if (StringUtils.isNotBlank(key)) {
query.eq("id",key)
.or().like("name",key)
.or().like("address",key)
.or().like("areacode",key);
}
IPage<WareInfoEntity> page = this.page(
new Query<WareInfoEntity>().getPage(params),
query
);
return new PageUtils(page);
}
商品库存-搜索
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<WareSkuEntity> query = new QueryWrapper<>();
String skuId = (String) params.get("skuId");
if (StringUtils.isNotBlank(skuId)) {
query.eq("sku_id",skuId);
}
String wareId = (String) params.get("wareId");
if (StringUtils.isNotBlank(wareId)) {
query.eq("ware_id",wareId);
}
IPage<WareSkuEntity> page = this.page(
new Query<WareSkuEntity>().getPage(params),
query
);
return new PageUtils(page);
}
采购单维护
搜索
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<PurchaseDetailEntity> query = new QueryWrapper<>();
String key = (String) params.get("key");
if (StringUtils.isNotBlank(key)) {
query.and(q -> {
q.eq("purchase_id", key).or().eq("sku_id", key);
});
}
String status = (String) params.get("status");
if (StringUtils.isNotBlank(status)) {
query.eq("status", status);
}
String wareId = (String) params.get("wareId");
if (StringUtils.isNotBlank(wareId)) {
query.eq("ware_id", wareId);
}
IPage<PurchaseDetailEntity> page = this.page(
new Query<PurchaseDetailEntity>().getPage(params),
query
);
return new PageUtils(page);
}
采购单合并-查询没有被分配的采购单
@RequestMapping("/unreceive/list")
public R unclaimedList(@RequestParam Map<String, Object> params) {
PageUtils page = purchaseService.queryPageUnreceive(params);
return R.ok().put("page", page);
}
@Override
public PageUtils queryPageUnreceive(Map<String, Object> params) {
IPage<PurchaseEntity> page = this.page(
new Query<PurchaseEntity>().getPage(params),
new QueryWrapper<PurchaseEntity>()
.eq("status",0).or().eq("status",1)
);
return new PageUtils(page);
}
合并采购需求
/**
* 合并采购需求单到采购单
*
* @param vo vo
* @return {@link R}
*/
@PostMapping("/merge")
public R merge(@RequestBody MergeVo vo) {
purchaseService.merge(vo);
return R.ok();
}
@Override
public void merge(MergeVo vo) {
Long purchaseId = vo.getPurchaseId();
if (purchaseId == null) {
// 1.没有采购单就新建一个
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setStatus(WareConstant.Purchase.create); // 采购单的状态为新建
purchaseEntity.setCreateTime(new Date());
purchaseEntity.setUpdateTime(new Date());
this.save(purchaseEntity);
purchaseId = purchaseEntity.getId();
}
// 2.修改采购需求单的id和状态
List<Long> items = vo.getItems();
Long finalPurchaseId = purchaseId;
List<PurchaseDetailEntity> purchaseDetailEntities = items.stream()
.map(i -> {
PurchaseDetailEntity purchaseDetail = new PurchaseDetailEntity();
purchaseDetail.setId(i);
purchaseDetail.setPurchaseId(finalPurchaseId);
purchaseDetail.setStatus(WareConstant.PurchaseDetail.assigned); // 修改采购需求单的状态为已分配
return purchaseDetail;
}).collect(Collectors.toList());
purchaseDetailService.updateBatchById(purchaseDetailEntities);
}
采购员领取采购单
PurchaseController
/**
* 采购员领取采购单
*
* @param ids id
* @return {@link R}
*/
@PostMapping("/received")
public R received(@RequestBody List<Long> ids) {
purchaseService.received(ids);
return R.ok();
}
@Override
@Transactional
public void received(List<Long> ids) {
// 1.修改采购单的状态(刚创建或者刚分配)
List<PurchaseEntity> receiveList = list(new QueryWrapper<PurchaseEntity>().in("id", ids)).stream()
.filter(i -> i.getStatus() == WareConstant.Purchase.create || i.getStatus() == WareConstant.Purchase.assigned)
.peek(i -> {
i.setStatus(WareConstant.Purchase.receive);
i.setUpdateTime(new Date());
})
.collect(Collectors.toList());
// 2.改变采购单的状态
updateBatchById(receiveList);
// 3.改变采购需求单的状态
receiveList.forEach(i -> {
List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailService.updateStatusByPurchaseId(i.getId()).stream()
.map(item -> {
PurchaseDetailEntity purchaseDetail = new PurchaseDetailEntity();
purchaseDetail.setId(item.getId());
purchaseDetail.setStatus(WareConstant.PurchaseDetail.buying); // 修改为采购中
return purchaseDetail;
}).collect(Collectors.toList());
purchaseDetailService.updateBatchById(purchaseDetailEntities);
});
}
完成采购
/**
* 完成采购
*
* @param vos vos
* @return {@link R}
*/
@PostMapping("/done")
public R finish(@RequestBody PurchaseDoneVo vos) {
purchaseService.done(vos);
return R.ok();
}
@Override
@Transactional
public void done(PurchaseDoneVo vos) {
// 1.改变采购项的状态
boolean flag = true;
List<PurchaseItemDoneVo> items = vos.getItems();
List<PurchaseDetailEntity> needUpdates = new ArrayList<>();
for (PurchaseItemDoneVo item : items) {
PurchaseDetailEntity purchaseDetail = new PurchaseDetailEntity();
if (item.getStatus() == WareConstant.PurchaseDetail.hasError) {
flag = false;
purchaseDetail.setStatus(WareConstant.PurchaseDetail.hasError);
} else {
purchaseDetail.setStatus(WareConstant.PurchaseDetail.finish);
// 3.将成功的采购单数据入库
PurchaseDetailEntity purchaseDetails = purchaseDetailService.getById(item.getItemId());
wareSkuService.addStock(purchaseDetails.getSkuId(),purchaseDetails.getWareId(),purchaseDetails.getSkuNum());
}
purchaseDetail.setId(item.getItemId());
needUpdates.add(purchaseDetail);
}
purchaseDetailService.updateBatchById(needUpdates);
// 2.改变采购单状态
Long id = vos.getId();
PurchaseEntity purchase = new PurchaseEntity();
purchase.setId(id);
purchase.setStatus(flag ? WareConstant.Purchase.finish:WareConstant.Purchase.hasError);
purchase.setUpdateTime(new Date());
purchaseService.updateById(purchase);
}
spu规格维护
获取spu的基本属性
@GetMapping("/base/listforspu/{spuId}")
public R baseAttrList(@PathVariable String spuId) {
List<ProductAttrValueEntity> list= productAttrValueService.baseAttrList(spuId);
return R.ok().put("data",list);
}
@Override
public List<ProductAttrValueEntity> baseAttrList(String spuId) {
return list(new QueryWrapper<ProductAttrValueEntity>()
.eq("spu_id", spuId));
}
修改规格信息
AttrController
/**
* 更新spu基本属性(规格参数)
*
* @param spuId spu id
* @param vos vos
* @return {@link R}
*/
@PostMapping("/update/{spuId}")
public R updateSpuBaseAttr(@PathVariable Long spuId,
@RequestBody List<ProductAttrValueEntity> vos) {
productAttrValueService.updateSpuBaseAttr(spuId,vos);
return R.ok().put("data", list);
}
@Override
@Transactional
public void updateSpuBaseAttr(Long spuId, List<ProductAttrValueEntity> vos) {
// 1.删除以前的数据
remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
// 2.保存
List<ProductAttrValueEntity> collect = vos.stream()
.peek(i -> i.setSpuId(spuId)).collect(Collectors.toList());
saveBatch(collect);
}