package com.ehu.bean;

import com.ehu.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Alan
 * @date 2018/11/12 14:53
 **/
public interface Product {
    /**
     * 支付时给第三方的body长度
     */
    int PRODUCT_NAME_LENGTH = 127;

    /**
     * 获取下单的产品名称，微信不能超过49个字
     *
     * @param products 产品
     * @return 产品名，逗号隔开
     */
    static String getNames(List<? extends Product> products) {
        String body = "";
        if (products != null && products.size() > 0) {
            StringBuilder sb = new StringBuilder();
            StringBuilder sbTem = new StringBuilder();
            int size = products.size(), countSum = 0;
            for (int i = 0; i < size; i++) {
                Product product = products.get(i);
                String productName = product.getName();
                sbTem.append(productName);
                int length = sbTem.toString().getBytes().length;
                if (length > PRODUCT_NAME_LENGTH) {
                    if (i == 0) {
                        sb.append(StringUtils.subStringSpecifyBytes(sbTem.toString(), PRODUCT_NAME_LENGTH));
                        countSum++;//计算有多少个商品被加入
                        break;
                    } else {
                        break;
                    }
                } else {
                    sb.append(productName);
                    countSum++;//计算有多少个商品被加入
                    if (size != 1) {
                        sb.append(",");
                        sbTem.append(",");
                    }
                }
            }
            if (size > 1) {
                sb.deleteCharAt(sb.lastIndexOf(","));
            }
            body = sb.toString();
            body = body.replaceAll(" ", "");

            if (size > countSum) {
                body = addSoOn(body);
            }
        }
        return body;
    }

    /**
     * 如果有多个商品
     *
     * @param body body
     * @return 处理后的body
     */
    static String addSoOn(String body) {
        String bodyTem = body + "等";
        if (bodyTem.getBytes().length > PRODUCT_NAME_LENGTH) {
            return body.substring(0, body.length() - 1) + "等";
        } else {
            return bodyTem;
        }
    }

    /**
     * 返回商品的名称
     *
     * @return 商品的名称
     */
    String getName();

    static void main(String[] args) {
        List<Product> products = new ArrayList<>();
        Product product = () -> "东山本港白鲳鱼满两斤包邮，不满两斤，";
        products.add(product);
        product = () -> "东山本港白鲳鱼满两斤包邮，不满两斤，必须拍邮费,,,/不然送,东山手工墨鱼汁肠满两斤从的的的";
        products.add(product);


        String names = getNames(products);
        System.out.println(names);
        System.out.println(names.length());
        System.out.println(names.getBytes().length);
    }
}

@@ -1034,68 +1034,153 @@
     
-sb.append(StringUtils.subStringSpecifyBytes(sbTem.toString()
+String str = sbTem.toString();%0A                        sb.append(StringUtils.subStringSpecifyBytes(str.length() %3E 49 ? str.substring(0, 49) : str
 , PR
@@ -2526,16 +2526,49 @@
 %E5%8C%85%E9%82%AE%EF%BC%8C%E4%B8%8D%E6%BB%A1%E4%B8%A4%E6%96%A4%EF%BC%8C
+%E5%BF%85%E9%A1%BB%E6%8B%8D%E9%82%AE%E8%B4%B9,,,/%E4%B8%8D%E7%84%B6%E9%80%81,%E4%B8%9C%E5%B1%B1%E6%89%8B%E5%B7%A5%E5%A2%A8%E9%B1%BC%E6%B1%81%E8%82%A0%E6%BB%A1%E4%B8%A4%E6%96%A4%E4%BB%8E%E7%9A%84%E7%9A%84%E7%9A%84%E5%A3%AB%E5%A4%A7%E5%A4%AB%E6%94%B6%E5%88%B0
 %22;%0A     

update Product