package com.fjhx.mail.service.my.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.customer.entity.customer.po.CustomerUser;
import com.fjhx.customer.entity.customer.vo.CustomerVo;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.customer.service.customer.CustomerUserService;
import com.fjhx.mail.entity.my.dto.MyFolderMessageAddBathDto;
import com.fjhx.mail.entity.my.dto.MyFolderMessageDto;
import com.fjhx.mail.entity.my.dto.MyFolderMessageSelectDto;
import com.fjhx.mail.entity.my.po.MyFolderMessage;
import com.fjhx.mail.entity.my.vo.MessageVo;
import com.fjhx.mail.mapper.my.MyFolderMessageMapper;
import com.fjhx.mail.service.my.MyFolderMessageService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * <p>
 * 我的文件夹关联邮件 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-29
 */
@Service
public class MyFolderMessageServiceImpl extends ServiceImpl<MyFolderMessageMapper, MyFolderMessage> implements MyFolderMessageService {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerUserService customerUserService;

    @Override
    public Page<MessageVo> getPage(MyFolderMessageSelectDto dto) {
        IWrapper<MyFolderMessage> wrapper = getWrapper();
        wrapper.eq("mfm", MyFolderMessage::getMyFolderId, dto.getMyFolderId());

        wrapper.and(ObjectUtil.isNotEmpty(dto.getKeyword()), q1 -> q1
                        .like("m.from_email", dto.getKeyword())
                        .or().like("m.subject", dto.getKeyword())
//                .or().like("mc.content", dto.getKeyword())
//                .or().like("c.name", dto.getKeyword())
//                .or().like("cu.name", dto.getKeyword())
                        .or().like("m.from_personal_name", dto.getKeyword())
        );

        wrapper.orderByDesc("mfm", MyFolderMessage::getUpdateTime);
        Page<MessageVo> page = this.baseMapper.getPage(dto.getPage(), wrapper, dto.getType(), SecurityUtils.getTenantId());

        List<MessageVo> records = page.getRecords();

        //赋值客户信息
        List<String> eMailList = records.stream().map(MessageVo::getFromEmail).distinct().collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(eMailList)) {
            List<Long> customerIds = customerUserService.listObject(CustomerUser::getCustomerId, q -> q.in(CustomerUser::getEmail, eMailList));
            if (ObjectUtil.isEmpty(customerIds)) {
                return page;
            }
            //赋值客户信息
            List<CustomerVo> customerInfoByIds = customerService.getCustomerInfoByIds(customerIds);
            Map<Long, CustomerVo> customerInfoMap = customerInfoByIds.stream().collect(Collectors.toMap(CustomerVo::getId, Function.identity()));
            for (MessageVo record : records) {
                record.setCustomerInfo(customerInfoMap.get(record.getCustomerId()));
            }
        }

        return page;
    }

    @Override
    public void add(MyFolderMessageDto dto) {
        MyFolderMessage myFolderMessage = getOne(q -> q.eq(MyFolderMessage::getMessageId, dto.getMessageId()));
        if (myFolderMessage != null) {
            myFolderMessage.setMyFolderId(dto.getMyFolderId());
            updateById(myFolderMessage);
        } else {
            save(dto);
        }
    }

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

    @Override
    public void addBath(MyFolderMessageAddBathDto dto) {
        List<Long> messageIdList = dto.getMessageIdList();
        Long myFolderId = dto.getMyFolderId();

        List<Long> existMessageIdList = listObject(MyFolderMessage::getMessageId, q -> q
                .eq(MyFolderMessage::getMyFolderId, myFolderId)
                .in(MyFolderMessage::getMessageId, messageIdList));

        List<MyFolderMessage> myFolderMessageList = messageIdList.stream()
                .filter(item -> !existMessageIdList.contains(item))
                .map(item -> {
                    MyFolderMessage myTagMessage = new MyFolderMessage();
                    myTagMessage.setMessageId(item);
                    myTagMessage.setMyFolderId(myFolderId);
                    return myTagMessage;
                })
                .collect(Collectors.toList());

        saveBatch(myFolderMessageList);

    }

}
