package com.dayou.service.impl;

import com.dayou.dto.DataPermissionCondition;
import com.dayou.entity.ItemUserLog;
import com.dayou.entity.MarketLog;
import com.dayou.mapper.MarketLogMapper;
import com.dayou.service.IBusinessReplyService;
import com.dayou.service.IMarketLogService;
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.MarketLogVO;
import org.springframework.beans.factory.annotation.Autowired;
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.Collections;
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-13
 */
@Service
public class MarketLogServiceImpl extends ServiceImpl<MarketLogMapper, MarketLog> implements IMarketLogService {


    @Autowired
    private MarketLogMapper marketLogMapper;


    @Override
    @SuppressWarnings("unchecked")
    public Page<MarketLogVO> selectPage(Page page, MarketLog marketLog){
        Long userId = LoginContext.getUserId();
        marketLog.setUserId(userId);
        return marketLogMapper.getPage(page,marketLog);
    }


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

    @Override
    public Boolean add(MarketLog marketLog){
        return  this.save(marketLog);
    }

    @Override
    public Boolean update(MarketLog marketLog){
        return  this.updateById(marketLog);
    }

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

    @Override
    public List<ItemLogCollectVO> logCollect(DataPermissionCondition condition) {
        List<ItemUserLogVO> logList = new ArrayList<>();
        List<ItemLogCollectVO> result = new ArrayList<>();
        logList = marketLogMapper.getList(condition.getStartDate(),condition.getEndDate(),condition.getUserIds(),condition.getKeyword());
        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;
    }
}
