package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.dao.ReturnVisitDao;
import com.yonge.cooleshow.biz.dal.dto.search.ReturnVisitSearch;
import com.yonge.cooleshow.biz.dal.entity.ReturnVisit;
import com.yonge.cooleshow.biz.dal.entity.SysUser;
import com.yonge.cooleshow.biz.dal.mapper.SysUserMapper;
import com.yonge.cooleshow.biz.dal.service.ReturnVisitService;
import com.yonge.cooleshow.biz.dal.vo.ReturnVisitVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ReturnVisitServiceImpl extends ServiceImpl<ReturnVisitDao, ReturnVisit> implements ReturnVisitService {
    private final static Logger log = LoggerFactory.getLogger(ReturnVisitServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper;

	@Override
    public ReturnVisitVo detail(Long id) {
        return baseMapper.detail(id);
    }
    
    @Override
    public IPage<ReturnVisitVo> selectPage(IPage<ReturnVisitVo> page, ReturnVisitSearch query){
        IPage<ReturnVisitVo> returnVisitVoIPage = page.setRecords(baseMapper.selectPage(page, query));
        List<ReturnVisitVo> records =
                returnVisitVoIPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return returnVisitVoIPage;
        }
        List<Long> collect = records.stream().map(ReturnVisitVo::getCreateBy).collect(Collectors.toList());


        List<SysUser> sysUsers = sysUserMapper.selectBatchIds(collect);
        Map<Long, String> userIdMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getId, SysUser::getUsername));

        for (ReturnVisitVo record : records) {
            record.setVisitUsername(userIdMap.get(record.getCreateBy()));
        }
        returnVisitVoIPage.setRecords(records);
        return returnVisitVoIPage;
    }
	
}