抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

Hello world!

Controller三个注释(获取值)

  1. @RequestBody请求体中的JSON
  2. @RequestParam是url?后跟的键值对
  3. @PathVariable是REST风格url中的{}变量

Object划分

image-20211028131128469

image-20211028131145962

image-20211028131152946

image-20211028131204191

配置服务内存

终于你也有今天啊老师,内存终于不够了吧

RunConfigurations -> xxx application -> environment -> Vm opinion -> 设置-Xmx100m,最大100m

平台属性—规格参数(后端)

新增

AttrRequestVo

使用自定义的VO对象用于封装数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.atguigu.gulimall.product.vo;
import lombok.Data;
/**
* 用于封装请求数据和响应数据
* 不需要添加上与sql相关的注解
*/
@Data
public class AttrVo {
/**
* 属性id
*/
private Long attrId;
/**
* 属性名
*/
private String attrName;
/**
* 是否需要检索[0-不需要,1-需要]
*/
private Integer searchType;
/**
* 属性图标
*/
private String icon;
/**
* 可选值列表[用逗号分隔]
*/
private String valueSelect;
/**
* 属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]
*/
private Integer attrType;
/**
* 启用状态[0 - 禁用,1 - 启用]
*/
private Long enable;
/**
* 所属分类
*/
private Long catelogId;
/**
* 快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整
*/
private Integer showDesc;
/*
* 分组ID
*/
private Long attrGroupId;
}

AttrController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;
/**
* 保存,这里使用自定义的Vo对象
*/
@RequestMapping("/save")
//@RequiresPermissions("product:attr:save")
public R save(@RequestBody AttrVo attr) {
attrService.saveAttr(attr);
return R.ok();
}
}

AttrServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
/**
* 保存规格参数,连表
*/
@Override
@Transactional // 事务
public void saveAttr(AttrVo attr) {
// 1、保存到attr表
AttrEntity attrEntity = new AttrEntity();
// 使用BeanUtil封装对应属性
BeanUtils.copyProperties(attr, attrEntity);
this.save(attrEntity);
// 2、保存到relation表
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrId(attrEntity.getAttrId());// 注意这里要使用attrEntity,因为使用attr是没有id的(没有添加mybatis的注解,所以没有自动生成id)
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationDao.insert(relationEntity);
}
}

查询分页

AttrResponseVo

原来可以使用继承的写法,学到了

1
2
3
4
5
6
7
8
9
10
11
@Data
public class AttrResponseVo extends AttrEntity {
/*
* 分类名
*/
private String catelogName;
/*
* 分组名
*/
private String groupName;
}

AttrController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;
/**
* 列表
*/
@RequestMapping("/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);
}
}

AttrServiceImpl

带新增的两个属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
/**
* 查询attr表分页
*
* @param params
* @param catelogId
* @return
*/
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
if (catelogId != 0) {
queryWrapper.eq("catelog_id", catelogId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
// and拼接,因为可能id为0所以已经含有了一个eq条件
queryWrapper.and((wrapper) -> {
wrapper.eq("attr_id", key).or().like("attr_name", key);
});
}
// 获得分页对象
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
// 同时还要添加上attrResponseVo中多的两个属性
// 但是不能直接连表查询,数据量太大了。(两表叉乘笛卡尔积)
// 所以先获取到分页后获取查询到的数据,再重新处理
PageUtils pageUtils = new PageUtils(page);
List<AttrEntity> records = page.getRecords();
List<AttrResponseVo> responseVoList = records.stream().map((attrEntity) -> {
AttrResponseVo responseVo = new AttrResponseVo();
// 先复制基本属性
BeanUtils.copyProperties(attrEntity, responseVo);
// 再加入新的两个属性
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
responseVo.setCatelogName(categoryEntity.getName());
}
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (relationEntity != null) {
responseVo.setGroupName(attrGroupDao.selectById(relationEntity.getAttrGroupId()).getAttrGroupName());
}
return responseVo;
}).collect(Collectors.toList());
// 重新设置结果集
pageUtils.setList(responseVoList);
return pageUtils;
}
}

查询回显

AttrResoponseVo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Data
public class AttrResponseVo extends AttrEntity {
/*
* 分类名
*/
private String catelogName;
/*
* 分组名
*/
private String groupName;
/*
* 分类完整路径
*/
private Long[] catelogPath;
/*
* 分组ID
*/
private Long attrGroupId;
}

AttrController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;

/**
* 信息,返回AttrResponseVo对象属性
*/
@RequestMapping("/info/{attrId}")
public R attrResponseInfo(@PathVariable("attrId") Long attrId) {
AttrResponseVo attr = attrService.getResponseAttrInfo(attrId);
return R.ok().put("attr", attr);
}

AttrServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
/**
* 获取信息,返回AttrResponseVo对象属性,带catelogPath完整路径
*/
@Override
public AttrResponseVo getResponseAttrInfo(Long attrId) {
AttrResponseVo responseVo = new AttrResponseVo();
AttrEntity attrEntity = this.getById(attrId);
BeanUtils.copyProperties(attrEntity, responseVo);
// 额外的属性
// 设置GroupName需要从relation表中查出groupid
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (relationEntity != null) {
responseVo.setAttrGroupId(relationEntity.getAttrGroupId());
// 设置GroupName属性
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
if (attrGroupEntity != null) {
responseVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
}
// 设置CatelogPath属性,直接从分类服务中拿方法
Long[] categoryPath = categoryService.findCategoryPath(attrEntity.getCatelogId());
responseVo.setCatelogPath(categoryPath);
// 设置CatelogName属性
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
responseVo.setCatelogName(categoryEntity.getName());
}
return responseVo;
}
}

修改

AttrController

需要用AttrRequestVo

1
2
3
4
5
6
7
8
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody AttrRequestVo attr) {
attrService.updateAttr(attr);
return R.ok();
}

AttrServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 修改,使用AttrRequestVo
*/
@Override
@Transactional
public void updateAttr(AttrRequestVo attr) {
// 修改基本表
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr, attrEntity);
this.updateById(attrEntity);
// 修改/新增relation表
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attr.getAttrId());
// 这里主要判断relation表中是否存在对应attr属性id的数据(即表中是否存入对应属性对应分组,如果没有对应分组,那么需要执行插入语句而不是修改语句)
Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
if (count > 0) {
relationDao.update(relationEntity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
} else {
relationDao.insert(relationEntity);
}
}

平台属性—销售属性(后端)

规格参数和销售属性其实就是attr属性中attrType的不同,所以controller还是同一个,只是请求地址中片段不同,只需要在requestMapping中获得参数判断一下即可,

常量类

在common模块中写一个类,里面写各种枚举

1
2
3
4
5
6
7
8
9
10
11
public class ProductConstant {
public enum AttrEnum {
ATTR_TYPE_BASE(1, "基本属性"), ATTR_TYPE_SALE(0, "销售属性");
private int code;
private String msg;
AttrEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
}

AttrController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;

/**
* 信息,返回AttrResponseVo对象属性
*/
@RequestMapping("/info/{attrId}")
public R attrResponseInfo(@PathVariable("attrId") Long attrId) {
AttrResponseVo attr = attrService.getResponseAttrInfo(attrId);
return R.ok().put("attr", attr);
}

/**
* 条件查询分页列表,返回AttrResponseVo对象属性
* attrType 用于判断是销售属性还是基本属性(详见文档)
*/
@GetMapping("/{attrType}/list/{catelogId}")
public R attrResponseList(@RequestParam Map<String, Object> params,
@PathVariable("attrType") String attrType,
@PathVariable("catelogId") Long catelogId) {
PageUtils page = attrService.queryBaseAttrPage(params, catelogId,attrType);

return R.ok().put("page", page);
}
}

AttrService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 条件查询attr表分页
*
* @param params
* @param catelogId
* @param attrType
* @return
*/
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId, String attrType) {
// 这里判断attrType,用于查询是规格参数还是销售属性
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type", "base".equalsIgnoreCase(attrType) ? 1 : 0);
if (catelogId != 0) {
queryWrapper.eq("catelog_id", catelogId);
}

/**
* 保存规格参数,连表
*/
@Override
@Transactional // 事务
public void saveAttr(AttrRequestVo attr) {
// 1、保存到attr表
AttrEntity attrEntity = new AttrEntity();
// 使用BeanUtil封装对应属性
BeanUtils.copyProperties(attr, attrEntity);
this.save(attrEntity);
// 2、保存到relation表
// 判断是否是销售属性,如果不是那么不需要添加到relation表
if (attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrId(attrEntity.getAttrId()); // 注意这里要使用attrEntity,因为使用attr是没有id的(没有添加mybatis的注解,所以没有自动生成id)
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationDao.insert(relationEntity);
}
}
}

平台属性—属性分组(后端)

查询关联

分组和属性多对多,需要关联表

AttrGroupController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
@Autowired
private AttrGroupService attrGroupService;
@Autowired
private CategoryService categoryService;
@Autowired
private AttrService attrService;

/**
* 获得对应分组Id的属性
*
* @param attrgroupId
* @return
*/
@GetMapping("/{attrgroupId}/attr/relation")
public R attrRelationList(@PathVariable("attrgroupId") String attrgroupId) {
List<AttrEntity> list = attrService.getRelationAttr(attrgroupId);
return R.ok().put("data", list);
}
}

