package com.qmth.paper.library.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.paper.library.business.bean.result.ScanStatExportResult;
import com.qmth.paper.library.business.bean.result.ScanStatResult;
import com.qmth.paper.library.business.entity.PaperLibrary;
import com.qmth.paper.library.business.mapper.PaperLibraryMapper;
import com.qmth.paper.library.business.service.PaperLibraryService;
import com.qmth.paper.library.business.service.PaperScanStatService;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.util.ServletUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class PaperScanStatServiceImpl extends ServiceImpl<PaperLibraryMapper, PaperLibrary> implements PaperScanStatService {

    private final Logger logger = LoggerFactory.getLogger(PaperScanStatServiceImpl.class);

    @Resource
    private PaperLibraryService paperLibraryService;

    @Override
    public IPage<ScanStatResult> pageScanStat(Long scanDate, String scanner, Integer pageNumber, Integer pageSize) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        String scanDateStr = null;
        if(scanDate != null && scanDate > 0) {
            scanDateStr = DateFormatUtils.format(scanDate, SystemConstant.SHORT_DATE_PATTERN);
        }

        IPage<ScanStatResult> scanStatPage = baseMapper.listScanStat(new Page<>(pageNumber, pageSize), schoolId, scanDateStr, scanner);
        for (ScanStatResult scanStatResult : scanStatPage.getRecords()) {
            //设置扫描学生数
            int studentCount = paperLibraryService.countScanStudentByScannerAndDate(scanStatResult.getCreateId(), scanStatResult.getScanDateStr());
            scanStatResult.setStudentCount(studentCount);
            scanStatResult.setScanDate(getLongDate(scanStatResult.getScanDateStr()));
        }
        return scanStatPage;
    }

    @Override
    public List<ScanStatExportResult> listScanStat(Long scanDate, String scanner) {
        Long schoolId = Long.valueOf(ServletUtil.getRequestHeaderSchoolId().toString());
        String scanDateStr = null;
        if(scanDate != null && scanDate > 0) {
            scanDateStr = DateFormatUtils.format(scanDate, SystemConstant.SHORT_DATE_PATTERN);
        }

        List<ScanStatExportResult> scanStatList = baseMapper.listScanStatExport(schoolId, scanDateStr, scanner);
        for(ScanStatExportResult scanStatExportResult : scanStatList) {
            //设置扫描学生数
            int studentCount = paperLibraryService.countScanStudentByScannerAndDate(scanStatExportResult.getCreateId(), scanStatExportResult.getScanDateStr());
            scanStatExportResult.setStudentCount(studentCount);
        }

        return scanStatList;
    }

    private Long getLongDate(String dateStr) {
        Date date;
        try {
            date = DateUtils.parseDate(dateStr+" 00:00:00", SystemConstant.DEFAULT_DATE_PATTERN);
        } catch (ParseException e) {
            logger.error(String.format("[扫描统计管理]日期转换失败,dateStr:%s", dateStr), e);
            return null;
        }
        return date.getTime();
    }

}
