package com.ema.common.mapstruct;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * MapStruct转换器
 *
 * @author LIJIAN
 */
@Component
public class MyMapStructMapper {

    public String asString(Date date) {
        return DateUtil.formatDateTime(date);
    }

    public String asString(ZonedDateTime date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return  date.toLocalDateTime().format(formatter);
    }

    public Date asDate(String date) {
        return DateUtil.parse(date);
    }

    public ZonedDateTime asZonedDateTime(String date) {
        String str= DateUtil.formatDateTime(DateUtil.parse(date));
        DateTimeFormatter str1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"));
        return ZonedDateTime.parse(str, str1);
    }

    public String asString(BigDecimal bigDecimal) {
        return bigDecimal != null ? bigDecimal.toString() : "";
    }

    public BigDecimal asBigDecimal(String decimalStr) {
        if (StrUtil.isNotBlank(decimalStr) && NumberUtil.isNumber(StrUtil.trim(decimalStr))) {
            return new BigDecimal(StrUtil.trim(decimalStr));
        }
        return null;
    }

    public String asString(Integer integer) {
        return integer != null ? String.valueOf(integer) : "";
    }

    public Integer asInteger(String intStr) {
        if (StrUtil.isNotBlank(intStr) && NumberUtil.isNumber(StrUtil.trim(intStr))) {
            return Integer.valueOf(StrUtil.trim(intStr));
        }
        return null;
    }

    public String asString(Long lon) {
        return lon != null ? String.valueOf(lon) : "";
    }

    public Long asLong(String lonStr) {
        if (StrUtil.isNotBlank(lonStr) && NumberUtil.isNumber(StrUtil.trim(lonStr))) {
            return Long.valueOf(StrUtil.trim(lonStr));
        }
        return null;
    }

    public String asString(Double doub) {
        return doub != null ? String.valueOf(doub) : "";
    }

    public Double asDouble(String doubStr) {
        if (StrUtil.isNotBlank(doubStr) && NumberUtil.isNumber(StrUtil.trim(doubStr))) {
            return Double.valueOf(StrUtil.trim(doubStr));
        }
        return null;
    }

    public String asString(List<String> list) {
        return CollUtil.join(list,",");
    }

    public List<String> asList(String listStr) {
        if (StrUtil.isNotBlank(listStr)) {
            return Arrays.stream(listStr.split(",")).collect(Collectors.toList());
        }
        return CollUtil.toList();
    }

    public String asString(Set<String> list) {
        return CollUtil.join(list,",");
    }

    public Set<String> asSet(String listStr) {
        if (StrUtil.isNotBlank(listStr)) {
            return Arrays.stream(listStr.split(",")).collect(Collectors.toSet());
        }
        return new HashSet<>();
    }

}
