1.实现了商家对商品的修改之商品数据回显
goods.html页面:
修改按钮改为a链接
<!--angulas静态页面传参:#?-->
<a href="goods_edit.html#?id={{entity.id}}" type="button" class="btn bg-olive btn-xs">修改</a>
跳转到goods_edit.html页面:
body标签中初始化findOne()方法
<body class="hold-transition skin-red sidebar-mini" ng-app="pinyougou" ng-controller="goodsController" ng-init="selectItemCat1List();findOne()">
goodsController.js中修改findOne()方法
//查询实体(goods_edit.html页面一加载,就执行findOne方法
$scope.findOne = function (id) {
//angular进行页面跳转后接收页面参数
id = $location.search()['id'];
// alert(id)
//新增操作不做查询
if (id != null) {
goodsService.findOne(id).success(
function (response) {
$scope.entity = response;
//将商品详情中的introduction属性赋值给富文本框
editor.html(response.tbGoodsDesc.introduction)
//将图片数据转换为JSON类型
response.tbGoodsDesc.itemImages = JSON.parse(response.tbGoodsDesc.itemImages);
//显示扩展属性(内容总是为空,因为给模板id绑定了watch方法,从重新去查一个template,只有text,没有value,会覆盖掉
response.tbGoodsDesc.customAttributeItems = JSON.parse(response.tbGoodsDesc.customAttributeItems);
//读取规格
$scope.entity.tbGoodsDesc.specificationItems= JSON.parse($scope.entity.tbGoodsDesc.specificationItems);
//准备SKU信息
for( var i=0;i<$scope.entity.tbItemList.length;i++ ){
$scope.entity.tbItemList[i].spec = JSON.parse( $scope.entity.tbItemList[i].spec);
}
}
);
}
}
改动了监听模板id变化的watch方法
//根据模板id的变化去模板表里找对应的所有品牌(type_template表里所有的brandIds)
$scope.$watch('entity.tbGoods.typeTemplateId', function (newValue, oldValue) {
typeTemplateService.findOne(newValue).success(
function (response) {
$scope.brandList = JSON.parse(response.brandIds);
//为了防止修改商品时扩展属性没有值,在这里加一个判断条件,只有是新增页面时再给模板赋值★★★★★★★★★
if ($location.search()['id'] == null) {
//根据模板id的变化去模板表里找对应的所有扩展属性(type_template表里所有的customAttributeItems)
$scope.entity.tbGoodsDesc.customAttributeItems = JSON.parse(response.customAttributeItems)
//别忘了初始化entity这个对象中的tbItemList这个集合属性(最后页面上就多显示了除了规格选项后面的部分,价格,库存,是否启用,是否默认这四个)
$scope.entity.tbItemList = [];
}
}
)
//根据模板id的变化去规格表里去找对应的所有规格和规格对应的所有选项
typeTemplateService.findSpecList(newValue).success(
function (response) {
//将返回来的所有规格和规格对应的所有选项构成的list集合
$scope.specList = response;
}
)
})
在findOne方法的下方加上checkAttributeValue方法
//根据规格名称和选项名称返回是否被勾选
$scope.checkAttributeValue = function (specName, optionName) {
var items = $scope.entity.tbGoodsDesc.specificationItems;
var object = searchObjectByKey(items, 'attributeName', specName);
if (object == null) {
return false;
} else {
if (object.attributeValue.indexOf(optionName) >= 0) {
return true;
} else {
return false;
}
}
}
并在goods_edit.html页面中的每一个规格选项的复选框上加上此方法,并传对应的参数
<!--遍历每一个规格选项-->
<span ng-repeat="option in specification.options">
<input ng-click="updateSpecAttribute($event,specification.text,option.optionName);createItemList()" type="checkbox" ng-checked="checkAttributeValue(specification.text,option.optionName)">{{option.optionName}}
</span>
后端部分:
GoodsController.java中:
@RequestMapping("/findOne")
public Goods findOne(Long id){
return goodsService.findOne(id);
}
GoodsServiceImpl.Java中:
//findOne将后台数据封装成包装类Goods
@Override
public Goods findOne(Long id) {
//获取商品TbGoods
TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
//获取商品详情TbGoodsDesc
TbGoodsDesc tbGoodsDesc = goodsDescMapper.selectByPrimaryKey(id);
TbItemExample example = new TbItemExample();
TbItemExample.Criteria criteria = example.createCriteria().andGoodsIdEqualTo(id);
//获得商品对应的SKU集合
List<TbItem> tbItems = itemMapper.selectByExample(example);
//封装
Goods goods = new Goods();
goods.setTbGoods(tbGoods);
goods.setTbGoodsDesc(tbGoodsDesc);
goods.setTbItemList(tbItems);
return goods;
}
2.实现了商家对商品的修改之提交
因为商品的添加和修改用的都是同一个界面goods_edit.html页面,所以提交按钮只能绑定一个方法,所以页面部分不做修改,保存按钮还是绑定add()方法
goodsController.js中
//保存
$scope.add = function () {
var id = $location.search()['id'];
// alert($location.search()['id'])
//判断当前页面是修改还是增加
// alert(id)//这个id添加商品时切换子页面就获取不到了
// if(id==null){//就是增加的操作
if($scope.entity.tbGoods.id == null){//就是增加的操作
//增加获取富文本的内容并赋值
$scope.entity.tbGoodsDesc.introduction = editor.html();
//提交之后清空富文本编辑框
editor.html();
goodsService.add($scope.entity).success(
function (response) {
if (response.success) {
//如果成功,清空当前$scope.entity对象
$scope.entity = {};
//添加成功的话,就跳转商品展示页面
location.href = "goods.html"
} else {
alert(response.message);
}
}
);
}else{
//就是修改操作
goodsService.update($scope.entity).success(
function (response) {
if(response.success){
//如果成功,清空当前$scope.entity对象
$scope.entity = {};
//添加成功的话,就跳转商品展示页面
location.href = "goods.html"
}else {
alert(response.message);
}
}
)
}
}
GoodsController.java中:
@RequestMapping("/update")
public Result update(@RequestBody Goods goods){
try {
goodsService.update(goods);
return new Result(true, "修改成功");
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "修改失败");
}
}
GoodsServiceImpl.java中:
/**
* 对商品进行修改操作
*/
@Override
public void update(Goods goods) {
TbGoods tbGoods = goods.getTbGoods();
// 设置未审核状态:如果是经过修改的商品,需要新审核★★★★★★★★★
tbGoods.setAuditStatus("0");
//更新商品
goodsMapper.updateByPrimaryKey(tbGoods);
//更新商品详情
TbGoodsDesc tbGoodsDesc = goods.getTbGoodsDesc();
goodsDescMapper.updateByPrimaryKey(tbGoodsDesc);
//更新SKU表(原理:先删后增)
//删
TbItemExample example = new TbItemExample();
TbItemExample.Criteria criteria = example.createCriteria().andGoodsIdEqualTo(tbGoods.getId());
itemMapper.deleteByExample(example);
//增
saveItemList(goods);
}
把添加到SKU表中数据的方法saveItemList(Goods goods)做一个封装,提取出来,提高了代码的复用性
//提取添加SKU商品表的方法
public void saveItemList(Goods goods){
//先判断goods.getTbGoods()表中isEnableSpec属性(规格)是否为启用状态
if ("1".equals(goods.getTbGoods().getIsEnableSpec())) {//把"1"写在前面,防止不点按钮值为空,出现空指针异常的错误
//给商品的SKU表添加数据
List<TbItem> tbItemList = goods.getTbItemList();
for (TbItem item : tbItemList) {
//添加SKU的商品标题(SPU+规格选项的名称)
String SPU = goods.getTbGoods().getGoodsName();
Map<String, String> map = JSON.parseObject(item.getSpec(), Map.class);
for (String key : map.keySet()) {
SPU += " " + map.get(key);
}
String SKU = SPU;
item.setTitle(SKU);
//添加商品卖点(goods.getTbGoods())
item.setSellPoint(goods.getTbGoods().getCaption());
//添加图片(采用图片列表的第一张即可)
List<Map> images = JSON.parseArray(goods.getTbGoodsDesc().getItemImages(), Map.class);
if (images.size() > 0) {
item.setImage(images.get(0).get("url").toString());
}
//添加SKU商品所属类目
item.setCategoryid(goods.getTbGoods().getCategory3Id());
//添加创建时间
item.setCreateTime(new Date());
//添加更新时间
item.setUpdateTime(new Date());
//添加goods_id(手动维护多对一的关系)
item.setGoodsId(goods.getTbGoods().getId());
//添加seller_id
item.setSellerId(goods.getTbGoods().getSellerId());
//添加分类的名称,(后面搜索时用到)
item.setCategory(itemCatMapper.selectByPrimaryKey(goods.getTbGoods().getCategory3Id()).getName());
//添加品牌的名称
String brandName = brandMapper.selectByPrimaryKey(goods.getTbGoods().getBrandId()).getName();
item.setBrand(brandName);
//添加商家的公司名
item.setSeller(sellerMapper.selectByPrimaryKey(goods.getTbGoods().getSellerId()).getName());
//别忘了将item对象存到对应的表中
itemMapper.insert(item);
}
} else {
//如果规格为不启用状态,那么就自定义一个空的item(除了Title不一样,别的都一样
TbItem item = new TbItem();
item.setTitle(goods.getTbGoods().getGoodsName());
//添加商品卖点(goods.getTbGoods())
item.setSellPoint(goods.getTbGoods().getCaption());
//添加图片(采用图片列表的第一张即可)
List<Map> images = JSON.parseArray(goods.getTbGoodsDesc().getItemImages(), Map.class);
if (images.size() > 0) {
item.setImage(images.get(0).get("url").toString());
}
//添加SKU商品所属类目
item.setCategoryid(goods.getTbGoods().getCategory3Id());
//添加创建时间
item.setCreateTime(new Date());
//添加更新时间
item.setUpdateTime(new Date());
//添加goods_id(手动维护多对一的关系)
item.setGoodsId(goods.getTbGoods().getId());
//添加seller_id
item.setSellerId(goods.getTbGoods().getSellerId());
//添加分类的名称,(后面搜索时用到)
item.setCategory(itemCatMapper.selectByPrimaryKey(goods.getTbGoods().getCategory3Id()).getName());
//添加品牌的名称
String brandName = brandMapper.selectByPrimaryKey(goods.getTbGoods().getBrandId()).getName();
item.setBrand(brandName);
//添加商家的公司名
item.setSeller(sellerMapper.selectByPrimaryKey(goods.getTbGoods().getSellerId()).getName());
//因为item不是页面传入,需要设置页面牵扯的默认值★★★★★★★★★★★★(容易忘)
item.setStatus("0");
item.setIsDefault("1");
item.setNum(9999);
item.setPrice(goods.getTbGoods().getPrice());
//别忘了将item对象存到对应的表中
itemMapper.insert(item);
}