package com.ruoyi.system.service.impl;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserNucleicTimeMapper;
import com.ruoyi.system.domain.UserNucleicTime;
import com.ruoyi.system.service.IUserNucleicTimeService;

/**
 * 导入人员核酸时间记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-15
 */
@Service
public class UserNucleicTimeServiceImpl implements IUserNucleicTimeService 
{
    @Autowired
    private UserNucleicTimeMapper userNucleicTimeMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询导入人员核酸时间记录
     * 
     * @param id 导入人员核酸时间记录主键
     * @return 导入人员核酸时间记录
     */
    @Override
    public UserNucleicTime selectUserNucleicTimeById(Long id)
    {
        return userNucleicTimeMapper.selectUserNucleicTimeById(id);
    }

    /**
     * 查询导入人员核酸时间记录列表
     * 
     * @param userNucleicTime 导入人员核酸时间记录
     * @return 导入人员核酸时间记录
     */
    @Override
    public List<UserNucleicTime> selectUserNucleicTimeList(UserNucleicTime userNucleicTime)
    {
        return userNucleicTimeMapper.selectUserNucleicTimeList(userNucleicTime);
    }

    @Override
    public List<UserNucleicTime> homeList() {

        //获取本周周一时间
        String startTime = DateUtils.getCurrentMonday()+" 00:00:00";
        String endTime = DateUtils.getTime();

        return userNucleicTimeMapper.selectHomeList(startTime,endTime);
    }

    /**
     * 新增导入人员核酸时间记录
     * 
     * @param userNucleicTime 导入人员核酸时间记录
     * @return 结果
     */
    @Override
    public int insertUserNucleicTime(UserNucleicTime userNucleicTime)
    {
        return userNucleicTimeMapper.insertUserNucleicTime(userNucleicTime);
    }

    /**
     * 修改导入人员核酸时间记录
     * 
     * @param userNucleicTime 导入人员核酸时间记录
     * @return 结果
     */
    @Override
    public int updateUserNucleicTime(UserNucleicTime userNucleicTime)
    {
        return userNucleicTimeMapper.updateUserNucleicTime(userNucleicTime);
    }

    /**
     * 批量删除导入人员核酸时间记录
     * 
     * @param ids 需要删除的导入人员核酸时间记录主键
     * @return 结果
     */
    @Override
    public int deleteUserNucleicTimeByIds(Long[] ids)
    {
        return userNucleicTimeMapper.deleteUserNucleicTimeByIds(ids);
    }

    /**
     * 删除导入人员核酸时间记录信息
     * 
     * @param id 导入人员核酸时间记录主键
     * @return 结果
     */
    @Override
    public int deleteUserNucleicTimeById(Long id)
    {
        return userNucleicTimeMapper.deleteUserNucleicTimeById(id);
    }


    /**
     * 10分钟执行一次刷新首页数据
     */
    @Override
    public void getIndexData() {
        Date nowDate = DateUtils.getNowDate();
        //获取本月人数
        int indexDataMonth = userNucleicTimeMapper.getIndexDataMonth(nowDate);
        //获取本周人数
        int indexDataWeek = userNucleicTimeMapper.getIndexDataWeek(nowDate);
        //获取上周周人数
        int indexDataLastWeek = userNucleicTimeMapper.getIndexDataLastWeek(nowDate);

        NumberFormat numberformat= NumberFormat.getPercentInstance();
        numberformat.setMinimumFractionDigits(2);
        String resultWeek = "0%";
        String resultLastWeek = "0%";
        int allCount = indexDataWeek + indexDataLastWeek;
        if (indexDataWeek > 0 ){
             resultWeek=numberformat.format((float)indexDataWeek/(float)allCount);
        }
        if (indexDataLastWeek > 0 ){
            resultLastWeek=numberformat.format((float)indexDataLastWeek/(float)allCount);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("indexDataMonth",indexDataMonth);
        map.put("indexDataWeek",indexDataWeek);
        map.put("indexDataLastWeek",indexDataLastWeek);
        map.put("resultWeek",resultWeek);
        map.put("resultLastWeek",resultLastWeek);
        redisCache.setCacheMap(CacheConstants.INDEX_DATA,map);
    }

    /**
     * 获取首页折线图/柱状图
     * @return
     */
    @Override
    public List<UserNucleicTime> getJobStyle() {
        Date nowDate = DateUtils.getNowDate();
        List<UserNucleicTime> jobStyle = userNucleicTimeMapper.getJobStyle(nowDate);
        return jobStyle;
    }

    @Override
    public int getHeSuanCount(Long deptId) {
        return userNucleicTimeMapper.getHeSuanCount(deptId);
    }

}
