package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.UserNucleicTime;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.IUserNucleicTimeService;
import com.ruoyi.system.service.IIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: tjf
 * @Date: 2022/8/15 14:34
 * @Describe:
 */
@Service
public class IndexServiceImpl implements IIndexService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IUserNucleicTimeService userNucleicTimeService;

    @Autowired
    private ISysDictTypeService dictTypeService;
    /**
     * 后台首页统计本月核酸人数,本周人数,对比上周人数
     * @return
     */
    @Override
    public AjaxResult getIndexData() {
        Map<String, Object> indexData = redisCache.getCacheMap(CacheConstants.INDEX_DATA);
        if (indexData != null && indexData.size() > 0){
            return AjaxResult.success(indexData);
        }
        //运行一下数据
        userNucleicTimeService.getIndexData();
        Map<String, Object> indexDataNew = redisCache.getCacheMap(CacheConstants.INDEX_DATA);
        return AjaxResult.success(indexDataNew);
    }

    /**
     * 本周职业 类别核酸情况统计
     * @return
     */
    @Override
    public AjaxResult getJobStyle() {
        Map<String,Object> map = new HashMap<>(2);
        //从字典值中获取X轴坐标
        List<SysDictData> occupationalCategory = dictTypeService.selectDictDataByType("occupational_category");
        List<String> dictLabelList = occupationalCategory.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
        map.put("x",dictLabelList);
        List<UserNucleicTime> jobStyle = userNucleicTimeService.getJobStyle();
        Map<String, List<UserNucleicTime>> collectJobStyle = jobStyle.stream().filter(e -> StringUtils.isNotEmpty(e.getJobStyle())).collect(Collectors.groupingBy(UserNucleicTime::getJobStyle));
        List<Integer> yResult = new ArrayList<>();
        for (String dictLabel : dictLabelList) {
            List<UserNucleicTime> userNucleicTimes = collectJobStyle.get(dictLabel);
            if (userNucleicTimes == null || userNucleicTimes.size() == 0){
                yResult.add(0);
            }else {
                //根据IDCard进行分组,就知道有多少个不同的身份证号码
                Map<String, List<UserNucleicTime>> collectIdCard = userNucleicTimes.stream().filter(e -> StringUtils.isNotEmpty(e.getIdCard())).collect(Collectors.groupingBy(UserNucleicTime::getIdCard));
                yResult.add(collectIdCard.size());
            }
        }
        map.put("y",yResult);
        return AjaxResult.success(map);
    }

    /**
     * 列表页计算多少次核酸
     * @return
     */
    @Override
    public AjaxResult getHeSuanCount(Long deptId) {
        int heSuanCount = userNucleicTimeService.getHeSuanCount(deptId);
        return AjaxResult.success(heSuanCount);
    }
}
