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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.oa.entity.external.po.ExternalAddressBook;
import com.fjhx.oa.mapper.external.ExternalAddressBookMapper;
import com.fjhx.oa.service.external.ExternalAddressBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.oa.entity.external.vo.ExternalAddressBookVo;
import com.fjhx.oa.entity.external.dto.ExternalAddressBookSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.oa.entity.external.dto.ExternalAddressBookDto;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * <p>
 * 外部通讯录 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-06
 */
@DS(SourceConstant.OA)
@Service
public class ExternalAddressBookServiceImpl extends ServiceImpl<ExternalAddressBookMapper, ExternalAddressBook> implements ExternalAddressBookService {

    @Override
    public Page<ExternalAddressBookVo> getPage(ExternalAddressBookSelectDto dto) {
        IWrapper<ExternalAddressBook> wrapper = getWrapper();
        wrapper.orderByDesc("eab", ExternalAddressBook::getId);
        Page<ExternalAddressBookVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public List<ExternalAddressBookVo> detail(Long id) {
        List<ExternalAddressBook> list = list(q -> q.eq(ExternalAddressBook::getContactsId, id));
        List<ExternalAddressBookVo> result = BeanUtil.copyToList(list, ExternalAddressBookVo.class);
        return result;
    }

    @Override
    public void add(ExternalAddressBookDto externalAddressBookDto) {
        this.save(externalAddressBookDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(ExternalAddressBookDto externalAddressBookDto) {
        List<ExternalAddressBook> externalAddressBookList = externalAddressBookDto.getExternalAddressBookList();
        for (ExternalAddressBook externalAddressBook :externalAddressBookList){
            externalAddressBook.setContactsId(externalAddressBookDto.getContactsId());
        }
//        List<Long> externalAddressBookIds = externalAddressBookList.stream().filter(item-> ObjectUtil.isNotEmpty(item.getId())).map(ExternalAddressBook::getId).collect(Collectors.toList());
//        if(ObjectUtil.isNotEmpty(externalAddressBookIds)) {
//            remove(q -> q.notIn(ExternalAddressBook::getId, externalAddressBookIds).eq(ExternalAddressBook::getContactsId, externalAddressBookDto.getContactsId()));
//        }else{
//            remove(q -> q.eq(ExternalAddressBook::getContactsId, externalAddressBookDto.getContactsId()));
//        }
        this.editLinked(externalAddressBookList,ExternalAddressBook::getContactsId,externalAddressBookDto.getContactsId());
        saveOrUpdateBatch(externalAddressBookList);
    }

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

}
