sku规格设计I中规格及J中规格明细之间的联动

来源:互联网 发布:科尔沃折叠刀淘宝 编辑:程序博客网 时间:2024/05/17 09:42

最近在开发SKU这块,负责写接口,在规格联动这块卡住了许久,最后在大神的帮助下,终于写出来了.现在我在此给大家分享

1.数据库设计   一个商品SPU对应多个SKU

sku表设计


sku表和规格的中间表


规格表


规格明细表


2.前端要达到的效果


3.思路


4.代码实现

/** * 获取当前sku 的货品所有的规格明细 * * @return */@ResponseBody@RequestMapping("/getSpecificationAll")public JData getSpecificationAll() {    JSONObject jsonObject = JSONObject.parseObject(getData());    Integer skuId = jsonObject.getInteger("skuId");    ProductSku productSku = productSkuService.selectBySkuId(skuId);    List<Integer> productSpecificationList = productSpecificationService.findBySkuId(skuId);    //获取所有的规格详细    List<Integer> specificationIds = productSpecificationService.getSpecificationsByProductId(productSku.getProductId());    List<SpecificationModel> specificationModels =new ArrayList<SpecificationModel>();    if(specificationIds!=null && specificationIds.size()>0){        specificationModels = specificationService.getAllSpecifications(specificationIds);        //sku与规格中间表list        List<Integer[]> listZH = CombinationUtils.Zhlist(productSpecificationList);        List<ProductSpecificationDetailIds> psdIds = productSpecificationService.getPsdIds(productSku.getProductId());        //规格联动置灰        changeGrey(specificationModels, listZH, psdIds);    }    return new JData(ReturnCode.SUCCESS, specificationModels);}
将前台传过来的规格明细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);        }    }}



阅读全文
0 0
原创粉丝点击