package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.UserInfoAttachMapper;
import com.crm.manager.service.UserInfoAttachService;
import com.crm.rely.backend.core.dto.user.info.UserAttachInfoDto;
import com.crm.rely.backend.core.pojo.table.UserInfoAttachTable;
import com.crm.rely.backend.exception.ServiceException;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class UserInfoAttachServiceImpl implements UserInfoAttachService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserInfoAttachMapper userInfoAttachMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttachInfo(Long userId, List<Long> attachIds) throws ServiceException {
        try {
            //删除原信息
            Integer exists = Optional.ofNullable(userInfoAttachMapper.countByUserId(userId)).orElse(0);
            if (exists > 0) {
                userInfoAttachMapper.removeByUserId(userId);
            }
            exists = Optional.ofNullable(userInfoAttachMapper.countByAttachId(userId)).orElse(0);
            if (exists > 0) {
                userInfoAttachMapper.removeByAttachId(userId);
            }

            if (CollectionUtils.isNotEmpty(attachIds)) {
                //保存现信息
                List<UserInfoAttachTable> tables = Stream.concat(attachIds.stream().map(attachId -> {
                    UserInfoAttachTable userInfoAttachTable = new UserInfoAttachTable();
                    userInfoAttachTable.setUserId(userId);
                    userInfoAttachTable.setAttachId(attachId);
                    return userInfoAttachTable;
                }), attachIds.stream().map(attachId -> {
                    UserInfoAttachTable userInfoAttachTable = new UserInfoAttachTable();
                    userInfoAttachTable.setUserId(attachId);
                    userInfoAttachTable.setAttachId(userId);
                    return userInfoAttachTable;
                })).collect(Collectors.toList());

                userInfoAttachMapper.batchSave(tables);
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ServiceException(e.getMessage());
        }

    }

    @Override
    public List<UserAttachInfoDto> getAttachInfoByUserId(Long userId) throws ServiceException {
        try {
            return Optional.ofNullable(userInfoAttachMapper.getViewByUserId(userId)).orElse(new ArrayList<>()).stream()
                    .map(view -> {
                        UserAttachInfoDto userAttachInfoDto = new UserAttachInfoDto();
                        userAttachInfoDto.setId(view.getId());
                        userAttachInfoDto.setName(view.getName());
                        return userAttachInfoDto;
                    }).collect(Collectors.toList());
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ServiceException(e.getMessage());
        }
    }
}
