package com.judong.chuanyiserver.util;

import cn.hutool.poi.excel.ExcelReader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 判断是否为空工具类
 *
 * @author Administrator
 */
public class Blank {

    /**
     * String 不为空
     *
     * @param str
     * @return
     */
    public static boolean notBlank(String str) {
        return str != null && !"".equals(str.trim());
    }

    /**
     * Integer 不为空
     *
     * @param num
     * @return
     */
    public static boolean notBlank(Integer num) {
        return num != null;
    }

    /**
     * List 不为空
     *
     * @param list
     * @return
     */
    public static boolean notBlank(@SuppressWarnings("rawtypes") List list) {
        return list != null && list.size() > 0;
    }

    /**
     * 判断数组是否为空
     *
     * @param objs
     * @return
     */
    public static boolean isNotEmpty(Object[] objs) {
        boolean flag = false;
        for (int i = 0; i < objs.length; i++) {
            if (objs[i] != null && !"".equals((objs[i]).toString().trim())) {
                flag = true;
            } else {
                flag = false;
                break;
            }
        }
        return flag;
    }

    /**
     * 判断对象不为空
     *
     * @param obj 对象名
     * @return 是否不为空
     */
    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * Object为空
     *
     * @param objects
     * @return
     */
    public static boolean isEmpty(Object... objects) {
        for (Object obj : objects) {
            if (isEmpty(obj)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Object 为空
     *
     * @param obj
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if ((obj instanceof String)) {
            return ((String) obj).trim().equals("");
        }
        if ((obj instanceof Map)) {
            return ((Map) obj).isEmpty();
        }
        if ((obj instanceof List)) {
            return ((List) obj).size() == 0;
        }
        if ((obj instanceof JSONObject)) {
            return ((JSONObject) obj).isEmpty();
        }
        if ((obj instanceof JSONArray)) {
            return ((JSONArray) obj).isEmpty();
        }
        return false;
    }

    /**
     * 根据传过来的属性与对象，判断对象是否正确 Object obj 检验对象 String[] params 不为空的属性值
     */
    public static boolean checkObjectParamNotNull(Object obj, String[] params) {
        boolean flag = true;
        Class<? extends Object> clazz = obj.getClass();
        for (String param : params) {
            try {
                Field field = clazz.getDeclaredField(param);
                field.setAccessible(true);
                Object val = field.get(obj);
                if (val == null || "".equals(val.toString())) {
                    flag = false;
                    break;
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    /**
     * Map转成实体对象
     *
     * @param map   map实体对象包含属性
     * @param clazz 实体对象类型
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static Object mapToObject(Map<String, Object> map, Class<?> clazz) throws Exception {
        if (map == null) {
            return null;
        }
        Object obj = null;
        obj = clazz.newInstance();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            field.setAccessible(true);
            field.set(obj, map.get(field.getName()));
        }
        return obj;
    }

    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> toListMap(String json) {
        List<Object> list = JSON.parseArray(json);
        List<Map<String, Object>> listw = new ArrayList<Map<String, Object>>();
        for (Object object : list) {
            Map<String, Object> ret = (Map<String, Object>) object;
            listw.add(ret);
        }
        return listw;

    }

    /**
     * data为空验证
     *
     * @param param
     * @return
     */
    public static Boolean isCheck(Map<String, Object> param) {
        if (!Objects.equals(param, null)) {
            Object obj = param.get("data");
            if (Objects.equals(obj, null) || "".equals(obj)) {
                return true;
            }
            return false;
        }
        return true;
    }

    /**
     * 验证是否为手机号
     * @param phone
     * @return
     */
    public static Boolean isPhone(String phone) {
        if (isEmpty(phone)) {
            return false;
        }

        String pattern = "^(1[3-9])\\d{9}$";
        if (!Pattern.matches(pattern, phone)) {
            return false;
        }

        return true;
    }
    
    public static void main(String[] args) {
    	Map<String, String> params = new HashMap<>();
    	params.put("区市场监管局", "区市场监督管理局");
    	params.put("区文化和旅游发展委员会", "区文化旅游委");
    	params.put("区教育委员会", "区教委");
    	params.put("区应急管理局", "区应急局");
    	params.put("区民族宗教事务委员会", "区民宗委");
    	params.put("区住房和城乡建设委员会", "区住房城乡建委");
    	params.put("区农业农村委员会", "区农业农村委");
    	
    	ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader("D:/cqf/nanan.xlsx");
    	List<Map<String,Object>> readAll = reader.readAll();
    	JSONArray array = new JSONArray();
    	JSONObject obj;
    	String sql = "INSERT INTO question_article (\r\n" + 
    			"	dataId,\r\n" + 
    			"	title,\r\n" + 
    			"	content,\r\n" + 
    			"	publishTime,\r\n" + 
    			"	departmentName,\r\n" + 
    			"	createTime,\r\n" + 
    			"	e_top\r\n" + 
    			")\r\n" + 
    			"VALUES";
    	int ii = 10000;
    	for (int i = 0; i < readAll.size(); i++) {
    		Map<String,Object> map = readAll.get(i);
    		obj = new JSONObject();
    		String name = String.valueOf(map.get("name")).substring(5);
			obj.put("name", Blank.notBlank(params.get(name)) ? params.get(name) : name);
			String remark = String.valueOf(map.get("remark"));
			String[] str = remark.split("\n");
			if (str.length == 3) {
				if (str[1].indexOf("常见问题问题：") == 0) {
					obj.put("title", str[1].substring(7));
				} else if (str[1].indexOf("问题：") == 0) {
					obj.put("title", str[1].substring(3));
				}
				if (str[2].indexOf("解答：答") == 0) {
					obj.put("content", str[2].substring(5));
				} else if (str[2].indexOf("解答：") == 0) {
					obj.put("content", str[2].substring(3));
				}
			} else {
				if (str[0].indexOf("常见问题问题：") == 0) {
					obj.put("title", str[0].substring(7));
				} else if (str[0].indexOf("问题：") == 0) {
					obj.put("title", str[0].substring(3));
				}
				if (str[1].indexOf("解答：答") == 0) {
					obj.put("content", str[1].substring(5));
				} else if (str[1].indexOf("解答：") == 0) {
					obj.put("content", str[1].substring(3));
				}
			}
    		array.add(obj);
    		sql += "("+ (ii+i) +", '"+ obj.get("title") +"', '"+ obj.get("content") +"', '"+ randomDate("2021-01-01","2021-12-13") +"', '"
    		+ obj.get("name") +"', now(), 0),";
		}
    	System.err.println(array);
    	System.err.println(sql);
	}

    private static String randomDate(String beginDate,String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);
 
            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            format.format(new Date(date));
            return  format.format(new Date(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end){
            return random(begin,end);
        }
        return rtn;
    }

}
