package com.platform.utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.platform.enums.ResultCodeEnum;
import com.platform.exception.CommonBaseException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author abnerhou
 * @date 2020/4/23 17:35
 * @desciption
 */
@Component
public class CommonDataUtils {

    public static BigDecimal getBigDecimalObj(Object data) {

        if (null == data) {
            return null;
        } else if (data instanceof String) {
            String dataStr = (String) data;
            if (!StringUtils.isEmpty(dataStr.trim())) {
                return new BigDecimal(dataStr.trim());
            }
        } else if (data instanceof Long) {
            Long dataLong = (Long) data;
            return BigDecimal.valueOf(dataLong);
        } else if (data instanceof Integer) {
            Integer dataInt = (Integer) data;
            return BigDecimal.valueOf(dataInt);

        } else if (data instanceof Double) {
            Double dataDouble = (Double) data;
            return BigDecimal.valueOf(dataDouble);
        }

        return null;

    }

    public static Integer getInteger(Object object) {
        if (null == object) {
            return new Integer(0);
        }
        if (object instanceof String) {
            String ojStr = (String) object;
            if (StringUtils.isEmpty(ojStr)) {
                //TODO:在斟酌这里的处理方式
                return new Integer(0);
            } else {
                return new Integer(Integer.parseInt(ojStr.trim()));
            }
        } else if (object instanceof Integer) {
            return (Integer) object;
        } else if (object instanceof Long) {
            return (Integer) object;
        } else if (object instanceof Double) {
            return (Integer) object;
        } else {
            return new Integer(0);
        }
    }

    public static String getToken(HttpServletRequest request) {
        if (null == request) {
            throw new CommonBaseException(ResultCodeEnum.D101, "token缺失");
        }
        String token = request.getParameter("token");
        if (StringUtils.isBlank(token)) {
            throw new CommonBaseException(ResultCodeEnum.D101, "token缺失");
        }
        return token;
    }

    public static Long getLongReturnNullIfNotExit(Object num) {
        if (null == num) {
            return null;
        } else if (num instanceof String) {
            if (!StringUtils.isEmpty((String) num)) {
                String numStr = (String) num;
                return Long.parseLong(numStr.trim());
            }
        } else if (num instanceof Integer) {
            return (Long) num;
        } else if (num instanceof Long) {
            return (Long) num;
        }
        return null;
    }

    public static <T> void assembleTimeQueryWrapper(Map<String, Object> constantQuery, String dynamicQuery,
                                                    QueryWrapper<T> queryWrapper, String idQuery,
                                                    String secondQuery, String thirdQuery) {
        assembleConstantQuery(queryWrapper, constantQuery);
        if (org.apache.commons.lang.StringUtils.isNotBlank(dynamicQuery)) {
            queryWrapper.and(wrapper -> wrapper
                    .or().like(org.apache.commons.lang.StringUtils.isNotBlank(secondQuery), secondQuery, dynamicQuery)
                    .or().like(org.apache.commons.lang.StringUtils.isNotBlank(thirdQuery), thirdQuery, dynamicQuery)
                    .or().like(dynamicQuery.length() <= 32 && org.apache.commons.lang.StringUtils.isNotBlank(idQuery), idQuery, dynamicQuery));
        }

    }

    public static <T> void assembleConstantQuery(QueryWrapper<T> queryWrapper, Map<String, Object> constantQuery) {
        if (null != queryWrapper && !CollectionUtils.isEmpty(constantQuery)) {
            for (Map.Entry<String, Object> entry : constantQuery.entrySet()) {
                queryWrapper.eq(entry.getKey(), entry.getValue());
            }
        }
    }

    public static Integer getIntegerWithDefault(Object object, boolean withDefault) {
        if (null == object) {

            return withDefault ? new Integer(0) : null;
        }
        if (object instanceof String) {
            String ojStr = (String) object;
            if (StringUtils.isEmpty(ojStr)) {

                return withDefault ? new Integer(0) : null;
            } else {
                return Integer.parseInt(ojStr.trim());
            }
        } else if (object instanceof Integer) {
            return (Integer) object;
        } else if (object instanceof Long) {
            return (Integer) object;
        } else if (object instanceof Double) {
            return (Integer) object;
        } else {
            return withDefault ? new Integer(0) : null;
        }
    }

    public static Map<String, Object> assembleResult(long totalNum, long totalPageNum, long currPageNum, Object list) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalNum", totalNum);
        resultMap.put("totalPageNum", totalPageNum);
        resultMap.put("curPage", currPageNum);
        resultMap.put("list", list);
        return resultMap;
    }

    public static Map<String, Object> assembleResultCustom(long totalNum, long totalPageNum, long currPageNum , long pageSize, Object list) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalCount", totalNum);
        resultMap.put("totalPageNum", totalPageNum);
        resultMap.put("currPage", currPageNum);
        resultMap.put("pageSize", pageSize);
        resultMap.put("list", list);
        return resultMap;
    }

    /**
     * @param requestCode
     * @param msg
     * @param data
     * @return Map<String,Object>
     * @throws
     * @Description:构建统一格式返回对象
     * @date 2016年9月2日
     * @author zhuliyun
     */
    public static Map<String, Object> toResponsObject(int requestCode, String msg, Object data) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("code", requestCode);
        obj.put("msg", msg);
        if (data != null){
            obj.put("page", data);
        }

        return obj;
    }

    /**
     * 判断是否为正确的手机号
     *
     * @param str
     * @return boolean
     */
    public static boolean isMobileStr(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        return str.matches("^1[0-9]*$");

    }


    public static String parseStrArrayToStr(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for (String str : list) {
            builder.append(str).append(",");
        }
        if (builder.length() > 1) {
            return builder.toString().substring(0, builder.length() - 1);
        } else {
            return builder.toString();
        }

    }


}
