package com.srs.comprehensive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.srs.common.annotation.DataSource;
import com.srs.common.enums.DataSourceType;
import com.srs.comprehensive.domain.CphStuScoreMiddle;
import com.srs.comprehensive.domain.Dto.CphStuScoreMiddleDto;
import com.srs.comprehensive.domain.Vo.CphStuScoreMiddleDtoVo;
import com.srs.comprehensive.mapper.CphStuScoreMiddleDtoMapper;
import com.srs.comprehensive.mapper.CphStuScoreMiddlesMapper;
import com.srs.comprehensive.service.ICphStuScoreMiddlesService;
import com.srs.comprehensive.util.ListSliceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class CphStuScoreMiddlesServiceImpl implements ICphStuScoreMiddlesService {

    @Autowired
    private CphStuScoreMiddlesMapper cphStuScoreMiddlesMapper;

    @Autowired
    private CphStuScoreMiddleDtoMapper cphStuScoreMiddleDtoMapper;

    @Override
    @DataSource(DataSourceType.DATABASE)//sqlserver
    public List<CphStuScoreMiddle> serectCphStuScoreMiddlesAdd() {
        List<CphStuScoreMiddle> cphStuScoreMiddles = cphStuScoreMiddlesMapper.selectList(null);//sql
        return cphStuScoreMiddles;
    }

    //分页查询学生成绩
    @Override
    @DataSource(DataSourceType.DATABASE)//sqlserver
    public List<Map<String,Object>> serectCphStuScoreMiddlesXh(int page, int pageSize,String xh) {
        return cphStuScoreMiddlesMapper.CphStuScoreMiddlePages(page,pageSize,xh);
    }

    //查询总数
    @Override
    @DataSource(DataSourceType.DATABASE)//sqlserver
    public int serectCphStuScoreMiddleCount(String stuNo) {
        return cphStuScoreMiddlesMapper.CphStuScoreMiddleCount(stuNo);
    }

    @Override
    public void typeConversion(List<Map<String,Object>> mapList){
        ArrayList<CphStuScoreMiddle> cphStuScoreMiddleArrayList = new ArrayList<>();

    }

    @Override
    @DataSource(DataSourceType.MASTER)//mysql
    //@Transactional(rollbackFor=Exception.class)
    public List<Map<String,Object>> insertCphStuScoreMiddlesDel(List<Map<String,Object>> cphStuScoreMiddleList) {
        List<Map<String,Object>> stuScoreMiddleAddData = new ArrayList<>(cphStuScoreMiddleList);
        Long aLong = cphStuScoreMiddleDtoMapper.selectCount(null);//总数
        int pageNum=1;
        int pageSize=50;
        long sum=aLong/ pageSize;//需要循环的次数
        long sumS=aLong%pageSize>0? sum + 1: sum;
        QueryWrapper<CphStuScoreMiddleDto> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByDesc("stu_no", "kcdm");
        for (pageNum=1; pageNum <= sumS; pageNum++) {
            List<CphStuScoreMiddleDto> cphStuScoreMiddleDtos = cphStuScoreMiddleDtoMapper
                    .selectPage(new Page<>(pageNum, pageSize), objectQueryWrapper).getRecords();

            // 修改
            List<CphStuScoreMiddleDto> intersection = new ArrayList<>();
            for (CphStuScoreMiddleDto dto : cphStuScoreMiddleDtos) {
                for (Map<String, Object> middle : cphStuScoreMiddleList) {
                    if (dto.getStuNo().equals(middle.get("XH").toString())
                            && dto.getKcdm().equals(middle.get("KCDM").toString())) {
                        CphStuScoreMiddleDto cphStuScoreMiddleDto = new CphStuScoreMiddleDto();
                        cphStuScoreMiddleDto.setId(dto.getId());
                        if (middle.get("XQMC") != null) {
                            cphStuScoreMiddleDto.setXqdm(middle.get("XQMC").toString());
                        }
                        if (middle.get("XSHDXF") != null) {
                            cphStuScoreMiddleDto.setXf(new BigDecimal(middle.get("XSHDXF").toString()));
                        }
                        if (middle.get("CJ") != null) {
                            if (Objects.equals(middle.get("CJ").toString(), "不合格")) {
                                cphStuScoreMiddleDto.setCj("45.00");
                            } else if (Objects.equals(middle.get("CJ").toString(), "合格")) {
                                cphStuScoreMiddleDto.setCj("65.00");
                            } else if (Objects.equals(middle.get("CJ").toString(), "中等")) {
                                cphStuScoreMiddleDto.setCj("75.00");
                            } else if (Objects.equals(middle.get("CJ").toString(), "良好")) {
                                cphStuScoreMiddleDto.setCj("85.00");
                            } else if (Objects.equals(middle.get("CJ").toString(), "优秀")) {
                                cphStuScoreMiddleDto.setCj("95.00");
                            } else if (Objects.equals(middle.get("CJ").toString(), "0.00")||
                                    Objects.equals(middle.get("CJ").toString(), "")||
                                    Objects.equals(middle.get("CJ").toString(), null)||
                                    Objects.equals(middle.get("CJ").toString(), "NULL")||
                                    Objects.equals(middle.get("CJ").toString(), "null")) {
                                if (middle.get("BkScore")!=null){
                                    if (
                                        !Objects.equals(middle.get("BkScore").toString(), "")||
                                        !Objects.equals(middle.get("BkScore").toString(), "null")||
                                        !Objects.equals(middle.get("BkScore").toString(), "NULL")||
                                        !Objects.equals(middle.get("BkScore").toString(), null)){
                                            cphStuScoreMiddleDto.setCj(middle.get("BkScore").toString());
                                    } else {
                                        cphStuScoreMiddleDto.setCj(middle.get("CJ").toString());
                                    }
                                }else {
                                    cphStuScoreMiddleDto.setCj(middle.get("CJ").toString());
                                }
                            } else{
                                cphStuScoreMiddleDto.setCj(middle.get("CJ").toString());
                            }
                        }
                        if (middle.get("XSHDJD") != null) {
                            cphStuScoreMiddleDto.setJd(new BigDecimal(middle.get("XSHDJD").toString()));
                        }
                        if (middle.get("FZLX") != null) {
                            cphStuScoreMiddleDto.setFzlx(Long.parseLong(middle.get("FZLX").toString()));
                        }
                        if (middle.get("XH") != null) {
                            cphStuScoreMiddleDto.setStuNo(middle.get("XH").toString());
                        }
                        if (middle.get("KCDM") != null) {
                            cphStuScoreMiddleDto.setKcdm(middle.get("KCDM").toString());
                        }
                        if (middle.get("passed") != null) {
                            cphStuScoreMiddleDto.setIsPass(middle.get("passed").toString());
                        }
                        intersection.add(cphStuScoreMiddleDto);
                        break;
                    }
                }
            }
            if (intersection.size() != 0) {
                List<List<CphStuScoreMiddleDto>> lists = ListSliceUtil.updateSlice(intersection);
                if (lists != null) {
                    for (List<CphStuScoreMiddleDto> dto : lists) {
                        cphStuScoreMiddleDtoMapper.updateBatchByIdList(dto);//批量更新
                    }
                }
            }
            // 添加
            List<Map<String, Object>> difference = new ArrayList<>(cphStuScoreMiddleList);
            //List<Map<String, Object>> differenceCopy = new ArrayList<>(cphStuScoreMiddleList);
            for (Map<String, Object> dtoS : difference) {//所有的减去交集
                for (CphStuScoreMiddleDto middleDto : intersection) {
                    if (dtoS.get("XH").toString().equals(middleDto.getStuNo()) &&
                            dtoS.get("KCDM").toString().equals(middleDto.getKcdm())) {
                        stuScoreMiddleAddData.remove(dtoS);
                        break;
                    }
                }
            }

//            List<CphStuScoreMiddleDto> cphStuScoreMiddleDto = differenceCopy.stream()
//                    .map(m -> convertToB(m)).collect(Collectors.toList());
//            if (!cphStuScoreMiddleDto.isEmpty()) {
//                cphStuScoreMiddleDtoMapper.insertBatchSomeColumn(cphStuScoreMiddleDto);//添加
//            }
            cphStuScoreMiddleDtos.clear();
        }
        return stuScoreMiddleAddData;
    }

    @Override
    public void syncCphStuScoreMiddles() {
            List<CphStuScoreMiddle> cphStuScoreMiddles = serectCphStuScoreMiddlesAdd();
            //insertCphStuScoreMiddlesDel(cphStuScoreMiddles);
    }

    @DataSource(DataSourceType.MASTER)//mysql
    //@Transactional(rollbackFor=Exception.class)
    public List<CphStuScoreMiddleDtoVo> insertCphStuScoreMiddlesDelS(String stuNo) {
        return cphStuScoreMiddleDtoMapper.getCphStuScoreMiddleAll(stuNo);
    }

    //转换
    private CphStuScoreMiddleDto convertToB(Map<String,Object> a) {
        CphStuScoreMiddleDto b = new CphStuScoreMiddleDto();
        // 进行相应的属性赋值
        if (a.get("CJ")!=null) {
            if (Objects.equals(a.get("CJ").toString(), "不合格")) {
                b.setCj("45.00");
            } else if (Objects.equals(a.get("CJ").toString(), "合格")) {
                b.setCj("65.00");
            } else if (Objects.equals(a.get("CJ").toString(), "中等")) {
                b.setCj("75.00");
            } else if (Objects.equals(a.get("CJ").toString(), "良好")) {
                b.setCj("85.00");
            } else if (Objects.equals(a.get("CJ").toString(), "优秀")) {
                b.setCj("95.00");
            } else if (Objects.equals(a.get("CJ").toString(), "0.00")||
                    Objects.equals(a.get("CJ").toString(), "")||
                    Objects.equals(a.get("CJ").toString(), null)||
                    Objects.equals(a.get("CJ").toString(), "NULL")||
                    Objects.equals(a.get("CJ").toString(), "null")) {
                System.out.println(a.get("CJ"));
                if (a.get("BkScore")!=null){
                    if (!Objects.equals(a.get("BkScore").toString(), "0.00") ||
                        !Objects.equals(a.get("BkScore").toString(), "")||
                        !Objects.equals(a.get("BkScore").toString(), "null")||
                        !Objects.equals(a.get("BkScore").toString(), "NULL")||
                        !Objects.equals(a.get("BkScore").toString(), null)){
                        b.setCj(a.get("BkScore").toString());
                    } else {
                        b.setCj(a.get("CJ").toString());
                    }
                }else {
                    b.setCj(a.get("CJ").toString());
                }
            }else {
                b.setCj(a.get("CJ").toString());
            }
        }
        b.setXqid(1L);
        if (a.get("XND") != null) {
            b.setXndm(a.get("XND").toString());
        }
        if (a.get("XQMC")!=null) {
            b.setXqdm(a.get("XQMC").toString());
        }
        if (a.get("XH")!=null) {
            b.setStuNo(a.get("XH").toString());
        }
        if (a.get("KCDM")!=null) {
            b.setKcdm(a.get("KCDM").toString());
        }
        if (a.get("XSHDXF")!=null) {
            b.setXf(new BigDecimal(a.get("XSHDXF").toString()));
        }
        if (a.get("XSHDJD")!=null) {
            b.setJd(new BigDecimal(a.get("XSHDJD").toString()));
        }
        if (a.get("FZLX")!=null) {
            b.setFzlx(Long.parseLong(a.get("FZLX").toString()));
        }
        if (a.get("Passed")!=null) {
            b.setIsPass(a.get("Passed").toString());
        }
        return b;
    }

    //同步学生成绩，先清空在添加
    @Override
    public void convertToT() {
        emptyTableDate();
        int pageNum=1;
        int pageSize=10000;
        int studentInfoNumber = serectCphStuScoreMiddleCount(null);//总数
        int sum=studentInfoNumber/ pageSize;//需要循环的次数
        int sumS=studentInfoNumber%pageSize>0? sum + 1: sum;
        QueryWrapper<CphStuScoreMiddleDto> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByDesc("stu_no", "kcdm");
        for (pageNum=1; pageNum <= sumS; pageNum++){
            List<Map<String,Object>> cphStuScoreMiddles = serectCphStuScoreMiddlesXh(pageNum,pageSize,null);
            selectCphStuScoreMiddleEmptyAndAdd(cphStuScoreMiddles);
            cphStuScoreMiddles.clear();
        }

    }

    //清空数据表
    @Override
    @DataSource(DataSourceType.MASTER)//mysql
    public void emptyTableDate(){
        cphStuScoreMiddleDtoMapper.updateTabeDate();
    }

    //同步学生成绩
    @Override
    @DataSource(DataSourceType.MASTER)//mysql
    public void selectCphStuScoreMiddleEmptyAndAdd(List<Map<String,Object>> cphStuScoreMiddles) {
        List<CphStuScoreMiddleDto> cphStuScoreMiddleDto = cphStuScoreMiddles.stream()
                .map(m -> convertToB(m)).collect(Collectors.toList());
        if (cphStuScoreMiddleDto.size() != 0) {
            cphStuScoreMiddleDtoMapper.insertBatchSomeColumn(cphStuScoreMiddleDto);//添加
        }

    }
}
