feature(uniapp商品): 商品加载

This commit is contained in:
luowenfeng 2022-08-31 18:52:26 +08:00
parent 4a39f2d9f8
commit 4207b2b61d
11 changed files with 187 additions and 133 deletions

View File

@ -5,8 +5,12 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.*;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryRespVO;
import cn.iocoder.yudao.module.product.convert.brand.ProductBrandConvert;
import cn.iocoder.yudao.module.product.convert.category.ProductCategoryConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
import cn.iocoder.yudao.module.product.service.brand.ProductBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@ -19,6 +23,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@ -74,4 +79,14 @@ public class ProductBrandController {
return success(ProductBrandConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/list")
@ApiOperation("获得品牌列表")
@PreAuthorize("@ss.hasPermission('product:brand:query')")
public CommonResult<List<ProductBrandRespVO>> getProductCategoryList(@Valid ProductBrandListReqVO listVO) {
List<ProductBrandDO> list = brandService.getBrandList(listVO);
list.sort(Comparator.comparing(ProductBrandDO::getSort));
return success(ProductBrandConvert.INSTANCE.convertList(list));
}
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.yudao.module.product.controller.admin.brand.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel(value = "管理后台 - 商品品牌分页 Request VO")
@Data
public class ProductBrandListReqVO {
@ApiModelProperty(value = "品牌名称", example = "芋道")
private String name;
}

View File

@ -40,7 +40,7 @@ public class ProductSkuDO extends BaseDO {
private String name;
/**
* SPU 编号
*
* <p>
* 关联 {@link ProductSpuDO#getId()}
*/
private Long spuId;
@ -71,7 +71,7 @@ public class ProductSkuDO extends BaseDO {
private String picUrl;
/**
* SKU 状态
*
* <p>
* 枚举 {@link CommonStatusEnum}
*/
private Integer status;
@ -100,13 +100,13 @@ public class ProductSkuDO extends BaseDO {
/**
* 属性编号
*
* <p>
* 关联 {@link ProductPropertyDO#getId()}
*/
private Long propertyId;
/**
* 属性值编号
*
* <p>
* 关联 {@link ProductPropertyValueDO#getId()}
*/
private Long valueId;

View File

@ -3,10 +3,14 @@ package cn.iocoder.yudao.module.product.dal.mysql.brand;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandBaseVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandListReqVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandPageReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ProductBrandMapper extends BaseMapperX<ProductBrandDO> {
@ -18,4 +22,9 @@ public interface ProductBrandMapper extends BaseMapperX<ProductBrandDO> {
.orderByDesc(ProductBrandDO::getId));
}
default List<ProductBrandDO> selectList(ProductBrandListReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<ProductBrandDO>()
.likeIfPresent(ProductBrandDO::getName, reqVO.getName()));
}
}

View File

@ -1,9 +1,7 @@
package cn.iocoder.yudao.module.product.service.brand;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
import javax.validation.Valid;
@ -55,6 +53,20 @@ public interface ProductBrandService {
*/
List<ProductBrandDO> getBrandList(Collection<Long> ids);
/**
* 获得品牌列表
* @param listVo 请求参数
* @return 品牌列表
*/
List<ProductBrandDO> getBrandList(ProductBrandListReqVO listVo);
/**
* 验证选择的商品分类是否合法
*
* @param id 分类编号
*/
void validateProductBrand(Long id);
/**
* 获得品牌分页
*

View File

@ -1,12 +1,11 @@
package cn.iocoder.yudao.module.product.service.brand;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.ProductBrandUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.brand.vo.*;
import cn.iocoder.yudao.module.product.convert.brand.ProductBrandConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.brand.ProductBrandDO;
import cn.iocoder.yudao.module.product.dal.mysql.brand.ProductBrandMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -16,6 +15,7 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_BRAND_NOT_EXISTS;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PRODUCT_CATEGORY_LEVEL;
/**
* 品牌 Service 实现类
@ -71,6 +71,18 @@ public class ProductBrandServiceImpl implements ProductBrandService {
return brandMapper.selectBatchIds(ids);
}
@Override
public List<ProductBrandDO> getBrandList(ProductBrandListReqVO listVo) {
return brandMapper.selectList(listVo);
}
@Override
public void validateProductBrand(Long id) {
if(getBrand(id) == null){
throw exception(PRODUCT_BRAND_NOT_EXISTS);
}
}
@Override
public PageResult<ProductBrandDO> getBrandPage(ProductBrandPageReqVO pageReqVO) {
return brandMapper.selectPage(pageReqVO);

View File

@ -140,7 +140,8 @@ public class ProductSkuServiceImpl implements ProductSkuService {
@Override
public List<ProductSkuDO> getSkusBySpuId(Long spuId) {
return productSkuMapper.selectBySpuIds(Collections.singletonList(spuId));
List<ProductSkuDO> productSkuDOS = productSkuMapper.selectBySpuIds(Collections.singletonList(spuId));
return productSkuDOS;
}
@Override

View File

@ -20,6 +20,7 @@ import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.iocoder.yudao.module.product.dal.mysql.spu.ProductSpuMapper;
import cn.iocoder.yudao.module.product.enums.spu.ProductSpuSpecTypeEnum;
import cn.iocoder.yudao.module.product.service.brand.ProductBrandService;
import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
import cn.iocoder.yudao.module.product.service.property.ProductPropertyService;
import cn.iocoder.yudao.module.product.service.sku.ProductSkuService;
@ -55,17 +56,19 @@ public class ProductSpuServiceImpl implements ProductSpuService {
@Resource
private ProductPropertyService productPropertyService;
@Resource
private ProductBrandService brandService;
@Override
@Transactional
public Long createProductSpu(ProductSpuCreateReqVO createReqVO) {
// 校验分类
categoryService.validateProductCategory(createReqVO.getCategoryId());
// TODO @校验品牌
// 校验品牌
brandService.validateProductBrand(createReqVO.getBrandId());
// 校验SKU
List<ProductSkuCreateOrUpdateReqVO> skuCreateReqList = createReqVO.getSkus();
productSkuService.validateProductSkus(skuCreateReqList, createReqVO.getSpecType());
// 插入 SPU
ProductSpuDO spu = ProductSpuConvert.INSTANCE.convert(createReqVO);
spu.setMarketPrice(CollectionUtils.getMaxValue(skuCreateReqList, ProductSkuCreateOrUpdateReqVO::getMarketPrice));
@ -73,7 +76,6 @@ public class ProductSpuServiceImpl implements ProductSpuService {
spu.setMinPrice(CollectionUtils.getMinValue(skuCreateReqList, ProductSkuCreateOrUpdateReqVO::getPrice));
spu.setTotalStock(CollectionUtils.getSumValue(skuCreateReqList, ProductSkuCreateOrUpdateReqVO::getStock, Integer::sum));
ProductSpuMapper.insert(spu);
// 插入 SKU
productSkuService.createProductSkus(skuCreateReqList, spu.getId());
// 返回
@ -128,27 +130,29 @@ public class ProductSpuServiceImpl implements ProductSpuService {
spuVO.setSkus(skuReqs);
List<ProductSkuRespVO.Property> properties = new ArrayList<>();
// 组合 sku 规格属性
for (ProductSkuRespVO productSkuRespVO : skuReqs) {
properties.addAll(productSkuRespVO.getProperties());
}
Map<Long, List<ProductSkuBaseVO.Property>> propertyMaps = properties.stream().collect(Collectors.groupingBy(ProductSkuBaseVO.Property::getPropertyId));
List<ProductPropertyRespVO> propertyAndValueList = productPropertyService.selectByIds(new ArrayList<>(propertyMaps.keySet()));
// 装载组装过后的属性
List<ProductPropertyViewRespVO> productPropertyViews = new ArrayList<>();
propertyAndValueList.forEach(p -> {
ProductPropertyViewRespVO productPropertyViewRespVO = new ProductPropertyViewRespVO();
productPropertyViewRespVO.setPropertyId(p.getId());
productPropertyViewRespVO.setName(p.getName());
Set<ProductPropertyViewRespVO.Tuple2> propertyValues = new HashSet<>();
Map<Long, ProductPropertyValueRespVO> propertyValueMaps = p.getPropertyValueList().stream().collect(Collectors.toMap(ProductPropertyValueRespVO::getId, pv -> pv));
propertyMaps.get(p.getId()).forEach(pv -> {
ProductPropertyViewRespVO.Tuple2 tuple2 = new ProductPropertyViewRespVO.Tuple2(pv.getValueId(), propertyValueMaps.get(pv.getValueId()).getName());
propertyValues.add(tuple2);
if(spu.getSpecType().equals(ProductSpuSpecTypeEnum.DISABLE.getType())) {
for (ProductSkuRespVO productSkuRespVO : skuReqs) {
properties.addAll(productSkuRespVO.getProperties());
}
Map<Long, List<ProductSkuBaseVO.Property>> propertyMaps = properties.stream().collect(Collectors.groupingBy(ProductSkuBaseVO.Property::getPropertyId));
List<ProductPropertyRespVO> propertyAndValueList = productPropertyService.selectByIds(new ArrayList<>(propertyMaps.keySet()));
// 装载组装过后的属性
List<ProductPropertyViewRespVO> productPropertyViews = new ArrayList<>();
propertyAndValueList.forEach(p -> {
ProductPropertyViewRespVO productPropertyViewRespVO = new ProductPropertyViewRespVO();
productPropertyViewRespVO.setPropertyId(p.getId());
productPropertyViewRespVO.setName(p.getName());
Set<ProductPropertyViewRespVO.Tuple2> propertyValues = new HashSet<>();
Map<Long, ProductPropertyValueRespVO> propertyValueMaps = p.getPropertyValueList().stream().collect(Collectors.toMap(ProductPropertyValueRespVO::getId, pv -> pv));
propertyMaps.get(p.getId()).forEach(pv -> {
ProductPropertyViewRespVO.Tuple2 tuple2 = new ProductPropertyViewRespVO.Tuple2(pv.getValueId(), propertyValueMaps.get(pv.getValueId()).getName());
propertyValues.add(tuple2);
});
productPropertyViewRespVO.setPropertyValues(propertyValues);
productPropertyViews.add(productPropertyViewRespVO);
});
productPropertyViewRespVO.setPropertyValues(propertyValues);
productPropertyViews.add(productPropertyViewRespVO);
});
spuVO.setProductPropertyViews(productPropertyViews);
spuVO.setProductPropertyViews(productPropertyViews);
}
// 组合分类
if (null != spuVO.getCategoryId()) {
LinkedList<Long> categoryArray = new LinkedList<>();

View File

@ -34,6 +34,15 @@ export function getBrand(id) {
})
}
// 获得品牌list
export function getBrandList() {
return request({
url: '/product/brand/list',
method: 'get'
})
}
// 获得品牌分页
export function getBrandPage(query) {
return request({

View File

@ -179,7 +179,7 @@
<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body destroy-on-close :close-on-click-modal="false" >
<save @closeDialog="open = false; getList()"/>
<save @closeDialog="closeDialog" :type="dialogType" :obj="dialogObj" v-if="open" />
</el-dialog>
</div>
</template>
@ -257,6 +257,10 @@ export default {
title: "",
//
open: false,
//
dialogType: "add",
//
dialogObj:{},
dateRangeCreateTime: [],
//
queryParams: {
@ -570,75 +574,25 @@ export default {
},
/** 新增按钮操作 */
handleAdd() {
this.reset();
this.dialogType = "add";
this.dialogObj={};
this.open = true;
this.title = "添加商品spu";
this.getPropertyPageList();
},
/** 修改按钮操作 */
handleUpdate(row) {
this.reset();
const id = row.id;
getSpu(id).then((response) => {
let dataSpu = response.data;
this.form = {
id: dataSpu.id,
name: dataSpu.name,
sellPoint: dataSpu.sellPoint,
description: dataSpu.sellPoint,
categoryId: dataSpu.sellPoint,
categoryIds: dataSpu.categoryIds,
picUrls: dataSpu.picUrls,
sort: dataSpu.sort,
likeCount: dataSpu.likeCount,
price: dataSpu.price,
quantity: dataSpu.quantity,
status: dataSpu.status,
isShowTagInput: undefined,
skus: [],
skusList: dataSpu.skus,
productPropertyViews: dataSpu.productPropertyViews,
// skus:dataSpu.productSkuRespVOS,
};
this.getDataHandle();
this.open = true;
this.title = "修改商品spu";
});
this.dialogType = "upd";
this.dialogObj.id = row.id;
this.open = true;
console.log("修改")
this.title = "修改商品spu";
},
getDataHandle() {
let that = this;
let productPropertyViews = JSON.parse(
JSON.stringify(this.form.productPropertyViews)
);
productPropertyViews = productPropertyViews.sort(
(a, b) => a.propertyId - b.propertyId
);
productPropertyViews.forEach((item) => {
item.propertyValues = item.propertyValues.sort((a, b) => a.v1 - b.v1);
});
let skuIds = [];
for (let i = 0; i < productPropertyViews.length; i++) {
let han = {
name: productPropertyViews[i].name,
propertyId: productPropertyViews[i].propertyId,
selectValues: [],
selectValueIds: [],
};
for (
let j = 0;
j < productPropertyViews[i].propertyValues.length;
j++
) {
han.selectValues.push(productPropertyViews[i].propertyValues[j].v2);
han.selectValueIds.push(productPropertyViews[i].propertyValues[j].v1);
}
skuIds.push(han);
}
this.skuTags = skuIds;
this.unUseTags = this.allhistoryTags.filter((v) =>
skuIds.every((val) => val.name != v.name)
);
this.getHandleTable();
closeDialog(){
console.log("关闭")
this.dialogType = "add";
this.dialogObj={};
this.open = false;
this.getList()
},
getHandleTable() {
this.form.skus = [];
@ -699,33 +653,6 @@ export default {
}
});
},
/** 提交按钮 */
submitForm() {
this.$refs["form"].validate((valid) => {
if (!valid) {
return;
}
this.form.picUrls = this.form.picUrls.split(",");
this.form.categoryId =
this.form.categoryIds[this.form.categoryIds.length - 1];
this.form.status = Number(this.form.status);
//
if (this.form.id != null) {
updateSpu(this.form).then((response) => {
this.$modal.msgSuccess("修改成功");
this.open = false;
this.getList();
});
return;
}
//
createSpu(this.form).then((response) => {
this.$modal.msgSuccess("新增成功");
this.open = false;
this.getList();
});
});
},
/** 删除按钮操作 */
handleDelete(row) {
const id = row.id;

View File

@ -193,6 +193,16 @@
<!-- 销售设置 -->
<el-tab-pane label="销售设置" name="fourth">
<el-form ref="fourth" :model="baseForm" :rules="rules" label-width="100px" style="width: 95%">
<el-form-item label="所属品牌" prop="brandId">
<el-select v-model="baseForm.brandId" placeholder="请选择">
<el-option
v-for="item in brandList"
:key="item.id"
:label="item.name"
:value="item.id">
</el-option>
</el-select>
</el-form-item>
<el-form-item label="虚拟销量" prop="virtualSalesCount">
<el-input v-model="baseForm.virtualSalesCount" placeholder="请输入虚拟销量" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"/>
</el-form-item>
@ -221,8 +231,10 @@
</template>
<script>
import {getBrandList} from "@/api/mall/product/brand";
import {getProductCategoryList} from "@/api/mall/product/category";
import {createSpu,} from "@/api/mall/product/spu";
import {createSpu, getSpu} from "@/api/mall/product/spu";
import {getPropertyPage,} from "@/api/mall/product/property";
import Editor from "@/components/Editor";
import ImageUpload from "@/components/ImageUpload";
@ -232,6 +244,13 @@ export default {
Editor,
ImageUpload
},
props:{//props
type:{
type:String,
default:"add" //
},
obj: Object
},
data() {
return {
activeName: "base",
@ -251,6 +270,7 @@ export default {
status: 0,
virtualSalesCount: 0,
showStock: true,
brandId: null
},
categoryList: [],
//
@ -270,6 +290,7 @@ export default {
// },
],
propertyPageList: [],
brandList: [],
specValue: null,
//
@ -283,11 +304,13 @@ export default {
},
};
},
created() {
this.getListBrand();
this.getListCategory();
this.getPropertyPageList();
if(this.type == 'upd'){
this.updateType(this.obj.id)
}
},
methods: {
removeSpec(index){
@ -351,6 +374,13 @@ export default {
getProductCategoryList().then((response) => {
this.categoryList = this.handleTree(response.data, "id", "parentId");
});
},
/** 查询品牌列表 */
getListBrand() {
//
getBrandList().then((response) => {
this.brandList = response.data;
});
},
cancel() {
this.$emit("closeDialog");
@ -366,7 +396,13 @@ export default {
rates.forEach(r => {
let properties = []
Array.of(r.spec).forEach(s => {
Array.of(s).forEach((v, i) => {
let obj;
if (s instanceof Array) {
obj = s;
}else{
obj = Array.of(s);
}
obj.forEach((v, i) => {
console.log(this.dynamicSpec, r, s, v, i)
let specValue = this.dynamicSpec[i].specValue.find(o => o.name == v);
console.log(specValue)
@ -382,9 +418,6 @@ export default {
rates[0].name = this.baseForm.name;
rates[0].status = this.baseForm.status;
}
console.log(rates)
let form = this.baseForm
if(form.picUrls instanceof Array){
form.picUrls = form.picUrls.flatMap(m=>m.split(','))
@ -422,6 +455,24 @@ export default {
spec.specValue = obj.propertyValueList;
this.dynamicSpec = dynamicSpec;
this.buildRatesFormRates();
},
updateType(id){
getSpu(id).then((response) =>{
console.log(response)
let data = response.data;
this.baseForm.name=data.name;
this.baseForm.sellPoint=data.sellPoint;
this.baseForm.categoryIds=data.categoryIds;
this.baseForm.sort=data.sort;
this.baseForm.description=data.description;
this.baseForm.picUrls=data.picUrls;
this.baseForm.status=data.status;
this.baseForm.virtualSalesCount=data.virtualSalesCount;
this.baseForm.showStock=data.showStock;
this.baseForm.brandId=data.brandId;
this.ratesForm.spec=data.specType;
this.ratesForm.rates=data.skus
})
}
},
};