package com.engine.recruit.entity.card;

import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.general.BaseBean;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

/**
 * @author:dxfeng
 * @createTime: 2024/08/01
 * @version: 1.0
 */
public class WorkInfo {
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private String start;
    private String end;
    private String time;
    private String company;
    private String job;

    public void setStart(String start) {
        this.start = start;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public String getTime() {
        if (StringUtils.isBlank(start)) {
            return "";
        }
        if (StringUtils.isBlank(end)) {
            return getDatePeriod(start, "");
        }
        return getDatePeriod(start, end);
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }


    private String getDatePeriod(String start, String end) {
        try {
            String dateStr = start + " ~  " + end;
            // 解析日期字符串为 LocalDate 对象
            LocalDate startDate = LocalDate.parse(start, DATE_FORMATTER);
            if (StringUtils.isBlank(end)) {
                end = DateUtil.getCurrentDate();
                dateStr = start + " ~  至今";
            }
            LocalDate endDate = LocalDate.parse(end, DATE_FORMATTER);

            // 计算两个日期之间的 Period 对象
            Period period = Period.between(startDate, endDate);

            String periodStr = "";
            // 提取相差的年和月
            int years = period.getYears();
            int months = period.getMonths();
            if (years > 0) {
                if (months == 0) {
                    periodStr = years + "年";
                } else {
                    periodStr = years + "年" + months + "个月";
                }
            } else {
                periodStr = months + "个月";
            }
            return dateStr + "(" + periodStr + ")";

        } catch (Exception e) {
            new BaseBean().writeLog("工作信息日期计算失败", e);
            return "";
        }
    }
}
