package org.jeecg.modules.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.yx.entity.YxHistoryScoreControlLine;
import org.jeecg.modules.yx.mapper.YxHistoryScoreControlLineMapper;
import org.jeecg.modules.yx.service.IYxHistoryScoreControlLineService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 历年各专业省控分数线
 * @Author: jeecg-boot
 * @Date: 2023-12-02
 * @Version: V1.0
 */
@Service
public class YxHistoryScoreControlLineServiceImpl extends ServiceImpl<YxHistoryScoreControlLineMapper, YxHistoryScoreControlLine> implements IYxHistoryScoreControlLineService {

    @Override
    public IPage<YxHistoryScoreControlLine> pageList(Page<YxHistoryScoreControlLine> page, YxHistoryScoreControlLine yxHistoryScoreControlLine) {
        IPage<YxHistoryScoreControlLine> yxHistoryScoreControlLineIPage = baseMapper.pageList(page, yxHistoryScoreControlLine);
        List<YxHistoryScoreControlLine> newRecords = new ArrayList<>();
        List<YxHistoryScoreControlLine> records = yxHistoryScoreControlLineIPage.getRecords();
        YxHistoryScoreControlLine historyScoreControlLine = null;
        for (YxHistoryScoreControlLine record : records) {
            if ("本科A段".equals(record.getBatch())) {
                historyScoreControlLine = new YxHistoryScoreControlLine();
                BeanUtils.copyProperties(record, historyScoreControlLine);
                historyScoreControlLine.setBatch("提前批");
                newRecords.add(historyScoreControlLine);
            }
            newRecords.add(record);
        }
        yxHistoryScoreControlLineIPage.setRecords(newRecords);
        return yxHistoryScoreControlLineIPage;
    }

    @Override
    public YxHistoryScoreControlLine getByProfessionalCategoryOfYear(String year, String professionalCategory, String category, String batch) {
        LambdaQueryWrapper<YxHistoryScoreControlLine> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(YxHistoryScoreControlLine::getYear, year);
        lambdaQueryWrapper.eq(YxHistoryScoreControlLine::getProfessionalCategory, professionalCategory);
        lambdaQueryWrapper.eq(YxHistoryScoreControlLine::getCategory, category);
        lambdaQueryWrapper.eq(YxHistoryScoreControlLine::getBatch, batch);
        lambdaQueryWrapper.last("limit 1");
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<YxHistoryScoreControlLine> listByProfessionalCategoryOfYear(String year, String professionalCategory, String category) {
        LambdaQueryWrapper<YxHistoryScoreControlLine> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(YxHistoryScoreControlLine::getYear, year);
        lambdaQueryWrapper.eq(YxHistoryScoreControlLine::getProfessionalCategory, professionalCategory);
        lambdaQueryWrapper.eq(YxHistoryScoreControlLine::getCategory, category);
        lambdaQueryWrapper.orderByDesc(YxHistoryScoreControlLine::getBatch);
        List<YxHistoryScoreControlLine> newList = new ArrayList<>();
        List<YxHistoryScoreControlLine> list = this.list(lambdaQueryWrapper);
        YxHistoryScoreControlLine yxHistoryScoreControlLine1 = null;
        for (YxHistoryScoreControlLine yxHistoryScoreControlLine : list) {
            if (yxHistoryScoreControlLine.getBatch().equals("本科A段")) {
                yxHistoryScoreControlLine1 = new YxHistoryScoreControlLine();
                BeanUtils.copyProperties(yxHistoryScoreControlLine, yxHistoryScoreControlLine1);
                yxHistoryScoreControlLine1.setBatch("提前批");
                newList.add(yxHistoryScoreControlLine1);
            }
            newList.add(yxHistoryScoreControlLine);
        }
        return newList;
    }

    @Override
    public Map<String, YxHistoryScoreControlLine> mapsBatchByProfessionalCategoryOfYear(String year, String professionalCategory, String category) {
        List<YxHistoryScoreControlLine> yxHistoryScoreControlLines = this.listByProfessionalCategoryOfYear(year, professionalCategory, category);
        return yxHistoryScoreControlLines.stream().collect(Collectors.toMap(y -> y.getBatch(), y -> y));
    }

    @Override
    public Map<String, YxHistoryScoreControlLine> allMaps() {
        List<YxHistoryScoreControlLine> list = this.list();
        Map<String, YxHistoryScoreControlLine> maps = new LinkedHashMap<>();
        String key = null;
        for (YxHistoryScoreControlLine record : list) {
            //文科_本科A段_美术与设计_2023
            key = record.getCategory() + "_" + record.getBatch() + "_" + record.getProfessionalCategory() + "_" + record.getYear();
            maps.put(key,record);
            if (record.getBatch().equals("本科A段")) {
                key = record.getCategory() + "_本科提前批_" + record.getProfessionalCategory() + "_" + record.getYear();
                maps.put(key,record);
            }
        }
        return maps;
    }
}
