package com.ycwl.basic.profitsharing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.profitsharing.dto.ProfitSharingRecordGroupVO;
import com.ycwl.basic.profitsharing.dto.ProfitSharingRecordReqQuery;
import com.ycwl.basic.profitsharing.dto.ProfitSharingRecordRespVO;
import com.ycwl.basic.profitsharing.dto.ProfitSharingRecordTableRespVo;
import com.ycwl.basic.profitsharing.dto.ProfitSharingTableRecordVO;
import com.ycwl.basic.profitsharing.entity.ProfitSharingConfig;
import com.ycwl.basic.profitsharing.entity.ProfitSharingUser;
import com.ycwl.basic.profitsharing.mapper.ProfitSharingRecordMapper;
import com.ycwl.basic.profitsharing.repository.ProfitSharingRepository;
import com.ycwl.basic.profitsharing.service.ProfitSharingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ProfitSharingRecordServiceImpl implements ProfitSharingRecordService {

    @Autowired
    private ProfitSharingRecordMapper profitSharingRecordMapper;
    @Autowired
    private ProfitSharingRepository profitSharingRepository;


    @Override
    public ProfitSharingRecordTableRespVo list(ProfitSharingRecordReqQuery req) {
        ProfitSharingRecordTableRespVo respVo = new ProfitSharingRecordTableRespVo();
        PageHelper.startPage(req.getPageNum(), req.getPageSize(), "create_time desc");
        List<ProfitSharingRecordGroupVO> list = profitSharingRecordMapper.listGroupByOrderId(req);
        PageInfo pageInfo = new PageInfo<>(list);
        respVo.setPage(pageInfo);
        if (list.isEmpty()) {
            return respVo;
        }
        List<ProfitSharingRecordRespVO> recordList = profitSharingRecordMapper.listByOrderIds(list.stream().map(ProfitSharingRecordGroupVO::getOrderId).collect(Collectors.toList()));
        List<Long> scenicList = recordList.stream().map(ProfitSharingRecordRespVO::getScenicId).distinct().collect(Collectors.toList());
        List<ProfitSharingConfig> scenicConfig = scenicList.stream()
                .map(scenicId -> profitSharingRepository.getScenicConfig(scenicId))
                .filter(item -> !Objects.isNull(item) && !Objects.isNull(item.getUsers()))
                .filter(item -> !item.getUsers().isEmpty())
                .collect(Collectors.toList());
        List<String> columnName = scenicConfig.stream()
                .flatMap(item -> item.getUsers().stream())
                .map(ProfitSharingUser::getName)
                .distinct().collect(Collectors.toList());
        respVo.setColumns(columnName);
        List<ProfitSharingTableRecordVO> data = list.stream()
                .collect(Collectors.groupingBy(ProfitSharingRecordGroupVO::getOrderId))
                .keySet()
                .stream()
                .map(profitSharingRecordGroupVOS -> {
                    return recordList.stream().filter(item -> item.getOrderId().equals(profitSharingRecordGroupVOS)).collect(Collectors.toList());
                })
                .map(value -> {
                    Map<String, ProfitSharingRecordRespVO> map = new HashMap<>();
                    value.forEach(item -> {
                        map.put(item.getUserName(), item);
                    });
                    ProfitSharingTableRecordVO vo = new ProfitSharingTableRecordVO();
                    ProfitSharingRecordRespVO recordRespVO = value.getFirst();
                    vo.setOrderId(recordRespVO.getOrderId());
                    vo.setScenicId(recordRespVO.getScenicId());
                    vo.setScenicName(recordRespVO.getScenicName());
                    vo.setCreateTime(recordRespVO.getCreateTime());
                    vo.setOrderAmount(recordRespVO.getOrderAmount());
                    vo.setRecords(map);
                    return vo;
                })
                .sorted((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()))
                .collect(Collectors.toList());
        respVo.setList(data);
        return respVo;
    }
}