package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InterviewTableMapper;
import com.ruoyi.system.domain.InterviewTable;
import com.ruoyi.system.service.IInterviewTableService;

/**
 * 网页访问记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-16
 */
@Service
public class InterviewTableServiceImpl implements IInterviewTableService 
{
    @Autowired
    private InterviewTableMapper interviewTableMapper;

    /**
     * 查询网页访问记录
     * 
     * @param interviewId 网页访问记录主键
     * @return 网页访问记录
     */
    @Override
    public InterviewTable selectInterviewTableByInterviewId(Long interviewId)
    {
        return interviewTableMapper.selectInterviewTableByInterviewId(interviewId);
    }

    /**
     * 查询网页访问记录列表
     * 
     * @param interviewTable 网页访问记录
     * @return 网页访问记录
     */
    @Override
    public List<InterviewTable> selectInterviewTableList(InterviewTable interviewTable)
    {
        return interviewTableMapper.selectInterviewTableList(interviewTable);
    }

    /**
     * 新增网页访问记录
     * 
     * @param interviewTable 网页访问记录
     * @return 结果
     */
    @Override
    public int insertInterviewTable(InterviewTable interviewTable)
    {
        String time = DateUtils.getDate();
        //获取年
        String year = time.split("-")[0];
        interviewTable.setYear(year);
        //获取月
        String month = time.split("-")[1];
        interviewTable.setMonth(month);
        //获取日
        String date = time.split("-")[2];
        interviewTable.setDay(date);
        interviewTable.setCreateTime(DateUtils.getNowDate());
        return interviewTableMapper.insertInterviewTable(interviewTable);
    }

    /**
     * 修改网页访问记录
     * 
     * @param interviewTable 网页访问记录
     * @return 结果
     */
    @Override
    public int updateInterviewTable(InterviewTable interviewTable)
    {
        interviewTable.setUpdateTime(DateUtils.getNowDate());
        return interviewTableMapper.updateInterviewTable(interviewTable);
    }

    /**
     * 批量删除网页访问记录
     * 
     * @param interviewIds 需要删除的网页访问记录主键
     * @return 结果
     */
    @Override
    public int deleteInterviewTableByInterviewIds(Long[] interviewIds)
    {
        return interviewTableMapper.deleteInterviewTableByInterviewIds(interviewIds);
    }

    /**
     * 删除网页访问记录信息
     * 
     * @param interviewId 网页访问记录主键
     * @return 结果
     */
    @Override
    public int deleteInterviewTableByInterviewId(Long interviewId)
    {
        return interviewTableMapper.deleteInterviewTableByInterviewId(interviewId);
    }