AttrService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 获得对应分组Id的基本属性(规格参数)
*
* @param attrgroupId
* @return
*/
@Override
public List<AttrEntity> getRelationAttr(String attrgroupId) {
List<AttrAttrgroupRelationEntity> relationList = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
List<Long> idList = relationList.stream().map((relationEntity) -> {
return relationEntity.getAttrId();
}).collect(Collectors.toList());
Collection<AttrEntity> attrEntities = this.listByIds(idList);
return (List<AttrEntity>) attrEntities;
}

删除关联

AttrGroupRelationVo

1
2
3
4
5
@Data
public class AttrGroupRelationVo {
private Long attrId;
private Long attrGroupId;
}

AttrGroupController

1
2
3
4
5
6
7
8
/**
* 删除关联
*/
@RequestMapping("/attr/relation/delete")
public R deleteRelation(AttrGroupRelationVo[] vos) {
attrService.deleteRelation(vos);
return R.ok();
}

AttrGroupService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 删除关联
*
* @param vos
*/

@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
// relationDao.delete(new QueryWrapper<>().eq("attr_id", attrId).eq("attr_group_id", groupId))
// Vos->relation对象集合
List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map((vo) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(vo, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
relationDao.deleteBatchRelation(collect);
}

AttrAttrGroupRelationDao

1
2
3
4
5
@Mapper
public interface AttrAttrgroupRelationDao extends BaseMapper<AttrAttrgroupRelationEntity> {

void deleteBatchRelation(@Param("entities") List<AttrAttrgroupRelationEntity> entities);
}
1
2
3
4
5
6
7
8
<delete id="deleteBatchRelation">
DELETE
FROM gulimall_pms.pms_attr_attrgroup_relation
WHERE
<foreach collection="entities" item="item" separator=" OR ">
(attr_id = #{item.attrId} AND attr_group_id = #{item.attrGroupId})
</foreach>
</delete>

查询未关联属性

AttrGroupController

1
2
3
4
5
6
7
8
9
10
11
/**
* 获得对应分组Id没有关联的属性
*
* @param attrgroupId
* @return
*/
@GetMapping("/{attrgroupId}/noattr/relation")
public R attrNoRelationList(@PathVariable("attrgroupId") String attrgroupId, @RequestParam Map<String, Object> params) {
PageUtils pageUtils = attrService.getNoRelationAttr(params, attrgroupId);
return R.ok().put("page", pageUtils);
}

AttrService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* 获得对应分组Id没有关联的属性
*
* @param params
* @param attrgroupId
* @return
*/
@Override
public PageUtils getNoRelationAttr(Map<String, Object> params, String attrgroupId) {
// 当前分组只能关联自己所属分类里面的属性
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
Long catelogId = attrGroupEntity.getCatelogId();
// 当前分组只能关联没有被别的分组引用的属性
// 获得当前分类下的其他分组
List<AttrGroupEntity> groupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId).ne("attr_group_id", attrgroupId));
List<Long> groupIdList = groupEntities.stream().map((item) -> {
return item.getAttrGroupId();
}).collect(Collectors.toList());
// 获得其他分组中对应的属性id
List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", groupIdList));
List<Long> attrIdList = relationEntities.stream().map((item) -> {
return item.getAttrId();
}).collect(Collectors.toList());
// 从当前分类的所有属性中取出上述属性
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).notIn("attr_id", attrIdList);
// 模糊查询判断
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
queryWrapper.and((wrapper) -> {
wrapper.eq("attr_id", key).or().like("attr_name", key);
});
}
IPage<AttrEntity> iPage = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
PageUtils pageUtils = new PageUtils(iPage);
return pageUtils;
}

TODO:这里有个很奇怪的地方,我修改一下关联的话,属性中的所属分组也会改变?

哦我傻了,这俩个就是对应的功能

添加关联

AttrGroupController

1
2
3
4
5
6
7
8
9
10
11
/**
* 新建关联
*
* @param vos
* @return
*/
@PostMapping("/attr/relation")
public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
relationService.saveBatch(vos);
return R.ok();
}

