package com.dayou.service.impl;

import com.dayou.dto.LogEcharsDTO;
import com.dayou.entity.ItemUserLog;
import com.dayou.mapper.ItemUserLogMapper;
import com.dayou.service.IItemUserLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.LoginContext;
import com.dayou.vo.ItemLogCollectVO;
import com.dayou.vo.ItemUserLogVO;
import com.dayou.vo.LogEcharsVO;
import com.dayou.vo.LogStatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

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


/**
 * <p>
 * 项目用户日志 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2022-12-08
 */
@Service
public class ItemUserLogServiceImpl extends ServiceImpl<ItemUserLogMapper, ItemUserLog> implements IItemUserLogService {


    @Autowired
    private ItemUserLogMapper itemUserLogMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<ItemUserLogVO> selectPage(Page page,ItemUserLogVO itemUserLogVO){
        Long userId = LoginContext.getUserId();
        return itemUserLogMapper.page(page,itemUserLogVO,userId);
    }


    @Override
    public ItemUserLogVO detail(Long id){
        return itemUserLogMapper.detail(id);
    }

    @Override
    public Boolean add(ItemUserLog itemUserLog){
        return  this.save(itemUserLog);
    }

    @Override
    public Boolean update(ItemUserLog itemUserLog){
        return  this.updateById(itemUserLog);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public List<ItemLogCollectVO> logCollect(Long departmentId, Boolean scope,Long itemId,LocalDate startDate,LocalDate endDate) {
        List<ItemUserLogVO> logList = new ArrayList<>();
        List<ItemLogCollectVO> result = new ArrayList<>();
        if (scope){
            Long userId = LoginContext.getUserId();
            logList = itemUserLogMapper.logListByUserId(userId,itemId,startDate,endDate);
        }
        else {
            logList = itemUserLogMapper.logListByDepartmentId(departmentId,itemId,startDate,endDate);
        }
        Map<Long, List<ItemUserLogVO>> collectByUser = logList.stream().collect(Collectors.groupingBy(ItemUserLog::getUserId));
        for(Map.Entry<Long, List<ItemUserLogVO>> byUserMap : collectByUser.entrySet()){
            Map<LocalDate, List<ItemUserLogVO>> collectByDate = byUserMap.getValue().stream().collect(Collectors.groupingBy(ItemUserLog::getLogDate));
            for (Map.Entry<LocalDate,List<ItemUserLogVO>> byDateMap : collectByDate.entrySet()) {
                ItemLogCollectVO itemLogCollectVO = new ItemLogCollectVO();
                itemLogCollectVO.setLogDate(byDateMap.getKey());
                itemLogCollectVO.setLogs(byDateMap.getValue());
                itemLogCollectVO.setUserId(byUserMap.getKey());
                itemLogCollectVO.setUserName(byUserMap.getValue().get(0).getUserName());
                result.add(itemLogCollectVO);
            }
        }
        Collections.sort(result);
        return result;
    }

    @Override
    public LogEcharsVO getLogEchars() {

        LogEcharsVO logEcharsVO = new LogEcharsVO();
        List<ItemUserLogVO> list = itemUserLogMapper.getLogs();

        List<String> itemNames = list.stream().map(ItemUserLogVO::getItemName).distinct().collect(Collectors.toList());
        logEcharsVO.setItemNames(itemNames);

        List<LocalDate> dates = list.stream().map(ItemUserLog::getLogDate).distinct().collect(Collectors.toList());
        LogEcharsVO.Axis xAsis = logEcharsVO.getXAsis();
        xAsis.setData(dates);

        Map<String, List<ItemUserLogVO>> collect = list.stream().collect(Collectors.groupingBy(ItemUserLogVO::getItemName));
        List<LogEcharsDTO> echarsDTOS = new ArrayList<>();
        for (Map.Entry<String,List<ItemUserLogVO>> map :collect.entrySet()){
            Map<LocalDate, List<ItemUserLogVO>> collect1 = map.getValue().stream().collect(Collectors.groupingBy(ItemUserLog::getLogDate));
            for (Map.Entry<LocalDate, List<ItemUserLogVO>> map1 : collect1.entrySet()){
                    LogEcharsDTO logEcharsDTO = new LogEcharsDTO();
                    logEcharsDTO.setItemName(map.getKey());
                    logEcharsDTO.setLogDate(map1.getKey());
                    logEcharsDTO.setSize(map1.getValue().size());
                    echarsDTOS.add(logEcharsDTO);
            }
        }

        List<LogEcharsVO.EcharsItem> items = logEcharsVO.getItems();


        Map<String, List<LogEcharsDTO>> collect1 = echarsDTOS.stream().collect(Collectors.groupingBy(LogEcharsDTO::getItemName));
        for (Map.Entry<String,List<LogEcharsDTO>> m : collect1.entrySet()){
            LogEcharsVO.EcharsItem echarsItem = new LogEcharsVO.EcharsItem();
            echarsItem.setName(m.getKey());
            List<Integer> nums = echarsItem.getData();
            List<LocalDate> collect2 = m.getValue().stream().map(LogEcharsDTO::getLogDate).collect(Collectors.toList());
            for (LocalDate date :dates){

                if (collect2.contains(date)){
                    for (LogEcharsDTO logEcharsDTO : m.getValue()){
                        if (logEcharsDTO.getLogDate().equals( date)){
                            nums.add(logEcharsDTO.getSize());
                        }

                    }

                }else {
                    nums.add(0);
                }

            }
            items.add(echarsItem);
        }
        return logEcharsVO;
    }

    @Override
    public LogStatVO logStat() {
        LogStatVO logStatVO = itemUserLogMapper.logStat();
        return logStatVO;
    }
}