    /**
     * PC首页浏览统计
     * @param type 查询类型（1：年，2：月）
     * @return
     */
    @Override
    public AjaxResult browse(String type) {
        String time = DateUtils.getDate();

        Map<String,Object> map = new HashMap<>();
        //查询所有数据
        List<InterviewTable> interviewTableList = interviewTableMapper.selectInterviewTableList(new InterviewTable());
        //总浏览次数
        int cs = 0;
        //总浏览人数
        int rs = 0;
        //累计浏览终端数
        int zd = 0;
        //人均停留时长
        String rj = "0";
        if(interviewTableList!=null && interviewTableList.size()>0){
            //总浏览次数
            cs = interviewTableList.size();
            //总浏览人数
            Set<String> equipId = interviewTableList.stream().map(InterviewTable::getEquipmentId).collect(Collectors.toSet());
            rs = equipId.size();
            //累计浏览终端数
            zd = interviewTableList.size();
            //人均停留时长
            Double sum = interviewTableList.stream().mapToDouble(InterviewTable::getInterviewTime).sum();
            BigDecimal a = BigDecimal.valueOf(Double.valueOf(sum)).divide(BigDecimal.valueOf(rs),2, BigDecimal.ROUND_HALF_UP);
            rj = a.toString();
        }
        //总浏览次数
        map.put("cs",cs);
        //总浏览人数
        map.put("rs",rs);
        //累计浏览终端数
        map.put("zd",zd);
        //人均停留时长
        map.put("rj",rj);

        Map<String,Object> zMap = new HashMap<>();
        List<Object> list1 = new ArrayList<>();
        List<Object> list2 = new ArrayList<>();
        List<Object> list3 = new ArrayList<>();
        List<Object> list4 = new ArrayList<>();
        //年
        if("1".equals(type)){
            //获取年
            String num = time.split("-")[0];
            //获取当前年的所有数据
            InterviewTable table = new InterviewTable();
            table.setYear(num);
            List<InterviewTable> interviewTableYear = interviewTableMapper.selectInterviewTableList(table);
            if(interviewTableYear!=null && interviewTableYear.size()>0){
                //根据month字段进行分组
                Map<String, List<InterviewTable>> monthMap = interviewTableYear.stream().collect(
                        Collectors.groupingBy(
                                InterviewTable -> InterviewTable.getMonth()
                        ));
                //循环1-12
                for (int i = 1; i < 13; i++) {
                    String rq = i+"月";
                    int rqzrs = 0;
                    int rqzcs = 0;
                    int rqzds = 0;
                    List<InterviewTable> interviewTableMonth = new ArrayList<>();
                    if(i<10){
                        interviewTableMonth = monthMap.get("0"+i);

                    }else{
                        interviewTableMonth = monthMap.get(String.valueOf(i));
                    }
                    if(interviewTableMonth!=null && interviewTableMonth.size()>0){
                        //总浏览人数
                        Set<String> equipId = interviewTableList.stream().map(InterviewTable::getEquipmentId).collect(Collectors.toSet());
                        rqzrs = equipId.size();
                        //总浏览次数
                        rqzcs = interviewTableMonth.size();
                        //累计浏览终端数
                        rqzds = interviewTableMonth.size();
                    }
                    //日期
                    list1.add(rq);
                    //总浏览人数
                    list2.add(rqzrs);
                    //总浏览次数
                    list3.add(rqzcs);
                    //累计浏览终端数
                    list4.add(rqzds);
                }

            }else{
                //循环1-12
                for (int i = 1; i < 13; i++) {
                    //日期
                    list1.add(i+"月");
                    //总浏览人数
                    list2.add(0);
                    //总浏览次数
                    list3.add(0);
                    //累计浏览终端数
                    list4.add(0);
                }
            }
        }
        //日
        if("2".equals(type)){
            //获取月
            String num = time.split("-")[1];
            //获取当前月天数
            LocalDate localDate = LocalDate.now();
            int numberOfDays = localDate.lengthOfMonth();

            //获取当前年的所有数据
            InterviewTable table = new InterviewTable();
            table.setMonth(num);
            List<InterviewTable> interviewTableMonth = interviewTableMapper.selectInterviewTableList(table);
            if(interviewTableMonth!=null && interviewTableMonth.size()>0){
                //根据day字段进行分组
                Map<String, List<InterviewTable>> dayMap = interviewTableMonth.stream().collect(
                        Collectors.groupingBy(
                                InterviewTable -> InterviewTable.getDay()
                        ));
                //循环当前月天数
                for (int i = 1; i < numberOfDays+1; i++) {
                    String rq = i+"号";
                    int rqzrs = 0;
                    int rqzcs = 0;
                    int rqzds = 0;
                    List<InterviewTable> interviewTableDay = new ArrayList<>();
                    if(i<10){
                        interviewTableDay = dayMap.get("0"+i);

                    }else{
                        interviewTableDay = dayMap.get(String.valueOf(i));
                    }
                    if(interviewTableDay!=null && interviewTableDay.size()>0){
                        //总浏览人数
                        Set<String> equipId = interviewTableList.stream().map(InterviewTable::getEquipmentId).collect(Collectors.toSet());
                        rqzrs = equipId.size();
                        //总浏览次数
                        rqzcs = interviewTableDay.size();
                        //累计浏览终端数
                        rqzds = interviewTableDay.size();
                    }
                    //日期
                    list1.add(rq);
                    //总浏览人数
                    list2.add(rqzrs);
                    //总浏览次数
                    list3.add(rqzcs);
                    //累计浏览终端数
                    list4.add(rqzds);
                }

            }else{
                //循环当前月天数
                for (int i = 1; i < numberOfDays+1; i++) {
                    //日期
                    list1.add(i+"月");
                    //总浏览人数
                    list2.add(0);
                    //总浏览次数
                    list3.add(0);
                    //累计浏览终端数
                    list4.add(0);
                }
            }
        }
        zMap.put("rq",list1);
        zMap.put("rqzrs",list2);
        zMap.put("rqzcs",list3);
        zMap.put("rqzds",list4);
        map.put("tjt",zMap);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult stay() {
        Map<String,Object> map = new HashMap<>();
        //查询所有数据
        List<InterviewTable> interviewTableList = interviewTableMapper.selectInterviewTableList(new InterviewTable());
        //0-5
        int one = 0;
        //5-10
        int two = 0;
        //10-20
        int three = 0;
        //20-30
        int four = 0;
        //30-60
        int five = 0;
        //60-
        int six = 0;

        //人均停留时长
        String rj = "0";
        if(interviewTableList!=null && interviewTableList.size()>0){
            //总浏览人数
            Set<String> equipId = interviewTableList.stream().map(InterviewTable::getEquipmentId).collect(Collectors.toSet());
            int rs = equipId.size();
            //人均停留时长
            Double sum = interviewTableList.stream().mapToDouble(InterviewTable::getInterviewTime).sum();
            BigDecimal a = BigDecimal.valueOf(Double.valueOf(sum)).divide(BigDecimal.valueOf(rs),2, BigDecimal.ROUND_HALF_UP);
            rj = a.toString();

            for (InterviewTable table : interviewTableList) {
                if(table.getInterviewTime()<=5){
                    one++;
                }else if(table.getInterviewTime()<=10){
                    two++;
                }else if(table.getInterviewTime()<=20){
                    three++;
                }else if(table.getInterviewTime()<=30){
                    four++;
                }else if(table.getInterviewTime()<=60){
                    five++;
                }else{
                    six++;
                }
            }

        }
        map.put("one",one);
        map.put("two",two);
        map.put("three",three);
        map.put("four",four);
        map.put("five",five);
        map.put("six",six);
        map.put("rj",rj);
        return AjaxResult.success(map);
    }


}