AttrGroupService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* 批量保存
*
* @param vos
*/
@Override
public void saveBatch(List<AttrGroupRelationVo> vos) {
List<AttrAttrgroupRelationEntity> relationEntityList = vos.stream().map((vo) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(vo, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
this.saveBatch(relationEntityList);
}

商品维护—发布商品(后端)

获取用户等级

配置网关application.yml

详见文档,需要member模块中的功能,之前还没有配过该模块的网关配置,配置一下即可

1
2
3
4
5
6
7
8
9
10
11
12
spring:
cloud:
gateway:
routes:
# member会员配置
- id: member_route
uri: lb://gulimall-member
predicates:
- Path=/api/member/**
# 实现转发
filters:
- RewritePath=/api/(?<segment>.*),/$\{segment}

页面模板

原本renren-generator生成的时候就有了,直接复制到前端src文件夹下即可

然后就好了,基本的增删改查

获得分类关联的品牌

BrandVo

1
2
3
4
5
@Data
public class BrandVo {
private Long brandId;
private String brandName;
}

CategoryBrandRelationController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
@Autowired
private CategoryBrandRelationService relationService;

/**
* 获取分类关联的所有品牌列表
*/
@GetMapping("/brand/list")
public R relationListByBrand(@RequestParam("catId") Long catId) {
List<BrandEntity> brandEntities = relationService.getBrandsByCatId(catId);
// 先获得brandEntity,在转换成Vo
List<BrandVo> list = brandEntities.stream().map((item) -> {
BrandVo brandVo = new BrandVo();
// 这里属性名没有对应,所以需要手动设置
brandVo.setBrandId(item.getBrandId());
brandVo.setBrandName(item.getName());
return brandVo;
}).collect(Collectors.toList());

return R.ok().put("data", list);
}
}

为什么这里不直接使用relation对象的原因是,为了重用,如果后面还有业务需要获得更多的brand信息,需要用到brandEntity

CategoryBrandRelationServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 根据catId查询关联的所有品牌的所有信息
*
* @param catId
* @return
*/
@Override
public List<BrandEntity> getBrandsByCatId(Long catId) {
List<CategoryBrandRelationEntity> relationList = baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
List<BrandEntity> collect = relationList.stream().map(relationEntity -> {
Long brandId = relationEntity.getBrandId();
return brandService.getById(brandId);
}).collect(Collectors.toList());
return collect;
}

获取分类关联的分组和属性

  1. 分类:手机手表……
  2. 分组:主体,基本信息,其他信息
  3. 属性:尺寸,颜色

属性从属于分组,分组从属于分类(多对一),分类和品牌多对多

AttrGroupWithAttrsVo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Data
public class AttrGroupWithAttrsVo {
/**
* 分组id
*/
private Long attrGroupId;
/**
* 组名
*/
private String attrGroupName;
/**
* 排序
*/
private Integer sort;
/**
* 描述
*/
private String descript;
/**
* 组图标
*/
private String icon;
/**
* 所属分类id
*/
private Long catelogId;
/**
* 该分组下的所有属性
*/
private List<AttrEntity> attrs;
}

AttrGroupController

1
2
3
4
5
6
7
8
9
10
11
public class AttrGroupController {
/**
* 获得对应分类Id关联的分组和属性
*/
@GetMapping("/{catelogId}/withattr")
public R getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId, @RequestParam Map<String, Object> params) {
// 查出分组
// 查出属性
List<AttrGroupWithAttrsVo> list = attrGroupService.getWithAttrByCatelogId(catelogId);
return R.ok().put("data", list);
}

AttrGroupService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 获得对应分类Id关联的分组和属性
*/
@Override
public List<AttrGroupWithAttrsVo> getWithAttrByCatelogId(Long catelogId) {
// 查询分组
List<AttrGroupEntity> attrGroupList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
List<AttrGroupWithAttrsVo> collect = attrGroupList.stream().map(attrGroupEntity -> {
AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo);
// 查询所有属性
List<AttrEntity> attrList = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId());
attrGroupWithAttrsVo.setAttrs(attrList);
return attrGroupWithAttrsVo;
}).collect(Collectors.toList());
return collect;
}

新增商品

Vo

准备一个Vo接收JSON数据,使用BEJSON网站下的转java实体类工具,然后下载即可

真不戳!

SpuInfoController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
@Autowired
private SpuInfoService spuInfoService;

/**
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("product:spuinfo:save")
public R save(@RequestBody SpuSaveVo spuSaveVo) {
// spuInfoService.save(spuInfo);
spuInfoService.saveSpuVo(spuSaveVo);
return R.ok();
}
}

远程服务

因为涉及到coupon模块,需要远程调用

  1. 开启服务

    @EnableDiscoveryClient // 被调用服务application上,开启nacos注册
    @EnableFeignClients(basePackages = “com.atguigu.gulimall.product.feign”) //feign

  2. TO : 由于两个服务之间需要公用一种对象,使用TO传输对象,可以写在common公共模块中

  3. 编写接口

远程服务接口

1
2
3
4
5
6
7
8
9
10
11
12
@FeignClient("gulimall-coupon")    // 声明调用的服务
public interface CouponFeignService {

/**
* 1 先将远程电调用服务方法的对象转为JSON
* 2 请求coupon服务中的save请求,JSON存放在请求体中
* 3 收到请求,将请求体中的JSON转为请求方法的参数
* (@ResponseBody方JSON和对象能够互相转换)
*/
@PostMapping("/coupon/spubounds/save")
R saveSpuBound(@RequestBody SpuBoundTo spuBoundTo);
}

SpuInfoServiceImpl

