package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.sop.business.bean.dto.UserArchivesDto;
import com.qmth.sop.business.entity.TBUserArchivesAllocationLog;
import com.qmth.sop.business.mapper.TBUserArchivesAllocationLogMapper;
import com.qmth.sop.business.service.TBUserArchivesAllocationLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.common.enums.SopRoleTypeEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 人员调配历史记录 服务实现类
 * </p>
 *
 * @author CaoZixuan
 * @since 2024-05-15
 */
@Service
public class TBUserArchivesAllocationLogServiceImpl extends ServiceImpl<TBUserArchivesAllocationLogMapper, TBUserArchivesAllocationLog> implements TBUserArchivesAllocationLogService {

    @Override
    public List<UserArchivesDto> findUserArchivesAllocationHistory(Long crmDetailId, SopRoleTypeEnum sopRoleTypeEnum) {
        List<TBUserArchivesAllocationLog> datasource = this.list(new QueryWrapper<TBUserArchivesAllocationLog>().lambda()
                .eq(TBUserArchivesAllocationLog::getCrmDetailId, crmDetailId)
                .eq(TBUserArchivesAllocationLog::getSopRoleType, sopRoleTypeEnum).orderByAsc(TBUserArchivesAllocationLog::getCreateTime));

        return datasource.stream().flatMap(e -> {
            UserArchivesDto userArchivesDto = new UserArchivesDto();
            userArchivesDto.setUserId(e.getUserId());
            userArchivesDto.setUserArchivesId(e.getArchivesId());
            userArchivesDto.setName(e.getArchivesName());
            userArchivesDto.setCity(e.getCity());
            userArchivesDto.setSupplierName(e.getSupplierName());
            userArchivesDto.setCreateTime(e.getCreateTime());
            return Stream.of(userArchivesDto);
        }).collect(Collectors.toList());
    }
}
