package com.gci.boot.modules.statis.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.modules.statis.service.InsuranceRecordService;
import com.gci.boot.modules.vehicles.mapper.TbInsuranceRecordMapper;
import com.gci.boot.modules.vehicles.vo.InsurancePriceVO;
import com.gci.boot.modules.vehicles.vo.InsuranceRecordStatisVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class InsuranceRecordServiceImpl implements InsuranceRecordService {

    @Autowired
    private TbInsuranceRecordMapper insuranceRecordMapper;

    @Override
    public Page getInsuranceRecordStatisPage(Page page, InsuranceRecordStatisVO vo) {
        List<InsuranceRecordStatisVO> list = insuranceRecordMapper.findYearStatisInsuranceRecord(page, vo);
        //补充 车辆年度保费、保费变化、次年保费预算
        List<InsurancePriceVO> priceList = insuranceRecordMapper.findStatisInsurancePrice();
        Map<String, Double> priceMap = priceList.stream()
                .collect(Collectors.toMap(price -> price.getPlateNumber() + "_" + price.getYearTime(), InsurancePriceVO::getAmount));
        for(InsuranceRecordStatisVO record : list){
            record.setAnnualPremium(priceMap.getOrDefault(record.getPlateNumber()+"_"+record.getStatisTime(),null));
            // 获取前一年
            int previousYear = Integer.parseInt(record.getStatisTime()) - 1;
            if(Objects.nonNull(record.getAnnualPremium()) && Objects.nonNull(priceMap.getOrDefault(record.getPlateNumber()+"_"+previousYear,null))){
                double change = record.getAnnualPremium() - priceMap.getOrDefault(record.getPlateNumber()+"_"+previousYear,null);
                DecimalFormat decimalFormat = new DecimalFormat("#.##");
                if (change > 0) {
                    record.setPremiumChange("+" + decimalFormat.format(change));
                } else if (change < 0) {
                    record.setPremiumChange(decimalFormat.format(change));
                } else {
                    record.setPremiumChange("0");
                }

            }
            // 获取后一年
            int nextYear = Integer.parseInt(record.getStatisTime()) + 1;
            record.setNextYearPremium(priceMap.getOrDefault(record.getPlateNumber()+"_"+nextYear,null));
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public Page getInsuranceRecordStatisPageV2(Page page, InsuranceRecordStatisVO vo) {
        //保险基础数据
        List<InsuranceRecordStatisVO> list = insuranceRecordMapper.findYearStatisInsuranceRecordV2(page, vo);
        //出险车次	出险金额（元） 	平均出险金额（元/车次）
        List<InsuranceRecordStatisVO> recordList = insuranceRecordMapper.findYearStatisInsuranceRecord(new Page<InsuranceRecordStatisVO>(1, -1), new InsuranceRecordStatisVO());
        //补充 车辆年度保费、保费变化、次年保费预算
        List<InsuranceRecordStatisVO> listAll = insuranceRecordMapper.findYearStatisInsuranceRecordV2(new Page<InsuranceRecordStatisVO>(1, -1), new InsuranceRecordStatisVO());
        Map<String, Double> priceMap = listAll.stream()
                .collect(Collectors.toMap(price -> price.getPlateNumber() + "_" + price.getStatisTime(), InsuranceRecordStatisVO::getAnnualPremium));
        for(InsuranceRecordStatisVO record : list){
            if(CollectionUtils.isNotEmpty(recordList)){
                InsuranceRecordStatisVO e = recordList.stream().filter(item -> item.getPlateNumber().equals(record.getPlateNumber()) && item.getStatisTime().equals(record.getStatisTime()))
                        .findFirst().orElse(null);
                if(Objects.nonNull(e)){
                    record.setInsuranceAmount(e.getInsuranceAmount());
                    record.setInsuranceTimes(e.getInsuranceTimes());
                    record.setAvgAmount(e.getAvgAmount());
                }
            }
            // 获取前一年
            int previousYear = Integer.parseInt(record.getStatisTime()) - 1;
            if(Objects.nonNull(record.getAnnualPremium()) && Objects.nonNull(priceMap.getOrDefault(record.getPlateNumber()+"_"+previousYear,null))){
                double change = record.getAnnualPremium() - priceMap.getOrDefault(record.getPlateNumber()+"_"+previousYear,null);
                DecimalFormat decimalFormat = new DecimalFormat("#.##");
                if (change > 0) {
                    record.setPremiumChange("+" + decimalFormat.format(change));
                } else if (change < 0) {
                    record.setPremiumChange(decimalFormat.format(change));
                } else {
                    record.setPremiumChange("0");
                }

            }
            // 获取后一年
            int nextYear = Integer.parseInt(record.getStatisTime()) + 1;
            record.setNextYearPremium(priceMap.getOrDefault(record.getPlateNumber()+"_"+nextYear,null));
        }
        page.setRecords(list);
        return page;
    }
}
