package com.alatus.mall.product.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.alatus.mall.product.entity.ProductAttrValueEntity;
import com.alatus.mall.product.service.AttrService;
import com.alatus.mall.product.service.ProductAttrValueService;
import com.alatus.mall.product.vo.AttrRespVo;
import com.alatus.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;
/**
* 商品属性
*
* @author Alatus
* @email 1571345941@qq.com
* @date 2024-06-08 16:06:24
*/
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;
@Autowired
private ProductAttrValueService productAttrValueService;
@GetMapping("/base/listforspu/{spuId}")
public R listForSpu(@PathVariable("spuId")Long spuId){
List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.baseAttrListForSpu(spuId);
return R.ok().put("data",productAttrValueEntityList);
}
@GetMapping("/{attrType}/list/{cateLogId}")
public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("cateLogId") Long cateLogId,@PathVariable("attrType")String type){
PageUtils page = attrService.queryBaseAttrPage(params,cateLogId,type);
return R.ok().put("page",page);
}
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = attrService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{attrId}")
public R info(@PathVariable("attrId") Long attrId){
AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
return R.ok().put("attr", attrRespVo);
}
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody AttrVo attrVo){
attrService.saveAttr(attrVo);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody AttrVo attrVo){
attrService.updateAttr(attrVo);
return R.ok();
}
/**
* 修改SPU信息
*/
@RequestMapping("/update/{spuId}")
public R updateSpu(@PathVariable("spuId")Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
productAttrValueService.updateSpuAttr(spuId,entities);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] attrIds){
attrService.removeByIds(Arrays.asList(attrIds));
return R.ok();
}
}
package com.alatus.mall.product.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.alatus.mall.product.entity.ProductAttrValueEntity;
import com.alatus.mall.product.service.AttrService;
import com.alatus.mall.product.service.ProductAttrValueService;
import com.alatus.mall.product.vo.AttrRespVo;
import com.alatus.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;
/**
* 商品属性
*
* @author Alatus
* @email 1571345941@qq.com
* @date 2024-06-08 16:06:24
*/
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;
@Autowired
private ProductAttrValueService productAttrValueService;
@GetMapping("/base/listforspu/{spuId}")
public R listForSpu(@PathVariable("spuId")Long spuId){
List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.baseAttrListForSpu(spuId);
return R.ok().put("data",productAttrValueEntityList);
}
@GetMapping("/{attrType}/list/{cateLogId}")
public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("cateLogId") Long cateLogId,@PathVariable("attrType")String type){
PageUtils page = attrService.queryBaseAttrPage(params,cateLogId,type);
return R.ok().put("page",page);
}
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = attrService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{attrId}")
public R info(@PathVariable("attrId") Long attrId){
AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
return R.ok().put("attr", attrRespVo);
}
/**
* 保存
*/
@RequestMapping("/save")
public R save(@RequestBody AttrVo attrVo){
attrService.saveAttr(attrVo);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
public R update(@RequestBody AttrVo attrVo){
attrService.updateAttr(attrVo);
return R.ok();
}
/**
* 修改SPU信息
*/
@RequestMapping("/update/{spuId}")
public R updateSpu(@PathVariable("spuId")Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
productAttrValueService.updateSpuAttr(spuId,entities);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
public R delete(@RequestBody Long[] attrIds){
attrService.removeByIds(Arrays.asList(attrIds));
return R.ok();
}
}
package com.alatus.mall.product.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.alatus.common.utils.PageUtils;
import com.alatus.mall.product.entity.ProductAttrValueEntity;
import java.util.List;
import java.util.Map;
/**
* spu属性值
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:05:29
*/
public interface ProductAttrValueService extends IService<ProductAttrValueEntity> {
PageUtils queryPage(Map<String, Object> params);
void saveProductAttr(List<ProductAttrValueEntity> productAttrValueEntities);
List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId);
void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities);
}
package com.alatus.mall.product.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.alatus.common.utils.PageUtils;
import com.alatus.mall.product.entity.ProductAttrValueEntity;
import java.util.List;
import java.util.Map;
/**
* spu属性值
*
* @author alatus
* @email 1571345941@qq.com
* @date 2024-03-12 13:05:29
*/
public interface ProductAttrValueService extends IService<ProductAttrValueEntity> {
PageUtils queryPage(Map<String, Object> params);
void saveProductAttr(List<ProductAttrValueEntity> productAttrValueEntities);
List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId);
void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities);
}
package com.alatus.mall.product.service.impl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import com.alatus.mall.product.dao.ProductAttrValueDao;
import com.alatus.mall.product.entity.ProductAttrValueEntity;
import com.alatus.mall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;
@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<ProductAttrValueEntity> page = this.page(
new Query<ProductAttrValueEntity>().getPage(params),
new QueryWrapper<ProductAttrValueEntity>()
);
return new PageUtils(page);
}
@Override
public void saveProductAttr(List<ProductAttrValueEntity> productAttrValueEntities) {
this.saveBatch(productAttrValueEntities);
}
@Override
public List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId) {
List<ProductAttrValueEntity> productAttrValueEntities = this.baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
return productAttrValueEntities;
}
@Override
@Transactional
public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
// 删除这个SpuId之前对应的所有属性
this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
List<ProductAttrValueEntity> productAttrValueEntities = entities.stream().map(item -> {
item.setSpuId(spuId);
return item;
}).collect(Collectors.toList());
this.saveBatch(productAttrValueEntities);
}
}
package com.alatus.mall.product.service.impl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import com.alatus.mall.product.dao.ProductAttrValueDao;
import com.alatus.mall.product.entity.ProductAttrValueEntity;
import com.alatus.mall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;
@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<ProductAttrValueEntity> page = this.page(
new Query<ProductAttrValueEntity>().getPage(params),
new QueryWrapper<ProductAttrValueEntity>()
);
return new PageUtils(page);
}
@Override
public void saveProductAttr(List<ProductAttrValueEntity> productAttrValueEntities) {
this.saveBatch(productAttrValueEntities);
}
@Override
public List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId) {
List<ProductAttrValueEntity> productAttrValueEntities = this.baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
return productAttrValueEntities;
}
@Override
@Transactional
public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
// 删除这个SpuId之前对应的所有属性
this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
List<ProductAttrValueEntity> productAttrValueEntities = entities.stream().map(item -> {
item.setSpuId(spuId);
return item;
}).collect(Collectors.toList());
this.saveBatch(productAttrValueEntities);
}
}