package com.hz.pm.api.dashboard.manage;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.hz.pm.api.common.model.entity.DataDTO;
import com.hz.pm.api.dashboard.constant.DashboardConstant;
import com.hz.pm.api.dashboard.model.vo.CostStatisticsVO;
import com.hz.pm.api.meeting.entity.domain.Meeting;
import com.hz.pm.api.meeting.entity.domain.MeetingExpert;
import com.hz.pm.api.meeting.service.IMeetingExpertService;
import com.hz.pm.api.meeting.service.IMeetingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname ProjectCostStatisticsManage
 * @Description
 * @Date 2023/12/05 17:44
 * @Author PoffyZhang
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ProjectCostStatisticsManage {

    private final IMeetingExpertService meetingExpertService;

    private final IMeetingService meetingService;

    List<Integer> thisTwoYears = Lists.newArrayList(LocalDateTime.now().getYear() - 1
            ,LocalDateTime.now().getYear());

    public CostStatisticsVO statistics() {
        CostStatisticsVO res = new CostStatisticsVO();
        List<Meeting> meetings = meetingService.list(Wrappers.lambdaQuery(Meeting.class));
        List<Long> meetingIds = meetings.stream().map(Meeting::getId)
                .collect(Collectors.toList());
        //算出所有的 同意的专家
        List<MeetingExpert> meetingExperts = meetingExpertService.listAgreedExperts(meetingIds);

        //评审费  每人500 (0.05万)
        BigDecimal reviewAoumtExpert = DashboardConstant.Expert.REVIEW_AOUMT_EXPERT;

        //计算累积
        res.setReviewAmount(reviewAoumtExpert.multiply(BigDecimal.valueOf(meetingExperts.size())));
        res.setPricingAmount(BigDecimal.ZERO);

        //近两年
        List<DataDTO> twoYearsReviews = Lists.newArrayList();
        List<DataDTO> twoYearsPricing = Lists.newArrayList();
        List<DataDTO> twoYearsAvg = Lists.newArrayList();

        DataDTO reviewAvg = new DataDTO();
        reviewAvg.setName("评审费");
        DataDTO pricingAvg = new DataDTO();
        pricingAvg.setName("核价费");
        BigDecimal reviewTwoYearTotal = BigDecimal.ZERO;
        for(Integer thisYear :  thisTwoYears){
            //查出 年份的 会议数据
            List<Meeting> yearMeetings = meetings.stream().filter(m -> {
                if(Objects.nonNull(m.getStartTime()) &&
                        ( thisYear.equals(m.getStartTime().getYear()))){
                    return Boolean.TRUE;
                }
                return Boolean.FALSE;
            }).collect(Collectors.toList());
            List<Long> yearMeetingIds = yearMeetings.stream().map(Meeting::getId)
                    .collect(Collectors.toList());
            //算出所有的 同意的专家
            List<MeetingExpert> yearMeetingExperts = Lists.newArrayList();
            if(CollUtil.isNotEmpty(yearMeetingIds)){
                yearMeetingExperts = meetingExpertService.listAgreedExperts(yearMeetingIds);
            }
            twoYearsReviews.add(DataDTO.of(thisYear.toString(),reviewAoumtExpert.multiply(BigDecimal
                    .valueOf(yearMeetingExperts.size()))));
            twoYearsPricing.add(DataDTO.of(thisYear.toString(),BigDecimal.ZERO));

            reviewTwoYearTotal = reviewTwoYearTotal.add(reviewAoumtExpert.multiply(BigDecimal
                    .valueOf(yearMeetingExperts.size())));
        }
        reviewAvg.setAmount(reviewTwoYearTotal.divide(BigDecimal.valueOf(2),BigDecimal.ROUND_CEILING,
                RoundingMode.HALF_UP));
        twoYearsAvg.add(reviewAvg);
        twoYearsAvg.add(pricingAvg);
        res.setTwoYearsReviews(twoYearsReviews);
        res.setTwoYearsPricing(twoYearsPricing);
        res.setTwoYearsAvg(twoYearsAvg);
        return res;
    }
}
