package com.fufan.mtsaas.tools;

import com.fufan.mtsaas.modules.common.bo.request.ReqBasePage;
import com.fufan.mtsaas.util.DataChangeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapTools {



    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, String> objectToMap(Object obj)  {
        Map<String, String> map = new HashMap<String,String>();
        try {
            Class<?> clazz = obj.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                try {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    Object temp = field.get(obj);
                    if(temp != null){
                        String value = field.get(obj).toString();
                        if(StringUtils.isNotEmpty(value)){
                            map.put(fieldName, value);
                        }
                    }
                }catch (Exception e){

                }
            }
            Class<?> page = ReqBasePage.class;
            for (Field field : page.getDeclaredFields()) {
                try {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    Object temp = field.get(obj);
                    if(temp != null){
                        String value = field.get(obj).toString();
                        if(StringUtils.isNotEmpty(value)){
                            map.put(fieldName, value);
                        }
                    }
                }catch (Exception e){

                }
            }
        }catch (Exception e){

        }

        return map;
    }


    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, String> objectAllToMap(Object obj)  {
        Map<String, String> map = new HashMap<String,String>();
        try {
            Class<?> clazz = obj.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                try {
                    field.setAccessible(true);
                    String fieldName = field.getName();
                    Object temp = field.get(obj);
                    if(temp != null){
                        String value = field.get(obj).toString();
                        if(StringUtils.isNotEmpty(value)){
                            map.put(fieldName, value);
                        }else {
                            map.put(fieldName, "");
                        }
                    }else {
                        map.put(fieldName, "");
                    }
                }catch (Exception e){

                }
            }
        }catch (Exception e){

        }

        return map;
    }
//    private static boolean batchSetData(List<?> dataList, Class<?> clazz, ProductStandardData productStandardData){
//        Object dataTemp = DataChangeUtil.getDataEntity(productStandardData,clazz);
//        for(Object model: dataList){
//            try {
//                merge(model,dataTemp);
//                System.out.println(model);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return true;
//    }

//    public static void main(String[] args) {
//        String.Format("{0:C}",2.5)
//    }
//    public static void main(String[] args) {
//        List<TProductSize> dataList = new ArrayList<>();
//        TProductSize tProductSize1 = new TProductSize();
//        TProductSize tProductSize2 = new TProductSize();
//        TProductSize tProductSize3 = new TProductSize();
//        tProductSize1.setCreateBy("666");
//        tProductSize2.setCreateBy("666");
//        tProductSize3.setCreateBy("666");
//        dataList.add(tProductSize1);
//        dataList.add(tProductSize2);
//        dataList.add(tProductSize3);
//        ProductStandardData productStandardData =new ProductStandardData();
//        productStandardData.setProductId(68686867L);
//        batchSetData(dataList,TProductSize.class,productStandardData);
//    }
public static void main(String[] args) {
        String aa = "1";
    for(int i=0;i< 10;i++){
        aa = aa + "1";
        System.out.println(handleMoney(aa));
    }
    System.out.println(handleMoney("909090"));
}
    private static String handleMoney(String allTotal){
        String data = allTotal + "";
        // 遍历所有字符
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < data.length(); i++) {
            int temp = (data.length() - i);
            if(temp % 3== 0 && temp < data.length()){
                result.append(",");
            }
            result.append(data.charAt(i));
        }
        return result.toString();
    }
    public static <M> void merge(M target, M destination) throws Exception {
        BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());

        // Iterate over all the attributes
        for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {

            // Only copy writable attributes
            if (descriptor.getWriteMethod() != null) {
                Object originalValue = descriptor.getReadMethod()
                        .invoke(target);

                // Only copy values values where the destination values is null
                if (originalValue == null) {
                    Object defaultValue = descriptor.getReadMethod().invoke(
                            destination);
                    if(defaultValue != null){
                        descriptor.getWriteMethod().invoke(target, defaultValue);
                    }
                }
            }
        }
    }
}