package com.ym.mec.biz.dal.dto;

import com.ym.mec.biz.dal.entity.IndexBaseMonthData;
import com.ym.mec.biz.dal.enums.IndexDataType;
import com.ym.mec.common.constant.CommonConstants;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @Author Joburgess
 * @Date 2021/1/7 0007
 */
public class IndexBaseDto {

    private IndexDataType dataType;

    private String title;

    private BigDecimal percent;

    private List<IndexBaseMonthData> indexMonthData;

    public IndexBaseDto() {
    }

    public IndexBaseDto(String title) {
        this.title = title;
    }

    public IndexBaseDto(IndexDataType dataType, String title) {
        this.dataType = dataType;
        this.title = title;
    }

    public IndexBaseDto(String title, BigDecimal percent, List<IndexBaseMonthData> indexMonthData) {
        this.title = title;
        this.percent = percent;
        this.indexMonthData = indexMonthData;
    }

    public IndexDataType getDataType() {
        return dataType;
    }

    public void setDataType(IndexDataType dataType) {
        this.dataType = dataType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public BigDecimal getPercent() {
        return percent;
    }

    public void setPercent(BigDecimal percent) {
        this.percent = percent;
    }

    public List<IndexBaseMonthData> getIndexMonthData() {
        return indexMonthData;
    }

    public void setIndexMonthData(List<IndexBaseMonthData> indexMonthData, Date currentMonth) {
        this.indexMonthData = indexMonthData;
        if(!CollectionUtils.isEmpty(indexMonthData)){
            BigDecimal total = indexMonthData.stream().map(IndexBaseMonthData::getTotalNum).reduce(BigDecimal.ZERO, BigDecimal::add);
            if(IndexDataType.ACTIVATION_RATE.equals(dataType)||IndexDataType.HOMEWORK_CREATE_RATE.equals(dataType)||IndexDataType.HOMEWORK_SUBMIT_RATE.equals(dataType)||IndexDataType.HOMEWORK_COMMENT_RATE.equals(dataType)){
                if(total.compareTo(BigDecimal.ZERO)==0){
                    this.percent = BigDecimal.ZERO;
                }else{
                    BigDecimal activateNum = indexMonthData.stream().map(IndexBaseMonthData::getActivateNum).reduce(BigDecimal.ZERO, BigDecimal::add);
                    this.percent = activateNum.divide(total, CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(100)).setScale(CommonConstants.DECIMAL_FINAL_PLACE, BigDecimal.ROUND_DOWN);
                }
            }else{
                this.percent = indexMonthData.stream().filter(i->currentMonth.compareTo(i.getMonth())==0).max(Comparator.comparing(IndexBaseMonthData::getMonth)).get().getPercent();
            }
        }
    }
}
