1. 程式人生 > >sku規格設計I中規格及J中規格明細之間的聯動

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);
        }
    }
}