package com.koobietech.eas.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.bean.BeanUtil;
import com.koobietech.eas.common.result.PageData;
import com.koobietech.eas.dao.Pojo.EasArcTlsScoresPojo;
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.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
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(1);
            params.setHeadRows(1);
            try {
                List<EasArcTlsScoresPojo> score = ExcelImportUtil.importExcel(
                        inputStream, EasArcTlsScoresPojo.class, params);
                for ( EasArcTlsScoresPojo easArcTlsScoresPojo : score) {
                    if ( easArcTlsScoresPojo.getScore() == null
                            || easArcTlsScoresPojo.getScore().equals("") ) {
                        continue;
                    }
                    easArcTlsScoresPojo.setCreateTime(new Date());
                    EasArcTlsScores easArcTlsScores = new EasArcTlsScores();
                    BeanUtil.copyProperties(easArcTlsScoresPojo, easArcTlsScores);
                    easArcTlsScoresMapper.insert(easArcTlsScores);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return true;
    }

}
