package com.koobietech.eas.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import cn.afterturn.easypoi.handler.inter.IReadHandler;
import com.koobietech.eas.common.result.PageData;
import com.koobietech.eas.mbg.mapper.EasArcTlsScoresMapper;
import com.koobietech.eas.mbg.model.EasArcTlsScores;
import com.koobietech.eas.mbg.model.EasArcTlsScoresExample;
import com.koobietech.eas.service.EasArcTlsScoresService;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class EasArcTlsScoresServiceImpl implements EasArcTlsScoresService {

    @Resource
    EasArcTlsScoresMapper easArcTlsScoresMapper;


    @Override
    public int addStudentScore(EasArcTlsScores easArcTlsScores) {
        easArcTlsScores.setCreateTime(new Date());
        easArcTlsScores.setModifyTime(new Date());
        return easArcTlsScoresMapper.insert(easArcTlsScores);
    }

    @Override
    public int updateStudentScore(EasArcTlsScores easArcTlsScores) {
        return easArcTlsScoresMapper.updateByPrimaryKey(easArcTlsScores);
    }

    @Override
    public List<EasArcTlsScores> selectAllStudentScores() {
        return easArcTlsScoresMapper.selectByExample(null);
    }

    @Override
    public PageData selectScoreByExample(EasArcTlsScores easArcTlsScores) {
        EasArcTlsScoresExample easArcTlsScoresExample = new EasArcTlsScoresExample();
        EasArcTlsScoresExample.Criteria criteria = easArcTlsScoresExample.createCriteria();
        if (Objects.nonNull(easArcTlsScores)){
            if (Objects.nonNull(easArcTlsScores.getId())) {
                criteria.andIdEqualTo(easArcTlsScores.getId());
            }
            if (StringUtils.hasText(easArcTlsScores.getStudentNumber())) {
                criteria.andStudentNumberLike("%"+easArcTlsScores.getStudentNumber()+"%");
            }
            if (Objects.nonNull(easArcTlsScores.getTestDate())) {
                criteria.andTestDateGreaterThan(easArcTlsScores.getTestDate());
            }
            if (Objects.nonNull(easArcTlsScores.getScore())) {
                criteria.andScoreEqualTo(easArcTlsScores.getScore());
            }
            if (Objects.nonNull(easArcTlsScores.getPassRate())) {
                criteria.andPassRateEqualTo(easArcTlsScores.getPassRate());
            }
            if (Objects.nonNull(easArcTlsScores.getExcelRate())) {
                criteria.andExcelRateEqualTo(easArcTlsScores.getExcelRate());
            }
            if (Objects.nonNull(easArcTlsScores.getCreateTime())) {
                criteria.andCreateTimeGreaterThan(easArcTlsScores.getCreateTime());
            }
            if (Objects.nonNull(easArcTlsScores.getModifyTime())) {
                criteria.andCreateTimeGreaterThan(easArcTlsScores.getModifyTime());
            }
            if (Objects.nonNull(easArcTlsScores.getCreateUid())) {
                criteria.andCreateUidEqualTo(easArcTlsScores.getCreateUid());
            }
            if (StringUtils.hasText(easArcTlsScores.getComment())) {
                criteria.andCommentLike("%"+easArcTlsScores.getComment()+"%");
            }
        }
        List<EasArcTlsScores> easArcTlsScores1 = easArcTlsScoresMapper.selectByExample(easArcTlsScoresExample);
        long l = easArcTlsScoresMapper.countByExample(easArcTlsScoresExample);
        return PageData.init(easArcTlsScores1,l);
    }

        @Override
    public Boolean importExcelScores(InputStream inputStream) {
            ImportParams params = new ImportParams();
            params.setTitleRows(0);
            params.setStartSheetIndex(1);
            try {
                ExcelImportUtil.importExcelBySax(
                        new FileInputStream("C:\\Users\\常忠宇\\Desktop\\新建XLSX工作表.xlsx"),
                        Map.class, params,
                        new IReadHandler<Map>() {
                            @Override
                            public void handler(Map o) {
                                System.out.println(o);
                            }
                            @Override
                            public void doAfterAll() {
                                System.out.println("全部执行完毕了--------------------------------");
                            }
                        });
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
//            ImportParams params = new ImportParams();
//            params.setTitleRows(0);
//            try {
//                List<EasArcTlsScores> list = new ExcelImportService().importExcelByIs(inputStream,
//                        EasArcTlsScores.class, params, false).getList();
//                for ( EasArcTlsScores eas : list) {
//                    System.out.println( eas );
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            return true;
    }

//    @Override
//    public Boolean importExcelScores(InputStream inputStream) {
//        try {
////            创建一个Excel实体
//            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
////            获取第一个表格页面
//            XSSFSheet sheetAt = workbook.getSheetAt(0);
////            获取第一个表格页面共有多少行
//            int lastRowNum = sheetAt.getLastRowNum();
////            遍历表格行数
//            for (int rowNum = 0; rowNum < lastRowNum; rowNum++) {
////                取表格中的每一行
//                XSSFRow row = sheetAt.getRow(rowNum);
////                取每一行共有多少列
//                short lastCellNum = row.getLastCellNum();
////                创建一个对象准备填入数据
//                EasArcTlsScores easArcTlsScores = new EasArcTlsScores();
////                循环表格列数
//                for (int cellNum = 0; cellNum < lastCellNum; cellNum++) {
//                    CellType cellType = CellType.forInt(row.getCell(cellNum).getCellType());
//                    switch (cellType) {
//                        case _NONE:break;
//                        case STRING:
//                            row.getCell(cellNum).getStringCellValue();
//                            break;
//                        case NUMERIC:
////                            判断是否是日期类型
//                            if (DateUtil.isCellDateFormatted(row.getCell(rowNum))){
////                                如果是日期类型将其转变为JavaDate格式
//                                Date javaDate = DateUtil.getJavaDate(row.getCell(cellNum).getNumericCellValue());
//                                if (cellNum == 5){
////                                    若是第五列将其设置为考试时间
//                                    easArcTlsScores.setTestDate( javaDate );
////                                    第九列设置为创建时间
//                                }else if (cellNum == 9) {
//                                    easArcTlsScores.setCreateTime( javaDate );
////                                    第十列设置为modifyTime
//                                }else if (cellNum == 10) {
//                                    easArcTlsScores.setModifyTime( javaDate );
//                                }
//                            }else {
//                                if (cellNum == 1) {
//                                    easArcTlsScores.setId((int)row.getCell(cellNum).getNumericCellValue());
//                                }
//                                if (cellNum == 2) {
//                                    easArcTlsScores.setStudentNumber(String.valueOf(row.getCell(cellNum).getNumericCellValue()));
//                                }
//                                if (cellNum == 3) {
//                                    easArcTlsScores.setCategoryId((int) row.getCell(cellNum).getNumericCellValue());
//                                }
//                                if (cellNum == 4) {
//                                    easArcTlsScores.setSubjectId((int) row.getCell(cellNum).getNumericCellValue());
//                                }
//                                if (cellNum == 6){
//                                    easArcTlsScores.setScore(BigDecimal.valueOf(row.getCell(cellNum).getNumericCellValue()));
//                                }
//                                if (cellNum == 7){
//                                    easArcTlsScores.setPassRate(BigDecimal.valueOf(row.getCell(cellNum).getNumericCellValue()));
//                                }
//                                if (cellNum == 8){
//                                    easArcTlsScores.setExcelRate(BigDecimal.valueOf(row.getCell(cellNum).getNumericCellValue()));
//                                }
//                                if (cellNum == 11){
//                                    easArcTlsScores.setCreateUid((int) row.getCell(cellNum).getNumericCellValue());
//                                }
//                            }
//                                break;
//                        case FORMULA:
//                            break;
//
//                        case BLANK:
//                            break;
//
//                        case BOOLEAN:
//                            break;
//
//                        case ERROR:
//                            break;
//                    }
//                }
//                easArcTlsScoresMapper.insert(easArcTlsScores);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return true;
//    }
}
