package com.alan344happyframework.core;

import com.alan344happyframework.constants.SeparatorConstants;
import com.alan344happyframework.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) {
                        String str = sbTem.toString();
                        sb.append(StringUtils.subStringSpecifyBytes(str.length() > 49 ? str.substring(0, 49) : str, 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.lastIndexOf(SeparatorConstants.COMMA)) + "等";
        } else {
            return bodyTem;
        }
    }

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

    static void main(String[] args) {
        List<Product> products = new ArrayList<>();
        Product product = () -> "【盒装新鲜毛豆米】300g";
        products.add(product);
        product = () -> "【鲜山羊排】500g±50g";
        products.add(product);
        product = () -> "【豆饼青菜】100g豆饼+250g青菜";
        products.add(product);
        product = () -> "【净甜玉米棒】1000g";
        products.add(product);
        product = () -> "【生菜】500g";
        products.add(product);


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

@@ -775,13 +775,8 @@
     
-for (
 int 
@@ -781,16 +781,35 @@
 t i = 0;
+%0A            for (;
  i %3C siz
@@ -1775,24 +1775,33 @@
         if (
+i %3E 0 && 
 size %3E 1) %7B%0A
@@ -2018,16 +2018,19 @@
 oOn(body
+, i
 );%0A     
@@ -2125,21 +2125,94 @@
 am body 
+ 
 body%0A
+     * @param index %E8%A1%A8%E7%A4%BAbody%E4%B8%AD%E6%9C%89%E5%A4%9A%E5%B0%91%E4%B8%AA%E5%95%86%E5%93%81%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%98%AF1%E4%B8%AA%E5%95%86%E5%93%81%EF%BC%8Cindex = 0%EF%BC%8C2%E4%B8%AA%E5%95%86%E5%93%81%EF%BC%8Cindex = 1%EF%BC%8C%E4%BB%A5%E6%AD%A4%E7%B1%BB%E6%8E%A8%0A
      * @
@@ -2272,16 +2272,27 @@
 ing body
+, int index
 ) %7B%0A    
@@ -2336,16 +2336,29 @@
     if (
+index %3E 0 && 
 bodyTem.
@@ -2480,32 +2480,166 @@
 .COMMA)) + %22%E7%AD%89%22;%0A
+        %7D else if (bodyTem.getBytes().length %3E PRODUCT_NAME_LENGTH) %7B%0A            return body.substring(0, body.length() - 10) + %22%E7%AD%89%22;%0A
         %7D else %7B
@@ -2901,17 +2901,16 @@
 %E9%B2%9C%E6%AF%9B%E8%B1%86%E7%B1%B3%E3%80%91300
-g
 %22;%0A     

fix 微信body过长