package com.fjhx.oa.service.internal.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.oa.entity.internal.dto.InternalAddressBookDto;
import com.fjhx.oa.entity.internal.po.InternalAddressBook;
import com.fjhx.oa.entity.internal.vo.SysUserVo;
import com.fjhx.oa.mapper.internal.InternalAddressBookMapper;
import com.fjhx.oa.service.internal.InternalAddressBookService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * <p>
 * 内部通讯录 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-04
 */
//@DS(SourceConstant.OA)
@Service
public class InternalAddressBookServiceImpl extends ServiceImpl<InternalAddressBookMapper, InternalAddressBook> implements InternalAddressBookService {

    @Autowired
    SysUserServiceImpl sysUserService;

    @Override
    public List<SysUserVo> getPage(SysUser user) {
//        IWrapper<InternalAddressBook> wrapper = getWrapper();
//        wrapper.orderByDesc("iab", InternalAddressBook::getId);
//        Page<InternalAddressBookVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        DynamicDataSourceContextHolder.push(SourceConstant.BASE);
        List<SysUser> sysUsers = sysUserService.selectUserList(user);
        DynamicDataSourceContextHolder.poll();

        List<SysUserVo> sysUserVoList = BeanUtil.copyToList(sysUsers, SysUserVo.class);
        List<Long> userIds = sysUserVoList.stream().map(SysUser::getUserId).collect(Collectors.toList());
        List<InternalAddressBook> list = list(q -> q.in(InternalAddressBook::getUserId, userIds));
        Map<Long, List<InternalAddressBook>> internalAddressBookMap = list.stream().collect(Collectors.groupingBy(InternalAddressBook::getUserId));
        for (SysUserVo sysUserVo : sysUserVoList) {
            List<InternalAddressBook> internalAddressBook = internalAddressBookMap.get(sysUserVo.getUserId());
            sysUserVo.setInternalAddressBookList(internalAddressBook);
        }
        return sysUserVoList;
    }

    @Override
    public List<InternalAddressBook> detail(Long userId) {
        List<InternalAddressBook> list = list(q -> q.eq(InternalAddressBook::getUserId, userId));
        return list;
    }

    @Override
    public void add(InternalAddressBookDto internalAddressBookDto) {
        this.save(internalAddressBookDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(InternalAddressBookDto internalAddressBookDto) {
        List<InternalAddressBook> internalAddressBookList = internalAddressBookDto.getInternalAddressBookList();
        for (InternalAddressBook internalAddressBook : internalAddressBookList) {
            internalAddressBook.setUserId(internalAddressBookDto.getUserId());
        }
//        List<Long> internalAddressBookIds = internalAddressBookList.stream().filter(item-> ObjectUtil.isNotEmpty(item.getId())).map(InternalAddressBook::getId).collect(Collectors.toList());
//        if(ObjectUtil.isNotEmpty(internalAddressBookIds)) {
//            remove(q -> q.notIn(InternalAddressBook::getId, internalAddressBookIds).eq(InternalAddressBook::getUserId, internalAddressBookDto.getUserId()));
//        }else{
//            //如果传的列表为空直接全部删除
//            remove(q -> q.eq(InternalAddressBook::getUserId, internalAddressBookDto.getUserId()));
//        }
        this.editLinked(internalAddressBookList, InternalAddressBook::getUserId, internalAddressBookDto.getUserId());
        saveOrUpdateBatch(internalAddressBookList);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
