package com.ruoyi.system.service.impl;

import com.aliyun.mns.common.utils.DateUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.ColumnNewsMapper;
import com.ruoyi.system.mapper.InspectInforMapper;
import com.ruoyi.system.mapper.ReportDetailMapper;
import com.ruoyi.system.mapper.ReportQueryLogMapper;
import com.ruoyi.system.service.IIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * @author tjf
 * @Date: 2021/12/31/11:06
 */
@Service
public class IndexServiceImpl implements IIndexService {
    @Autowired
    private InspectInforMapper inspectInforMapper;
    @Autowired
    private ReportDetailMapper reportDetailMapper;
    @Autowired
    private ReportQueryLogMapper reportQueryLogMapper;
    @Autowired
    private ColumnNewsMapper columnNewsMapper;

    /**
     * 获取后台首页
     *
     * @return
     */
    @Override
    public AjaxResult getIndex() {
        Map<String, Object> map = new HashMap<>();
        LocalDate nowDate = LocalDate.now();
        DecimalFormat df = new DecimalFormat("00.0%");
        //全部送检数量
        int inspectNum = inspectInforMapper.selectInspectInforCount(null, null, null);
        //今日送检数量
        int inspectTodayNum = inspectInforMapper.selectInspectInforCount(nowDate, null, null);
        String inspect = "00.0%";
        if (inspectNum > 0) {
            inspect = df.format(inspectTodayNum / inspectNum);
        }
        //全部报告数量
        int reportDetailNum = reportDetailMapper.selectReportDetailCount(null, null, null);
        //今日报告数量
        int reportDetailTodayNum = reportDetailMapper.selectReportDetailCount(nowDate, null, null);
        String reportDetail = "00.0%";
        if (reportDetailNum > 0) {
            reportDetail = df.format(reportDetailTodayNum / reportDetailNum);
        }
        //查询总次数
        int reportQueryLogNum = reportQueryLogMapper.selectReportQueryLogCount(null, null, null);
        //今日查询次数
        int reportQueryLogTodayNum = reportQueryLogMapper.selectReportQueryLogCount(nowDate, null, null);
        String reportQueryLog = "00.0%";
        if (reportQueryLogNum > 0) {
            reportQueryLog = df.format(reportQueryLogTodayNum / reportQueryLogNum);
        }
        //总发文数量
        int columnNewsNum = columnNewsMapper.selectColumnNewsCount(null, null, null);
        //今日发文数量
        int columnNewsTodayNum = columnNewsMapper.selectColumnNewsCount(nowDate, null, null);
        String columnNews = "00.0%";
        if (columnNewsNum > 0) {
            columnNews = df.format(columnNewsTodayNum / columnNewsNum);
        }
        //前一周的所有时间
        List<LocalDate> middleDate = DateUtils.getMiddleDate(nowDate.minusWeeks(1), nowDate);
        List<Integer> inspectNumDate = new ArrayList<>();
        List<Integer> reportDetailNumDate = new ArrayList<>();
        List<Integer> columnNewsNumDate = new ArrayList<>();
        List<Integer> reportQueryLogNumDate = new ArrayList<>();
        for (LocalDate localDate : middleDate) {
            inspectNumDate.add(inspectInforMapper.selectInspectInforCount(localDate, null, null));
            reportDetailNumDate.add(reportDetailMapper.selectReportDetailCount(localDate, null, null));
            reportQueryLogNumDate.add(reportQueryLogMapper.selectReportQueryLogCount(localDate, null, null));
            columnNewsNumDate.add(columnNewsMapper.selectColumnNewsCount(localDate, null, null));
        }
        map.put("inspectNum",inspectNum);
        map.put("inspectTodayNum",inspectTodayNum);
        map.put("inspect",reportDetail);
        map.put("reportDetailNum",reportDetailNum);
        map.put("reportDetailTodayNum",reportDetailTodayNum);
        map.put("reportDetail",reportDetail);
        map.put("reportQueryLogNum",reportQueryLogNum);
        map.put("reportQueryLogTodayNum",reportQueryLogTodayNum);
        map.put("reportQueryLog",reportQueryLog);
        map.put("columnNewsNum",columnNewsNum);
        map.put("columnNewsTodayNum",columnNewsTodayNum);
        map.put("columnNews",columnNews);
        map.put("x",middleDate);
        map.put("inspectNumDate",inspectNumDate);
        map.put("reportDetailNumDate",reportDetailNumDate);
        map.put("columnNewsNumDate",columnNewsNumDate);
        map.put("reportQueryLogNumDate",reportQueryLogNumDate);
        return AjaxResult.success(map);
    }
}
