sku规格设计I中规格及J中规格明细之间的联动
最近在开发SKU这块,负责写接口,在规格联动这块卡住了许久,最后在大神的帮助下,终于写出来了.现在我在此给大家分享
1.数据库设计 一个商品SPU对应多个SKU
sku表设计
sku表和规格的中间表
规格表
规格明细表
2.前端要达到的效果
3.思路
在sku详情页面
点击规格进行切换,请求数据,插入的参数:当前的skuid ,3个规格明细id(此为选中的sku规格明细id)
参数接收完毕后,做以下准备
a、将规格明细进行组合,传过来的【容量,颜色,码数】 组合后结果为为:容量+颜色,容易+码数,颜色+码数
List<Integer[]> listZH = CombinationUtils.Zhlist(specificationDetailIdList);
b、获取所有的上架的规格详细list
List<ProductSpecificationDetailIds> psdIds = productSpecificationService.getPsdIds(ps.getProductId());
c、获取所有的规格id,并查出规格下的规格明细
//获取所有的规格id List<Integer> specificationIds = productSpecificationService.getSpecificationsByProductId(ps.getProductId()); //根据规格id查询出所对应的规格明细 List<SpecificationModel> specificationModels = specificationService.getAllSpecifications(specificationIds);getAllSpecifications方法的
<resultMap id="BaseSepecificationMap" type="com.psp.dao.model.SpecificationModel"> <id column="sid" property="sid" jdbcType="INTEGER"/> <result column="specification_name" property="specificationName" jdbcType="VARCHAR"/> <result column="shop_sign" property="shopSign" jdbcType="VARCHAR"/> <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/> <result column="create_person" property="createPerson" jdbcType="VARCHAR"/> <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/> <result column="update_person" property="updatePerson" jdbcType="VARCHAR"/> <collection property="specificationDetailList" ofType="com.psp.dao.model.SpecificationDetail" resultMap="BaseSepecificationDetailMap"> </collection> </resultMap> <resultMap id="BaseSepecificationDetailMap" type="com.psp.dao.model.SpecificationDetail"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="specification_id" property="specificationId" jdbcType="INTEGER"/> <result column="name" property="name" jdbcType="VARCHAR"/> <result column="shop_sign" property="shopSign" jdbcType="VARCHAR"/> <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/> <result column="create_person" property="createPerson" jdbcType="VARCHAR"/> <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/> <result column="update_person" property="updatePerson" jdbcType="VARCHAR"/> </resultMap> <select id="getAllSpecifications" resultMap="BaseSepecificationMap" parameterType="java.lang.Integer"> SELECT sd.id id, s.id sid, s.specification_name, sd. NAME ,sd.specification_id specification_id FROM `specification` s LEFT JOIN specification_detail sd ON s.id = sd.specification_id where sd.id in <foreach collection="specificationIds" item="id" separator="," open="(" close=")"> #{id} </foreach> </select>
准备工作做好后,现在来规格联动置灰(这段逻辑比较难懂,自己也是想了很久才想出来的)
4.代码实现
/** * 规格联动 * * @return */ @ResponseBody @RequestMapping("/checkSpecificationGrey") public JData checkSpecificationGrey() { JSONObject jsonObject = JSONObject.parseObject(getData()); Integer skuId = jsonObject.getInteger("skuId"); String specificationDetailIds = jsonObject.getString("specificationDetailIds"); JSONArray ja = JSONObject.parseArray(specificationDetailIds); SpecificationInfo specificationInfo = new SpecificationInfo(); List<Integer> specificationDetailIdList = new ArrayList<Integer>(); if (ja.size() > 0) { for (int i = 0; i < ja.size(); i++) { Integer integer = ja.getInteger(i); specificationDetailIdList.add(integer); } } //sku与规格中间表list List<Integer[]> listZH = CombinationUtils.Zhlist(specificationDetailIdList); List<ProductSpecification> psList = specificationService.getProductSpecificationByThrId(skuId); ProductSpecification ps = psList.get(0); List<ProductSpecificationDetailIds> psdIds =
productSpecificationService.getPsdIds(ps.getProductId()); //获取所有的规格详细 List<Integer> specificationIds =
productSpecificationService.getSpecificationsByProductId(ps.getProductId()); List<SpecificationModel> specificationModels =
specificationService.getAllSpecifications(specificationIds); //规格联动置灰 changeGrey(specificationModels, listZH, psdIds); specificationInfo.setSpecificationModelList(specificationModels); //根据规格联动获取skuId Integer newSkuId = productSpecificationService.generateSkuId(specificationDetailIdList,
specificationDetailIdList.size(), ps.getProductId()); ProductModel byId = productService.getProductModelBySkuId(newSkuId); List<ProductModel> products = new ArrayList<ProductModel>(); products.add(byId); List<ProductModel> productsPirce = productService.getProductsPirce(products); if (productsPirce != null && productsPirce.size() > 0) { ProductModel productModel = productsPirce.get(0); if (productModel.getWholesalePrice() != null) { specificationInfo.setWholesalePrice(productModel.getWholesalePrice()); } if (productModel.getPspAgentManagementId() != null) { specificationInfo.setPspAgentManagementId(productModel.getPspAgentManagementId()); } specificationInfo.setName(productModel.getSkuName()); specificationInfo.setPackageUnit(productModel.getPackageUnit()); specificationInfo.setSalePrice(productModel.getSalePrice()); } specificationInfo.setSkuId(newSkuId); return new JData(ReturnCode.SUCCESS, specificationInfo); }将前台传过来的规格明细id进行组合
public static List<Integer[]> Zhlist(List<Integer> list){ List<Integer[]> listZH =new ArrayList<Integer[]>(); for (int i=0;i<list.size();i++){ Integer[] array =new Integer[list.size()-1]; int count=0; for (int j=0;j<list.size();j++){ if(i!=j){ array[count] =list.get(j); count++; } } listZH.add(array); } return listZH; }
此规格联动置灰方法时公有的,初始化和切换规格都可以用
lsitZh是前台传过来的规格明细id组合 [黄色id,100ml id] [黄色id,30id] [100ml id,30id]
specificationModels 是一个集合,集合SpecificationModel 规格基本属性 + 规格明细List集合
psdIds 是所有上架的规格id组合 [黄色,100ml,10] [黄色,100ml,30].......这是都是上架的
字段grey是将规格明细置灰
/** * 规格联动置灰 * * @param specificationModels 规格详细 * @param listZH 前台传过来的规格详细id 组合 * @param psdIds 所有上架的规格id组合 */ public void changeGrey(List<SpecificationModel> specificationModels, List<Integer[]> listZH, List<ProductSpecificationDetailIds> psdIds) { for (int i = 0; i < specificationModels.size(); i++) { Integer[] integers = listZH.get(i); List<SpecificationDetail> specificationDetailList = specificationModels.get(i).getSpecificationDetailList(); //规格明细 for (SpecificationDetail sfd : specificationDetailList) { boolean canSelect = false; for (int j = 0; j < psdIds.size(); j++) { List<Integer> specificationDetailIdNList = psdIds.get(j).getSpecificationDetailIdNList(); int count = 0; boolean needContinue = false; for (int k = 0; k < specificationModels.size(); k++) { if (k == i) { if (sfd.getId().equals(specificationDetailIdNList.get(i))) { } else { needContinue = true; break; } } else { if (integers[count].equals(specificationDetailIdNList.get(k))) { count++; } else { needContinue = true; break; } } if (k == specificationModels.size() - 1) { canSelect = true; } } if (needContinue) continue; if (canSelect) { sfd.setGrey(true); break; } } if (sfd.getGrey() == null) sfd.setGrey(false); } } }