package com.gz.service.welcome.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.gz.dto.system.ArchivesTreeDTO;
import com.gz.mapper.borrow.ArchiveBorrowMapper;
import com.gz.mapper.system.ArchivesTreeMapper;
import com.gz.mapper.welcome.StatisticsMapper;
import com.gz.rvo.borrow.ArchiveBorrowHistoryRVO;
import com.gz.rvo.welcome.ArchiveCountRVO;
import com.gz.rvo.welcome.BorrowCountRVO;
import com.gz.rvo.welcome.BorrowRecordRVO;
import com.gz.service.welcome.StatisticsService;
import com.gz.utils.JwtUtils;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 统计Service
 *
 * @author LiuchangLan
 * @date 2021/3/2 9:25
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private ArchivesTreeMapper archivesTreeMapper;

    @Resource
    private ArchiveBorrowMapper archiveBorrowMapper;

    @Resource
    private StatisticsMapper statisticsMapper;

    @Override
    public List<BorrowRecordRVO> selectWaitHandleRecord() {
        return statisticsMapper.selectWaitHandleRecord();
    }

    @Override
    public List<BorrowRecordRVO> selectWaitReturnRecord() {
        return statisticsMapper.selectWaitReturnRecord();
    }

    @Override
    public JSONObject selectArchiveByType() {
        List<ArchiveCountRVO> archiveCountRVOS = statisticsMapper.selectArchiveByMl();
        List<ArchivesTreeDTO> archivesTreeDTOS = archivesTreeMapper.selectAll();
        List<ArchiveCountRVO> data = new ArrayList<>();
        ArrayList<String> x = new ArrayList<>();
        ArrayList<Integer> y = new ArrayList<>();
        for (ArchivesTreeDTO archivesTreeDTO : archivesTreeDTOS) {
            if (archivesTreeDTO.getParentId() == -1) {
                int count = 0;
                for (ArchivesTreeDTO treeDTO : archivesTreeDTOS) {
                    if (archivesTreeDTO.getId().equals(treeDTO.getParentId())) {
                        count += archiveCountRVOS.stream().filter(e -> e.getTitle().equals(treeDTO.getCode())).mapToInt(e -> {
                            Integer a = e.getCount();
                            return a;
                        }).sum();
                    } else if (archivesTreeDTO.getId().equals(treeDTO.getId())) {
                        count += archiveCountRVOS.stream().filter(e -> e.getTitle().equals(treeDTO.getCode())).mapToInt(e -> {
                            Integer a = e.getCount();
                            return a;
                        }).sum();
                    }
                }
                data.add(new ArchiveCountRVO(archivesTreeDTO.getTitle(), count));
                x.add(archivesTreeDTO.getTitle().substring(0, archivesTreeDTO.getTitle().indexOf("档案")));
                y.add(count);
            }
        }
        JSONObject result = new JSONObject();
        result.put("x", x);
        result.put("y", y);
//        result.put("data", data);
        return result;
    }

    @Override
    public JSONObject selectArchiveByYear() {
        ArrayList<String> x = new ArrayList<>();
        ArrayList<Integer> y = new ArrayList<>();
        List<ArchiveCountRVO> archiveCountRVOS = statisticsMapper.selectArchiveByYear();
        for (ArchiveCountRVO archiveCountRVO : archiveCountRVOS) {
            x.add(archiveCountRVO.getTitle());
        }
        JSONObject result = new JSONObject();
        result.put("x", x);
        archiveCountRVOS.stream().forEach(e -> y.add(e.getCount()));
//        result.put("data", archiveCountRVOS);
        result.put("y", y);
        return result;
    }

    @Override
    public Map<String, Object> selectBorrowCountByDate() {
        // 所有历史记录
        List<ArchiveBorrowHistoryRVO> archiveBorrowHistorys = statisticsMapper.selectBorrowCountByDate();
        // 所有树
        List<ArchivesTreeDTO> archivesTreeDTOS = archivesTreeMapper.selectAll();
        // 根据月份分组
        Map<String, List<ArchiveBorrowHistoryRVO>> byMonth = archiveBorrowHistorys
                .stream()
                .collect(Collectors.groupingBy(e -> DateUtil.format(DateUtil.parse(e.getCreateTime()), "yyyy-MM")));

        ArrayList<String> x = new ArrayList<>();
        ArrayList<Map<String, Object>> y = new ArrayList<>();
        ArrayList<String> z = new ArrayList<>();
        // 循环每个月的数量
        for (String key : byMonth.keySet()) {
            x.add(key);
        }
        // 循环类别
        for (ArchivesTreeDTO parentDTO : archivesTreeDTOS) {
            if (parentDTO.getParentId() == -1) {
                z.add(parentDTO.getTitle());
                Map<String, Object> result = new HashMap<>();
                result.put("name", parentDTO.getTitle());
                result.put("type", "line");
                result.put("stack", "借阅量");
                List<Integer> counts = new ArrayList();
                for (String key : x) {
                    Integer count = 0;
                    for (ArchiveBorrowHistoryRVO rvo : byMonth.get(key)) {
                        if (parentDTO.getCode().equals(rvo.getCode())) {
                            count++;
                        }
                    }
                    counts.add(count);
                }
                result.put("data", counts);
                y.add(result);
            }
        }
        Collections.reverse(x);
        Collections.reverse(y);
        return new HashMap<String, Object>() {{
            put("x", x);
            put("y", y);
        }};
    }


    @Override
    public JSONObject selectMyCount() {
        String userId = JwtUtils.getCurrentUserJwtPayload().getId();
        JSONObject result = new JSONObject();
        // 到期未归还
        result.put("waitReturnCount", statisticsMapper.selectMyWaitReturnRecord(userId).size());
        // 即将到期
        result.put("dueSoonCount", statisticsMapper.selectMyDueSoonRecord(userId).size());
        // 申请未处理
        result.put("notHandleCount", statisticsMapper.selectRuditNotHandleCount(userId));
        // 历史借阅数量
        result.put("historyBorrowCount", statisticsMapper.selectMyHistoryBorrowCount(userId));
        return result;
    }

    @Override
    public List<BorrowRecordRVO> selectMyWaitReturnRecord() {
        return statisticsMapper.selectMyWaitReturnRecord(JwtUtils.getCurrentUserJwtPayload().getId());
    }

    @Override
    public List<BorrowRecordRVO> selectMyDueSoonRecord() {
        return statisticsMapper.selectMyDueSoonRecord(JwtUtils.getCurrentUserJwtPayload().getId());
    }
}