因为这个商品Vo涉及表众多,还有关联到远程调用,所以代码量较长。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
@Autowired
SpuInfoDescService spuInfoDescService;
@Autowired
SpuImagesService spuImagesService;
@Autowired
ProductAttrValueService productAttrValueService;
@Autowired
AttrService attrService;
@Autowired
SkuInfoService skuInfoService;
@Autowired
SkuImagesService skuImagesService;
@Autowired
SkuSaleAttrValueService skuSaleAttrValueService;
@Autowired
CouponFeignService couponFeignService;
/**
* 保存saveSpuVoInfo,前端接收的一个对象,涉及多个表
*
* @param spuSaveVo
*/
@Transactional
@Override
public void saveSpuVoInfo(SpuSaveVo spuSaveVo) {
// 保存spu info: spu_info
SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
BeanUtils.copyProperties(spuSaveVo, spuInfoEntity);
spuInfoEntity.setCreateTime(new Date());
spuInfoEntity.setUpdateTime(new Date());
this.save(spuInfoEntity);

// 保存spu info_desc: spu_info_desc
List<String> decrypts = spuSaveVo.getDecript();
SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
spuInfoDescEntity.setSpuId(spuInfoEntity.getId()); // 保存之后会将id自动封装到infoEntity
spuInfoDescEntity.setDecript(String.join(",", decrypts)); // 使用,拼接
spuInfoDescService.save(spuInfoDescEntity);

// 保存spu images: spu_images
List<String> images = spuSaveVo.getImages();
List<SpuImagesEntity> spuImagesList = images.stream().map(image -> {
SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
spuImagesEntity.setSpuId(spuInfoEntity.getId());
spuImagesEntity.setImgUrl(image);
return spuImagesEntity;
}).collect(Collectors.toList());
spuImagesService.saveBatch(spuImagesList);

// 保存spu product_attr_value: product_attr_valu
List<BaseAttrs> baseAttrs = spuSaveVo.getBaseAttrs();
List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
ProductAttrValueEntity attrValueEntity = new ProductAttrValueEntity();
attrValueEntity.setAttrId(attr.getAttrId());
attrValueEntity.setSpuId(spuInfoEntity.getId());
attrValueEntity.setAttrName(attrService.getById(attr.getAttrId()).getAttrName());
attrValueEntity.setAttrValue(attr.getAttrValues());
attrValueEntity.setQuickShow(attr.getShowDesc());
return attrValueEntity;
}).collect(Collectors.toList());
productAttrValueService.saveBatch(collect);

// 保存spu 积分信息: sms_spu_bounds


// 保存sku对应sku_info: sku_info、sku_images、sku_sale_attr_value、sms_sku_ladder、sms_sku_full_reduction、sms_member_price
List<Skus> skus = spuSaveVo.getSkus();
if (skus != null && skus.size() > 0) {
skus.forEach(sku -> {
// 1 保存基本信息
SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
BeanUtils.copyProperties(sku, skuInfoEntity);
skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
skuInfoEntity.setSaleCount(0L);
skuInfoEntity.setSpuId(spuInfoEntity.getId());
String defaultImg = "";
for (Images img : sku.getImages()) { // 这里用for而不是foreach,注意看报错的原因,lamda表达式中的变量要final
if (img.getDefaultImg() == 1) {
defaultImg = img.getImgUrl();
}
}
skuInfoEntity.setSkuDefaultImg(defaultImg);
skuInfoService.save(skuInfoEntity);
Long skuId = skuInfoEntity.getSkuId(); // 必须是保存完之后才能获得

// 2 保存图片
List<SkuImagesEntity> skuImagesList = sku.getImages().stream().map(img -> {
SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
skuImagesEntity.setSkuId(skuId);
skuImagesEntity.setImgUrl(img.getImgUrl());
skuImagesEntity.setDefaultImg(img.getDefaultImg());
return skuImagesEntity;
}).collect(Collectors.toList());
skuImagesService.saveBatch(skuImagesList);

// 3 保存属性
List<Attr> attrList = sku.getAttr();
List<SkuSaleAttrValueEntity> skuSaleAttrValueList = attrList.stream().map(attr -> {
SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
BeanUtils.copyProperties(attr, skuSaleAttrValueEntity);
skuSaleAttrValueEntity.setSkuId(skuId);
return skuSaleAttrValueEntity;
}).collect(Collectors.toList());
skuSaleAttrValueService.saveBatch(skuSaleAttrValueList);

// 以下需要远程服务
// 4 保存优惠
Bounds bounds = spuSaveVo.getBounds();
SpuBoundTo spuBoundTo = new SpuBoundTo();
BeanUtils.copyProperties(bounds, spuBoundTo);
spuBoundTo.setSpuId(spuInfoEntity.getId());
R saveSpuBound = couponFeignService.saveSpuBound(spuBoundTo);
if (saveSpuBound.getCode() != 0) {
log.error("远程保存spu积分信息失败");

}
// 5 保存满减
SkuReductionTo skuReductionTo = new SkuReductionTo();
BeanUtils.copyProperties(sku, skuReductionTo);
skuReductionTo.setSkuId(skuId);
R saveSkuReduction = couponFeignService.saveSkuReduction(skuReductionTo);
if (saveSkuReduction.getCode() != 0) {
log.error("远程保存spu积满减信息失败");
}
});
}
}
}

SkuFullReductionServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
@Autowired
SkuLadderService skuLadderService;
@Autowired
MemberPriceService memberPriceService;
@Override
public void saveSkuReduction(SkuReductionTo skuReductionTo) {
// 保存ladder
SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
skuLadderService.save(skuLadderEntity);
// 保存满减
SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
this.save(skuFullReductionEntity);
// 保存会员价格
List<MemberPriceTo> memberPriceToList = skuReductionTo.getMemberPrice();
List<MemberPriceEntity> memberPriceList = memberPriceToList.stream().map(memberPriceTo -> {
MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
memberPriceEntity.setMemberLevelId(memberPriceTo.getId());
memberPriceEntity.setMemberLevelName(memberPriceTo.getName());
memberPriceEntity.setMemberPrice(memberPriceTo.getPrice());
memberPriceEntity.setAddOther(1);
return memberPriceEntity;
}).collect(Collectors.toList());
memberPriceService.saveBatch(memberPriceList);
}
}

debug & test

妈耶一次成功有点开心

TODO: 失败的情况没有编写

商品维护—SPU管理(后端)

时间戳格式化

1
2
3
4
spring:
jackson:
# 时间戳格式化
date-format: yyyy-MM-dd HH:mm:ss

SPU待条件检索

1
2
3
4
5
6
7
8
9
/**
* 列表
*/
@RequestMapping("/list")
//@RequiresPermissions("product:spuinfo:list")
public R list(@RequestParam Map<String, Object> params) {
PageUtils page = spuInfoService.queryPageByCondition(params);
return R.ok().put("page", page);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
*待条件查询
*/
@Override
public PageUtils queryPageByCondition(Map<String, Object> params) {
QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();

String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
wrapper.and((w) -> {
w.eq("id", key).or().like("spu_name", key);
});
}
String status = (String) params.get("status");
if (!StringUtils.isEmpty(status)) {
wrapper.eq("publish_status", status);
}
String brandId = (String) params.get("brandId");
if (!StringUtils.isEmpty(brandId)) {
wrapper.eq("brand_id", brandId);
}
String catelogId = (String) params.get("catelogId");
if (!StringUtils.isEmpty(catelogId)) {
wrapper.eq("catalog_id", catelogId);
}

IPage<SpuInfoEntity> page = this.page(
new Query<SpuInfoEntity>().getPage(params),
wrapper
);

return new PageUtils(page);
}

SKU检索

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
@Autowired
private SkuInfoService skuInfoService;
/**
* 列表
*/
@RequestMapping("/list")
//@RequiresPermissions("product:skuinfo:list")
public R list(@RequestParam Map<String, Object> params) {
PageUtils page = skuInfoService.queryPageByCondition(params);
return R.ok().put("page", page);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@Override
public PageUtils queryPageByCondition(Map<String, Object> params) {
QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
wrapper.and((w) -> {
w.eq("sku_id", key).or().like("sku_name", key);
});
}
String brandId = (String) params.get("brandId");
if (!StringUtils.isEmpty(brandId)) {
wrapper.eq("brand_id", brandId);
}
String catelogId = (String) params.get("catelogId");
if (!StringUtils.isEmpty(catelogId)) {
wrapper.eq("catalog_id", catelogId);
}
String min = (String) params.get("min");
if (!StringUtils.isEmpty(min)) {
wrapper.ge("price", min);
}
String max = (String) params.get("max");
if (!StringUtils.isEmpty(max)) {
try {
BigDecimal bigDecimal = new BigDecimal(max);
if (bigDecimal.compareTo(new BigDecimal(0)) == 1) {
wrapper.le("price", max);
}
} catch (Exception e) {
}
}

IPage<SkuInfoEntity> page = this.page(
new Query<SkuInfoEntity>().getPage(params),
wrapper
);
return new PageUtils(page);
}

库存系统—仓库维护(后端)

仓库相关表:gulimall_wms_xxx

服务:ware

服务基础搭建

  1. application.yml中配置nacos、application name

  2. 注解开启nacos(@EnableDiscoveryClient)(com.alibaba.nacos.api.exception.NacosException: java.lang.reflect.InvocationTargetException这个报错是因为没有配置bootstrap)

  3. 配置日志

    1
    2
    3
    4
    # 用于输出日志
    logging:
    level:
    com.atguigu: debug
  4. 配置网关

  5. 配置mybatis分页插件

