package com.ag.base;

import cn.hutool.core.date.DateUtil;
import com.ag.util.TimeUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/12 0012.
 */
public class ParamUtil<T> extends SqlUtil<T> {

    public T packPOJO(Map<String, String> map, Class<T> clz){
        T t = getEntity(clz);
        if(t == null){
            return null;
        }
        Method[] ms=t.getClass().getMethods();
        for(Method m: ms) {
            String fieldName = getFieldName(m);
            if(fieldName == null){
                continue;
            }
            if(!StringUtils.isEmpty(map.get(fieldName))){
                setMethodValue(t, m, map.get(fieldName));
                continue;
            }
            String colName = toLower(fieldName).toUpperCase();
            if(!StringUtils.isEmpty(map.get(colName))){
                setMethodValue(t, m, map.get(colName));
            }
        }
        return t;

    }

    private void setMethodValue(T t, Method m, String paramVal) {
        String paramType = m.getParameterTypes()[0].toString();
        try {
            if(paramType.contains("String")){
                m.invoke(t, paramVal);
            } else if(paramType.contains("Integer")){
                m.invoke(t, Integer.valueOf(paramVal));
            } else if(paramType.contains("Double")){
                m.invoke(t, Double.valueOf(paramVal));
            } else if(paramType.contains("Short")){
                m.invoke(t, Short.valueOf(paramVal));
            } else if(paramType.contains("Long")){
                m.invoke(t, Long.valueOf(paramVal));
            } else if(paramType.contains("Date")){
                /*if (paramVal.length()>19) {
                    paramVal = paramVal.substring(0, 19);
                }*/
                m.invoke(t, TimeUtil.getTimestamp(paramVal));
            } else if(paramType.contains("BigDecimal")){
                m.invoke(t, new BigDecimal(paramVal));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void printMap(Map<String,String> map){
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
        }
    }

    public static void main(String[] args) {
        /*List<String> mlist = new ArrayList<>();
        mlist.add("zhu");
        mlist.add("wen");
        mlist.add("tao");
        // List转成数组
        String[] array = mlist.toArray(new String[0]);
        // 输出数组
        for (int i = 0; i < array.length; i++) {
            System.out.println("array--> " + array[i]);
        }*/
        Date date = DateUtil.parse("2021-06-01 00:00:00.000");
        System.out.println(DateUtil.formatDateTime(date));
    }
}
