data = new HashMap<>();
+ data.put("total", total);
+ data.put("items", goodsList);
+
+ return ResponseUtil.ok(data);
+ }
+
+ private Object validate(GoodsAllinone goodsAllinone) {
+ DtsGoods goods = goodsAllinone.getGoods();
+ String name = goods.getName();
+ if (StringUtils.isEmpty(name)) {
+ return ResponseUtil.badArgument();
+ }
+ String goodsSn = goods.getGoodsSn();
+ if (StringUtils.isEmpty(goodsSn)) {
+ return ResponseUtil.badArgument();
+ }
+ // 品牌商可以不设置,如果设置则需要验证品牌商存在
+ Integer brandId = goods.getBrandId();
+ if (brandId != null && brandId != 0) {
+ if (brandService.findById(brandId) == null) {
+ return ResponseUtil.badArgumentValue();
+ }
+ }
+ // 分类可以不设置,如果设置则需要验证分类存在
+ Integer categoryId = goods.getCategoryId();
+ if (categoryId != null && categoryId != 0) {
+ if (categoryService.findById(categoryId) == null) {
+ return ResponseUtil.badArgumentValue();
+ }
+ }
+
+ DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
+ for (DtsGoodsAttribute attribute : attributes) {
+ String attr = attribute.getAttribute();
+ if (StringUtils.isEmpty(attr)) {
+ return ResponseUtil.badArgument();
+ }
+ String value = attribute.getValue();
+ if (StringUtils.isEmpty(value)) {
+ return ResponseUtil.badArgument();
+ }
+ }
+
+ DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
+ for (DtsGoodsSpecification specification : specifications) {
+ String spec = specification.getSpecification();
+ if (StringUtils.isEmpty(spec)) {
+ return ResponseUtil.badArgument();
+ }
+ String value = specification.getValue();
+ if (StringUtils.isEmpty(value)) {
+ return ResponseUtil.badArgument();
+ }
+ }
+
+ DtsGoodsProduct[] products = goodsAllinone.getProducts();
+ for (DtsGoodsProduct product : products) {
+ Integer number = product.getNumber();
+ if (number == null || number < 0) {
+ return ResponseUtil.badArgument();
+ }
+
+ BigDecimal price = product.getPrice();
+ if (price == null) {
+ return ResponseUtil.badArgument();
+ }
+
+ String[] productSpecifications = product.getSpecifications();
+ if (productSpecifications.length == 0) {
+ return ResponseUtil.badArgument();
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * 编辑商品
+ *
+ * TODO
+ * 目前商品修改的逻辑是
+ * 1. 更新Dts_goods表
+ * 2. 逻辑删除Dts_goods_specification、Dts_goods_attribute、Dts_goods_product
+ * 3. 添加Dts_goods_specification、Dts_goods_attribute、Dts_goods_product
+ *
+ * 这里商品三个表的数据采用删除再添加的策略是因为
+ * 商品编辑页面,支持管理员添加删除商品规格、添加删除商品属性,因此这里仅仅更新是不可能的,
+ * 只能删除三个表旧的数据,然后添加新的数据。
+ * 但是这里又会引入新的问题,就是存在订单商品货品ID指向了失效的商品货品表。
+ * 因此这里会拒绝管理员编辑商品,如果订单或购物车中存在商品。
+ * 所以这里可能需要重新设计。
+ */
+ @Transactional
+ public Object update(GoodsAllinone goodsAllinone) {
+ Object error = validate(goodsAllinone);
+ if (error != null) {
+ return error;
+ }
+
+ DtsGoods goods = goodsAllinone.getGoods();
+ DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
+ DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
+ DtsGoodsProduct[] products = goodsAllinone.getProducts();
+
+ Integer id = goods.getId();
+ // 检查是否存在购物车商品或者订单商品
+ // 如果存在则拒绝修改商品。
+ if (orderGoodsService.checkExist(id)) {
+ logger.warn("商品已经在订单中,不能修改");
+ return ResponseUtil.fail(GOODS_UPDATE_NOT_ALLOWED, "商品已经在订单中,不能修改");
+ }
+ if (cartService.checkExist(id)) {
+ logger.warn("商品已经在购物车中,不能修改");
+ return ResponseUtil.fail(GOODS_UPDATE_NOT_ALLOWED, "商品已经在购物车中,不能修改");
+ }
+
+ //将生成的分享图片地址写入数据库
+ String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
+ goods.setShareUrl(url);
+
+ // 商品基本信息表Dts_goods
+ if (goodsService.updateById(goods) == 0) {
+ logger.error("更新数据失败");
+ throw new RuntimeException("更新数据失败");
+ }
+
+ Integer gid = goods.getId();
+ specificationService.deleteByGid(gid);
+ attributeService.deleteByGid(gid);
+ productService.deleteByGid(gid);
+
+ // 商品规格表Dts_goods_specification
+ for (DtsGoodsSpecification specification : specifications) {
+ specification.setGoodsId(goods.getId());
+ specificationService.add(specification);
+ }
+
+ // 商品参数表Dts_goods_attribute
+ for (DtsGoodsAttribute attribute : attributes) {
+ attribute.setGoodsId(goods.getId());
+ attributeService.add(attribute);
+ }
+
+ // 商品货品表Dts_product
+ for (DtsGoodsProduct product : products) {
+ product.setGoodsId(goods.getId());
+ productService.add(product);
+ }
+ qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
+
+ return ResponseUtil.ok();
+ }
+
+ @Transactional
+ public Object delete(DtsGoods goods) {
+ Integer id = goods.getId();
+ if (id == null) {
+ return ResponseUtil.badArgument();
+ }
+
+ Integer gid = goods.getId();
+ goodsService.deleteById(gid);
+ specificationService.deleteByGid(gid);
+ attributeService.deleteByGid(gid);
+ productService.deleteByGid(gid);
+ return ResponseUtil.ok();
+ }
+
+ @Transactional
+ public Object create(GoodsAllinone goodsAllinone) {
+ Object error = validate(goodsAllinone);
+ if (error != null) {
+ return error;
+ }
+
+ DtsGoods goods = goodsAllinone.getGoods();
+ DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
+ DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
+ DtsGoodsProduct[] products = goodsAllinone.getProducts();
+
+ String name = goods.getName();
+ if (goodsService.checkExistByName(name)) {
+ logger.error("商品名已经存在");
+ return ResponseUtil.fail(GOODS_NAME_EXIST, "商品名已经存在");
+ }
+
+ // 商品基本信息表Dts_goods
+ goodsService.add(goods);
+
+ //将生成的分享图片地址写入数据库
+ String url = qCodeService.createGoodShareImage(goods.getId().toString(), goods.getPicUrl(), goods.getName());
+ if (!StringUtils.isEmpty(url)) {
+ goods.setShareUrl(url);
+ if (goodsService.updateById(goods) == 0) {
+ logger.error("更新数据失败");
+ throw new RuntimeException("更新数据失败");
+ }
+ }
+
+ // 商品规格表Dts_goods_specification
+ for (DtsGoodsSpecification specification : specifications) {
+ specification.setGoodsId(goods.getId());
+ specificationService.add(specification);
+ }
+
+ // 商品参数表Dts_goods_attribute
+ for (DtsGoodsAttribute attribute : attributes) {
+ attribute.setGoodsId(goods.getId());
+ attributeService.add(attribute);
+ }
+
+ // 商品货品表Dts_product
+ for (DtsGoodsProduct product : products) {
+ product.setGoodsId(goods.getId());
+ productService.add(product);
+ }
+ return ResponseUtil.ok();
+ }
+
+ public Object list2() {
+ // http://element-cn.eleme.io/#/zh-CN/component/cascader
+ // 管理员设置“所属分类”
+ List l1CatList = categoryService.queryL1();
+ List categoryList = new ArrayList<>(l1CatList.size());
+
+ for (DtsCategory l1 : l1CatList) {
+ CatVo l1CatVo = new CatVo();
+ l1CatVo.setValue(l1.getId());
+ l1CatVo.setLabel(l1.getName());
+
+ List l2CatList = categoryService.queryByPid(l1.getId());
+ List children = new ArrayList<>(l2CatList.size());
+ for (DtsCategory l2 : l2CatList) {
+ CatVo l2CatVo = new CatVo();
+ l2CatVo.setValue(l2.getId());
+ l2CatVo.setLabel(l2.getName());
+ children.add(l2CatVo);
+ }
+ l1CatVo.setChildren(children);
+
+ categoryList.add(l1CatVo);
+ }
+
+ // http://element-cn.eleme.io/#/zh-CN/component/select
+ // 管理员设置“所属品牌商”
+ List list = brandService.all();
+ List