获得仓库列表带条件

修改一下原来的queryPage即可,添加上condition

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* 模糊查询
* @param params
* @return
*/
@Override
public PageUtils queryPageByCondition(Map<String, Object> params) {
QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)) {
// and拼接,因为可能id为0所以已经含有了一个eq条件
wrapper.eq("id", key).or().like("name", key).or().like("address", key);
}
IPage<WareInfoEntity> page = this.page(
new Query<WareInfoEntity>().getPage(params),
wrapper
);
return new PageUtils(page);
}

库存系统—商品库存(后端)

获得库存列表带条件

同样

库存系统—采购需求(后端)

获得需求列表带条件

同样

获得未领取的采购单列表

将多个采购需求合并到一个采购单当中(并且该采购单未被领取)

PurchaseController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
@Autowired
private PurchaseService purchaseService;

/**
* 获取所有未被领取(或新建)的采购单
*/
@RequestMapping("/unreceive/list")
//@RequiresPermissions("ware:purchase:list")
public R unreceiveList(@RequestParam Map<String, Object> params) {
PageUtils page = purchaseService.queryPageUnreceive(params);
return R.ok().put("page", page);
}

PurchaseServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
/**
* 获取所有未被领取(或新建)的采购单
*/
@Override
public PageUtils queryPageUnreceive(Map<String, Object> params) {
QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("status", 0).or().eq("status", 1);

IPage<PurchaseEntity> page = this.page(
new Query<PurchaseEntity>().getPage(params),
queryWrapper
);

return new PageUtils(page);
}

合并采购单

如果前端没有传递采购单的id那么就新建一个采购单

WareConstant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class WareConstant {
public enum PurchaseStatusEnum {
CREATED(0, "新建"), ASSIGN(1, "已分配"), RECEIVE(2, "已领取"), FINISHED(3, "已完成"), HASERROR(4, "异常");
private int code;
private String msg;
PurchaseStatusEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
public enum PurchaseDetailStatusEnum {
CREATED(0, "新建"), ASSIGN(1, "已分配"), BUYYING(2, "正在采购"), FINISHED(3, "已完成"), HASERROR(4, "异常");
private int code;
private String msg;

PurchaseDetailStatusEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}

public int getCode() {
return code;
}

public String getMsg() {
return msg;
}
}
}

MergeVo

1
2
3
4
5
6
7
8
/**
* 合并采购单请求对象
*/
@Data
public class MergeVo {
private Long purchaseId;
private List<Long> items;
}

PurchaseController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
@Autowired
private PurchaseService purchaseService;

/**
* 合并采购单
*/
@PostMapping("/merge")
public R merge(@RequestBody MergeVo mergeVo) { //@RequestBody请求体中的JSON,@RequestParam是url?后跟的键值对,@PathVariable是REST风格url中的{}变量
purchaseService.mergePurchase(mergeVo);
return R.ok()
}

PurchaseServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 合并采购单
*/
@Transactional
@Override
public void mergePurchase(MergeVo mergeVo) {
Long purchaseId = mergeVo.getPurchaseId();
if (purchaseId == null) {
// 没有采购单id,那么需要新建
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
this.save(purchaseEntity);
purchaseId = purchaseEntity.getId();
}
List<Long> items = mergeVo.getItems();
Long finalPurchaseId = purchaseId;
// 将采购需求id集合对应对象修改状态和采购单id
List<PurchaseDetailEntity> detailEntities = items.stream().map(detailId -> {
PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
detailEntity.setId(detailId);
detailEntity.setPurchaseId(finalPurchaseId);
detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGN.getCode());
return detailEntity;
}).collect(Collectors.toList());
detailService.updateBatchById(detailEntities);
}

领取采购单

PurchaseController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
@Autowired
private PurchaseService purchaseService;

/**
* 领取采购单
*/
@PostMapping("/received")
public R received(@RequestBody List<Long> ids) {
purchaseService.receivedByIds(ids);
return R.ok();
}

PurchaseServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 领取采购单
*/
@Override
public void receivedByIds(List<Long> ids) {
// 判断是否为新建或者已分配(给当前用户,但是这里涉及权限)
// 改变采购单状态
List<PurchaseEntity> purchaseEntityList = ids.stream().map(id -> {
return this.getById(id);
}).filter(item -> {
Integer status = item.getStatus();
return (status == WareConstant.PurchaseStatusEnum.ASSIGN.getCode()) || status == WareConstant.PurchaseStatusEnum.CREATED.getCode();
}).map(purchaseEntity -> {
purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
return purchaseEntity;
}).collect(Collectors.toList());
this.updateBatchById(purchaseEntityList);
// 改变采购项状态
purchaseEntityList.forEach(purchaseEntity -> {
List<PurchaseDetailEntity> detailEntityList = detailService.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", purchaseEntity.getId()));
List<PurchaseDetailEntity> list = detailEntityList.stream().map(detail -> {
PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
detailEntity.setId(detail.getId());
detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGN.getCode());
return detailEntity;
}).collect(Collectors.toList());
detailService.updateBatchById(list);
});
}

