package cn.ezhizao.project.business.order.domain;

import cn.ezhizao.common.utils.DateUtils;
import cn.ezhizao.project.business.upgrade.domain.BizArchiveInputDetailUpdate;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

@Data
public class OrderExcelImportLoopGroupModel {

    private Long companyId;

    private Long taskTypeId;

    private Date startMonth;

    private Date endMonth;

    private Integer monthNum = 0;

    private List<BizArchiveInputDetailUpdate> modelList = new ArrayList<>();

    public Boolean isIncludeModel(BizArchiveInputDetailUpdate model) {
        if (modelList.isEmpty()) {
            return true;
        }

        Date inputStartMonth = model.getStartMonth();
        Date inputEndMonth = model.getEndMonth();
        Date currentStartMonth = this.getStartMonth();
        Date currentEndMonth = this.getEndMonth();

        if (DateUtils.addMonths(inputEndMonth, 1).compareTo(currentStartMonth) == 0
                || DateUtils.addMonths(currentEndMonth, 1).compareTo(inputStartMonth) == 0) {
            return true;
        }
        return false;
    }

    public void addModel(BizArchiveInputDetailUpdate model) {

        this.modelList.add(model);

        this.modelList = this.modelList.stream().sorted(Comparator.comparing(BizArchiveInputDetailUpdate::getStartMonth))
                .collect(Collectors.toList());

        this.startMonth = this.modelList.get(0).getStartMonth();

        this.endMonth = this.modelList.get(this.modelList.size() - 1).getEndMonth();

        this.monthNum += (model.getServiceNum() + model.getFreeNum());
    }
}
