package com.dayou.service.impl;

import com.dayou.common.BaseEntity;
import com.dayou.entity.Note;
import com.dayou.mapper.NoteMapper;
import com.dayou.service.INoteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.vo.NoteCountVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 便签 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-01-12
 */
@Service
public class NoteServiceImpl extends ServiceImpl<NoteMapper, Note> implements INoteService {


    @Override
    @SuppressWarnings("unchecked")
    public Page<Note> selectPage(Page page,Note note){
        return this.page(page, new QueryWrapper<Note>(note));
    }


    @Override
    public Note detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(Note note){
        return  this.save(note);
    }

    @Override
    public Boolean update(Note note){
        return  this.updateById(note);
    }

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

    @Override
    public List<Note> getList(Note note) {
        List<Note> list = this.list(new LambdaQueryWrapper<Note>()
                .eq(Note::getUserId, note.getUserId())
                .eq(Note::getNoteDate, note.getNoteDate()).eq(BaseEntity::getDeleted, Boolean.FALSE));
        return list;
    }

    @Override
    public List<NoteCountVO> getCount(Long userId) {
        List<NoteCountVO> result = new ArrayList<>();

        List<Note> notes = this.list(new LambdaQueryWrapper<Note>().eq(Note::getUserId, userId).eq(BaseEntity::getDeleted, Boolean.FALSE));

        if (CollectionUtils.isEmpty(notes)){
            return result;
        }
        Map<LocalDate, Long> collect = notes.stream().collect(Collectors.groupingBy(Note::getNoteDate, Collectors.counting()));


        for (Map.Entry<LocalDate,Long> map : collect.entrySet()){
            NoteCountVO noteCountVO = new NoteCountVO();
            noteCountVO.setCount(map.getValue());
            noteCountVO.setNoteDate(map.getKey());
            result.add(noteCountVO);
        }
        return result;
    }
}