完成采购

自动修改对应采购内容的库存数

PurchaseDoneItemVo

1
2
3
4
5
6
7
8
9
/**
* 完成采购单的请求item
*/
@Data
public class PurchaseDoneItemVo {
private Long itemId;
private Integer status;
private String reason;
}

PurchaseFinishedVo

1
2
3
4
5
6
7
8
9
10
11
12
@Data
public class PurchaseFinishedVo {
/*
*采购单id
*/
@NotNull
private Long id;
/*
*采购项
*/
private List<PurchaseDoneItemVo> items;
}

PurchaseController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
@Autowired
private PurchaseService purchaseService;

/**
* 完成采购单
*/
@PostMapping("/done")
public R finish(@RequestBody PurchaseFinishedVo finishedVo) {
purchaseService.finishPurchase(finishedVo);
return R.ok();
}

PurchaseServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* 完成采购单
*/
@Override
public void finishPurchase(PurchaseFinishedVo finishedVo) {
// 1 改变采购项状态
boolean purchaseFinished = true;
List<PurchaseDetailEntity> updatedDetailList = new ArrayList<>();
List<PurchaseDoneItemVo> doneItemVoList = finishedVo.getItems();
for (PurchaseDoneItemVo doneItemVo : doneItemVoList) {
PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
if (doneItemVo.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
// 如果有一个采购项异常,则采购单状态未完成
purchaseFinished = false;
} else {
// 3 将成功采购的采购项修改库存信息
PurchaseDetailEntity detail = detailService.getById(doneItemVo.getItemId());
wareSkuService.addStock(detail.getSkuId(), detail.getWareId(), detail.getSkuNum());
}
// TODO 判断采购数是否完成
detailEntity.setStatus(doneItemVo.getStatus());
detailEntity.setId(doneItemVo.getItemId());
updatedDetailList.add(detailEntity);
}
detailService.updateBatchById(updatedDetailList);

// 2 改变采购单状态(所有采购项都完成后才可完成,否则为异常)
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setId(finishedVo.getId());
if (purchaseFinished) {
purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISHED.getCode());
} else {
purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.HASERROR.getCode());
}
this.updateById(purchaseEntity);

}

WareSkuServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
*将成功采购的采购项修改库存信息
*/
@Override
public void addStock(Long skuId, Long wareId, Integer skuNum) {
// 判断是否有该库存,没有则新增
Integer count = baseMapper.selectCount(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
if (count == 0) {
WareSkuEntity wareSkuEntity = new WareSkuEntity();
wareSkuEntity.setSkuId(skuId);
wareSkuEntity.setWareId(wareId);
wareSkuEntity.setStock(skuNum);
baseMapper.insert(wareSkuEntity);
} else
baseMapper.addStock(skuId, wareId, skuNum);
}

WareSkuDao

1
2
3
4
5
6
7
<!-- 将成功采购的采购项修改库存信息 -->
<update id="addStock">
UPDATE gulimall_wms.wms_ware_sku
SET stock = stock + #{skuNum}
WHERE sku_id = #{skuId}
AND ware_id = #{wareId}
</update>

商品维护—SKU规格维护(后端)

这是一个按钮触发的页面,具体解决方案如下

谷粒商城踩坑汇总(分布式基础(全栈开发篇)) - 大厨无盐煮 - 博客园 (cnblogs.com)

获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;

@Autowired
private ProductAttrValueService valueService;

/**
* 查出商品的规格属性
*/
@GetMapping("/base/listforspu/{spuId}")
public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
List<ProductAttrValueEntity> data = valueService.baseAttrListForSpu(spuId);
return R.ok().put("data", data);
}
1
2
3
4
5
6
@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
@Override
public List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId) {
return baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
}

修改

1
2
3
4
5
6
7
8
9
10
11
12
@RestController
@RequestMapping("product/attr")
public class AttrController {
/**
* 修改,使用AttrRequestVo
*/
@PostMapping("/update/{spuId}")
public R updateSpuAttr(@PathVariable("spuId") String spuId,
@RequestBody List<ProductAttrValueEntity> entities) {
valueService.updateSpuAttr(spuId, entities);
return R.ok();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 修改SPU的ATTr
*/
@Transactional
@Override
public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
List<ProductAttrValueEntity> collect = entities.stream().map(item -> {
item.setSpuId(spuId);
return item;
}).collect(Collectors.toList());
this.saveBatch(collect);
}

分布式基础篇—完结撒花🎉

image-20211031153716835

评论




🧡💛💚💙💜🖤🤍